2 * Simple test of multiple textures
12 #define MAX_TEXTURES 8
16 static GLfloat Xrot
= 0, Yrot
= 0, Zrot
= 0;
17 static GLboolean Anim
= GL_TRUE
;
18 static GLboolean Blend
= GL_FALSE
;
19 static GLboolean MipMap
= 1+GL_FALSE
;
21 static GLuint NumTextures
;
22 static GLuint Textures
[MAX_TEXTURES
];
23 static float TexRot
[MAX_TEXTURES
][3];
24 static float TexPos
[MAX_TEXTURES
][3];
25 static float TexAspect
[MAX_TEXTURES
];
27 static const char *DefaultFiles
[] = {
29 "../images/reflect.rgb",
30 "../images/tree2.rgba",
38 Xrot
= glutGet(GLUT_ELAPSED_TIME
) * 0.02;
39 Yrot
= glutGet(GLUT_ELAPSED_TIME
) * 0.04;
50 for (i
= 0; i
< NumTextures
; i
++) {
51 GLfloat ar
= TexAspect
[i
];
54 glTranslatef(TexPos
[i
][0], TexPos
[i
][1], TexPos
[i
][2]);
55 glRotatef(TexRot
[i
][0], 1, 0, 0);
56 glRotatef(TexRot
[i
][1], 0, 1, 0);
57 glRotatef(TexRot
[i
][2], 0, 0, 1);
59 glBindTexture(GL_TEXTURE_2D
, Textures
[i
]);
61 glTexCoord2f( 0.0, 0.0 ); glVertex2f( -ar
, -1.0 );
62 glTexCoord2f( 1.0, 0.0 ); glVertex2f( ar
, -1.0 );
63 glTexCoord2f( 1.0, 1.0 ); glVertex2f( ar
, 1.0 );
64 glTexCoord2f( 0.0, 1.0 ); glVertex2f( -ar
, 1.0 );
74 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
78 glDisable(GL_DEPTH_TEST
);
82 glEnable(GL_DEPTH_TEST
);
86 glRotatef(Xrot
, 1, 0, 0);
87 glRotatef(Yrot
, 0, 1, 0);
88 glRotatef(Zrot
, 0, 0, 1);
99 Reshape(int width
, int height
)
101 glViewport(0, 0, width
, height
);
102 glMatrixMode(GL_PROJECTION
);
104 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 50.0);
105 glMatrixMode(GL_MODELVIEW
);
107 glTranslatef(0.0, 0.0, -10.0);
112 RandFloat(float min
, float max
)
114 float x
= (float) (rand() % 1000) * 0.001;
115 x
= x
* (max
- min
) + min
;
126 srand(glutGet(GLUT_ELAPSED_TIME
));
128 for (i
= 0; i
< NumTextures
; i
++) {
129 TexRot
[i
][0] = RandFloat(0.0, 360);
130 TexRot
[i
][1] = RandFloat(0.0, 360);
131 TexRot
[i
][2] = RandFloat(0.0, 360);
132 TexPos
[i
][0] = RandFloat(-k
, k
);
133 TexPos
[i
][1] = RandFloat(-k
, k
);
134 TexPos
[i
][2] = RandFloat(-k
, k
);
143 for (i
= 0; i
< NumTextures
; i
++) {
144 glBindTexture(GL_TEXTURE_2D
, Textures
[i
]);
146 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
147 GL_LINEAR_MIPMAP_LINEAR
);
148 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
151 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
152 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
159 Key(unsigned char key
, int x
, int y
)
161 const GLfloat step
= 3.0;
188 glutDestroyWindow(Win
);
193 printf("Blend=%s MipMap=%s\n",
202 SpecialKey(int key
, int x
, int y
)
204 const GLfloat step
= 3.0;
226 LoadTextures(GLuint n
, const char *files
[])
230 NumTextures
= n
< MAX_TEXTURES
? n
: MAX_TEXTURES
;
232 glGenTextures(n
, Textures
);
234 for (i
= 0; i
< n
; i
++) {
236 glBindTexture(GL_TEXTURE_2D
, Textures
[i
]);
237 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
238 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
239 if (!LoadRGBMipmaps2(files
[i
], GL_TEXTURE_2D
, GL_RGB
, &w
, &h
)) {
240 printf("Error: couldn't load %s\n", files
[i
]);
243 TexAspect
[i
] = (float) w
/ (float) h
;
244 printf("Loaded %s\n", files
[i
]);
250 Init(int argc
, const char *argv
[])
253 LoadTextures(4, DefaultFiles
);
255 LoadTextures(argc
- 1, argv
+ 1);
259 glEnable(GL_TEXTURE_2D
);
261 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
262 glColor4f(1, 1, 1, 0.5);
265 /* setup lighting, etc */
266 glEnable(GL_LIGHTING
);
276 printf(" textures [file.rgb] ...\n");
278 printf(" a - toggle animation\n");
279 printf(" b - toggle blending\n");
280 printf(" m - toggle mipmapping\n");
281 printf(" r - randomize\n");
282 printf(" ESC - exit\n");
287 main(int argc
, char *argv
[])
289 glutInit(&argc
, argv
);
290 glutInitWindowPosition(0, 0);
291 glutInitWindowSize(400, 400);
292 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
293 Win
= glutCreateWindow(argv
[0]);
294 glutReshapeFunc(Reshape
);
295 glutKeyboardFunc(Key
);
296 glutSpecialFunc(SpecialKey
);
297 glutDisplayFunc(Draw
);
300 Init(argc
, (const char **) argv
);