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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
/*
* Test GL_EXT_framebuffer_object render-to-texture
*
* Draw a teapot into a texture image with stenciling.
* Then draw a textured quad using that texture.
*
* Brian Paul
* 18 Apr 2005
*/
#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* For debug */
#define DEPTH 1
#define STENCIL 1
#define DRAW 1
static int Win = 0;
static int Width = 400, Height = 400;
static GLenum TexTarget = GL_TEXTURE_2D; /*GL_TEXTURE_RECTANGLE_ARB;*/
static int TexWidth = 512, TexHeight = 512;
/*static int TexWidth = 600, TexHeight = 600;*/
static GLuint MyFB;
static GLuint TexObj;
static GLuint DepthRB, StencilRB;
static GLboolean Anim = GL_FALSE;
static GLfloat Rot = 0.0;
static GLboolean UsePackedDepthStencil = GL_FALSE;
static GLuint TextureLevel = 1; /* which texture level to render to */
static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
static GLboolean Cull = GL_FALSE;
static GLboolean Wireframe = GL_FALSE;
static void
CheckError(int line)
{
GLenum err = glGetError();
if (err) {
printf("GL Error 0x%x at line %d\n", (int) err, line);
}
}
static void
Idle(void)
{
Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
glutPostRedisplay();
}
static void
RenderTexture(void)
{
GLenum status;
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);
/* draw to texture image */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
printf("Framebuffer incomplete!!!\n");
}
glViewport(0, 0, TexWidth, TexHeight);
glClearColor(0.5, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
CheckError(__LINE__);
#if DEPTH
glEnable(GL_DEPTH_TEST);
#endif
#if STENCIL
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_NEVER, 1, ~0);
glStencilOp(GL_REPLACE, GL_KEEP, GL_REPLACE);
#endif
CheckError(__LINE__);
#if DEPTH || STENCIL
/* draw diamond-shaped stencil pattern */
glColor3f(0, 1, 0);
glBegin(GL_POLYGON);
glVertex2f(-0.2, 0.0);
glVertex2f( 0.0, -0.2);
glVertex2f( 0.2, 0.0);
glVertex2f( 0.0, 0.2);
glEnd();
#endif
/* draw teapot where stencil != 1 */
#if STENCIL
glStencilFunc(GL_NOTEQUAL, 1, ~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
#endif
CheckError(__LINE__);
if (Wireframe) {
glPolygonMode(GL_FRONT, GL_LINE);
}
else {
glPolygonMode(GL_FRONT, GL_FILL);
}
if (Cull) {
/* cull back */
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
}
else {
glDisable(GL_CULL_FACE);
}
#if 0
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();
#else
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glRotatef(0.5 * Rot, 1.0, 0.0, 0.0);
glFrontFace(GL_CW); /* Teapot patches backward */
glutSolidTeapot(0.5);
glFrontFace(GL_CCW);
glPopMatrix();
glDisable(GL_LIGHTING);
/*
PrintStencilHistogram(TexWidth, TexHeight);
*/
#endif
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#if DRAW
/* Bind normal framebuffer */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
#endif
CheckError(__LINE__);
}
static void
Display(void)
{
float ar = (float) Width / (float) Height;
RenderTexture();
/* draw textured quad in the window */
#if DRAW
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -7.0);
glViewport(0, 0, Width, Height);
glClearColor(0.25, 0.25, 0.25, 0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(Rot, 0, 1, 0);
glEnable(TexTarget);
glBindTexture(TexTarget, TexObj);
glBegin(GL_POLYGON);
glColor3f(0.25, 0.25, 0.25);
if (TexTarget == GL_TEXTURE_2D) {
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(1, 0);
glVertex2f(1, -1);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(0, 1);
glVertex2f(-1, 1);
}
else {
assert(TexTarget == GL_TEXTURE_RECTANGLE_ARB);
glTexCoord2f(0, 0);
glVertex2f(-1, -1);
glTexCoord2f(TexWidth, 0);
glVertex2f(1, -1);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(TexWidth, TexHeight);
glVertex2f(1, 1);
glTexCoord2f(0, TexHeight);
glVertex2f(-1, 1);
}
glEnd();
glPopMatrix();
glDisable(TexTarget);
#endif
glutSwapBuffers();
CheckError(__LINE__);
}
static void
Reshape(int width, int height)
{
glViewport(0, 0, width, height);
Width = width;
Height = height;
}
static void
CleanUp(void)
{
#if DEPTH
glDeleteRenderbuffersEXT(1, &DepthRB);
#endif
#if STENCIL
if (!UsePackedDepthStencil)
glDeleteRenderbuffersEXT(1, &StencilRB);
#endif
glDeleteFramebuffersEXT(1, &MyFB);
glDeleteTextures(1, &TexObj);
glutDestroyWindow(Win);
exit(0);
}
static void
Key(unsigned char key, int x, int y)
{
(void) x;
(void) y;
switch (key) {
case 'a':
Anim = !Anim;
if (Anim)
glutIdleFunc(Idle);
else
glutIdleFunc(NULL);
break;
case 'c':
Cull = !Cull;
break;
case 'w':
Wireframe = !Wireframe;
break;
case 's':
Rot += 2.0;
break;
case 'S':
Rot -= 2.0;
break;
case 27:
CleanUp();
break;
}
glutPostRedisplay();
}
static void
Usage(void)
{
printf("Usage:\n");
printf(" a Toggle animation\n");
printf(" s/s Step/rotate\n");
printf(" c Toggle back-face culling\n");
printf(" w Toggle wireframe mode (front-face only)\n");
printf(" Esc Exit\n");
}
static void
Init(int argc, char *argv[])
{
static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 };
GLboolean ARB_fbo = glutExtensionSupported("GL_ARB_framebuffer_object");
GLint i;
if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
printf("GL_EXT_framebuffer_object not found!\n");
exit(0);
}
if (argc > 1 && strcmp(argv[1], "-ds") == 0) {
if (!glutExtensionSupported("GL_EXT_packed_depth_stencil")) {
printf("GL_EXT_packed_depth_stencil not found!\n");
exit(0);
}
UsePackedDepthStencil = GL_TRUE;
printf("Using GL_EXT_packed_depth_stencil\n");
}
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
/* gen framebuffer id, delete it, do some assertions, just for testing */
glGenFramebuffersEXT(1, &MyFB);
assert(MyFB);
assert(!glIsFramebufferEXT(MyFB));
if (!ARB_fbo) {
glDeleteFramebuffersEXT(1, &MyFB);
assert(!glIsFramebufferEXT(MyFB));
}
/* Note, continue to use MyFB below */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
assert(glIsFramebufferEXT(MyFB));
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i);
assert(i == MyFB);
/* Make texture object/image */
glGenTextures(1, &TexObj);
glBindTexture(TexTarget, TexObj);
/* make two image levels */
glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(TexTarget, 1, TexIntFormat, TexWidth/2, TexHeight/2, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
TexWidth = TexWidth >> TextureLevel;
TexHeight = TexHeight >> TextureLevel;
glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
CheckError(__LINE__);
/* Render color to texture */
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
TexTarget, TexObj, TextureLevel);
#if DEPTH
/* make depth renderbuffer */
glGenRenderbuffersEXT(1, &DepthRB);
assert(DepthRB);
assert(!glIsRenderbufferEXT(DepthRB));
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRB);
assert(glIsRenderbufferEXT(DepthRB));
if (UsePackedDepthStencil)
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT,
TexWidth, TexHeight);
else
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT,
TexWidth, TexHeight);
CheckError(__LINE__);
glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
GL_RENDERBUFFER_DEPTH_SIZE_EXT, &i);
CheckError(__LINE__);
printf("Depth renderbuffer size = %d bits\n", i);
assert(i > 0);
/* attach DepthRB to MyFB */
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, DepthRB);
#endif
CheckError(__LINE__);
#if STENCIL
if (UsePackedDepthStencil) {
/* DepthRb is a combined depth/stencil renderbuffer */
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, DepthRB);
}
else {
/* make stencil renderbuffer */
glGenRenderbuffersEXT(1, &StencilRB);
assert(StencilRB);
assert(!glIsRenderbufferEXT(StencilRB));
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, StencilRB);
assert(glIsRenderbufferEXT(StencilRB));
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX,
TexWidth, TexHeight);
/* attach StencilRB to MyFB */
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, StencilRB);
}
glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
GL_RENDERBUFFER_STENCIL_SIZE_EXT, &i);
CheckError(__LINE__);
printf("Stencil renderbuffer size = %d bits\n", i);
assert(i > 0);
#endif
CheckError(__LINE__);
/* bind regular framebuffer */
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
/* lighting */
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat);
}
int
main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(Width, Height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
Win = glutCreateWindow(argv[0]);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Key);
glutDisplayFunc(Display);
if (Anim)
glutIdleFunc(Idle);
Init(argc, argv);
Usage();
glutMainLoop();
return 0;
}
|