updated email addresses
[mesa.git] / progs / tests / texrect.c
1 /* $Id: texrect.c,v 1.2 2002/10/15 14:43:55 brianp Exp $ */
2
3 /* GL_NV_texture_rectangle test
4 *
5 * Brian Paul
6 * 14 June 2002
7 */
8
9
10 #define GL_GLEXT_PROTOTYPES
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <GL/glut.h>
16
17 #include "readtex.c" /* I know, this is a hack. */
18
19 #define TEXTURE_0_FILE "../images/girl.rgb"
20 #define TEXTURE_1_FILE "../images/reflect.rgb"
21
22 #define TEX0 1
23 #define TEX7 8
24 #define ANIMATE 10
25 #define QUIT 100
26
27 static GLboolean Animate = GL_TRUE;
28 static GLint NumUnits = 2;
29 static GLboolean TexEnabled[8];
30 static GLint Width[8], Height[8]; /* image sizes */
31 static GLenum Format[8];
32
33 static GLfloat Xrot = 00.0, Yrot = 00.0, Zrot = 0.0;
34
35
36 static void Idle( void )
37 {
38 Zrot += 1.0;
39 glutPostRedisplay();
40 }
41
42
43 static void DrawObject(void)
44 {
45 GLint i;
46 GLfloat d = 0; /* set this >0 to test clamping */
47
48 glColor3f(.1, .1, .1); /* modulate this */
49
50 glPushMatrix();
51
52 glRotatef(Zrot, 0, 0, 1);
53
54 glBegin(GL_QUADS);
55
56 for (i = 0; i < NumUnits; i++)
57 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, -d, -d);
58 glVertex2f(-1.0, -1.0);
59
60 for (i = 0; i < NumUnits; i++)
61 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, Width[i]+d, -3);
62 glVertex2f(1.0, -1.0);
63
64 for (i = 0; i < NumUnits; i++)
65 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, Width[i]+d, Height[i]+3);
66 glVertex2f(1.0, 1.0);
67
68 for (i = 0; i < NumUnits; i++)
69 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, -d, Height[i]+d);
70 glVertex2f(-1.0, 1.0);
71
72 glEnd();
73 glPopMatrix();
74 }
75
76
77 static void Display( void )
78 {
79 glClear( GL_COLOR_BUFFER_BIT );
80
81 glPushMatrix();
82 glRotatef(Xrot, 1.0, 0.0, 0.0);
83 glRotatef(Yrot, 0.0, 1.0, 0.0);
84 glRotatef(Zrot, 0.0, 0.0, 1.0);
85 glScalef(5.0, 5.0, 5.0);
86 DrawObject();
87 glPopMatrix();
88
89 glutSwapBuffers();
90 }
91
92
93 static void Reshape( int width, int height )
94 {
95 glViewport( 0, 0, width, height );
96 glMatrixMode( GL_PROJECTION );
97 glLoadIdentity();
98 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 100.0 );
99 glMatrixMode( GL_MODELVIEW );
100 glLoadIdentity();
101 glTranslatef( 0.0, 0.0, -35.0 );
102 }
103
104
105 static void ModeMenu(int entry)
106 {
107 if (entry >= TEX0 && entry < TEX0 + NumUnits) {
108 /* toggle */
109 GLint i = entry - TEX0;
110 TexEnabled[i] = !TexEnabled[i];
111 glActiveTextureARB(GL_TEXTURE0_ARB + i);
112 if (TexEnabled[i]) {
113 glEnable(GL_TEXTURE_RECTANGLE_NV);
114 }
115 else {
116 glDisable(GL_TEXTURE_RECTANGLE_NV);
117 }
118 printf("Enabled: ");
119 for (i = 0; i < NumUnits; i++)
120 printf("%d ", (int) TexEnabled[i]);
121 printf("\n");
122 }
123 else if (entry==ANIMATE) {
124 Animate = !Animate;
125 if (Animate)
126 glutIdleFunc(Idle);
127 else
128 glutIdleFunc(NULL);
129 }
130 else if (entry==QUIT) {
131 exit(0);
132 }
133
134 glutPostRedisplay();
135 }
136
137
138 static void Key( unsigned char key, int x, int y )
139 {
140 (void) x;
141 (void) y;
142 switch (key) {
143 case 27:
144 exit(0);
145 break;
146 }
147 glutPostRedisplay();
148 }
149
150
151 static void SpecialKey( int key, int x, int y )
152 {
153 float step = 3.0;
154 (void) x;
155 (void) y;
156
157 switch (key) {
158 case GLUT_KEY_UP:
159 Xrot += step;
160 break;
161 case GLUT_KEY_DOWN:
162 Xrot -= step;
163 break;
164 case GLUT_KEY_LEFT:
165 Yrot += step;
166 break;
167 case GLUT_KEY_RIGHT:
168 Yrot -= step;
169 break;
170 }
171 glutPostRedisplay();
172 }
173
174
175 static void Init( int argc, char *argv[] )
176 {
177 GLuint texObj[8];
178 GLint size, i;
179
180 if (!glutExtensionSupported("GL_ARB_multitexture")) {
181 printf("Sorry, GL_ARB_multitexture needed by this program\n");
182 exit(1);
183 }
184
185 if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
186 printf("Sorry, GL_NV_texture_rectangle needed by this program\n");
187 exit(1);
188 }
189
190 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
191 printf("%d texture units supported, using 2.\n", NumUnits);
192 if (NumUnits > 2)
193 NumUnits = 2;
194
195 glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, &size);
196 printf("%d x %d max texture rectangle size\n", size, size);
197
198 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
199
200 for (i = 0; i < NumUnits; i++) {
201 TexEnabled[i] = GL_TRUE;
202 }
203
204 /* allocate two texture objects */
205 glGenTextures(NumUnits, texObj);
206
207 /* setup the texture objects */
208 for (i = 0; i < NumUnits; i++) {
209
210 glActiveTextureARB(GL_TEXTURE0_ARB + i);
211
212 glBindTexture(GL_TEXTURE_RECTANGLE_NV, texObj[i]);
213 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,
214 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
215 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,
216 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
217
218 if (i == 0) {
219 GLubyte *img = LoadRGBImage(TEXTURE_0_FILE, &Width[0], &Height[0],
220 &Format[0]);
221 if (!img) {
222 printf("Error: couldn't load texture image\n");
223 exit(1);
224 }
225 printf("Texture %d: %s (%d x %d)\n", i,
226 TEXTURE_0_FILE, Width[0], Height[0]);
227 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB,
228 Width[0], Height[0], 0,
229 Format[0], GL_UNSIGNED_BYTE, img);
230 }
231 else {
232 GLubyte *img = LoadRGBImage(TEXTURE_1_FILE, &Width[1], &Height[1],
233 &Format[1]);
234 if (!img) {
235 printf("Error: couldn't load texture image\n");
236 exit(1);
237 }
238 printf("Texture %d: %s (%d x %d)\n", i,
239 TEXTURE_1_FILE, Width[1], Height[1]);
240 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB,
241 Width[1], Height[1], 0,
242 Format[1], GL_UNSIGNED_BYTE, img);
243 }
244
245 if (i < 1)
246 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
247 else
248 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
249
250 if (TexEnabled[i])
251 glEnable(GL_TEXTURE_RECTANGLE_NV);
252 }
253
254 glShadeModel(GL_FLAT);
255 glClearColor(0.3, 0.3, 0.4, 1.0);
256
257 if (argc > 1 && strcmp(argv[1], "-info")==0) {
258 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
259 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
260 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
261 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
262 }
263 }
264
265
266 int main( int argc, char *argv[] )
267 {
268 GLint i;
269
270 glutInit( &argc, argv );
271 glutInitWindowSize( 300, 300 );
272 glutInitWindowPosition( 0, 0 );
273 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
274 glutCreateWindow(argv[0] );
275
276 Init( argc, argv );
277
278 glutReshapeFunc( Reshape );
279 glutKeyboardFunc( Key );
280 glutSpecialFunc( SpecialKey );
281 glutDisplayFunc( Display );
282 if (Animate)
283 glutIdleFunc( Idle );
284
285 glutCreateMenu(ModeMenu);
286
287 for (i = 0; i < NumUnits; i++) {
288 char s[100];
289 sprintf(s, "Toggle Texture %d", i);
290 glutAddMenuEntry(s, TEX0 + i);
291 }
292 glutAddMenuEntry("Toggle Animation", ANIMATE);
293 glutAddMenuEntry("Quit", QUIT);
294 glutAttachMenu(GLUT_RIGHT_BUTTON);
295
296 glutMainLoop();
297 return 0;
298 }