blob: 50a4b00afce361a7830f185d1c56e2200b51a62b [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
Keith Whitwella58065d2009-03-10 13:11:23 +000012#include <GL/glew.h>
Brian Paul32fe2332005-11-16 14:48:11 +000013#include <GL/glut.h>
Brian Paule4b23562005-05-04 20:11:35 +000014#include <assert.h>
15#include <stdio.h>
16#include <stdlib.h>
Brian Paul32fe2332005-11-16 14:48:11 +000017#include <string.h>
Brian Paule4b23562005-05-04 20:11:35 +000018#include <math.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 Paule4b23562005-05-04 20:11:35 +000083 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
Brian Paule4b23562005-05-04 20:11:35 +000084
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);
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 */
174 glBindFramebufferEXT(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 Paul32fe2332005-11-16 14:48:11 +0000255 glDeleteRenderbuffersEXT(1, &DepthRB);
Brian Paul64945412006-03-24 23:17:06 +0000256#endif
257#if STENCIL
Brian Paul0906f472009-01-22 15:25:10 -0700258 glDeleteRenderbuffersEXT(1, &StencilRB);
Brian Paul64945412006-03-24 23:17:06 +0000259#endif
Brian Paul32fe2332005-11-16 14:48:11 +0000260 glDeleteFramebuffersEXT(1, &MyFB);
261
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
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);
Brian Paul07f96a62009-02-27 15:46:40 -0700412 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
413 glDeleteRenderbuffersEXT(1, depthRbOut);
414 *depthRbOut = 0;
415 glDeleteRenderbuffersEXT(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
466/*
467 * Make FBO to render into given texture.
468 */
469static GLuint
470MakeFBO_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
540static void
541Init(void)
542{
Brian Paule4b23562005-05-04 20:11:35 +0000543 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
544 printf("GL_EXT_framebuffer_object not found!\n");
545 exit(0);
546 }
Brian Paul32fe2332005-11-16 14:48:11 +0000547
Brian Paule4b23562005-05-04 20:11:35 +0000548 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
549
Brian Paul64945412006-03-24 23:17:06 +0000550 /* lighting */
Brian Paul0906f472009-01-22 15:25:10 -0700551 {
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);
Brian Paul07f96a62009-02-27 15:46:40 -0700566 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 }
Brian Paul0906f472009-01-22 15:25:10 -0700573
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);
Brian Paul0906f472009-01-22 15:25:10 -0700577 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
578 }
579
580 MyFB = MakeFBO_RenderTexture(TexObj);
581}
582
583
584static void
585Usage(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");
Brian Paul4626e572009-03-11 16:27:58 -0600590 printf(" -ds2 Try GL_ARB_framebuffer_object's GL_DEPTH_STENCIL_ATTACHMENT\n");
Brian Paul0906f472009-01-22 15:25:10 -0700591 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");
Brian Paule4b23562005-05-04 20:11:35 +0000597}
598
599
600int
601main(int argc, char *argv[])
602{
603 glutInit(&argc, argv);
604 glutInitWindowPosition(0, 0);
605 glutInitWindowSize(Width, Height);
606 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
Brian2282d812007-03-06 16:33:00 -0700607 Win = glutCreateWindow(argv[0]);
Keith Whitwella58065d2009-03-10 13:11:23 +0000608 glewInit();
Brian Paule4b23562005-05-04 20:11:35 +0000609 glutReshapeFunc(Reshape);
610 glutKeyboardFunc(Key);
611 glutDisplayFunc(Display);
612 if (Anim)
613 glutIdleFunc(Idle);
Brian Paul0906f472009-01-22 15:25:10 -0700614
615 ParseArgs(argc, argv);
616 Init();
Briandf198d22007-12-04 10:48:05 -0700617 Usage();
Brian Paul0906f472009-01-22 15:25:10 -0700618
Brian Paule4b23562005-05-04 20:11:35 +0000619 glutMainLoop();
620 return 0;
621}