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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*
* Test mipmap generation and lod bias.
*
* Brian Paul
* 17 March 2008
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/glext.h>
#include "readtex.h"
#define TEXTURE_FILE "../images/arch.rgb"
#define LEVELS 8
#define SIZE (1<<LEVELS)
static int TexWidth = SIZE, TexHeight = SIZE;
static int WinWidth = 1044, WinHeight = 900;
static GLfloat Bias = 0.0;
static GLboolean ScaleQuads = GL_FALSE;
static GLboolean Linear = GL_FALSE;
static GLint Win = 0;
static GLint RenderTextureLevel = 0;
static GLuint TexObj;
static void
CheckError(int line)
{
GLenum err = glGetError();
if (err) {
printf("GL Error 0x%x at line %d\n", (int) err, line);
}
}
static void
PrintString(const char *s)
{
while (*s) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
s++;
}
}
static void
MipGenTexture( void )
{
/* test auto mipmap generation */
GLint width, height, i;
GLenum format;
GLubyte *image = LoadRGBImage(TEXTURE_FILE, &width, &height, &format);
if (!image) {
printf("Error: could not load texture image %s\n", TEXTURE_FILE);
exit(1);
}
/* resize to TexWidth x TexHeight */
if (width != TexWidth || height != TexHeight) {
GLubyte *newImage = malloc(TexWidth * TexHeight * 4);
fprintf(stderr, "rescale %d %d to %d %d\n", width, height,
TexWidth, TexHeight);
fflush(stderr);
gluScaleImage(format, width, height, GL_UNSIGNED_BYTE, image,
TexWidth, TexHeight, GL_UNSIGNED_BYTE, newImage);
free(image);
image = newImage;
}
printf("Using GL_SGIS_generate_mipmap\n");
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, format, TexWidth, TexHeight, 0,
format, GL_UNSIGNED_BYTE, image);
free(image);
/* make sure mipmap was really generated correctly */
width = TexWidth;
height = TexHeight;
for (i = 0; i < 9; i++) {
GLint w, h;
glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(GL_TEXTURE_2D, i, GL_TEXTURE_HEIGHT, &h);
printf("Level %d size: %d x %d\n", i, w, h);
assert(w == width);
assert(h == height);
width /= 2;
height /= 2;
}
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_FALSE);
}
static void
ResetTextureLevel( int i )
{
GLubyte tex2d[SIZE*SIZE][4];
{
GLint Width = TexWidth / (1 << i);
GLint Height = TexHeight / (1 << i);
GLint s, t;
for (s = 0; s < Width; s++) {
for (t = 0; t < Height; t++) {
tex2d[t*Width+s][0] = ((s / 16) % 2) ? 0 : 255;
tex2d[t*Width+s][1] = ((t / 16) % 2) ? 0 : 255;
tex2d[t*Width+s][2] = 128;
tex2d[t*Width+s][3] = 255;
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, Width, Height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, tex2d);
}
}
static void
ResetTexture( void )
{
#if 0
/* This doesn't work so well as the arch texture is 512x512.
*/
LoadRGBMipmaps(TEXTURE_FILE, GL_RGB);
#else
{
int i;
for (i = 0; i <= LEVELS; i++)
{
ResetTextureLevel(i);
}
}
#endif
}
static void
RenderTexture( void )
{
GLenum status;
GLuint MyFB;
fprintf(stderr, "RenderTextureLevel %d\n", RenderTextureLevel);
fflush(stderr);
/* gen framebuffer id, delete it, do some assertions, just for testing */
glGenFramebuffersEXT(1, &MyFB);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
assert(glIsFramebufferEXT(MyFB));
CheckError(__LINE__);
/* Render color to texture */
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, TexObj,
RenderTextureLevel);
CheckError(__LINE__);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -15.0);
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
printf("Framebuffer incomplete!!!\n");
}
glViewport(0, 0,
TexWidth / (1 << RenderTextureLevel),
TexHeight / (1 << RenderTextureLevel));
glClearColor(0.5, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
CheckError(__LINE__);
glBegin(GL_POLYGON);
glColor3f(1, 0, 0);
glVertex2f(-1, -1);
glColor3f(0, 1, 0);
glVertex2f(1, -1);
glColor3f(0, 0, 1);
glVertex2f(0, 1);
glEnd();
/* Bind normal framebuffer */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
CheckError(__LINE__);
glDeleteFramebuffersEXT(1, &MyFB);
CheckError(__LINE__);
glClearColor(0, 0, 0, 0);
}
static void
Display(void)
{
int x, y, bias;
char str[100];
int texWidth = TexWidth, texHeight = TexHeight;
glViewport(0, 0, WinHeight, WinHeight);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WinWidth, 0, WinHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1,1,1);
if (Linear) {
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);
}
y = WinHeight - 300;
x = 4;
for (bias = -1; bias < 11; bias++) {
if (ScaleQuads) {
if (bias > 0) {
if (texWidth == 1 && texHeight == 1)
break;
texWidth = TexWidth >> bias;
texHeight = TexHeight >> bias;
if (texWidth < 1)
texWidth = 1;
if (texHeight < 1)
texHeight = 1;
}
glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0);
}
else {
glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, bias);
}
glRasterPos2f(x, y + TexHeight + 5);
if (ScaleQuads)
sprintf(str, "Texture Level %d: %d x %d",
(bias < 0 ? 0 : bias),
texWidth, texHeight);
else
sprintf(str, "Texture LOD Bias = %d", bias);
PrintString(str);
glPushMatrix();
glTranslatef(x, y, 0);
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(texWidth, 0);
glTexCoord2f(1, 1); glVertex2f(texWidth, texHeight);
glTexCoord2f(0, 1); glVertex2f(0, texHeight);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
x += TexWidth + 4;
if (x >= WinWidth) {
x = 4;
y -= 300;
}
}
glutSwapBuffers();
}
static void
Reshape(int width, int height)
{
WinWidth = width;
WinHeight = height;
}
static void
Key(unsigned char key, int x, int y)
{
(void) x;
(void) y;
switch (key) {
case 'b':
Bias -= 10;
break;
case 'B':
Bias += 10;
break;
case 'l':
Linear = !Linear;
break;
case 'v':
RenderTextureLevel++;
break;
case 'V':
RenderTextureLevel--;
break;
case 'r':
RenderTexture();
break;
case 'X':
ResetTexture();
break;
case 'x':
ResetTextureLevel(RenderTextureLevel);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
Bias = 100.0 * (key - '0');
break;
case 's':
ScaleQuads = !ScaleQuads;
break;
case ' ':
MipGenTexture();
Bias = 0;
Linear = 0;
RenderTextureLevel = 0;
ScaleQuads = 0;
break;
case 27:
glutDestroyWindow(Win);
exit(0);
break;
}
glutPostRedisplay();
}
static void
Init(void)
{
GLfloat maxBias;
if (!glutExtensionSupported("GL_EXT_texture_lod_bias")) {
printf("Sorry, GL_EXT_texture_lod_bias not supported by this renderer.\n");
exit(1);
}
if (!glutExtensionSupported("GL_SGIS_generate_mipmap")) {
printf("Sorry, GL_SGIS_generate_mipmap not supported by this renderer.\n");
exit(1);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &TexObj);
glBindTexture(GL_TEXTURE_2D, TexObj);
if (1)
MipGenTexture();
else
ResetTexture();
/* mipmapping required for this extension */
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS_EXT, &maxBias);
printf("GL_RENDERER: %s\n", (char*) glGetString(GL_RENDERER));
printf("LOD bias range: [%g, %g]\n", -maxBias, maxBias);
printf("Press 's' to toggle quad scaling\n");
}
int
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(WinWidth, WinHeight);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
Win = glutCreateWindow(argv[0]);
glewInit();
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Display);
Init();
glutMainLoop();
return 0;
}
|