2 * Simple test of multiple textures
13 #define TEST_MIPMAPS 0
15 #define MAX_TEXTURES 8
19 static GLfloat Xrot
= 0, Yrot
= 0, Zrot
= 0;
20 static GLboolean Anim
= GL_TRUE
;
21 static GLboolean Blend
= GL_FALSE
;
22 static GLuint Filter
= 0;
23 static GLboolean Clamp
= GL_FALSE
;
25 static GLuint NumTextures
;
26 static GLuint Textures
[MAX_TEXTURES
];
27 static float TexRot
[MAX_TEXTURES
][3];
28 static float TexPos
[MAX_TEXTURES
][3];
29 static float TexAspect
[MAX_TEXTURES
];
31 static const char *DefaultFiles
[] = {
33 "../images/reflect.rgb",
34 "../images/tree2.rgba",
44 } FilterModes
[NUM_FILTERS
] = {
45 { GL_NEAREST
, GL_NEAREST
, "Nearest,Nearest" },
46 { GL_LINEAR
, GL_LINEAR
, "Linear,Linear" },
47 { GL_NEAREST_MIPMAP_NEAREST
, GL_NEAREST
, "NearestMipmapNearest,Nearest" },
48 { GL_LINEAR_MIPMAP_NEAREST
, GL_LINEAR
, "LinearMipmapNearest,Linear" },
49 { GL_LINEAR_MIPMAP_LINEAR
, GL_LINEAR
, "LinearMipmapLinear,Linear" }
58 Xrot
= glutGet(GLUT_ELAPSED_TIME
) * 0.02;
59 Yrot
= glutGet(GLUT_ELAPSED_TIME
) * 0.04;
70 for (i
= 0; i
< NumTextures
; i
++) {
71 GLfloat ar
= TexAspect
[i
];
74 glTranslatef(TexPos
[i
][0], TexPos
[i
][1], TexPos
[i
][2]);
75 glRotatef(TexRot
[i
][0], 1, 0, 0);
76 glRotatef(TexRot
[i
][1], 0, 1, 0);
77 glRotatef(TexRot
[i
][2], 0, 0, 1);
79 glBindTexture(GL_TEXTURE_2D
, Textures
[i
]);
82 glTexCoord2f( -0.5, -0.5 ); glVertex2f( -ar
, -1.0 );
83 glTexCoord2f( 1.5, -0.5 ); glVertex2f( ar
, -1.0 );
84 glTexCoord2f( 1.5, 1.5 ); glVertex2f( ar
, 1.0 );
85 glTexCoord2f( -0.5, 1.5 ); glVertex2f( -ar
, 1.0 );
87 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -ar
, -1.0 );
88 glTexCoord2f( 1.0, 0.0 ); glVertex2f( ar
, -1.0 );
89 glTexCoord2f( 1.0, 1.0 ); glVertex2f( ar
, 1.0 );
90 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -ar
, 1.0 );
101 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
105 glDisable(GL_DEPTH_TEST
);
109 glEnable(GL_DEPTH_TEST
);
113 glRotatef(Xrot
, 1, 0, 0);
114 glRotatef(Yrot
, 0, 1, 0);
115 glRotatef(Zrot
, 0, 0, 1);
126 Reshape(int width
, int height
)
128 glViewport(0, 0, width
, height
);
129 glMatrixMode(GL_PROJECTION
);
131 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 50.0);
132 glMatrixMode(GL_MODELVIEW
);
134 glTranslatef(0.0, 0.0, -10.0);
139 RandFloat(float min
, float max
)
141 float x
= (float) (rand() % 1000) * 0.001;
142 x
= x
* (max
- min
) + min
;
153 srand(glutGet(GLUT_ELAPSED_TIME
));
155 for (i
= 0; i
< NumTextures
; i
++) {
156 TexRot
[i
][0] = RandFloat(0.0, 360);
157 TexRot
[i
][1] = RandFloat(0.0, 360);
158 TexRot
[i
][2] = RandFloat(0.0, 360);
159 TexPos
[i
][0] = RandFloat(-k
, k
);
160 TexPos
[i
][1] = RandFloat(-k
, k
);
161 TexPos
[i
][2] = RandFloat(-k
, k
);
170 for (i
= 0; i
< NumTextures
; i
++) {
171 glBindTexture(GL_TEXTURE_2D
, Textures
[i
]);
172 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
173 FilterModes
[Filter
].min
);
174 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
,
175 FilterModes
[Filter
].mag
);
178 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
179 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
182 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
183 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
190 Key(unsigned char key
, int x
, int y
)
192 const GLfloat step
= 3.0;
208 Filter
= (Filter
+ 1) % NUM_FILTERS
;
227 glutDestroyWindow(Win
);
232 printf("Blend=%s Filter=%s\n",
234 FilterModes
[Filter
].name
);
241 SpecialKey(int key
, int x
, int y
)
243 const GLfloat step
= 3.0;
265 LoadTextures(GLuint n
, const char *files
[])
269 NumTextures
= n
< MAX_TEXTURES
? n
: MAX_TEXTURES
;
271 glGenTextures(n
, Textures
);
275 for (i
= 0; i
< n
; i
++) {
277 glBindTexture(GL_TEXTURE_2D
, Textures
[i
]);
280 static const GLubyte color
[9][4] = {
292 GLubyte image
[256*256*4];
295 for (level
= 0; level
<= 8; level
++) {
296 for (i
= 0; i
< w
* h
; i
++) {
297 image
[i
*4+0] = color
[level
][0];
298 image
[i
*4+1] = color
[level
][1];
299 image
[i
*4+2] = color
[level
][2];
300 image
[i
*4+3] = color
[level
][3];
302 printf("Load level %d: %d x %d\n", level
, w
>>level
, h
>>level
);
303 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGBA
, w
>>level
, h
>>level
, 0,
304 GL_RGBA
, GL_UNSIGNED_BYTE
, image
);
308 if (!LoadRGBMipmaps2(files
[i
], GL_TEXTURE_2D
, GL_RGB
, &w
, &h
)) {
309 printf("Error: couldn't load %s\n", files
[i
]);
313 TexAspect
[i
] = (float) w
/ (float) h
;
314 printf("Loaded %s\n", files
[i
]);
320 Init(int argc
, const char *argv
[])
323 LoadTextures(4, DefaultFiles
);
325 LoadTextures(argc
- 1, argv
+ 1);
329 glEnable(GL_TEXTURE_2D
);
331 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
332 glColor4f(1, 1, 1, 0.5);
335 /* setup lighting, etc */
336 glEnable(GL_LIGHTING
);
346 printf(" textures [file.rgb] ...\n");
348 printf(" a - toggle animation\n");
349 printf(" b - toggle blending\n");
350 printf(" f - change texture filter mode\n");
351 printf(" r - randomize\n");
352 printf(" ESC - exit\n");
357 main(int argc
, char *argv
[])
359 glutInit(&argc
, argv
);
360 glutInitWindowPosition(0, 0);
361 glutInitWindowSize(700, 700);
362 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
363 Win
= glutCreateWindow(argv
[0]);
364 glutReshapeFunc(Reshape
);
365 glutKeyboardFunc(Key
);
366 glutSpecialFunc(SpecialKey
);
367 glutDisplayFunc(Draw
);
370 Init(argc
, (const char **) argv
);