1 /* $Id: manytex.c,v 1.4 2002/10/18 17:47:36 kschultz 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 GLint
*TextureWidth
= NULL
, *TextureHeight
= NULL
;
22 static GLboolean
*TextureResidency
= NULL
;
23 static GLint TexWidth
= 128, TexHeight
= 128;
24 static GLfloat Zrot
= 0;
25 static GLboolean Anim
= GL_TRUE
;
26 static GLint WinWidth
= 500, WinHeight
= 400;
27 static GLboolean MipMap
= GL_FALSE
;
28 static GLboolean LinearFilter
= GL_FALSE
;
29 static GLboolean RandomSize
= GL_FALSE
;
30 static GLint Rows
, Columns
;
31 static GLint LowPriorityCount
= 0;
34 static void Idle( void )
41 static void Display( void )
43 GLfloat spacing
= WinWidth
/ Columns
;
44 GLfloat size
= spacing
* 0.4;
52 b
= glAreTexturesResident(NumTextures
, TextureID
, TextureResidency
);
54 printf("all resident\n");
58 for (i
= 0; i
< NumTextures
; i
++) {
59 if (TextureResidency
[i
]) {
63 printf("%d of %d texture resident\n", resident
, NumTextures
);
67 /* render the textured quads */
68 glClear( GL_COLOR_BUFFER_BIT
);
69 for (i
= 0; i
< NumTextures
; i
++) {
70 GLint row
= i
/ Columns
;
71 GLint col
= i
% Columns
;
72 GLfloat x
= col
* spacing
+ spacing
* 0.5;
73 GLfloat y
= row
* spacing
+ spacing
* 0.5;
75 GLfloat maxDim
= (TextureWidth
[i
] > TextureHeight
[i
])
76 ? TextureWidth
[i
] : TextureHeight
[i
];
77 GLfloat w
= TextureWidth
[i
] / maxDim
;
78 GLfloat h
= TextureHeight
[i
] / maxDim
;
81 glTranslatef(x
, y
, 0.0);
82 glRotatef(Zrot
, 0, 0, 1);
83 glScalef(size
, size
, 1);
85 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
88 glTexCoord2f(0, 0); glVertex2f(-1, -1);
89 glTexCoord2f(1, 0); glVertex2f( 1, -1);
90 glTexCoord2f(1, 1); glVertex2f( 1, 1);
91 glTexCoord2f(0, 1); glVertex2f(-1, 1);
93 glTexCoord2f(0, 0); glVertex2f(-w
, -h
);
94 glTexCoord2f(1, 0); glVertex2f( w
, -h
);
95 glTexCoord2f(1, 1); glVertex2f( w
, h
);
96 glTexCoord2f(0, 1); glVertex2f(-w
, h
);
106 static void Reshape( int width
, int height
)
110 glViewport( 0, 0, width
, height
);
111 glMatrixMode( GL_PROJECTION
);
113 glOrtho(0, width
, 0, height
, -1, 1);
114 glMatrixMode( GL_MODELVIEW
);
120 * Return a random int in [min, max].
122 static int RandomInt(int min
, int max
)
125 int j
= i
% (max
- min
+ 1);
131 static void Init( void )
136 printf("Creating %d %s random-size textures, ", NumTextures
,
137 MipMap
? "Mipmapped" : "non-Mipmapped");
140 printf("Creating %d %s %d x %d textures, ", NumTextures
,
141 MipMap
? "Mipmapped" : "non-Mipmapped",
142 TexWidth
, TexHeight
);
146 printf("bilinear filtering\n");
149 printf("nearest filtering\n");
153 /* compute number of rows and columns of rects */
155 GLfloat area
= (GLfloat
) (WinWidth
* WinHeight
) / (GLfloat
) NumTextures
;
156 GLfloat edgeLen
= sqrt(area
);
158 Columns
= WinWidth
/ edgeLen
;
159 Rows
= (NumTextures
+ Columns
- 1) / Columns
;
160 printf("Rows: %d Cols: %d\n", Rows
, Columns
);
165 TextureID
= (GLuint
*) malloc(sizeof(GLuint
) * NumTextures
);
167 glGenTextures(NumTextures
, TextureID
);
170 if (!TextureResidency
) {
171 TextureResidency
= (GLboolean
*) malloc(sizeof(GLboolean
) * NumTextures
);
172 assert(TextureResidency
);
176 TextureWidth
= (GLint
*) malloc(sizeof(GLint
) * NumTextures
);
177 assert(TextureWidth
);
179 if (!TextureHeight
) {
180 TextureHeight
= (GLint
*) malloc(sizeof(GLint
) * NumTextures
);
181 assert(TextureHeight
);
184 for (i
= 0; i
< NumTextures
; i
++) {
192 glBindTexture(GL_TEXTURE_2D
, TextureID
[i
]);
194 if (i
< LowPriorityCount
)
195 glTexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_PRIORITY
, 0.5F
);
199 int k
= (glutGet(GLUT_ELAPSED_TIME
) % 7) + 2;
203 TexWidth
= 1 << RandomInt(2, 7);
204 TexHeight
= 1 << RandomInt(2, 7);
205 printf("Random size of %3d: %d x %d\n", i
, TexWidth
, TexHeight
);
209 TextureWidth
[i
] = TexWidth
;
210 TextureHeight
[i
] = TexHeight
;
212 texImage
= (GLubyte
*) malloc(4 * TexWidth
* TexHeight
* sizeof(GLubyte
));
215 /* determine texture color */
216 color
[0] = (GLint
) (255.0 * ((float) col
/ (Columns
- 1)));
218 color
[2] = (GLint
) (255.0 * ((float) row
/ (Rows
- 1)));
221 /* fill in solid-colored teximage */
222 for (j
= 0; j
< TexWidth
* TexHeight
; j
++) {
223 texImage
[j
*4+0] = color
[0];
224 texImage
[j
*4+1] = color
[1];
225 texImage
[j
*4+2] = color
[2];
226 texImage
[j
*4+3] = color
[3];
231 GLint w
= TexWidth
, h
= TexHeight
;
233 glTexImage2D(GL_TEXTURE_2D
, level
, GL_RGBA
, w
, h
, 0,
234 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
235 if (w
== 1 && h
== 1)
242 /*printf("%d: %d x %d\n", level, w, h);*/
245 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
246 GL_LINEAR_MIPMAP_LINEAR
);
247 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
250 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
251 GL_NEAREST_MIPMAP_NEAREST
);
252 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
256 /* Set corners to white */
258 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
259 k
= (TexWidth
- 1) * 4;
260 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
261 k
= (TexWidth
* TexHeight
- TexWidth
) * 4;
262 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
263 k
= (TexWidth
* TexHeight
- 1) * 4;
264 texImage
[k
+0] = texImage
[k
+1] = texImage
[k
+2] = texImage
[k
+3] = 255;
266 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, TexWidth
, TexHeight
, 0,
267 GL_RGBA
, GL_UNSIGNED_BYTE
, texImage
);
269 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
270 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
273 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
274 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
281 glEnable(GL_TEXTURE_2D
);
285 static void Key( unsigned char key
, int x
, int y
)
287 const GLfloat step
= 3.0;
318 int main( int argc
, char *argv
[] )
322 glutInit( &argc
, argv
);
323 glutInitWindowPosition( 0, 0 );
324 glutInitWindowSize( WinWidth
, WinHeight
);
325 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
326 glutCreateWindow(argv
[0]);
327 glutReshapeFunc( Reshape
);
328 glutKeyboardFunc( Key
);
329 glutDisplayFunc( Display
);
333 for (i
= 1; i
< argc
; i
++) {
334 if (strcmp(argv
[i
], "-n") == 0) {
335 NumTextures
= atoi(argv
[i
+1]);
336 if (NumTextures
<= 0) {
337 printf("Error, bad number of textures\n");
342 else if (strcmp(argv
[i
], "-mipmap") == 0) {
345 else if (strcmp(argv
[i
], "-linear") == 0) {
346 LinearFilter
= GL_TRUE
;
348 else if (strcmp(argv
[i
], "-size") == 0) {
349 TexWidth
= atoi(argv
[i
+1]);
350 TexHeight
= atoi(argv
[i
+2]);
351 assert(TexWidth
>= 1);
352 assert(TexHeight
>= 1);
355 else if (strcmp(argv
[i
], "-randomsize") == 0) {
356 RandomSize
= GL_TRUE
;
358 else if (strcmp(argv
[i
], "-lowpri") == 0) {
359 LowPriorityCount
= atoi(argv
[i
+1]);
364 printf(" manytex [options]\n");
365 printf("Options:\n");
366 printf(" -n <number of texture objects>\n");
367 printf(" -size <width> <height> - specify texture size\n");
368 printf(" -randomsize - use random size textures\n");
369 printf(" -mipmap - generate mipmaps\n");
370 printf(" -linear - use linear filtering instead of nearest\n");
371 printf(" -lowpri <n> - Set lower priority on <n> textures\n");