1 /* $Id: texrect.c,v 1.2 2002/10/15 14:43:55 brianp Exp $ */
3 /* GL_NV_texture_rectangle test
10 #define GL_GLEXT_PROTOTYPES
17 #include "readtex.c" /* I know, this is a hack. */
19 #define TEXTURE_0_FILE "../images/girl.rgb"
20 #define TEXTURE_1_FILE "../images/reflect.rgb"
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];
33 static GLfloat Xrot
= 00.0, Yrot
= 00.0, Zrot
= 0.0;
36 static void Idle( void )
43 static void DrawObject(void)
46 GLfloat d
= 0; /* set this >0 to test clamping */
48 glColor3f(.1, .1, .1); /* modulate this */
52 glRotatef(Zrot
, 0, 0, 1);
56 for (i
= 0; i
< NumUnits
; i
++)
57 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, -d
, -d
);
58 glVertex2f(-1.0, -1.0);
60 for (i
= 0; i
< NumUnits
; i
++)
61 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, Width
[i
]+d
, -3);
62 glVertex2f(1.0, -1.0);
64 for (i
= 0; i
< NumUnits
; i
++)
65 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, Width
[i
]+d
, Height
[i
]+3);
68 for (i
= 0; i
< NumUnits
; i
++)
69 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
+ i
, -d
, Height
[i
]+d
);
70 glVertex2f(-1.0, 1.0);
77 static void Display( void )
79 glClear( GL_COLOR_BUFFER_BIT
);
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);
93 static void Reshape( int width
, int height
)
95 glViewport( 0, 0, width
, height
);
96 glMatrixMode( GL_PROJECTION
);
98 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 100.0 );
99 glMatrixMode( GL_MODELVIEW
);
101 glTranslatef( 0.0, 0.0, -35.0 );
105 static void ModeMenu(int entry
)
107 if (entry
>= TEX0
&& entry
< TEX0
+ NumUnits
) {
109 GLint i
= entry
- TEX0
;
110 TexEnabled
[i
] = !TexEnabled
[i
];
111 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
113 glEnable(GL_TEXTURE_RECTANGLE_NV
);
116 glDisable(GL_TEXTURE_RECTANGLE_NV
);
119 for (i
= 0; i
< NumUnits
; i
++)
120 printf("%d ", (int) TexEnabled
[i
]);
123 else if (entry
==ANIMATE
) {
130 else if (entry
==QUIT
) {
138 static void Key( unsigned char key
, int x
, int y
)
151 static void SpecialKey( int key
, int x
, int y
)
175 static void Init( int argc
, char *argv
[] )
180 if (!glutExtensionSupported("GL_ARB_multitexture")) {
181 printf("Sorry, GL_ARB_multitexture needed by this program\n");
185 if (!glutExtensionSupported("GL_NV_texture_rectangle")) {
186 printf("Sorry, GL_NV_texture_rectangle needed by this program\n");
190 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB
, &NumUnits
);
191 printf("%d texture units supported, using 2.\n", NumUnits
);
195 glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_NV
, &size
);
196 printf("%d x %d max texture rectangle size\n", size
, size
);
198 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
200 for (i
= 0; i
< NumUnits
; i
++) {
201 TexEnabled
[i
] = GL_TRUE
;
204 /* allocate two texture objects */
205 glGenTextures(NumUnits
, texObj
);
207 /* setup the texture objects */
208 for (i
= 0; i
< NumUnits
; i
++) {
210 glActiveTextureARB(GL_TEXTURE0_ARB
+ i
);
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
);
219 GLubyte
*img
= LoadRGBImage(TEXTURE_0_FILE
, &Width
[0], &Height
[0],
222 printf("Error: couldn't load texture image\n");
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
);
232 GLubyte
*img
= LoadRGBImage(TEXTURE_1_FILE
, &Width
[1], &Height
[1],
235 printf("Error: couldn't load texture image\n");
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
);
246 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
248 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_ADD
);
251 glEnable(GL_TEXTURE_RECTANGLE_NV
);
254 glShadeModel(GL_FLAT
);
255 glClearColor(0.3, 0.3, 0.4, 1.0);
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
));
266 int main( int argc
, char *argv
[] )
270 glutInit( &argc
, argv
);
271 glutInitWindowSize( 300, 300 );
272 glutInitWindowPosition( 0, 0 );
273 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
274 glutCreateWindow(argv
[0] );
278 glutReshapeFunc( Reshape
);
279 glutKeyboardFunc( Key
);
280 glutSpecialFunc( SpecialKey
);
281 glutDisplayFunc( Display
);
283 glutIdleFunc( Idle
);
285 glutCreateMenu(ModeMenu
);
287 for (i
= 0; i
< NumUnits
; i
++) {
289 sprintf(s
, "Toggle Texture %d", i
);
290 glutAddMenuEntry(s
, TEX0
+ i
);
292 glutAddMenuEntry("Toggle Animation", ANIMATE
);
293 glutAddMenuEntry("Quit", QUIT
);
294 glutAttachMenu(GLUT_RIGHT_BUTTON
);