ARB prog parser: fix parameter binding type
[mesa.git] / progs / demos / fbotexture.c
1 /*
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
12 #include <GL/glut.h>
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <math.h>
18 #include "extfuncs.h"
19
20 /* For debug */
21 #define DEPTH 1
22 #define STENCIL 1
23 #define DRAW 1
24
25
26 static int Win = 0;
27 static int Width = 400, Height = 400;
28
29 #if 1
30 static GLenum TexTarget = GL_TEXTURE_2D;
31 static int TexWidth = 512, TexHeight = 512;
32 static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
33 #else
34 static GLenum TexTarget = GL_TEXTURE_RECTANGLE_ARB;
35 static int TexWidth = 200, TexHeight = 200;
36 static GLenum TexIntFormat = GL_RGB5; /* either GL_RGB or GL_RGBA */
37 #endif
38 static GLuint TextureLevel = 0; /* which texture level to render to */
39
40 static GLuint MyFB;
41 static GLuint TexObj;
42 static GLuint DepthRB = 0, StencilRB = 0;
43 static GLboolean Anim = GL_FALSE;
44 static GLfloat Rot = 0.0;
45 static GLboolean UsePackedDepthStencil = GL_FALSE;
46 static GLboolean UsePackedDepthStencilBoth = GL_FALSE;
47 static GLboolean Use_ARB_fbo = GL_FALSE;
48 static GLboolean Cull = GL_FALSE;
49 static GLboolean Wireframe = GL_FALSE;
50
51
52 static void
53 CheckError(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
62 static void
63 Idle(void)
64 {
65 Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
66 glutPostRedisplay();
67 }
68
69
70 static void
71 RenderTexture(void)
72 {
73 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
82 /* draw to texture image */
83 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, MyFB);
84
85 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
86 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);
94 CheckError(__LINE__);
95
96 #if DEPTH
97 glEnable(GL_DEPTH_TEST);
98 #endif
99
100 #if STENCIL
101 glEnable(GL_STENCIL_TEST);
102 glStencilFunc(GL_NEVER, 1, ~0);
103 glStencilOp(GL_REPLACE, GL_KEEP, GL_REPLACE);
104 #endif
105
106 CheckError(__LINE__);
107
108 #if DEPTH || STENCIL
109 /* 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();
117 #endif
118
119 /* draw teapot where stencil != 1 */
120 #if STENCIL
121 glStencilFunc(GL_NOTEQUAL, 1, ~0);
122 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
123 #endif
124
125 CheckError(__LINE__);
126
127 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
143 #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);
157 glFrontFace(GL_CW); /* Teapot patches backward */
158 glutSolidTeapot(0.5);
159 glFrontFace(GL_CCW);
160 glPopMatrix();
161 glDisable(GL_LIGHTING);
162 /*
163 PrintStencilHistogram(TexWidth, TexHeight);
164 */
165 #endif
166
167 glDisable(GL_DEPTH_TEST);
168 glDisable(GL_STENCIL_TEST);
169 glDisable(GL_CULL_FACE);
170 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
171
172 #if DRAW
173 /* Bind normal framebuffer */
174 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, 0);
175 #endif
176
177 CheckError(__LINE__);
178 }
179
180
181
182 static void
183 Display(void)
184 {
185 float ar = (float) Width / (float) Height;
186
187 RenderTexture();
188
189 /* draw textured quad in the window */
190 #if DRAW
191 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);
205 glEnable(TexTarget);
206 glBindTexture(TexTarget, TexObj);
207 glBegin(GL_POLYGON);
208 glColor3f(0.25, 0.25, 0.25);
209 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 }
232 glEnd();
233 glPopMatrix();
234 glDisable(TexTarget);
235 #endif
236
237 glutSwapBuffers();
238 CheckError(__LINE__);
239 }
240
241
242 static void
243 Reshape(int width, int height)
244 {
245 glViewport(0, 0, width, height);
246 Width = width;
247 Height = height;
248 }
249
250
251 static void
252 CleanUp(void)
253 {
254 #if DEPTH
255 glDeleteRenderbuffers_func(1, &DepthRB);
256 #endif
257 #if STENCIL
258 glDeleteRenderbuffers_func(1, &StencilRB);
259 #endif
260 glDeleteFramebuffers_func(1, &MyFB);
261
262 glDeleteTextures(1, &TexObj);
263
264 glutDestroyWindow(Win);
265
266 exit(0);
267 }
268
269
270 static void
271 Key(unsigned char key, int x, int y)
272 {
273 (void) x;
274 (void) y;
275 switch (key) {
276 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;
298 }
299 glutPostRedisplay();
300 }
301
302
303 /**
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 */
310 static GLboolean
311 AttachDepthAndStencilBuffers(GLuint fbo,
312 GLsizei width, GLsizei height,
313 GLboolean tryDepthStencil,
314 GLboolean bindDepthStencil,
315 GLuint *depthRbOut, GLuint *stencilRbOut)
316 {
317 GLenum status;
318
319 *depthRbOut = *stencilRbOut = 0;
320
321 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, fbo);
322
323 if (tryDepthStencil) {
324 GLuint rb;
325
326 glGenRenderbuffers_func(1, &rb);
327 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb);
328 glRenderbufferStorage_func(GL_RENDERBUFFER_EXT,
329 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 */
336 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
337 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 */
344 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
345 GL_DEPTH_ATTACHMENT_EXT,
346 GL_RENDERBUFFER_EXT, rb);
347 if (glGetError())
348 return GL_FALSE;
349
350 /* and attach to stencil attachment point */
351 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
352 GL_STENCIL_ATTACHMENT_EXT,
353 GL_RENDERBUFFER_EXT, rb);
354 if (glGetError())
355 return GL_FALSE;
356 }
357
358 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
359 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
370 glGenRenderbuffers_func(1, &rb);
371 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb);
372 glRenderbufferStorage_func(GL_RENDERBUFFER_EXT,
373 GL_DEPTH_COMPONENT,
374 width, height);
375 if (glGetError())
376 return GL_FALSE;
377
378 /* attach to depth attachment point */
379 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
380 GL_DEPTH_ATTACHMENT_EXT,
381 GL_RENDERBUFFER_EXT, rb);
382 if (glGetError())
383 return GL_FALSE;
384
385 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
386 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
396 glGenRenderbuffers_func(1, &rb);
397 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb);
398 glRenderbufferStorage_func(GL_RENDERBUFFER_EXT,
399 GL_STENCIL_INDEX,
400 width, height);
401 if (glGetError())
402 return GL_FALSE;
403
404 /* attach to depth attachment point */
405 glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT,
406 GL_STENCIL_ATTACHMENT_EXT,
407 GL_RENDERBUFFER_EXT, rb);
408 if (glGetError())
409 return GL_FALSE;
410
411 status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT);
412 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
413 glDeleteRenderbuffers_func(1, depthRbOut);
414 *depthRbOut = 0;
415 glDeleteRenderbuffers_func(1, &rb);
416 return GL_FALSE;
417 }
418
419 *stencilRbOut = rb;
420 }
421
422 return GL_TRUE;
423 }
424
425
426 static void
427 ParseArgs(int argc, char *argv[])
428 {
429 GLint i;
430 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 }
464
465
466 static void
467 SetupFunctionPointers(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
497 /*
498 * Make FBO to render into given texture.
499 */
500 static GLuint
501 MakeFBO_RenderTexture(GLuint texObj)
502 {
503 GLuint fb;
504 GLint sizeFudge = 0;
505
506 glGenFramebuffers_func(1, &fb);
507 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, fb);
508 /* Render color to texture */
509 glFramebufferTexture2D_func(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
510 TexTarget, texObj, TextureLevel);
511
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 {
544 GLint bits, w, h, name;
545
546 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, DepthRB);
547 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
548 GL_RENDERBUFFER_WIDTH_EXT, &w);
549 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
550 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
554 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
555 GL_RENDERBUFFER_DEPTH_SIZE_EXT, &bits);
556 printf("Depth renderbuffer size = %d bits\n", bits);
557
558 glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, StencilRB);
559 glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT,
560 GL_RENDERBUFFER_STENCIL_SIZE_EXT, &bits);
561 printf("Stencil renderbuffer size = %d bits\n", bits);
562
563 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 }
584 /* bind the regular framebuffer */
585 glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, 0);
586
587 return fb;
588 }
589
590
591 static void
592 Init(void)
593 {
594 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
595 printf("GL_EXT_framebuffer_object not found!\n");
596 exit(0);
597 }
598
599 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
600
601 SetupFunctionPointers();
602
603 /* lighting */
604 {
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);
619 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 }
626
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);
630 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
631 }
632
633 MyFB = MakeFBO_RenderTexture(TexObj);
634 }
635
636
637 static void
638 Usage(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");
643 printf(" -ds2 Try GL_ARB_framebuffer_object's GL_DEPTH_STENCIL_ATTACHMENT\n");
644 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");
650 }
651
652
653 int
654 main(int argc, char *argv[])
655 {
656 glutInit(&argc, argv);
657 glutInitWindowPosition(0, 0);
658 glutInitWindowSize(Width, Height);
659 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
660 Win = glutCreateWindow(argv[0]);
661 glutReshapeFunc(Reshape);
662 glutKeyboardFunc(Key);
663 glutDisplayFunc(Display);
664 if (Anim)
665 glutIdleFunc(Idle);
666
667 ParseArgs(argc, argv);
668 Init();
669 Usage();
670
671 glutMainLoop();
672 return 0;
673 }