Merge commit 'origin/gallium-master-merge'
[mesa.git] / progs / tests / subtex.c
1 /*
2 * Test glTexSubImage mid-way through a frame.
3 *
4 * The same texture is used for both quads but it gets redefined
5 * with glTexSubImage (or glTexImage) after the first quad.
6 */
7
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include "GL/glut.h"
13
14 static GLuint Window = 0;
15 static GLboolean Anim = GL_FALSE;
16 static GLfloat Angle = 0.0f;
17
18
19
20 static void
21 first_texture(void)
22 {
23 static int width=8, height=8;
24 static GLubyte tex1[] = {
25 0, 0, 0, 0, 0, 0, 0, 0,
26 0, 0, 0, 0, 1, 0, 0, 0,
27 0, 0, 0, 1, 1, 0, 0, 0,
28 0, 0, 0, 0, 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, 1, 1, 1, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0 };
33
34 GLubyte tex[64][3];
35 GLint i, j;
36
37 /* red on white */
38 for (i=0;i<height;i++) {
39 for (j=0;j<width;j++) {
40 int p = i*width+j;
41 if (tex1[(height-i-1)*width+j]) {
42 tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
43 }
44 else {
45 tex[p][0] = 255; tex[p][1] = 255; tex[p][2] = 255;
46 }
47 }
48 }
49
50 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
51 GL_RGB, GL_UNSIGNED_BYTE, tex );
52 }
53
54
55 static void
56 second_texture(void)
57 {
58 static int width=8, height=8;
59
60 static GLubyte tex2[] = {
61 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 2, 2, 0, 0, 0,
63 0, 0, 2, 0, 0, 2, 0, 0,
64 0, 0, 0, 0, 0, 2, 0, 0,
65 0, 0, 0, 0, 2, 0, 0, 0,
66 0, 0, 0, 2, 0, 0, 0, 0,
67 0, 0, 2, 2, 2, 2, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0 };
69
70 GLubyte tex[64][3];
71 GLint i, j;
72
73 /* green on blue */
74 for (i=0;i<height;i++) {
75 for (j=0;j<width;j++) {
76 int p = i*width+j;
77 if (tex2[(height-i-1)*width+j]) {
78 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
79 }
80 else {
81 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
82 }
83 }
84 }
85 #if 0
86 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
87 GL_RGB, GL_UNSIGNED_BYTE, tex );
88 #else
89 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
90 GL_RGB, GL_UNSIGNED_BYTE, tex );
91 #endif
92 }
93
94
95
96 static void draw( void )
97 {
98 glClear( GL_COLOR_BUFFER_BIT );
99
100 glColor3f( 1.0, 1.0, 1.0 );
101
102 /* draw first polygon */
103 glPushMatrix();
104 glTranslatef( -1.0, 0.0, 0.0 );
105 glRotatef( Angle, 0.0, 0.0, 1.0 );
106
107 first_texture();
108
109 glBegin( GL_POLYGON );
110 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
111 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
112 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
113 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
114 glEnd();
115 glPopMatrix();
116
117 /* draw second polygon */
118 glPushMatrix();
119 glTranslatef( 1.0, 0.0, 0.0 );
120 glRotatef( Angle-90.0, 0.0, 1.0, 0.0 );
121
122 second_texture();
123
124 glBegin( GL_POLYGON );
125 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -1.0, -1.0 );
126 glTexCoord2f( 1.0, 0.0 ); glVertex2f( 1.0, -1.0 );
127 glTexCoord2f( 1.0, 1.0 ); glVertex2f( 1.0, 1.0 );
128 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -1.0, 1.0 );
129 glEnd();
130 glPopMatrix();
131
132 glutSwapBuffers();
133 }
134
135
136
137 static void idle( void )
138 {
139 static double t0 = -1.;
140 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
141 if (t0 < 0.0)
142 t0 = t;
143 dt = t - t0;
144 t0 = t;
145 Angle += 120.0*dt;
146 glutPostRedisplay();
147 }
148
149
150
151 /* change view Angle, exit upon ESC */
152 static void key(unsigned char k, int x, int y)
153 {
154 (void) x;
155 (void) y;
156 switch (k) {
157 case 'a':
158 Anim = !Anim;
159 if (Anim)
160 glutIdleFunc( idle );
161 else
162 glutIdleFunc( NULL );
163 break;
164 case 27:
165 glutDestroyWindow(Window);
166 exit(0);
167 }
168 }
169
170
171
172 /* new window size or exposure */
173 static void reshape( int width, int height )
174 {
175 glViewport(0, 0, (GLint)width, (GLint)height);
176 glMatrixMode(GL_PROJECTION);
177 glLoadIdentity();
178 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
179 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
180 glMatrixMode(GL_MODELVIEW);
181 glLoadIdentity();
182 glTranslatef( 0.0, 0.0, -8.0 );
183 }
184
185
186 static void init( void )
187 {
188 /* Setup texturing */
189 glEnable( GL_TEXTURE_2D );
190 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
191
192
193 glBindTexture( GL_TEXTURE_2D, 0 );
194 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
195 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
196 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
197 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
198 }
199
200
201
202 int main( int argc, char *argv[] )
203 {
204 glutInit(&argc, argv);
205 glutInitWindowPosition(0, 0);
206 glutInitWindowSize(300, 300);
207 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
208
209 Window = glutCreateWindow("Texture Objects");
210 if (!Window) {
211 exit(1);
212 }
213
214 init();
215
216 glutReshapeFunc( reshape );
217 glutKeyboardFunc( key );
218 if (Anim)
219 glutIdleFunc( idle );
220 glutDisplayFunc( draw );
221 glutMainLoop();
222 return 0;
223 }