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