Make git ignore files only generated at build time.
[mesa.git] / progs / tests / multitexarray.c
1 /*
2 * Test vertex arrays and multitexture.
3 * Press 'a' to toggle vertex arrays on/off.
4 * When you run this program you should see a square with four colors:
5 *
6 * +------+------+
7 * |yellow| pink |
8 * +------+------+
9 * |green | blue |
10 * +------+------+
11 */
12
13
14 #include <assert.h>
15 #include <math.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include "GL/glut.h"
20
21 static GLuint Window = 0;
22
23 static GLuint TexObj[2];
24 static GLfloat Angle = 0.0f;
25 static GLboolean UseArrays = 1, Anim = 0;
26
27 static GLfloat VertArray[4][2] = {
28 {-1.2, -1.2}, {1.2, -1.2}, {1.2, 1.2}, {-1.2, 1.2}
29 };
30
31 static GLfloat Tex0Array[4][2] = {
32 {0, 0}, {1, 0}, {1, 1}, {0, 1}
33 };
34
35 static GLfloat Tex1Array[4][2] = {
36 {0, 0}, {1, 0}, {1, 1}, {0, 1}
37 };
38
39
40 static void init_arrays(void)
41 {
42 glVertexPointer(2, GL_FLOAT, 0, VertArray);
43 glEnableClientState(GL_VERTEX_ARRAY);
44
45 glClientActiveTextureARB(GL_TEXTURE0_ARB);
46 glTexCoordPointer(2, GL_FLOAT, 0, Tex0Array);
47 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
48
49 glClientActiveTextureARB(GL_TEXTURE1_ARB);
50 glTexCoordPointer(2, GL_FLOAT, 0, Tex1Array);
51 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
52 }
53
54
55 static void draw( void )
56 {
57 glClear( GL_COLOR_BUFFER_BIT );
58
59 glColor3f( 0.0, 0.0, 0.0 );
60
61 /* draw first polygon */
62 glPushMatrix();
63 glRotatef( Angle, 0.0, 0.0, 1.0 );
64
65 if (UseArrays) {
66 glDrawArrays(GL_POLYGON, 0, 4);
67 }
68 else {
69 glBegin( GL_POLYGON );
70 glTexCoord2f( 0.0, 0.0 );
71 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
72 glVertex2f( -1.0, -1.0 );
73
74 glTexCoord2f( 1.0, 0.0 );
75 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
76 glVertex2f( 1.0, -1.0 );
77
78 glTexCoord2f( 1.0, 1.0 );
79 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
80 glVertex2f( 1.0, 1.0 );
81
82 glTexCoord2f( 0.0, 1.0 );
83 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
84 glVertex2f( -1.0, 1.0 );
85 glEnd();
86 }
87
88 glPopMatrix();
89
90 glutSwapBuffers();
91 }
92
93
94
95 static void idle( void )
96 {
97 Angle += 2.0;
98 glutPostRedisplay();
99 }
100
101
102
103 /* change view Angle, exit upon ESC */
104 static void key(unsigned char k, int x, int y)
105 {
106 (void) x;
107 (void) y;
108 switch (k) {
109 case 'a':
110 UseArrays = !UseArrays;
111 printf("UseArrays: %d\n", UseArrays);
112 break;
113 case ' ':
114 Anim = !Anim;
115 if (Anim)
116 glutIdleFunc(idle);
117 else
118 glutIdleFunc(NULL);
119 break;
120 case 27:
121 glDeleteTextures( 2, TexObj );
122 glutDestroyWindow(Window);
123 exit(0);
124 }
125 glutPostRedisplay();
126 }
127
128
129
130 /* new window size or exposure */
131 static void reshape( int width, int height )
132 {
133 glViewport(0, 0, (GLint)width, (GLint)height);
134 glMatrixMode(GL_PROJECTION);
135 glLoadIdentity();
136 /* glOrtho( -3.0, 3.0, -3.0, 3.0, -10.0, 10.0 );*/
137 glFrustum( -2.0, 2.0, -2.0, 2.0, 6.0, 20.0 );
138 glMatrixMode(GL_MODELVIEW);
139 glLoadIdentity();
140 glTranslatef( 0.0, 0.0, -8.0 );
141 }
142
143
144 static void init( void )
145 {
146 static int width=8, height=8;
147 GLubyte tex[64][3];
148 GLint i, j;
149
150 /* generate texture object IDs */
151 glGenTextures( 2, TexObj );
152
153 /*
154 * setup first texture object
155 */
156 glActiveTextureARB(GL_TEXTURE0_ARB);
157 glEnable( GL_TEXTURE_2D );
158 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
159
160 glBindTexture( GL_TEXTURE_2D, TexObj[0] );
161 assert(glIsTexture(TexObj[0]));
162
163 /* red over black */
164 for (i=0;i<height;i++) {
165 for (j=0;j<width;j++) {
166 int p = i*width+j;
167 if (i < height / 2) {
168 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 0;
169 }
170 else {
171 tex[p][0] = 255; tex[p][1] = 0; tex[p][2] = 0;
172 }
173 }
174 }
175
176 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
177 GL_RGB, GL_UNSIGNED_BYTE, tex );
178 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
179 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
180 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
181 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
182
183
184 /*
185 * setup second texture object
186 */
187 glActiveTextureARB(GL_TEXTURE1_ARB);
188 glEnable( GL_TEXTURE_2D );
189 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
190
191 glBindTexture( GL_TEXTURE_2D, TexObj[1] );
192 assert(glIsTexture(TexObj[1]));
193
194 /* left=green, right = blue */
195 for (i=0;i<height;i++) {
196 for (j=0;j<width;j++) {
197 int p = i*width+j;
198 if (j < width / 2) {
199 tex[p][0] = 0; tex[p][1] = 255; tex[p][2] = 0;
200 }
201 else {
202 tex[p][0] = 0; tex[p][1] = 0; tex[p][2] = 255;
203 }
204 }
205 }
206 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, height, 0,
207 GL_RGB, GL_UNSIGNED_BYTE, tex );
208 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
209 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
210 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
211 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
212 }
213
214
215
216 int main( int argc, char *argv[] )
217 {
218 glutInit(&argc, argv);
219 glutInitWindowPosition(0, 0);
220 glutInitWindowSize(300, 300);
221 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
222
223 Window = glutCreateWindow("Texture Objects");
224 if (!Window) {
225 exit(1);
226 }
227
228 init();
229 init_arrays();
230
231 glutReshapeFunc( reshape );
232 glutKeyboardFunc( key );
233 if (Anim)
234 glutIdleFunc( idle );
235 glutDisplayFunc( draw );
236 glutMainLoop();
237 return 0;
238 }