1 /* $Id: texrect.c,v 1.5 2004/05/06 20:27:32 brianp Exp $ */
3 /* GL_NV_texture_rectangle test
10 #define GL_GLEXT_PROTOTYPES
18 #define TEXTURE_0_FILE "../images/girl.rgb"
19 #define TEXTURE_1_FILE "../images/reflect.rgb"
25 #define CLAMP_TO_EDGE 21
26 #define CLAMP_TO_BORDER 22
27 #define LINEAR_FILTER 30
28 #define NEAREST_FILTER 31
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;
47 static void DrawObject(void)
50 GLfloat d
= 10; /* so we can see how borders are handled */
52 glColor3f(.1, .1, .1); /* modulate this */
56 glRotatef(Zrot
, 0, 0, 1);
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
);
72 for (i
= 0; i
< NumUnits
; i
++)
73 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, -d
, Height
[i
]+d
);
74 glVertex2f(-1.0, 1.0);
81 static void Display( void )
83 glClear( GL_COLOR_BUFFER_BIT
);
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);
97 static void Reshape( int width
, int height
)
99 glViewport( 0, 0, width
, height
);
100 glMatrixMode( GL_PROJECTION
);
102 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 100.0 );
103 glMatrixMode( GL_MODELVIEW
);
105 glTranslatef( 0.0, 0.0, -35.0 );
109 static void ModeMenu(int entry
)
112 if (entry
>= TEX0
&& entry
< TEX0
+ NumUnits
) {
115 TexEnabled
[i
] = !TexEnabled
[i
];
116 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
118 glEnable(GL_TEXTURE_RECTANGLE_NV
);
121 glDisable(GL_TEXTURE_RECTANGLE_NV
);
124 for (i
= 0; i
< NumUnits
; i
++)
125 printf("%d ", (int) TexEnabled
[i
]);
128 else if (entry
==ANIMATE
) {
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
) {
179 static void Key( unsigned char key
, int x
, int y
)
205 static void SpecialKey( int key
, int x
, int y
)
229 static void Init( int argc
, char *argv
[] )
231 const GLenum wrap
= GL_CLAMP
;
235 if (!glutExtensionSupported("GL_ARB_multitexture")) {
236 printf("Sorry, GL_ARB_multitexture needed by this program\n");
240 if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
241 printf("Sorry, GL_NV_texture_rectangle needed by this program\n");
245 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &NumUnits
);
246 printf("%d texture units supported, using 2.\n", NumUnits
);
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
);
276 GLubyte
*img
= LoadRGBImage(TEXTURE_0_FILE
, &Width
[0], &Height
[0],
279 printf("Error: couldn't load texture image\n");
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
);
289 GLubyte
*img
= LoadRGBImage(TEXTURE_1_FILE
, &Width
[1], &Height
[1],
292 printf("Error: couldn't load texture image\n");
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
);
303 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
305 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
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
[] )
327 glutInit( &argc
, argv
);
328 glutInitWindowSize( 300, 300 );
329 glutInitWindowPosition( 0, 0 );
330 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
331 glutCreateWindow(argv
[0] );
335 glutReshapeFunc( Reshape
);
336 glutKeyboardFunc( Key
);
337 glutSpecialFunc( SpecialKey
);
338 glutDisplayFunc( Display
);
340 glutIdleFunc( Idle
);
342 glutCreateMenu(ModeMenu
);
344 for (i
= 0; i
< NumUnits
; i
++) {
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
);