Convert libGL and DRI drivers to require libdrm.
[mesa-demos.git] / src / tests / texrect.c
blob61c1fdd6b4fa61f2902af5c118c7731aded64b99
1 /* $Id: texrect.c,v 1.5 2004/05/06 20:27:32 brianp Exp $ */
3 /* GL_NV_texture_rectangle test
5 * Brian Paul
6 * 14 June 2002
7 */
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 #include "readtex.h"
18 #define TEXTURE_0_FILE "../images/girl.rgb"
19 #define TEXTURE_1_FILE "../images/reflect.rgb"
21 #define TEX0 1
22 #define TEX7 8
23 #define ANIMATE 10
24 #define CLAMP 20
25 #define CLAMP_TO_EDGE 21
26 #define CLAMP_TO_BORDER 22
27 #define LINEAR_FILTER 30
28 #define NEAREST_FILTER 31
29 #define QUIT 100
31 static GLboolean Animate = GL_FALSE;
32 static GLint NumUnits = 2;
33 static GLboolean TexEnabled[8];
34 static GLint Width[8], Height[8]; /* image sizes */
35 static GLenum Format[8];
37 static GLfloat Xrot = 00.0, Yrot = 00.0, Zrot = 0.0;
40 static void Idle( void )
42 Zrot = glutGet(GLUT_ELAPSED_TIME) * 0.01;
43 glutPostRedisplay();
47 static void DrawObject(void)
49 GLint i;
50 GLfloat d = 10; /* so we can see how borders are handled */
52 glColor3f(.1, .1, .1); /* modulate this */
54 glPushMatrix();
56 glRotatef(Zrot, 0, 0, 1);
58 glBegin(GL_QUADS);
60 for (i = 0; i < NumUnits; i++)
61 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, -d, -d);
62 glVertex2f(-1.0, -1.0);
64 for (i = 0; i < NumUnits; i++)
65 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, Width[i]+d, -d);
66 glVertex2f(1.0, -1.0);
68 for (i = 0; i < NumUnits; i++)
69 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, Width[i]+d, Height[i]+d);
70 glVertex2f(1.0, 1.0);
72 for (i = 0; i < NumUnits; i++)
73 glMultiTexCoord2fARB(GL_TEXTURE0_ARB + i, -d, Height[i]+d);
74 glVertex2f(-1.0, 1.0);
76 glEnd();
77 glPopMatrix();
81 static void Display( void )
83 glClear( GL_COLOR_BUFFER_BIT );
85 glPushMatrix();
86 glRotatef(Xrot, 1.0, 0.0, 0.0);
87 glRotatef(Yrot, 0.0, 1.0, 0.0);
88 glRotatef(Zrot, 0.0, 0.0, 1.0);
89 glScalef(5.0, 5.0, 5.0);
90 DrawObject();
91 glPopMatrix();
93 glutSwapBuffers();
97 static void Reshape( int width, int height )
99 glViewport( 0, 0, width, height );
100 glMatrixMode( GL_PROJECTION );
101 glLoadIdentity();
102 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 100.0 );
103 glMatrixMode( GL_MODELVIEW );
104 glLoadIdentity();
105 glTranslatef( 0.0, 0.0, -35.0 );
109 static void ModeMenu(int entry)
111 GLint i;
112 if (entry >= TEX0 && entry < TEX0 + NumUnits) {
113 /* toggle */
114 i = entry - TEX0;
115 TexEnabled[i] = !TexEnabled[i];
116 glActiveTextureARB(GL_TEXTURE0_ARB + i);
117 if (TexEnabled[i]) {
118 glEnable(GL_TEXTURE_RECTANGLE_NV);
120 else {
121 glDisable(GL_TEXTURE_RECTANGLE_NV);
123 printf("Enabled: ");
124 for (i = 0; i < NumUnits; i++)
125 printf("%d ", (int) TexEnabled[i]);
126 printf("\n");
128 else if (entry==ANIMATE) {
129 Animate = !Animate;
130 if (Animate)
131 glutIdleFunc(Idle);
132 else
133 glutIdleFunc(NULL);
135 else if (entry==CLAMP) {
136 for (i = 0; i < NumUnits; i++) {
137 glActiveTextureARB(GL_TEXTURE0_ARB + i);
138 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP);
139 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP);
142 else if (entry==CLAMP_TO_EDGE) {
143 for (i = 0; i < NumUnits; i++) {
144 glActiveTextureARB(GL_TEXTURE0_ARB + i);
145 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
146 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
149 else if (entry==CLAMP_TO_BORDER) {
150 for (i = 0; i < NumUnits; i++) {
151 glActiveTextureARB(GL_TEXTURE0_ARB + i);
152 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
153 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
156 else if (entry==NEAREST_FILTER) {
157 for (i = 0; i < NumUnits; i++) {
158 glActiveTextureARB(GL_TEXTURE0_ARB + i);
159 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
160 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
163 else if (entry==LINEAR_FILTER) {
164 for (i = 0; i < NumUnits; i++) {
165 glActiveTextureARB(GL_TEXTURE0_ARB + i);
166 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
171 else if (entry==QUIT) {
172 exit(0);
175 glutPostRedisplay();
179 static void Key( unsigned char key, int x, int y )
181 (void) x;
182 (void) y;
183 switch (key) {
184 case 'z':
185 Zrot -= 1.0;
186 break;
187 case 'Z':
188 Zrot += 1.0;
189 break;
190 case 'a':
191 Animate = !Animate;
192 if (Animate)
193 glutIdleFunc(Idle);
194 else
195 glutIdleFunc(NULL);
196 break;
197 case 27:
198 exit(0);
199 break;
201 glutPostRedisplay();
205 static void SpecialKey( int key, int x, int y )
207 float step = 3.0;
208 (void) x;
209 (void) y;
211 switch (key) {
212 case GLUT_KEY_UP:
213 Xrot += step;
214 break;
215 case GLUT_KEY_DOWN:
216 Xrot -= step;
217 break;
218 case GLUT_KEY_LEFT:
219 Yrot += step;
220 break;
221 case GLUT_KEY_RIGHT:
222 Yrot -= step;
223 break;
225 glutPostRedisplay();
229 static void Init( int argc, char *argv[] )
231 const GLenum wrap = GL_CLAMP;
232 GLuint texObj[8];
233 GLint size, i;
235 if (!glutExtensionSupported("GL_ARB_multitexture")) {
236 printf("Sorry, GL_ARB_multitexture needed by this program\n");
237 exit(1);
240 if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
241 printf("Sorry, GL_NV_texture_rectangle needed by this program\n");
242 exit(1);
245 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &NumUnits);
246 printf("%d texture units supported, using 2.\n", NumUnits);
247 if (NumUnits > 2)
248 NumUnits = 2;
250 glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV, &size);
251 printf("%d x %d max texture rectangle size\n", size, size);
253 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
255 for (i = 0; i < NumUnits; i++) {
256 TexEnabled[i] = GL_TRUE;
259 /* allocate two texture objects */
260 glGenTextures(NumUnits, texObj);
262 /* setup the texture objects */
263 for (i = 0; i < NumUnits; i++) {
265 glActiveTextureARB(GL_TEXTURE0_ARB + i);
267 glBindTexture(GL_TEXTURE_RECTANGLE_NV, texObj[i]);
268 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,
269 GL_TEXTURE_MIN_FILTER, GL_NEAREST);
270 glTexParameteri(GL_TEXTURE_RECTANGLE_NV,
271 GL_TEXTURE_MAG_FILTER, GL_NEAREST);
272 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, wrap);
273 glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, wrap);
275 if (i == 0) {
276 GLubyte *img = LoadRGBImage(TEXTURE_0_FILE, &Width[0], &Height[0],
277 &Format[0]);
278 if (!img) {
279 printf("Error: couldn't load texture image\n");
280 exit(1);
282 printf("Texture %d: %s (%d x %d)\n", i,
283 TEXTURE_0_FILE, Width[0], Height[0]);
284 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB,
285 Width[0], Height[0], 0,
286 Format[0], GL_UNSIGNED_BYTE, img);
288 else {
289 GLubyte *img = LoadRGBImage(TEXTURE_1_FILE, &Width[1], &Height[1],
290 &Format[1]);
291 if (!img) {
292 printf("Error: couldn't load texture image\n");
293 exit(1);
295 printf("Texture %d: %s (%d x %d)\n", i,
296 TEXTURE_1_FILE, Width[1], Height[1]);
297 glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB,
298 Width[1], Height[1], 0,
299 Format[1], GL_UNSIGNED_BYTE, img);
302 if (i < 1)
303 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
304 else
305 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
307 if (TexEnabled[i])
308 glEnable(GL_TEXTURE_RECTANGLE_NV);
311 glShadeModel(GL_FLAT);
312 glClearColor(0.3, 0.3, 0.4, 1.0);
314 if (argc > 1 && strcmp(argv[1], "-info")==0) {
315 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
316 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
317 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
318 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
323 int main( int argc, char *argv[] )
325 GLint i;
327 glutInit( &argc, argv );
328 glutInitWindowSize( 300, 300 );
329 glutInitWindowPosition( 0, 0 );
330 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
331 glutCreateWindow(argv[0] );
333 Init( argc, argv );
335 glutReshapeFunc( Reshape );
336 glutKeyboardFunc( Key );
337 glutSpecialFunc( SpecialKey );
338 glutDisplayFunc( Display );
339 if (Animate)
340 glutIdleFunc( Idle );
342 glutCreateMenu(ModeMenu);
344 for (i = 0; i < NumUnits; i++) {
345 char s[100];
346 sprintf(s, "Toggle Texture %d", i);
347 glutAddMenuEntry(s, TEX0 + i);
349 glutAddMenuEntry("Toggle Animation", ANIMATE);
350 glutAddMenuEntry("GL_CLAMP", CLAMP);
351 glutAddMenuEntry("GL_CLAMP_TO_EDGE", CLAMP_TO_EDGE);
352 glutAddMenuEntry("GL_CLAMP_TO_BORDER", CLAMP_TO_BORDER);
353 glutAddMenuEntry("GL_NEAREST", NEAREST_FILTER);
354 glutAddMenuEntry("GL_LINEAR", LINEAR_FILTER);
355 glutAddMenuEntry("Quit", QUIT);
356 glutAttachMenu(GLUT_RIGHT_BUTTON);
358 glutMainLoop();
359 return 0;