1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/* $Id: manytex.c,v 1.2 2003-08-23 01:28:59 jonsmirl Exp $ */
/*
* test handling of many texture maps
* Also tests texture priority and residency.
*
* Brian Paul
* August 2, 2000
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
static GLint NumTextures = 20;
static GLuint *TextureID = NULL;
static GLint *TextureWidth = NULL, *TextureHeight = NULL;
static GLboolean *TextureResidency = NULL;
static GLint TexWidth = 128, TexHeight = 128;
static GLfloat Zrot = 0;
static GLboolean Anim = GL_TRUE;
static GLint WinWidth = 500, WinHeight = 400;
static GLboolean MipMap = GL_FALSE;
static GLboolean LinearFilter = GL_FALSE;
static GLboolean RandomSize = GL_FALSE;
static GLint Rows, Columns;
static GLint LowPriorityCount = 0;
static void Idle( void )
{
Zrot += 1.0;
glutPostRedisplay();
}
static void Display( void )
{
GLfloat spacing = WinWidth / Columns;
GLfloat size = spacing * 0.4;
GLint i;
/* test residency */
if (0)
{
GLboolean b;
GLint i, resident;
b = glAreTexturesResident(NumTextures, TextureID, TextureResidency);
if (b) {
printf("all resident\n");
}
else {
resident = 0;
for (i = 0; i < NumTextures; i++) {
if (TextureResidency[i]) {
resident++;
}
}
printf("%d of %d texture resident\n", resident, NumTextures);
}
}
/* render the textured quads */
glClear( GL_COLOR_BUFFER_BIT );
for (i = 0; i < NumTextures; i++) {
GLint row = i / Columns;
GLint col = i % Columns;
GLfloat x = col * spacing + spacing * 0.5;
GLfloat y = row * spacing + spacing * 0.5;
GLfloat maxDim = (TextureWidth[i] > TextureHeight[i])
? TextureWidth[i] : TextureHeight[i];
GLfloat w = TextureWidth[i] / maxDim;
GLfloat h = TextureHeight[i] / maxDim;
glPushMatrix();
glTranslatef(x, y, 0.0);
glRotatef(Zrot, 0, 0, 1);
glScalef(size, size, 1);
glBindTexture(GL_TEXTURE_2D, TextureID[i]);
glBegin(GL_POLYGON);
#if 0
glTexCoord2f(0, 0); glVertex2f(-1, -1);
glTexCoord2f(1, 0); glVertex2f( 1, -1);
glTexCoord2f(1, 1); glVertex2f( 1, 1);
glTexCoord2f(0, 1); glVertex2f(-1, 1);
#else
glTexCoord2f(0, 0); glVertex2f(-w, -h);
glTexCoord2f(1, 0); glVertex2f( w, -h);
glTexCoord2f(1, 1); glVertex2f( w, h);
glTexCoord2f(0, 1); glVertex2f(-w, h);
#endif
glEnd();
glPopMatrix();
}
glutSwapBuffers();
}
static void Reshape( int width, int height )
{
WinWidth = width;
WinHeight = height;
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
/*
* Return a random int in [min, max].
*/
static int RandomInt(int min, int max)
{
int i = rand();
int j = i % (max - min + 1);
return min + j;
}
static void Init( void )
{
GLint i;
if (RandomSize) {
printf("Creating %d %s random-size textures, ", NumTextures,
MipMap ? "Mipmapped" : "non-Mipmapped");
}
else {
printf("Creating %d %s %d x %d textures, ", NumTextures,
MipMap ? "Mipmapped" : "non-Mipmapped",
TexWidth, TexHeight);
}
if (LinearFilter) {
printf("bilinear filtering\n");
}
else {
printf("nearest filtering\n");
}
/* compute number of rows and columns of rects */
{
GLfloat area = (GLfloat) (WinWidth * WinHeight) / (GLfloat) NumTextures;
GLfloat edgeLen = sqrt(area);
Columns = WinWidth / edgeLen;
Rows = (NumTextures + Columns - 1) / Columns;
printf("Rows: %d Cols: %d\n", Rows, Columns);
}
if (!TextureID) {
TextureID = (GLuint *) malloc(sizeof(GLuint) * NumTextures);
assert(TextureID);
glGenTextures(NumTextures, TextureID);
}
if (!TextureResidency) {
TextureResidency = (GLboolean *) malloc(sizeof(GLboolean) * NumTextures);
assert(TextureResidency);
}
if (!TextureWidth) {
TextureWidth = (GLint *) malloc(sizeof(GLint) * NumTextures);
assert(TextureWidth);
}
if (!TextureHeight) {
TextureHeight = (GLint *) malloc(sizeof(GLint) * NumTextures);
assert(TextureHeight);
}
for (i = 0; i < NumTextures; i++) {
GLubyte color[4];
GLubyte *texImage;
GLint j, row, col;
row = i / Columns;
col = i % Columns;
glBindTexture(GL_TEXTURE_2D, TextureID[i]);
if (i < LowPriorityCount)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 0.5F);
if (RandomSize) {
#if 0
int k = (glutGet(GLUT_ELAPSED_TIME) % 7) + 2;
TexWidth = 1 << k;
TexHeight = 1 << k;
#else
TexWidth = 1 << RandomInt(2, 7);
TexHeight = 1 << RandomInt(2, 7);
printf("Random size of %3d: %d x %d\n", i, TexWidth, TexHeight);
#endif
}
TextureWidth[i] = TexWidth;
TextureHeight[i] = TexHeight;
texImage = (GLubyte*) malloc(4 * TexWidth * TexHeight * sizeof(GLubyte));
assert(texImage);
/* determine texture color */
color[0] = (GLint) (255.0 * ((float) col / (Columns - 1)));
color[1] = 127;
color[2] = (GLint) (255.0 * ((float) row / (Rows - 1)));
color[3] = 255;
/* fill in solid-colored teximage */
for (j = 0; j < TexWidth * TexHeight; j++) {
texImage[j*4+0] = color[0];
texImage[j*4+1] = color[1];
texImage[j*4+2] = color[2];
texImage[j*4+3] = color[3];
}
if (MipMap) {
GLint level = 0;
GLint w = TexWidth, h = TexHeight;
while (1) {
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w, h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, texImage);
if (w == 1 && h == 1)
break;
if (w > 1)
w /= 2;
if (h > 1)
h /= 2;
level++;
/*printf("%d: %d x %d\n", level, w, h);*/
}
if (LinearFilter) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
}
else {
/* Set corners to white */
int k = 0;
texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
k = (TexWidth - 1) * 4;
texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
k = (TexWidth * TexHeight - TexWidth) * 4;
texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
k = (TexWidth * TexHeight - 1) * 4;
texImage[k+0] = texImage[k+1] = texImage[k+2] = texImage[k+3] = 255;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, texImage);
if (LinearFilter) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
}
free(texImage);
}
glEnable(GL_TEXTURE_2D);
}
static void Key( unsigned char key, int x, int y )
{
const GLfloat step = 3.0;
(void) x;
(void) y;
switch (key) {
case 'a':
Anim = !Anim;
if (Anim)
glutIdleFunc(Idle);
else
glutIdleFunc(NULL);
break;
case 's':
Idle();
break;
case 'z':
Zrot -= step;
break;
case 'Z':
Zrot += step;
break;
case ' ':
Init();
break;
case 27:
exit(0);
break;
}
glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
GLint i;
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( WinWidth, WinHeight );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glutCreateWindow(argv[0]);
glutReshapeFunc( Reshape );
glutKeyboardFunc( Key );
glutDisplayFunc( Display );
if (Anim)
glutIdleFunc(Idle);
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-n") == 0) {
NumTextures = atoi(argv[i+1]);
if (NumTextures <= 0) {
printf("Error, bad number of textures\n");
return 1;
}
i++;
}
else if (strcmp(argv[i], "-mipmap") == 0) {
MipMap = GL_TRUE;
}
else if (strcmp(argv[i], "-linear") == 0) {
LinearFilter = GL_TRUE;
}
else if (strcmp(argv[i], "-size") == 0) {
TexWidth = atoi(argv[i+1]);
TexHeight = atoi(argv[i+2]);
assert(TexWidth >= 1);
assert(TexHeight >= 1);
i += 2;
}
else if (strcmp(argv[i], "-randomsize") == 0) {
RandomSize = GL_TRUE;
}
else if (strcmp(argv[i], "-lowpri") == 0) {
LowPriorityCount = atoi(argv[i+1]);
i++;
}
else {
printf("Usage:\n");
printf(" manytex [options]\n");
printf("Options:\n");
printf(" -n <number of texture objects>\n");
printf(" -size <width> <height> - specify texture size\n");
printf(" -randomsize - use random size textures\n");
printf(" -mipmap - generate mipmaps\n");
printf(" -linear - use linear filtering instead of nearest\n");
printf(" -lowpri <n> - Set lower priority on <n> textures\n");
return 0;
}
}
Init();
glutMainLoop();
return 0;
}
|