blob: 56482663dc4c1329f11730e951ad1d44fc6e8bf5 [file] [log] [blame]
Brian Paule4b23562005-05-04 20:11:35 +00001/*
2 * Test GL_EXT_framebuffer_object render-to-texture
3 *
4 * Draw a teapot into a texture image with stenciling.
5 * Then draw a textured quad using that texture.
6 *
7 * Brian Paul
8 * 18 Apr 2005
9 */
10
11
Brian Paul32fe2332005-11-16 14:48:11 +000012#include <GL/glut.h>
Brian Paule4b23562005-05-04 20:11:35 +000013#include <assert.h>
14#include <stdio.h>
15#include <stdlib.h>
Brian Paul32fe2332005-11-16 14:48:11 +000016#include <string.h>
Brian Paule4b23562005-05-04 20:11:35 +000017#include <math.h>
Brian Paulfb643652009-06-11 15:50:47 -060018#include "extfuncs.h"
Brian Paul32fe2332005-11-16 14:48:11 +000019
Brian Paul64945412006-03-24 23:17:06 +000020/* For debug */
21#define DEPTH 1
22#define STENCIL 1
23#define DRAW 1
24
Brian Paule4b23562005-05-04 20:11:35 +000025
Brian2282d812007-03-06 16:33:00 -070026static int Win = 0;
Brian Paule4b23562005-05-04 20:11:35 +000027static int Width = 400, Height = 400;
Brian Paul64945412006-03-24 23:17:06 +000028
Brian Paul07f96a62009-02-27 15:46:40 -070029#if 1
30static GLenum TexTarget = GL_TEXTURE_2D;
Brian Paule4b23562005-05-04 20:11:35 +000031static int TexWidth = 512, TexHeight = 512;
Brian Paul07f96a62009-02-27 15:46:40 -070032static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
33#else
34static GLenum TexTarget = GL_TEXTURE_RECTANGLE_ARB;
35static int TexWidth = 200, TexHeight = 200;
36static GLenum TexIntFormat = GL_RGB5; /* either GL_RGB or GL_RGBA */
37#endif
38static GLuint TextureLevel = 0; /* which texture level to render to */
Brian Paul64945412006-03-24 23:17:06 +000039
Brian Paule4b23562005-05-04 20:11:35 +000040static GLuint MyFB;
41static GLuint TexObj;
Brian Paul0906f472009-01-22 15:25:10 -070042static GLuint DepthRB = 0, StencilRB = 0;
Brian Paule4b23562005-05-04 20:11:35 +000043static GLboolean Anim = GL_FALSE;
44static GLfloat Rot = 0.0;
Brian Paul32fe2332005-11-16 14:48:11 +000045static GLboolean UsePackedDepthStencil = GL_FALSE;
Brian Paul0906f472009-01-22 15:25:10 -070046static GLboolean UsePackedDepthStencilBoth = GL_FALSE;
47static GLboolean Use_ARB_fbo = GL_FALSE;
Briandf198d22007-12-04 10:48:05 -070048static GLboolean Cull = GL_FALSE;
49static GLboolean Wireframe = GL_FALSE;
Brian Paule4b23562005-05-04 20:11:35 +000050
51
52static void
53CheckError(int line)
54{
55 GLenum err = glGetError();
56 if (err) {
57 printf("GL Error 0x%x at line %d\n", (int) err, line);
58 }
59}
60
61
62static void
63Idle(void)
64{
Brian Paul64945412006-03-24 23:17:06 +000065 Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
Brian Paule4b23562005-05-04 20:11:35 +000066 glutPostRedisplay();
67}
68
69
70static void
71RenderTexture(void)
72{
Brian Paule4b23562005-05-04 20:11:35 +000073 GLenum status;
74
75 glMatrixMode(GL_PROJECTION);
76 glLoadIdentity();
77 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
78 glMatrixMode(GL_MODELVIEW);
79 glLoadIdentity();
80 glTranslatef(0.0, 0.0, -15.0);
81
Brian Paul7edf1e82005-10-04 15:16:27 +000082 /* draw to texture image */
Brian Paulfb643652009-06-11 15:50:47 -060083 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, MyFB);
Brian Paule4b23562005-05-04 20:11:35 +000084
Brian Paulfb643652009-06-11 15:50:47 -060085 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
Brian Paule4b23562005-05-04 20:11:35 +000086 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
87 printf("Framebuffer incomplete!!!\n");
88 }
89
90 glViewport(0, 0, TexWidth, TexHeight);
91
92 glClearColor(0.5, 0.5, 1.0, 0.0);
93 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
Brian Paul32fe2332005-11-16 14:48:11 +000094 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +000095
Brian Paul64945412006-03-24 23:17:06 +000096#if DEPTH
Brian Paule4b23562005-05-04 20:11:35 +000097 glEnable(GL_DEPTH_TEST);
Brian Paul64945412006-03-24 23:17:06 +000098#endif
99
100#if STENCIL
Brian Paule4b23562005-05-04 20:11:35 +0000101 glEnable(GL_STENCIL_TEST);
102 glStencilFunc(GL_NEVER, 1, ~0);
103 glStencilOp(GL_REPLACE, GL_KEEP, GL_REPLACE);
Brian Paul64945412006-03-24 23:17:06 +0000104#endif
Brian Paule4b23562005-05-04 20:11:35 +0000105
Brian Paul32fe2332005-11-16 14:48:11 +0000106 CheckError(__LINE__);
107
Brian Paul64945412006-03-24 23:17:06 +0000108#if DEPTH || STENCIL
Brian Paule4b23562005-05-04 20:11:35 +0000109 /* draw diamond-shaped stencil pattern */
110 glColor3f(0, 1, 0);
111 glBegin(GL_POLYGON);
112 glVertex2f(-0.2, 0.0);
113 glVertex2f( 0.0, -0.2);
114 glVertex2f( 0.2, 0.0);
115 glVertex2f( 0.0, 0.2);
116 glEnd();
Brian Paul64945412006-03-24 23:17:06 +0000117#endif
Brian Paule4b23562005-05-04 20:11:35 +0000118
119 /* draw teapot where stencil != 1 */
Brian Paul64945412006-03-24 23:17:06 +0000120#if STENCIL
Brian Paule4b23562005-05-04 20:11:35 +0000121 glStencilFunc(GL_NOTEQUAL, 1, ~0);
122 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
Brian Paul64945412006-03-24 23:17:06 +0000123#endif
Brian Paule4b23562005-05-04 20:11:35 +0000124
Brian Paul32fe2332005-11-16 14:48:11 +0000125 CheckError(__LINE__);
126
Briandf198d22007-12-04 10:48:05 -0700127 if (Wireframe) {
128 glPolygonMode(GL_FRONT, GL_LINE);
129 }
130 else {
131 glPolygonMode(GL_FRONT, GL_FILL);
132 }
133
134 if (Cull) {
135 /* cull back */
136 glCullFace(GL_BACK);
137 glEnable(GL_CULL_FACE);
138 }
139 else {
140 glDisable(GL_CULL_FACE);
141 }
142
Brian Paule4b23562005-05-04 20:11:35 +0000143#if 0
144 glBegin(GL_POLYGON);
145 glColor3f(1, 0, 0);
146 glVertex2f(-1, -1);
147 glColor3f(0, 1, 0);
148 glVertex2f(1, -1);
149 glColor3f(0, 0, 1);
150 glVertex2f(0, 1);
151 glEnd();
152#else
153 glEnable(GL_LIGHTING);
154 glEnable(GL_LIGHT0);
155 glPushMatrix();
156 glRotatef(0.5 * Rot, 1.0, 0.0, 0.0);
Briandf198d22007-12-04 10:48:05 -0700157 glFrontFace(GL_CW); /* Teapot patches backward */
Brian Paule4b23562005-05-04 20:11:35 +0000158 glutSolidTeapot(0.5);
Briandf198d22007-12-04 10:48:05 -0700159 glFrontFace(GL_CCW);
Brian Paule4b23562005-05-04 20:11:35 +0000160 glPopMatrix();
161 glDisable(GL_LIGHTING);
Brian Paul64945412006-03-24 23:17:06 +0000162 /*
163 PrintStencilHistogram(TexWidth, TexHeight);
164 */
Brian Paule4b23562005-05-04 20:11:35 +0000165#endif
Brian Paul64945412006-03-24 23:17:06 +0000166
Brian Paule4b23562005-05-04 20:11:35 +0000167 glDisable(GL_DEPTH_TEST);
168 glDisable(GL_STENCIL_TEST);
Briandf198d22007-12-04 10:48:05 -0700169 glDisable(GL_CULL_FACE);
170 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Brian Paule4b23562005-05-04 20:11:35 +0000171
Brian Paul64945412006-03-24 23:17:06 +0000172#if DRAW
Brian Paule4b23562005-05-04 20:11:35 +0000173 /* Bind normal framebuffer */
Brian Paulfb643652009-06-11 15:50:47 -0600174 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, 0);
Brian Paul64945412006-03-24 23:17:06 +0000175#endif
Brian Paule4b23562005-05-04 20:11:35 +0000176
177 CheckError(__LINE__);
178}
179
180
181
182static void
183Display(void)
184{
185 float ar = (float) Width / (float) Height;
186
187 RenderTexture();
188
189 /* draw textured quad in the window */
Brian Paul64945412006-03-24 23:17:06 +0000190#if DRAW
Brian Paule4b23562005-05-04 20:11:35 +0000191 glMatrixMode(GL_PROJECTION);
192 glLoadIdentity();
193 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
194 glMatrixMode(GL_MODELVIEW);
195 glLoadIdentity();
196 glTranslatef(0.0, 0.0, -7.0);
197
198 glViewport(0, 0, Width, Height);
199
200 glClearColor(0.25, 0.25, 0.25, 0);
201 glClear(GL_COLOR_BUFFER_BIT);
202
203 glPushMatrix();
204 glRotatef(Rot, 0, 1, 0);
Brian Paul64945412006-03-24 23:17:06 +0000205 glEnable(TexTarget);
206 glBindTexture(TexTarget, TexObj);
Brian Paule4b23562005-05-04 20:11:35 +0000207 glBegin(GL_POLYGON);
208 glColor3f(0.25, 0.25, 0.25);
Brian Paul64945412006-03-24 23:17:06 +0000209 if (TexTarget == GL_TEXTURE_2D) {
210 glTexCoord2f(0, 0);
211 glVertex2f(-1, -1);
212 glTexCoord2f(1, 0);
213 glVertex2f(1, -1);
214 glColor3f(1.0, 1.0, 1.0);
215 glTexCoord2f(1, 1);
216 glVertex2f(1, 1);
217 glTexCoord2f(0, 1);
218 glVertex2f(-1, 1);
219 }
220 else {
221 assert(TexTarget == GL_TEXTURE_RECTANGLE_ARB);
222 glTexCoord2f(0, 0);
223 glVertex2f(-1, -1);
224 glTexCoord2f(TexWidth, 0);
225 glVertex2f(1, -1);
226 glColor3f(1.0, 1.0, 1.0);
227 glTexCoord2f(TexWidth, TexHeight);
228 glVertex2f(1, 1);
229 glTexCoord2f(0, TexHeight);
230 glVertex2f(-1, 1);
231 }
Brian Paule4b23562005-05-04 20:11:35 +0000232 glEnd();
233 glPopMatrix();
Brian Paul64945412006-03-24 23:17:06 +0000234 glDisable(TexTarget);
235#endif
236
Brian Paule4b23562005-05-04 20:11:35 +0000237 glutSwapBuffers();
238 CheckError(__LINE__);
239}
240
241
242static void
243Reshape(int width, int height)
244{
245 glViewport(0, 0, width, height);
246 Width = width;
247 Height = height;
248}
249
250
251static void
Brian Paul32fe2332005-11-16 14:48:11 +0000252CleanUp(void)
253{
Brian Paul64945412006-03-24 23:17:06 +0000254#if DEPTH
Brian Paulfb643652009-06-11 15:50:47 -0600255 glDeleteRenderbuffers_func(1, &DepthRB);
Brian Paul64945412006-03-24 23:17:06 +0000256#endif
257#if STENCIL
Brian Paulfb643652009-06-11 15:50:47 -0600258 glDeleteRenderbuffers_func(1, &StencilRB);
Brian Paul64945412006-03-24 23:17:06 +0000259#endif
Brian Paulfb643652009-06-11 15:50:47 -0600260 glDeleteFramebuffers_func(1, &MyFB);
Brian Paul32fe2332005-11-16 14:48:11 +0000261
262 glDeleteTextures(1, &TexObj);
263
Brian2282d812007-03-06 16:33:00 -0700264 glutDestroyWindow(Win);
265
Brian Paul32fe2332005-11-16 14:48:11 +0000266 exit(0);
267}
268
269
270static void
Brian Paule4b23562005-05-04 20:11:35 +0000271Key(unsigned char key, int x, int y)
272{
273 (void) x;
274 (void) y;
275 switch (key) {
Briandf198d22007-12-04 10:48:05 -0700276 case 'a':
277 Anim = !Anim;
278 if (Anim)
279 glutIdleFunc(Idle);
280 else
281 glutIdleFunc(NULL);
282 break;
283 case 'c':
284 Cull = !Cull;
285 break;
286 case 'w':
287 Wireframe = !Wireframe;
288 break;
289 case 's':
290 Rot += 2.0;
291 break;
292 case 'S':
293 Rot -= 2.0;
294 break;
295 case 27:
296 CleanUp();
297 break;
Brian Paule4b23562005-05-04 20:11:35 +0000298 }
299 glutPostRedisplay();
300}
301
302
Brian Paul0906f472009-01-22 15:25:10 -0700303/**
304 * Attach depth and stencil renderbuffer(s) to the given framebuffer object.
305 * \param tryDepthStencil if true, try to use a combined depth+stencil buffer
306 * \param bindDepthStencil if true, and tryDepthStencil is true, bind with
307 * the GL_DEPTH_STENCIL_ATTACHMENT target.
308 * \return GL_TRUE for success, GL_FALSE for failure
309 */
310static GLboolean
311AttachDepthAndStencilBuffers(GLuint fbo,
312 GLsizei width, GLsizei height,
313 GLboolean tryDepthStencil,
314 GLboolean bindDepthStencil,
315 GLuint *depthRbOut, GLuint *stencilRbOut)
Briandf198d22007-12-04 10:48:05 -0700316{
Brian Paul0906f472009-01-22 15:25:10 -0700317 GLenum status;
318
319 *depthRbOut = *stencilRbOut = 0;
320
Brian Paulfb643652009-06-11 15:50:47 -0600321 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, fbo);
Brian Paul0906f472009-01-22 15:25:10 -0700322
323 if (tryDepthStencil) {
324 GLuint rb;
325
Brian Paulfb643652009-06-11 15:50:47 -0600326 glGenRenderbuffers_func(1, &rb);
327 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb);
328 glRenderbufferStorage_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700329 GL_DEPTH24_STENCIL8_EXT,
330 width, height);
331 if (glGetError())
332 return GL_FALSE;
333
334 if (bindDepthStencil) {
335 /* attach to both depth and stencil at once */
Brian Paulfb643652009-06-11 15:50:47 -0600336 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700337 GL_DEPTH_STENCIL_ATTACHMENT,
338 GL_RENDERBUFFER_EXT, rb);
339 if (glGetError())
340 return GL_FALSE;
341 }
342 else {
343 /* attach to depth attachment point */
Brian Paulfb643652009-06-11 15:50:47 -0600344 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700345 GL_DEPTH_ATTACHMENT_EXT,
346 GL_RENDERBUFFER_EXT, rb);
347 if (glGetError())
348 return GL_FALSE;
349
350 /* and attach to stencil attachment point */
Brian Paulfb643652009-06-11 15:50:47 -0600351 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700352 GL_STENCIL_ATTACHMENT_EXT,
353 GL_RENDERBUFFER_EXT, rb);
354 if (glGetError())
355 return GL_FALSE;
356 }
357
Brian Paulfb643652009-06-11 15:50:47 -0600358 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
Brian Paul0906f472009-01-22 15:25:10 -0700359 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
360 return GL_FALSE;
361
362 *depthRbOut = *stencilRbOut = rb;
363 return GL_TRUE;
364 }
365
366 /* just depth renderbuffer */
367 {
368 GLuint rb;
369
Brian Paulfb643652009-06-11 15:50:47 -0600370 glGenRenderbuffers_func(1, &rb);
371 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb);
372 glRenderbufferStorage_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700373 GL_DEPTH_COMPONENT,
374 width, height);
375 if (glGetError())
376 return GL_FALSE;
377
378 /* attach to depth attachment point */
Brian Paulfb643652009-06-11 15:50:47 -0600379 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700380 GL_DEPTH_ATTACHMENT_EXT,
381 GL_RENDERBUFFER_EXT, rb);
382 if (glGetError())
383 return GL_FALSE;
384
Brian Paulfb643652009-06-11 15:50:47 -0600385 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
Brian Paul0906f472009-01-22 15:25:10 -0700386 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
387 return GL_FALSE;
388
389 *depthRbOut = rb;
390 }
391
392 /* just stencil renderbuffer */
393 {
394 GLuint rb;
395
Brian Paulfb643652009-06-11 15:50:47 -0600396 glGenRenderbuffers_func(1, &rb);
397 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb);
398 glRenderbufferStorage_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700399 GL_STENCIL_INDEX,
400 width, height);
401 if (glGetError())
402 return GL_FALSE;
403
404 /* attach to depth attachment point */
Brian Paulfb643652009-06-11 15:50:47 -0600405 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700406 GL_STENCIL_ATTACHMENT_EXT,
407 GL_RENDERBUFFER_EXT, rb);
408 if (glGetError())
409 return GL_FALSE;
410
Brian Paulfb643652009-06-11 15:50:47 -0600411 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
Brian Paul07f96a62009-02-27 15:46:40 -0700412 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
Brian Paulfb643652009-06-11 15:50:47 -0600413 glDeleteRenderbuffers_func(1, depthRbOut);
Brian Paul07f96a62009-02-27 15:46:40 -0700414 *depthRbOut = 0;
Brian Paulfb643652009-06-11 15:50:47 -0600415 glDeleteRenderbuffers_func(1, &rb);
Brian Paul0906f472009-01-22 15:25:10 -0700416 return GL_FALSE;
Brian Paul07f96a62009-02-27 15:46:40 -0700417 }
Brian Paul0906f472009-01-22 15:25:10 -0700418
419 *stencilRbOut = rb;
420 }
421
422 return GL_TRUE;
Briandf198d22007-12-04 10:48:05 -0700423}
424
425
426static void
Brian Paul0906f472009-01-22 15:25:10 -0700427ParseArgs(int argc, char *argv[])
Brian Paule4b23562005-05-04 20:11:35 +0000428{
429 GLint i;
Brian Paul0906f472009-01-22 15:25:10 -0700430 for (i = 1; i < argc; i++) {
431 if (strcmp(argv[i], "-ds") == 0) {
432 if (!glutExtensionSupported("GL_EXT_packed_depth_stencil")) {
433 printf("GL_EXT_packed_depth_stencil not found!\n");
434 exit(0);
435 }
436 UsePackedDepthStencil = GL_TRUE;
437 printf("Using GL_EXT_packed_depth_stencil\n");
438 }
439 else if (strcmp(argv[i], "-ds2") == 0) {
440 if (!glutExtensionSupported("GL_EXT_packed_depth_stencil")) {
441 printf("GL_EXT_packed_depth_stencil not found!\n");
442 exit(0);
443 }
444 if (!glutExtensionSupported("GL_ARB_framebuffer_object")) {
445 printf("GL_ARB_framebuffer_object not found!\n");
446 exit(0);
447 }
448 UsePackedDepthStencilBoth = GL_TRUE;
449 printf("Using GL_EXT_packed_depth_stencil and GL_DEPTH_STENCIL attachment point\n");
450 }
451 else if (strcmp(argv[i], "-arb") == 0) {
452 if (!glutExtensionSupported("GL_ARB_framebuffer_object")) {
453 printf("Sorry, GL_ARB_framebuffer object not supported!\n");
454 }
455 else {
456 Use_ARB_fbo = GL_TRUE;
457 }
458 }
459 else {
460 printf("Unknown option: %s\n", argv[i]);
461 }
462 }
463}
Brian Paule4b23562005-05-04 20:11:35 +0000464
Brian Paul0906f472009-01-22 15:25:10 -0700465
Brian Paulfb643652009-06-11 15:50:47 -0600466static void
467SetupFunctionPointers(void)
468{
469 GetExtensionFuncs();
470
471 if (Use_ARB_fbo) {
472 /* no-op: use the ARB functions as-is */
473 }
474 else {
475 /* set the ARB-flavor function pointers to point to the EXT functions */
476 glIsRenderbuffer_func = glIsRenderbufferEXT_func;
477 glBindRenderbuffer_func = glBindRenderbufferEXT_func;
478 glDeleteRenderbuffers_func = glDeleteRenderbuffersEXT_func;
479 glGenRenderbuffers_func = glGenRenderbuffersEXT_func;
480 glRenderbufferStorage_func = glRenderbufferStorageEXT_func;
481 glGetRenderbufferParameteriv_func = glGetRenderbufferParameterivEXT_func;
482 glIsFramebuffer_func = glIsFramebufferEXT_func;
483 glBindFramebuffer_func = glBindFramebufferEXT_func;
484 glDeleteFramebuffers_func = glDeleteFramebuffersEXT_func;
485 glGenFramebuffers_func = glGenFramebuffersEXT_func;
486 glCheckFramebufferStatus_func = glCheckFramebufferStatusEXT_func;
487 glFramebufferTexture1D_func = glFramebufferTexture1DEXT_func;
488 glFramebufferTexture2D_func = glFramebufferTexture2DEXT_func;
489 glFramebufferTexture3D_func = glFramebufferTexture3DEXT_func;
490 glFramebufferRenderbuffer_func = glFramebufferRenderbufferEXT_func;
491 glGetFramebufferAttachmentParameteriv_func = glGetFramebufferAttachmentParameterivEXT_func;
492 glGenerateMipmap_func = glGenerateMipmapEXT_func;
493 }
494}
495
496
Brian Paul0906f472009-01-22 15:25:10 -0700497/*
498 * Make FBO to render into given texture.
499 */
500static GLuint
Brian Paule8957f42009-08-14 14:15:18 -0600501MakeFBO_RenderTexture(GLuint texObj)
Brian Paul0906f472009-01-22 15:25:10 -0700502{
503 GLuint fb;
504 GLint sizeFudge = 0;
505
Brian Paulfb643652009-06-11 15:50:47 -0600506 glGenFramebuffers_func(1, &fb);
507 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, fb);
Brian Paul0906f472009-01-22 15:25:10 -0700508 /* Render color to texture */
Brian Paulfb643652009-06-11 15:50:47 -0600509 glFramebufferTexture2D_func(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
Brian Paule8957f42009-08-14 14:15:18 -0600510 TexTarget, texObj, TextureLevel);
Brian Paul0906f472009-01-22 15:25:10 -0700511
512 if (Use_ARB_fbo) {
513 /* use a smaller depth buffer to see what happens */
514 sizeFudge = 90;
515 }
516
517 /* Setup depth and stencil buffers */
518 {
519 GLboolean b;
520 b = AttachDepthAndStencilBuffers(fb,
521 TexWidth - sizeFudge,
522 TexHeight - sizeFudge,
523 UsePackedDepthStencil,
524 UsePackedDepthStencilBoth,
525 &DepthRB, &StencilRB);
526 if (!b) {
527 /* try !UsePackedDepthStencil */
528 b = AttachDepthAndStencilBuffers(fb,
529 TexWidth - sizeFudge,
530 TexHeight - sizeFudge,
531 !UsePackedDepthStencil,
532 UsePackedDepthStencilBoth,
533 &DepthRB, &StencilRB);
534 }
535 if (!b) {
536 printf("Unable to create/attach depth and stencil renderbuffers "
537 " to FBO!\n");
538 exit(1);
539 }
540 }
541
542 /* queries */
543 {
Brian Paule8957f42009-08-14 14:15:18 -0600544 GLint bits, w, h, name;
Brian Paul0906f472009-01-22 15:25:10 -0700545
Brian Paulfb643652009-06-11 15:50:47 -0600546 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, DepthRB);
547 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700548 GL_RENDERBUFFER_WIDTH_EXT, &w);
Brian Paulfb643652009-06-11 15:50:47 -0600549 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700550 GL_RENDERBUFFER_HEIGHT_EXT, &h);
551 printf("Color/Texture size: %d x %d\n", TexWidth, TexHeight);
552 printf("Depth buffer size: %d x %d\n", w, h);
553
Brian Paulfb643652009-06-11 15:50:47 -0600554 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700555 GL_RENDERBUFFER_DEPTH_SIZE_EXT, &bits);
556 printf("Depth renderbuffer size = %d bits\n", bits);
557
Brian Paulfb643652009-06-11 15:50:47 -0600558 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, StencilRB);
559 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
Brian Paul0906f472009-01-22 15:25:10 -0700560 GL_RENDERBUFFER_STENCIL_SIZE_EXT, &bits);
561 printf("Stencil renderbuffer size = %d bits\n", bits);
Brian Paul0906f472009-01-22 15:25:10 -0700562
Brian Paule8957f42009-08-14 14:15:18 -0600563 glGetFramebufferAttachmentParameteriv_func(GL_FRAMEBUFFER_EXT,
564 GL_COLOR_ATTACHMENT0,
565 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT,
566 &name);
567 printf("Render to texture name: %d\n", texObj);
568 printf("Color attachment[0] name: %d\n", name);
569 assert(texObj == name);
570
571 glGetFramebufferAttachmentParameteriv_func(GL_FRAMEBUFFER_EXT,
572 GL_STENCIL_ATTACHMENT,
573 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT,
574 &name);
575 printf("Stencil attachment name: %d\n", name);
576
577 glGetFramebufferAttachmentParameteriv_func(GL_FRAMEBUFFER_EXT,
578 GL_DEPTH_ATTACHMENT,
579 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT,
580 &name);
581 printf("Depth attachment name: %d\n", name);
582
583 }
Brian Paul0906f472009-01-22 15:25:10 -0700584 /* bind the regular framebuffer */
Brian Paulfb643652009-06-11 15:50:47 -0600585 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, 0);
Brian Paul0906f472009-01-22 15:25:10 -0700586
587 return fb;
588}
589
590
591static void
592Init(void)
593{
Brian Paule4b23562005-05-04 20:11:35 +0000594 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
595 printf("GL_EXT_framebuffer_object not found!\n");
596 exit(0);
597 }
Brian Paul32fe2332005-11-16 14:48:11 +0000598
Brian Paule4b23562005-05-04 20:11:35 +0000599 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
600
Brian Paulfb643652009-06-11 15:50:47 -0600601 SetupFunctionPointers();
602
Brian Paul64945412006-03-24 23:17:06 +0000603 /* lighting */
Brian Paul0906f472009-01-22 15:25:10 -0700604 {
605 static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 };
606 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat);
607 }
608
609 /*
610 * Make texture object/image (we'll render into this texture)
611 */
612 {
613 glGenTextures(1, &TexObj);
614 glBindTexture(TexTarget, TexObj);
615
616 /* make two image levels */
617 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
618 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Brian Paul07f96a62009-02-27 15:46:40 -0700619 if (TexTarget == GL_TEXTURE_2D) {
620 glTexImage2D(TexTarget, 1, TexIntFormat, TexWidth/2, TexHeight/2, 0,
621 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
622 TexWidth = TexWidth >> TextureLevel;
623 TexHeight = TexHeight >> TextureLevel;
624 glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
625 }
Brian Paul0906f472009-01-22 15:25:10 -0700626
627 glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
628 glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
629 glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
Brian Paul0906f472009-01-22 15:25:10 -0700630 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
631 }
632
633 MyFB = MakeFBO_RenderTexture(TexObj);
634}
635
636
637static void
638Usage(void)
639{
640 printf("Usage:\n");
641 printf(" -ds Use combined depth/stencil renderbuffer\n");
642 printf(" -arb Try GL_ARB_framebuffer_object's mismatched buffer sizes\n");
Brian Paul4626e572009-03-11 16:27:58 -0600643 printf(" -ds2 Try GL_ARB_framebuffer_object's GL_DEPTH_STENCIL_ATTACHMENT\n");
Brian Paul0906f472009-01-22 15:25:10 -0700644 printf("Keys:\n");
645 printf(" a Toggle animation\n");
646 printf(" s/s Step/rotate\n");
647 printf(" c Toggle back-face culling\n");
648 printf(" w Toggle wireframe mode (front-face only)\n");
649 printf(" Esc Exit\n");
Brian Paule4b23562005-05-04 20:11:35 +0000650}
651
652
653int
654main(int argc, char *argv[])
655{
656 glutInit(&argc, argv);
657 glutInitWindowPosition(0, 0);
658 glutInitWindowSize(Width, Height);
659 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
Brian2282d812007-03-06 16:33:00 -0700660 Win = glutCreateWindow(argv[0]);
Brian Paule4b23562005-05-04 20:11:35 +0000661 glutReshapeFunc(Reshape);
662 glutKeyboardFunc(Key);
663 glutDisplayFunc(Display);
664 if (Anim)
665 glutIdleFunc(Idle);
Brian Paul0906f472009-01-22 15:25:10 -0700666
667 ParseArgs(argc, argv);
668 Init();
Briandf198d22007-12-04 10:48:05 -0700669 Usage();
Brian Paul0906f472009-01-22 15:25:10 -0700670
Brian Paule4b23562005-05-04 20:11:35 +0000671 glutMainLoop();
672 return 0;
673}