3 * test handling of many texture maps
4 * Also tests texture priority and residency.
17 #include "glut_wrap.h"
20 static GLint NumTextures
= 20;
21 static GLuint
*TextureID
= NULL
;
22 static GLint
*TextureWidth
= NULL
, *TextureHeight
= NULL
;
23 static GLboolean
*TextureResidency
= NULL
;
24 static GLint TexWidth
= 128, TexHeight
= 128;
25 static GLfloat Zrot
= 0;
26 static GLboolean Anim
= GL_TRUE
;
27 static GLint WinWidth
= 500, WinHeight
= 400;
28 static GLboolean MipMap
= GL_FALSE
;
29 static GLboolean LinearFilter
= GL_FALSE
;
30 static GLboolean RandomSize
= GL_FALSE
;
31 static GLint Rows
, Columns
;
32 static GLint LowPriorityCount
= 0;
36 static void Idle( void )
43 static void Display( void )
45 GLfloat spacing
= WinWidth
/ Columns
;
46 GLfloat size
= spacing
* 0.4;
54 b
= glAreTexturesResident(NumTextures
, TextureID
, TextureResidency
);
56 printf("all resident\n");
60 for (i
= 0; i
< NumTextures
; i
++) {
61 if (TextureResidency
[i
]) {
65 printf("%d of %d texture resident\n", resident
, NumTextures
);
69 /* render the textured quads */
70 glClear( GL_COLOR_BUFFER_BIT
);
71 for (i
= 0; i
< NumTextures
; i
++) {
72 GLint row
= i
/ Columns
;
73 GLint col
= i
% Columns
;
74 GLfloat x
= col
* spacing
+ spacing
* 0.5;
75 GLfloat y
= row
* spacing
+ spacing
* 0.5;
77 GLfloat maxDim
= (TextureWidth
[i
] > TextureHeight
[i
])
78 ? TextureWidth
[i
] : TextureHeight
[i
];
79 GLfloat w
= TextureWidth
[i
] / maxDim
;
80 GLfloat h
= TextureHeight
[i
] / maxDim
;
83 glTranslatef(x
, y
, 0.0);
84 glRotatef(Zrot
, 0, 0, 1);
85 glScalef(size
, size
, 1);
87 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
90 glTexCoord2f(0, 0); glVertex2f(-1, -1);
91 glTexCoord2f(1, 0); glVertex2f( 1, -1);
92 glTexCoord2f(1, 1); glVertex2f( 1, 1);
93 glTexCoord2f(0, 1); glVertex2f(-1, 1);
95 glTexCoord2f(0, 0); glVertex2f(-w
, -h
);
96 glTexCoord2f(1, 0); glVertex2f( w
, -h
);
97 glTexCoord2f(1, 1); glVertex2f( w
, h
);
98 glTexCoord2f(0, 1); glVertex2f(-w
, h
);
108 static void Reshape( int width
, int height
)
112 glViewport( 0, 0, width
, height
);
113 glMatrixMode( GL_PROJECTION
);
115 glOrtho(0, width
, 0, height
, -1, 1);
116 glMatrixMode( GL_MODELVIEW
);
122 * Return a random int in [min, max].
124 static int RandomInt(int min
, int max
)
127 int j
= i
% (max
- min
+ 1);
132 static void DeleteTextures(void)
134 glDeleteTextures(NumTextures
, TextureID
);
141 static void Init( void )
146 printf("Creating %d %s random-size textures, ", NumTextures
,
147 MipMap
? "Mipmapped" : "non-Mipmapped");
150 printf("Creating %d %s %d x %d textures, ", NumTextures
,
151 MipMap
? "Mipmapped" : "non-Mipmapped",
152 TexWidth
, TexHeight
);
156 printf("bilinear filtering\n");
159 printf("nearest filtering\n");
163 /* compute number of rows and columns of rects */
165 GLfloat area
= (GLfloat
) (WinWidth
* WinHeight
) / (GLfloat
) NumTextures
;
166 GLfloat edgeLen
= sqrt(area
);
168 Columns
= WinWidth
/ edgeLen
;
169 Rows
= (NumTextures
+ Columns
- 1) / Columns
;
170 printf("Rows: %d Cols: %d\n", Rows
, Columns
);
175 TextureID
= (GLuint
*) malloc(sizeof(GLuint
) * NumTextures
);
177 glGenTextures(NumTextures
, TextureID
);
180 if (!TextureResidency
) {
181 TextureResidency
= (GLboolean
*) malloc(sizeof(GLboolean
) * NumTextures
);
182 assert(TextureResidency
);
186 TextureWidth
= (GLint
*) malloc(sizeof(GLint
) * NumTextures
);
187 assert(TextureWidth
);
189 if (!TextureHeight
) {
190 TextureHeight
= (GLint
*) malloc(sizeof(GLint
) * NumTextures
);
191 assert(TextureHeight
);
194 for (i
= 0; i
< NumTextures
; i
++) {
202 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
204 if (i
< LowPriorityCount
)
205 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_PRIORITY
, 0.5F
);
209 int k
= (glutGet(GLUT_ELAPSED_TIME
) % 7) + 2;
213 TexWidth
= 1 << RandomInt(2, 7);
214 TexHeight
= 1 << RandomInt(2, 7);
215 printf("Random size of %3d: %d x %d\n", i
, TexWidth
, TexHeight
);
219 TextureWidth
[i
] = TexWidth
;
220 TextureHeight
[i
] = TexHeight
;
222 texImage
= (GLubyte
*) malloc(4 * TexWidth
* TexHeight
* sizeof(GLubyte
));
225 /* determine texture color */
226 color
[0] = (GLint
) (255.0 * ((float) col
/ (Columns
- 1)));
228 color
[2] = (GLint
) (255.0 * ((float) row
/ (Rows
- 1)));
231 /* fill in solid-colored teximage */
232 for (j
= 0; j
< TexWidth
* TexHeight
; j
++) {
233 texImage
[j
*4+0] = color
[0];
234 texImage
[j
*4+1] = color
[1];
235 texImage
[j
*4+2] = color
[2];
236 texImage
[j
*4+3] = color
[3];
241 GLint w
= TexWidth
, h
= TexHeight
;
243 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGBA
, w
, h
, 0,
244 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
245 if (w
== 1 && h
== 1)
252 /*printf("%d: %d x %d\n", level, w, h);*/
255 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
256 GL_LINEAR_MIPMAP_LINEAR
);
257 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
260 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
261 GL_NEAREST_MIPMAP_NEAREST
);
262 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
266 /* Set corners to white */
268 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
269 k
= (TexWidth
- 1) * 4;
270 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
271 k
= (TexWidth
* TexHeight
- TexWidth
) * 4;
272 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
273 k
= (TexWidth
* TexHeight
- 1) * 4;
274 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
276 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TexWidth
, TexHeight
, 0,
277 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
279 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
280 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
283 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
284 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
291 glEnable(GL_TEXTURE_2D
);
295 static void Key( unsigned char key
, int x
, int y
)
297 const GLfloat step
= 3.0;
323 glutDestroyWindow(Win
);
331 int main( int argc
, char *argv
[] )
335 glutInit( &argc
, argv
);
336 glutInitWindowPosition( 0, 0 );
337 glutInitWindowSize( WinWidth
, WinHeight
);
338 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
339 Win
= glutCreateWindow(argv
[0]);
341 glutReshapeFunc( Reshape
);
342 glutKeyboardFunc( Key
);
343 glutDisplayFunc( Display
);
347 for (i
= 1; i
< argc
; i
++) {
348 if (strcmp(argv
[i
], "-n") == 0) {
349 NumTextures
= atoi(argv
[i
+1]);
350 if (NumTextures
<= 0) {
351 printf("Error, bad number of textures\n");
356 else if (strcmp(argv
[i
], "-mipmap") == 0) {
359 else if (strcmp(argv
[i
], "-linear") == 0) {
360 LinearFilter
= GL_TRUE
;
362 else if (strcmp(argv
[i
], "-size") == 0) {
363 TexWidth
= atoi(argv
[i
+1]);
364 TexHeight
= atoi(argv
[i
+2]);
365 assert(TexWidth
>= 1);
366 assert(TexHeight
>= 1);
369 else if (strcmp(argv
[i
], "-randomsize") == 0) {
370 RandomSize
= GL_TRUE
;
372 else if (strcmp(argv
[i
], "-lowpri") == 0) {
373 LowPriorityCount
= atoi(argv
[i
+1]);
378 printf(" manytex [options]\n");
379 printf("Options:\n");
380 printf(" -n <number of texture objects>\n");
381 printf(" -size <width> <height> - specify texture size\n");
382 printf(" -randomsize - use random size textures\n");
383 printf(" -mipmap - generate mipmaps\n");
384 printf(" -linear - use linear filtering instead of nearest\n");
385 printf(" -lowpri <n> - Set lower priority on <n> textures\n");