1 /* $Id: texrect.c,v 1.1 2002/06/15 03:08:19 brianp Exp $ */
3 /* GL_NV_texture_rectangle test
16 #include "readtex.c" /* I know, this is a hack. */
18 #define TEXTURE_0_FILE "../images/girl.rgb"
19 #define TEXTURE_1_FILE "../images/reflect.rgb"
26 static GLboolean Animate
= GL_TRUE
;
27 static GLint NumUnits
= 2;
28 static GLboolean TexEnabled
[8];
29 static GLint Width
[8], Height
[8]; /* image sizes */
30 static GLenum Format
[8];
32 static GLfloat Xrot
= 00.0, Yrot
= 00.0, Zrot
= 0.0;
35 static void Idle( void )
42 static void DrawObject(void)
45 GLfloat d
= 0; /* set this >0 to test clamping */
47 glColor3f(.1, .1, .1); /* modulate this */
51 glRotatef(Zrot
, 0, 0, 1);
55 for (i
= 0; i
< NumUnits
; i
++)
56 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, -d
, -d
);
57 glVertex2f(-1.0, -1.0);
59 for (i
= 0; i
< NumUnits
; i
++)
60 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, Width
[i
]+d
, -3);
61 glVertex2f(1.0, -1.0);
63 for (i
= 0; i
< NumUnits
; i
++)
64 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, Width
[i
]+d
, Height
[i
]+3);
67 for (i
= 0; i
< NumUnits
; i
++)
68 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, -d
, Height
[i
]+d
);
69 glVertex2f(-1.0, 1.0);
76 static void Display( void )
78 glClear( GL_COLOR_BUFFER_BIT
);
81 glRotatef(Xrot
, 1.0, 0.0, 0.0);
82 glRotatef(Yrot
, 0.0, 1.0, 0.0);
83 glRotatef(Zrot
, 0.0, 0.0, 1.0);
84 glScalef(5.0, 5.0, 5.0);
92 static void Reshape( int width
, int height
)
94 glViewport( 0, 0, width
, height
);
95 glMatrixMode( GL_PROJECTION
);
97 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 100.0 );
98 glMatrixMode( GL_MODELVIEW
);
100 glTranslatef( 0.0, 0.0, -35.0 );
104 static void ModeMenu(int entry
)
106 if (entry
>= TEX0
&& entry
< TEX0
+ NumUnits
) {
108 GLint i
= entry
- TEX0
;
109 TexEnabled
[i
] = !TexEnabled
[i
];
110 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
112 glEnable(GL_TEXTURE_RECTANGLE_NV
);
115 glDisable(GL_TEXTURE_RECTANGLE_NV
);
118 for (i
= 0; i
< NumUnits
; i
++)
119 printf("%d ", (int) TexEnabled
[i
]);
122 else if (entry
==ANIMATE
) {
129 else if (entry
==QUIT
) {
137 static void Key( unsigned char key
, int x
, int y
)
150 static void SpecialKey( int key
, int x
, int y
)
174 static void Init( int argc
, char *argv
[] )
179 if (!glutExtensionSupported("GL_ARB_multitexture")) {
180 printf("Sorry, GL_ARB_multitexture needed by this program\n");
184 if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
185 printf("Sorry, GL_NV_texture_rectangle needed by this program\n");
189 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &NumUnits
);
190 printf("%d texture units supported, using 2.\n", NumUnits
);
194 glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV
, &size
);
195 printf("%d x %d max texture rectangle size\n", size
, size
);
197 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
199 for (i
= 0; i
< NumUnits
; i
++) {
200 TexEnabled
[i
] = GL_TRUE
;
203 /* allocate two texture objects */
204 glGenTextures(NumUnits
, texObj
);
206 /* setup the texture objects */
207 for (i
= 0; i
< NumUnits
; i
++) {
209 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
211 glBindTexture(GL_TEXTURE_RECTANGLE_NV
, texObj
[i
]);
212 glTexParameteri(GL_TEXTURE_RECTANGLE_NV
,
213 GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
214 glTexParameteri(GL_TEXTURE_RECTANGLE_NV
,
215 GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
218 GLubyte
*img
= LoadRGBImage(TEXTURE_0_FILE
, &Width
[0], &Height
[0],
221 printf("Error: couldn't load texture image\n");
224 printf("Texture %d: %s (%d x %d)\n", i
,
225 TEXTURE_0_FILE
, Width
[0], Height
[0]);
226 glTexImage2D(GL_TEXTURE_RECTANGLE_NV
, 0, GL_RGB
,
227 Width
[0], Height
[0], 0,
228 Format
[0], GL_UNSIGNED_BYTE
, img
);
231 GLubyte
*img
= LoadRGBImage(TEXTURE_1_FILE
, &Width
[1], &Height
[1],
234 printf("Error: couldn't load texture image\n");
237 printf("Texture %d: %s (%d x %d)\n", i
,
238 TEXTURE_1_FILE
, Width
[1], Height
[1]);
239 glTexImage2D(GL_TEXTURE_RECTANGLE_NV
, 0, GL_RGB
,
240 Width
[1], Height
[1], 0,
241 Format
[1], GL_UNSIGNED_BYTE
, img
);
245 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
247 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
250 glEnable(GL_TEXTURE_RECTANGLE_NV
);
253 glShadeModel(GL_FLAT
);
254 glClearColor(0.3, 0.3, 0.4, 1.0);
256 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
257 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
258 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
259 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
260 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
265 int main( int argc
, char *argv
[] )
269 glutInit( &argc
, argv
);
270 glutInitWindowSize( 300, 300 );
271 glutInitWindowPosition( 0, 0 );
272 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
273 glutCreateWindow(argv
[0] );
277 glutReshapeFunc( Reshape
);
278 glutKeyboardFunc( Key
);
279 glutSpecialFunc( SpecialKey
);
280 glutDisplayFunc( Display
);
282 glutIdleFunc( Idle
);
284 glutCreateMenu(ModeMenu
);
286 for (i
= 0; i
< NumUnits
; i
++) {
288 sprintf(s
, "Toggle Texture %d", i
);
289 glutAddMenuEntry(s
, TEX0
+ i
);
291 glutAddMenuEntry("Toggle Animation", ANIMATE
);
292 glutAddMenuEntry("Quit", QUIT
);
293 glutAttachMenu(GLUT_RIGHT_BUTTON
);