1 /* $Id: manytex.c,v 1.1 2000/08/02 17:57:56 brianp Exp $ */
4 * test handling of many texture maps
5 * Also tests texture priority and residency.
19 static GLint NumTextures
= 20;
20 static GLuint
*TextureID
= NULL
;
21 static GLboolean
*TextureResidency
= NULL
;
22 static GLint TexWidth
= 128, TexHeight
= 128;
23 static GLfloat Zrot
= 0;
24 static GLboolean Anim
= GL_TRUE
;
25 static GLint WinWidth
= 500, WinHeight
= 400;
26 static GLboolean MipMap
= GL_FALSE
;
27 static GLboolean LinearFilter
= GL_FALSE
;
28 static GLboolean RandomSize
= GL_FALSE
;
29 static GLint Rows
, Columns
;
30 static GLuint LowPriorityCount
= 0;
34 static void Idle( void )
41 static void Display( void )
43 GLfloat spacing
= WinWidth
/ Columns
;
44 GLfloat size
= spacing
* 0.4;
51 b
= glAreTexturesResident(NumTextures
, TextureID
, TextureResidency
);
53 printf("all resident\n");
57 for (i
= 0; i
< NumTextures
; i
++) {
58 if (TextureResidency
[i
]) {
62 printf("%d of %d texture resident\n", resident
, NumTextures
);
66 /* render the textured quads */
67 glClear( GL_COLOR_BUFFER_BIT
);
68 for (i
= 0; i
< NumTextures
; i
++) {
69 GLint row
= i
/ Columns
;
70 GLint col
= i
% Columns
;
71 GLfloat x
= col
* spacing
+ spacing
* 0.5;
72 GLfloat y
= row
* spacing
+ spacing
* 0.5;
75 glTranslatef(x
, y
, 0.0);
76 glRotatef(Zrot
, 0, 0, 1);
77 glScalef(size
, size
, 1);
79 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
81 glTexCoord2f(0, 0); glVertex2f(-1, -1);
82 glTexCoord2f(1, 0); glVertex2f( 1, -1);
83 glTexCoord2f(1, 1); glVertex2f( 1, 1);
84 glTexCoord2f(0, 1); glVertex2f(-1, 1);
93 static void Reshape( int width
, int height
)
97 glViewport( 0, 0, width
, height
);
98 glMatrixMode( GL_PROJECTION
);
100 glOrtho(0, width
, 0, height
, -1, 1);
101 glMatrixMode( GL_MODELVIEW
);
107 static void Init( void )
112 printf("Creating %d %s random-size textures, ", NumTextures
,
113 MipMap
? "Mipmapped" : "non-Mipmapped");
116 printf("Creating %d %s %d x %d textures, ", NumTextures
,
117 MipMap
? "Mipmapped" : "non-Mipmapped",
118 TexWidth
, TexHeight
);
122 printf("bilinear filtering\n");
125 printf("nearest filtering\n");
129 /* compute number of rows and columns of rects */
131 GLfloat area
= (GLfloat
) (WinWidth
* WinHeight
) / (GLfloat
) NumTextures
;
132 GLfloat edgeLen
= sqrt(area
);
134 Columns
= WinWidth
/ edgeLen
;
135 Rows
= (NumTextures
+ Columns
- 1) / Columns
;
136 printf("Rows: %d Cols: %d\n", Rows
, Columns
);
141 TextureID
= (GLuint
*) malloc(sizeof(GLuint
) * NumTextures
);
143 glGenTextures(NumTextures
, TextureID
);
146 if (!TextureResidency
) {
147 TextureResidency
= (GLboolean
*) malloc(sizeof(GLboolean
) * NumTextures
);
148 assert(TextureResidency
);
151 for (i
= 0; i
< NumTextures
; i
++) {
159 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
161 if (i
< LowPriorityCount
)
162 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_PRIORITY
, 0.5F
);
165 int k
= (glutGet(GLUT_ELAPSED_TIME
) % 7) + 2;
170 texImage
= (GLubyte
*) malloc(4 * TexWidth
* TexHeight
* sizeof(GLubyte
));
173 /* determine texture color */
174 color
[0] = (GLint
) (255.0 * ((float) col
/ (Columns
- 1)));
176 color
[2] = (GLint
) (255.0 * ((float) row
/ (Rows
- 1)));
179 /* fill in solid-colored teximage */
180 for (j
= 0; j
< TexWidth
* TexHeight
; j
++) {
181 texImage
[j
*4+0] = color
[0];
182 texImage
[j
*4+1] = color
[1];
183 texImage
[j
*4+2] = color
[2];
184 texImage
[j
*4+3] = color
[3];
189 GLint w
= TexWidth
, h
= TexHeight
;
191 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0,
192 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
193 if (w
== 1 && h
== 1)
200 /*printf("%d: %d x %d\n", level, w, h);*/
203 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
204 GL_LINEAR_MIPMAP_LINEAR
);
205 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
208 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
209 GL_NEAREST_MIPMAP_NEAREST
);
210 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
214 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TexWidth
, TexHeight
, 0,
215 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
217 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
218 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
221 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
222 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
229 glEnable(GL_TEXTURE_2D
);
233 static void Key( unsigned char key
, int x
, int y
)
235 const GLfloat step
= 3.0;
263 int main( int argc
, char *argv
[] )
267 glutInit( &argc
, argv
);
268 glutInitWindowPosition( 0, 0 );
269 glutInitWindowSize( WinWidth
, WinHeight
);
270 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
271 glutCreateWindow(argv
[0]);
272 glutReshapeFunc( Reshape
);
273 glutKeyboardFunc( Key
);
274 glutDisplayFunc( Display
);
278 for (i
= 1; i
< argc
; i
++) {
279 if (strcmp(argv
[i
], "-n") == 0) {
280 NumTextures
= atoi(argv
[i
+1]);
281 if (NumTextures
<= 0) {
282 printf("Error, bad number of textures\n");
287 else if (strcmp(argv
[i
], "-mipmap") == 0) {
290 else if (strcmp(argv
[i
], "-linear") == 0) {
291 LinearFilter
= GL_TRUE
;
293 else if (strcmp(argv
[i
], "-size") == 0) {
294 TexWidth
= atoi(argv
[i
+1]);
295 TexHeight
= atoi(argv
[i
+2]);
296 assert(TexWidth
>= 1);
297 assert(TexHeight
>= 1);
300 else if (strcmp(argv
[i
], "-randomsize") == 0) {
301 RandomSize
= GL_TRUE
;
303 else if (strcmp(argv
[i
], "-lowpri") == 0) {
304 LowPriorityCount
= atoi(argv
[i
+1]);
309 printf(" manytex [options]\n");
310 printf("Options:\n");
311 printf(" -n <number of texture objects>\n");
312 printf(" -size <width> <height> - specify texture size\n");
313 printf(" -randomsize - use random size textures\n");
314 printf(" -mipmap - generate mipmaps\n");
315 printf(" -linear - use linear filtering instead of nearest\n");
316 printf(" -lowpri <n> - Set lower priority on <n> textures\n");