ae993576b025cbc7befc1accdebff7be525ccc62
[mesa.git] / progs / tests / 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 #define GL_GLEXT_PROTOTYPES
13 #include <GL/glut.h>
14 #include <assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <math.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 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
84
85 status = glCheckFramebufferStatusEXT(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 glBindFramebufferEXT(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 glDeleteRenderbuffersEXT(1, &DepthRB);
256 #endif
257 #if STENCIL
258 glDeleteRenderbuffersEXT(1, &StencilRB);
259 #endif
260 glDeleteFramebuffersEXT(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 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
322
323 if (tryDepthStencil) {
324 GLuint rb;
325
326 glGenRenderbuffersEXT(1, &rb);
327 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
328 glRenderbufferStorageEXT(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 glFramebufferRenderbufferEXT(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 glFramebufferRenderbufferEXT(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 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
352 GL_STENCIL_ATTACHMENT_EXT,
353 GL_RENDERBUFFER_EXT, rb);
354 if (glGetError())
355 return GL_FALSE;
356 }
357
358 status = glCheckFramebufferStatusEXT(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 glGenRenderbuffersEXT(1, &rb);
371 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
372 glRenderbufferStorageEXT(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 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
380 GL_DEPTH_ATTACHMENT_EXT,
381 GL_RENDERBUFFER_EXT, rb);
382 if (glGetError())
383 return GL_FALSE;
384
385 status = glCheckFramebufferStatusEXT(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 glGenRenderbuffersEXT(1, &rb);
397 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
398 glRenderbufferStorageEXT(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 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
406 GL_STENCIL_ATTACHMENT_EXT,
407 GL_RENDERBUFFER_EXT, rb);
408 if (glGetError())
409 return GL_FALSE;
410
411 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
412 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
413 glDeleteRenderbuffersEXT(1, depthRbOut);
414 *depthRbOut = 0;
415 glDeleteRenderbuffersEXT(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 /*
467 * Make FBO to render into given texture.
468 */
469 static GLuint
470 MakeFBO_RenderTexture(GLuint TexObj)
471 {
472 GLuint fb;
473 GLint sizeFudge = 0;
474
475 glGenFramebuffersEXT(1, &fb);
476 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
477 /* Render color to texture */
478 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
479 TexTarget, TexObj, TextureLevel);
480
481 if (Use_ARB_fbo) {
482 /* use a smaller depth buffer to see what happens */
483 sizeFudge = 90;
484 }
485
486 /* Setup depth and stencil buffers */
487 {
488 GLboolean b;
489 b = AttachDepthAndStencilBuffers(fb,
490 TexWidth - sizeFudge,
491 TexHeight - sizeFudge,
492 UsePackedDepthStencil,
493 UsePackedDepthStencilBoth,
494 &DepthRB, &StencilRB);
495 if (!b) {
496 /* try !UsePackedDepthStencil */
497 b = AttachDepthAndStencilBuffers(fb,
498 TexWidth - sizeFudge,
499 TexHeight - sizeFudge,
500 !UsePackedDepthStencil,
501 UsePackedDepthStencilBoth,
502 &DepthRB, &StencilRB);
503 }
504 if (!b) {
505 printf("Unable to create/attach depth and stencil renderbuffers "
506 " to FBO!\n");
507 exit(1);
508 }
509 }
510
511 /* queries */
512 {
513 GLint bits, w, h;
514
515 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRB);
516 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
517 GL_RENDERBUFFER_WIDTH_EXT, &w);
518 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
519 GL_RENDERBUFFER_HEIGHT_EXT, &h);
520 printf("Color/Texture size: %d x %d\n", TexWidth, TexHeight);
521 printf("Depth buffer size: %d x %d\n", w, h);
522
523 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
524 GL_RENDERBUFFER_DEPTH_SIZE_EXT, &bits);
525 printf("Depth renderbuffer size = %d bits\n", bits);
526
527 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, StencilRB);
528 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
529 GL_RENDERBUFFER_STENCIL_SIZE_EXT, &bits);
530 printf("Stencil renderbuffer size = %d bits\n", bits);
531 }
532
533 /* bind the regular framebuffer */
534 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
535
536 return fb;
537 }
538
539
540 static void
541 Init(void)
542 {
543 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
544 printf("GL_EXT_framebuffer_object not found!\n");
545 exit(0);
546 }
547
548 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
549
550 /* lighting */
551 {
552 static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 };
553 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat);
554 }
555
556 /*
557 * Make texture object/image (we'll render into this texture)
558 */
559 {
560 glGenTextures(1, &TexObj);
561 glBindTexture(TexTarget, TexObj);
562
563 /* make two image levels */
564 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
565 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
566 if (TexTarget == GL_TEXTURE_2D) {
567 glTexImage2D(TexTarget, 1, TexIntFormat, TexWidth/2, TexHeight/2, 0,
568 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
569 TexWidth = TexWidth >> TextureLevel;
570 TexHeight = TexHeight >> TextureLevel;
571 glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
572 }
573
574 glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
575 glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
576 glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
577 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
578 }
579
580 MyFB = MakeFBO_RenderTexture(TexObj);
581 }
582
583
584 static void
585 Usage(void)
586 {
587 printf("Usage:\n");
588 printf(" -ds Use combined depth/stencil renderbuffer\n");
589 printf(" -arb Try GL_ARB_framebuffer_object's mismatched buffer sizes\n");
590 printf(" -ds2 Tye GL_ARB_framebuffer_object's GL_DEPTH_STENCIL_ATTACHMENT\n");
591 printf("Keys:\n");
592 printf(" a Toggle animation\n");
593 printf(" s/s Step/rotate\n");
594 printf(" c Toggle back-face culling\n");
595 printf(" w Toggle wireframe mode (front-face only)\n");
596 printf(" Esc Exit\n");
597 }
598
599
600 int
601 main(int argc, char *argv[])
602 {
603 glutInit(&argc, argv);
604 glutInitWindowPosition(0, 0);
605 glutInitWindowSize(Width, Height);
606 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
607 Win = glutCreateWindow(argv[0]);
608 glutReshapeFunc(Reshape);
609 glutKeyboardFunc(Key);
610 glutDisplayFunc(Display);
611 if (Anim)
612 glutIdleFunc(Idle);
613
614 ParseArgs(argc, argv);
615 Init();
616 Usage();
617
618 glutMainLoop();
619 return 0;
620 }