mesa-demos: NOTE! Default branch is now main
[mesa-demos.git] / src / tests / subtex.c
blobe3cf2ba1de3258798d4dc5678c1f667c9ab1fe26
1 /*
2 * Test glTexSubImage mid-way through a frame.
4 * The same texture is used for both quads but it gets redefined
5 * with glTexSubImage (or glTexImage) after the first quad.
6 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "glad/glad.h"
13 #include "glut_wrap.h"
15 static GLuint Window = 0;
16 static GLboolean Anim = GL_FALSE;
17 static GLfloat Angle = 0.0f;
21 static void
22 first_texture(void)
24 static int width=8, height=8;
25 static GLubyte tex1[] = {
26 0, 0, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 1, 0, 0, 0,
28 0, 0, 0, 1, 1, 0, 0, 0,
29 0, 0, 0, 0, 1, 0, 0, 0,
30 0, 0, 0, 0, 1, 0, 0, 0,
31 0, 0, 0, 0, 1, 0, 0, 0,
32 0, 0, 0, 1, 1, 1, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0 };
35 GLubyte tex[64][3];
36 GLint i, j;
38 /* red on white */
39 for (i=0;i<height;i++) {
40 for (j=0;j<width;j++) {
41 int p = i*width+j;
42 if (tex1[(height-i-1)*width+j]) {
43 tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
45 else {
46 tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
51 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
52 GL_RGB, GL_UNSIGNED_BYTE, tex );
56 static void
57 second_texture(void)
59 static int width=8, height=8;
61 static GLubyte tex2[] = {
62 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 2, 2, 0, 0, 0,
64 0, 0, 2, 0, 0, 2, 0, 0,
65 0, 0, 0, 0, 0, 2, 0, 0,
66 0, 0, 0, 0, 2, 0, 0, 0,
67 0, 0, 0, 2, 0, 0, 0, 0,
68 0, 0, 2, 2, 2, 2, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0 };
71 GLubyte tex[64][3];
72 GLint i, j;
74 /* green on blue */
75 for (i=0;i<height;i++) {
76 for (j=0;j<width;j++) {
77 int p = i*width+j;
78 if (tex2[(height-i-1)*width+j]) {
79 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
81 else {
82 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
86 #if 0
87 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
88 GL_RGB, GL_UNSIGNED_BYTE, tex );
89 #else
90 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
91 GL_RGB, GL_UNSIGNED_BYTE, tex );
92 #endif
97 static void draw( void )
99 glClear( GL_COLOR_BUFFER_BIT );
101 glColor3f( 1.0, 1.0, 1.0 );
103 /* draw first polygon */
104 glPushMatrix();
105 glTranslatef( -1.0, 0.0, 0.0 );
106 glRotatef( Angle, 0.0, 0.0, 1.0 );
108 first_texture();
110 glBegin( GL_POLYGON );
111 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
112 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
113 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
114 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
115 glEnd();
116 glPopMatrix();
118 /* draw second polygon */
119 glPushMatrix();
120 glTranslatef( 1.0, 0.0, 0.0 );
121 glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
123 second_texture();
125 glBegin( GL_POLYGON );
126 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
127 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
128 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
129 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
130 glEnd();
131 glPopMatrix();
133 glutSwapBuffers();
138 static void idle( void )
140 static double t0 = -1.;
141 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
142 if (t0 < 0.0)
143 t0 = t;
144 dt = t - t0;
145 t0 = t;
146 Angle += 120.0*dt;
147 glutPostRedisplay();
152 /* change view Angle, exit upon ESC */
153 static void key(unsigned char k, int x, int y)
155 (void) x;
156 (void) y;
157 switch (k) {
158 case 'a':
159 Anim = !Anim;
160 if (Anim)
161 glutIdleFunc( idle );
162 else
163 glutIdleFunc( NULL );
164 break;
165 case 27:
166 glutDestroyWindow(Window);
167 exit(0);
173 /* new window size or exposure */
174 static void reshape( int width, int height )
176 glViewport(0, 0, (GLint)width, (GLint)height);
177 glMatrixMode(GL_PROJECTION);
178 glLoadIdentity();
179 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
180 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
181 glMatrixMode(GL_MODELVIEW);
182 glLoadIdentity();
183 glTranslatef( 0.0, 0.0, -8.0 );
187 static void init( void )
189 /* Setup texturing */
190 glEnable( GL_TEXTURE_2D );
191 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
194 glBindTexture( GL_TEXTURE_2D, 0 );
195 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
196 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
197 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
198 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
203 int main( int argc, char *argv[] )
205 glutInit(&argc, argv);
206 glutInitWindowPosition(0, 0);
207 glutInitWindowSize(300, 300);
208 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
210 Window = glutCreateWindow("Texture Objects");
211 gladLoadGL();
212 if (!Window) {
213 exit(1);
216 init();
218 glutReshapeFunc( reshape );
219 glutKeyboardFunc( key );
220 if (Anim)
221 glutIdleFunc( idle );
222 glutDisplayFunc( draw );
223 glutMainLoop();
224 return 0;