1 /* $Id: manytex.c,v 1.2 2003/08/23 01:28:59 jonsmirl Exp $ */
4 * test handling of many texture maps
5 * Also tests texture priority and residency.
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;
35 static void Idle( void )
42 static void Display( void )
44 GLfloat spacing
= WinWidth
/ Columns
;
45 GLfloat size
= spacing
* 0.4;
53 b
= glAreTexturesResident(NumTextures
, TextureID
, TextureResidency
);
55 printf("all resident\n");
59 for (i
= 0; i
< NumTextures
; i
++) {
60 if (TextureResidency
[i
]) {
64 printf("%d of %d texture resident\n", resident
, NumTextures
);
68 /* render the textured quads */
69 glClear( GL_COLOR_BUFFER_BIT
);
70 for (i
= 0; i
< NumTextures
; i
++) {
71 GLint row
= i
/ Columns
;
72 GLint col
= i
% Columns
;
73 GLfloat x
= col
* spacing
+ spacing
* 0.5;
74 GLfloat y
= row
* spacing
+ spacing
* 0.5;
76 GLfloat maxDim
= (TextureWidth
[i
] > TextureHeight
[i
])
77 ? TextureWidth
[i
] : TextureHeight
[i
];
78 GLfloat w
= TextureWidth
[i
] / maxDim
;
79 GLfloat h
= TextureHeight
[i
] / maxDim
;
82 glTranslatef(x
, y
, 0.0);
83 glRotatef(Zrot
, 0, 0, 1);
84 glScalef(size
, size
, 1);
86 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
89 glTexCoord2f(0, 0); glVertex2f(-1, -1);
90 glTexCoord2f(1, 0); glVertex2f( 1, -1);
91 glTexCoord2f(1, 1); glVertex2f( 1, 1);
92 glTexCoord2f(0, 1); glVertex2f(-1, 1);
94 glTexCoord2f(0, 0); glVertex2f(-w
, -h
);
95 glTexCoord2f(1, 0); glVertex2f( w
, -h
);
96 glTexCoord2f(1, 1); glVertex2f( w
, h
);
97 glTexCoord2f(0, 1); glVertex2f(-w
, h
);
107 static void Reshape( int width
, int height
)
111 glViewport( 0, 0, width
, height
);
112 glMatrixMode( GL_PROJECTION
);
114 glOrtho(0, width
, 0, height
, -1, 1);
115 glMatrixMode( GL_MODELVIEW
);
121 * Return a random int in [min, max].
123 static int RandomInt(int min
, int max
)
126 int j
= i
% (max
- min
+ 1);
132 static void Init( void )
137 printf("Creating %d %s random-size textures, ", NumTextures
,
138 MipMap
? "Mipmapped" : "non-Mipmapped");
141 printf("Creating %d %s %d x %d textures, ", NumTextures
,
142 MipMap
? "Mipmapped" : "non-Mipmapped",
143 TexWidth
, TexHeight
);
147 printf("bilinear filtering\n");
150 printf("nearest filtering\n");
154 /* compute number of rows and columns of rects */
156 GLfloat area
= (GLfloat
) (WinWidth
* WinHeight
) / (GLfloat
) NumTextures
;
157 GLfloat edgeLen
= sqrt(area
);
159 Columns
= WinWidth
/ edgeLen
;
160 Rows
= (NumTextures
+ Columns
- 1) / Columns
;
161 printf("Rows: %d Cols: %d\n", Rows
, Columns
);
166 TextureID
= (GLuint
*) malloc(sizeof(GLuint
) * NumTextures
);
168 glGenTextures(NumTextures
, TextureID
);
171 if (!TextureResidency
) {
172 TextureResidency
= (GLboolean
*) malloc(sizeof(GLboolean
) * NumTextures
);
173 assert(TextureResidency
);
177 TextureWidth
= (GLint
*) malloc(sizeof(GLint
) * NumTextures
);
178 assert(TextureWidth
);
180 if (!TextureHeight
) {
181 TextureHeight
= (GLint
*) malloc(sizeof(GLint
) * NumTextures
);
182 assert(TextureHeight
);
185 for (i
= 0; i
< NumTextures
; i
++) {
193 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
195 if (i
< LowPriorityCount
)
196 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_PRIORITY
, 0.5F
);
200 int k
= (glutGet(GLUT_ELAPSED_TIME
) % 7) + 2;
204 TexWidth
= 1 << RandomInt(2, 7);
205 TexHeight
= 1 << RandomInt(2, 7);
206 printf("Random size of %3d: %d x %d\n", i
, TexWidth
, TexHeight
);
210 TextureWidth
[i
] = TexWidth
;
211 TextureHeight
[i
] = TexHeight
;
213 texImage
= (GLubyte
*) malloc(4 * TexWidth
* TexHeight
* sizeof(GLubyte
));
216 /* determine texture color */
217 color
[0] = (GLint
) (255.0 * ((float) col
/ (Columns
- 1)));
219 color
[2] = (GLint
) (255.0 * ((float) row
/ (Rows
- 1)));
222 /* fill in solid-colored teximage */
223 for (j
= 0; j
< TexWidth
* TexHeight
; j
++) {
224 texImage
[j
*4+0] = color
[0];
225 texImage
[j
*4+1] = color
[1];
226 texImage
[j
*4+2] = color
[2];
227 texImage
[j
*4+3] = color
[3];
232 GLint w
= TexWidth
, h
= TexHeight
;
234 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGBA
, w
, h
, 0,
235 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
236 if (w
== 1 && h
== 1)
243 /*printf("%d: %d x %d\n", level, w, h);*/
246 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
247 GL_LINEAR_MIPMAP_LINEAR
);
248 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
251 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
252 GL_NEAREST_MIPMAP_NEAREST
);
253 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
257 /* Set corners to white */
259 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
260 k
= (TexWidth
- 1) * 4;
261 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
262 k
= (TexWidth
* TexHeight
- TexWidth
) * 4;
263 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
264 k
= (TexWidth
* TexHeight
- 1) * 4;
265 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
267 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TexWidth
, TexHeight
, 0,
268 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
270 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
271 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
274 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
275 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
282 glEnable(GL_TEXTURE_2D
);
286 static void Key( unsigned char key
, int x
, int y
)
288 const GLfloat step
= 3.0;
319 int main( int argc
, char *argv
[] )
323 glutInit( &argc
, argv
);
324 glutInitWindowPosition( 0, 0 );
325 glutInitWindowSize( WinWidth
, WinHeight
);
326 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
327 glutCreateWindow(argv
[0]);
328 glutReshapeFunc( Reshape
);
329 glutKeyboardFunc( Key
);
330 glutDisplayFunc( Display
);
334 for (i
= 1; i
< argc
; i
++) {
335 if (strcmp(argv
[i
], "-n") == 0) {
336 NumTextures
= atoi(argv
[i
+1]);
337 if (NumTextures
<= 0) {
338 printf("Error, bad number of textures\n");
343 else if (strcmp(argv
[i
], "-mipmap") == 0) {
346 else if (strcmp(argv
[i
], "-linear") == 0) {
347 LinearFilter
= GL_TRUE
;
349 else if (strcmp(argv
[i
], "-size") == 0) {
350 TexWidth
= atoi(argv
[i
+1]);
351 TexHeight
= atoi(argv
[i
+2]);
352 assert(TexWidth
>= 1);
353 assert(TexHeight
>= 1);
356 else if (strcmp(argv
[i
], "-randomsize") == 0) {
357 RandomSize
= GL_TRUE
;
359 else if (strcmp(argv
[i
], "-lowpri") == 0) {
360 LowPriorityCount
= atoi(argv
[i
+1]);
365 printf(" manytex [options]\n");
366 printf("Options:\n");
367 printf(" -n <number of texture objects>\n");
368 printf(" -size <width> <height> - specify texture size\n");
369 printf(" -randomsize - use random size textures\n");
370 printf(" -mipmap - generate mipmaps\n");
371 printf(" -linear - use linear filtering instead of nearest\n");
372 printf(" -lowpri <n> - Set lower priority on <n> textures\n");