Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 12 | #include <GL/glut.h> |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 16 | #include <string.h> |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 17 | #include <math.h> |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 18 | #include "extfuncs.h" |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 19 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 20 | /* For debug */ |
| 21 | #define DEPTH 1 |
| 22 | #define STENCIL 1 |
| 23 | #define DRAW 1 |
| 24 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 25 | |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 26 | static int Win = 0; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 27 | static int Width = 400, Height = 400; |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 28 | |
Brian Paul | 07f96a6 | 2009-02-27 15:46:40 -0700 | [diff] [blame] | 29 | #if 1 |
| 30 | static GLenum TexTarget = GL_TEXTURE_2D; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 31 | static int TexWidth = 512, TexHeight = 512; |
Brian Paul | 07f96a6 | 2009-02-27 15:46:40 -0700 | [diff] [blame] | 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 */ |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 39 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 40 | static GLuint MyFB; |
| 41 | static GLuint TexObj; |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 42 | static GLuint DepthRB = 0, StencilRB = 0; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 43 | static GLboolean Anim = GL_FALSE; |
| 44 | static GLfloat Rot = 0.0; |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 45 | static GLboolean UsePackedDepthStencil = GL_FALSE; |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 46 | static GLboolean UsePackedDepthStencilBoth = GL_FALSE; |
| 47 | static GLboolean Use_ARB_fbo = GL_FALSE; |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 48 | static GLboolean Cull = GL_FALSE; |
| 49 | static GLboolean Wireframe = GL_FALSE; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 | { |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 65 | Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 66 | glutPostRedisplay(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static void |
| 71 | RenderTexture(void) |
| 72 | { |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 | |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame] | 82 | /* draw to texture image */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 83 | glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, MyFB); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 84 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 85 | status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 94 | CheckError(__LINE__); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 95 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 96 | #if DEPTH |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 97 | glEnable(GL_DEPTH_TEST); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 98 | #endif |
| 99 | |
| 100 | #if STENCIL |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 101 | glEnable(GL_STENCIL_TEST); |
| 102 | glStencilFunc(GL_NEVER, 1, ~0); |
| 103 | glStencilOp(GL_REPLACE, GL_KEEP, GL_REPLACE); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 104 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 105 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 106 | CheckError(__LINE__); |
| 107 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 108 | #if DEPTH || STENCIL |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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(); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 117 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 118 | |
| 119 | /* draw teapot where stencil != 1 */ |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 120 | #if STENCIL |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 121 | glStencilFunc(GL_NOTEQUAL, 1, ~0); |
| 122 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 123 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 124 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 125 | CheckError(__LINE__); |
| 126 | |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 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 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 157 | glFrontFace(GL_CW); /* Teapot patches backward */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 158 | glutSolidTeapot(0.5); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 159 | glFrontFace(GL_CCW); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 160 | glPopMatrix(); |
| 161 | glDisable(GL_LIGHTING); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 162 | /* |
| 163 | PrintStencilHistogram(TexWidth, TexHeight); |
| 164 | */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 165 | #endif |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 166 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 167 | glDisable(GL_DEPTH_TEST); |
| 168 | glDisable(GL_STENCIL_TEST); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 169 | glDisable(GL_CULL_FACE); |
| 170 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 171 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 172 | #if DRAW |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 173 | /* Bind normal framebuffer */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 174 | glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, 0); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 175 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 */ |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 190 | #if DRAW |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 205 | glEnable(TexTarget); |
| 206 | glBindTexture(TexTarget, TexObj); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 207 | glBegin(GL_POLYGON); |
| 208 | glColor3f(0.25, 0.25, 0.25); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 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 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 232 | glEnd(); |
| 233 | glPopMatrix(); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 234 | glDisable(TexTarget); |
| 235 | #endif |
| 236 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 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 |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 252 | CleanUp(void) |
| 253 | { |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 254 | #if DEPTH |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 255 | glDeleteRenderbuffers_func(1, &DepthRB); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 256 | #endif |
| 257 | #if STENCIL |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 258 | glDeleteRenderbuffers_func(1, &StencilRB); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 259 | #endif |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 260 | glDeleteFramebuffers_func(1, &MyFB); |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 261 | |
| 262 | glDeleteTextures(1, &TexObj); |
| 263 | |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 264 | glutDestroyWindow(Win); |
| 265 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 266 | exit(0); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static void |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 271 | Key(unsigned char key, int x, int y) |
| 272 | { |
| 273 | (void) x; |
| 274 | (void) y; |
| 275 | switch (key) { |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 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; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 298 | } |
| 299 | glutPostRedisplay(); |
| 300 | } |
| 301 | |
| 302 | |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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) |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 316 | { |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 317 | GLenum status; |
| 318 | |
| 319 | *depthRbOut = *stencilRbOut = 0; |
| 320 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 321 | glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, fbo); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 322 | |
| 323 | if (tryDepthStencil) { |
| 324 | GLuint rb; |
| 325 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 326 | glGenRenderbuffers_func(1, &rb); |
| 327 | glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb); |
| 328 | glRenderbufferStorage_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 336 | glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 344 | glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 351 | glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 352 | GL_STENCIL_ATTACHMENT_EXT, |
| 353 | GL_RENDERBUFFER_EXT, rb); |
| 354 | if (glGetError()) |
| 355 | return GL_FALSE; |
| 356 | } |
| 357 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 358 | status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 370 | glGenRenderbuffers_func(1, &rb); |
| 371 | glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb); |
| 372 | glRenderbufferStorage_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 373 | GL_DEPTH_COMPONENT, |
| 374 | width, height); |
| 375 | if (glGetError()) |
| 376 | return GL_FALSE; |
| 377 | |
| 378 | /* attach to depth attachment point */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 379 | glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 380 | GL_DEPTH_ATTACHMENT_EXT, |
| 381 | GL_RENDERBUFFER_EXT, rb); |
| 382 | if (glGetError()) |
| 383 | return GL_FALSE; |
| 384 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 385 | status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 396 | glGenRenderbuffers_func(1, &rb); |
| 397 | glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, rb); |
| 398 | glRenderbufferStorage_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 399 | GL_STENCIL_INDEX, |
| 400 | width, height); |
| 401 | if (glGetError()) |
| 402 | return GL_FALSE; |
| 403 | |
| 404 | /* attach to depth attachment point */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 405 | glFramebufferRenderbuffer_func(GL_FRAMEBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 406 | GL_STENCIL_ATTACHMENT_EXT, |
| 407 | GL_RENDERBUFFER_EXT, rb); |
| 408 | if (glGetError()) |
| 409 | return GL_FALSE; |
| 410 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 411 | status = glCheckFramebufferStatus_func(GL_FRAMEBUFFER_EXT); |
Brian Paul | 07f96a6 | 2009-02-27 15:46:40 -0700 | [diff] [blame] | 412 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 413 | glDeleteRenderbuffers_func(1, depthRbOut); |
Brian Paul | 07f96a6 | 2009-02-27 15:46:40 -0700 | [diff] [blame] | 414 | *depthRbOut = 0; |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 415 | glDeleteRenderbuffers_func(1, &rb); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 416 | return GL_FALSE; |
Brian Paul | 07f96a6 | 2009-02-27 15:46:40 -0700 | [diff] [blame] | 417 | } |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 418 | |
| 419 | *stencilRbOut = rb; |
| 420 | } |
| 421 | |
| 422 | return GL_TRUE; |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | |
| 426 | static void |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 427 | ParseArgs(int argc, char *argv[]) |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 428 | { |
| 429 | GLint i; |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 464 | |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 465 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 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 | |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 506 | glGenFramebuffers_func(1, &fb); |
| 507 | glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, fb); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 508 | /* Render color to texture */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 509 | glFramebufferTexture2D_func(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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; |
| 545 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 546 | glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, DepthRB); |
| 547 | glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 548 | GL_RENDERBUFFER_WIDTH_EXT, &w); |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 549 | glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 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 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 554 | glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 555 | GL_RENDERBUFFER_DEPTH_SIZE_EXT, &bits); |
| 556 | printf("Depth renderbuffer size = %d bits\n", bits); |
| 557 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 558 | glBindRenderbuffer_func(GL_RENDERBUFFER_EXT, StencilRB); |
| 559 | glGetRenderbufferParameteriv_func(GL_RENDERBUFFER_EXT, |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 560 | GL_RENDERBUFFER_STENCIL_SIZE_EXT, &bits); |
| 561 | printf("Stencil renderbuffer size = %d bits\n", bits); |
| 562 | } |
| 563 | |
| 564 | /* bind the regular framebuffer */ |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 565 | glBindFramebuffer_func(GL_FRAMEBUFFER_EXT, 0); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 566 | |
| 567 | return fb; |
| 568 | } |
| 569 | |
| 570 | |
| 571 | static void |
| 572 | Init(void) |
| 573 | { |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 574 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 575 | printf("GL_EXT_framebuffer_object not found!\n"); |
| 576 | exit(0); |
| 577 | } |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 578 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 579 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 580 | |
Brian Paul | fb64365 | 2009-06-11 15:50:47 -0600 | [diff] [blame^] | 581 | SetupFunctionPointers(); |
| 582 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 583 | /* lighting */ |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 584 | { |
| 585 | static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 }; |
| 586 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Make texture object/image (we'll render into this texture) |
| 591 | */ |
| 592 | { |
| 593 | glGenTextures(1, &TexObj); |
| 594 | glBindTexture(TexTarget, TexObj); |
| 595 | |
| 596 | /* make two image levels */ |
| 597 | glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0, |
| 598 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Brian Paul | 07f96a6 | 2009-02-27 15:46:40 -0700 | [diff] [blame] | 599 | if (TexTarget == GL_TEXTURE_2D) { |
| 600 | glTexImage2D(TexTarget, 1, TexIntFormat, TexWidth/2, TexHeight/2, 0, |
| 601 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 602 | TexWidth = TexWidth >> TextureLevel; |
| 603 | TexHeight = TexHeight >> TextureLevel; |
| 604 | glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel); |
| 605 | } |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 606 | |
| 607 | glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 608 | glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 609 | glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 610 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 611 | } |
| 612 | |
| 613 | MyFB = MakeFBO_RenderTexture(TexObj); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | static void |
| 618 | Usage(void) |
| 619 | { |
| 620 | printf("Usage:\n"); |
| 621 | printf(" -ds Use combined depth/stencil renderbuffer\n"); |
| 622 | printf(" -arb Try GL_ARB_framebuffer_object's mismatched buffer sizes\n"); |
Brian Paul | 4626e57 | 2009-03-11 16:27:58 -0600 | [diff] [blame] | 623 | printf(" -ds2 Try GL_ARB_framebuffer_object's GL_DEPTH_STENCIL_ATTACHMENT\n"); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 624 | printf("Keys:\n"); |
| 625 | printf(" a Toggle animation\n"); |
| 626 | printf(" s/s Step/rotate\n"); |
| 627 | printf(" c Toggle back-face culling\n"); |
| 628 | printf(" w Toggle wireframe mode (front-face only)\n"); |
| 629 | printf(" Esc Exit\n"); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | |
| 633 | int |
| 634 | main(int argc, char *argv[]) |
| 635 | { |
| 636 | glutInit(&argc, argv); |
| 637 | glutInitWindowPosition(0, 0); |
| 638 | glutInitWindowSize(Width, Height); |
| 639 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 640 | Win = glutCreateWindow(argv[0]); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 641 | glutReshapeFunc(Reshape); |
| 642 | glutKeyboardFunc(Key); |
| 643 | glutDisplayFunc(Display); |
| 644 | if (Anim) |
| 645 | glutIdleFunc(Idle); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 646 | |
| 647 | ParseArgs(argc, argv); |
| 648 | Init(); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame] | 649 | Usage(); |
Brian Paul | 0906f47 | 2009-01-22 15:25:10 -0700 | [diff] [blame] | 650 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 651 | glutMainLoop(); |
| 652 | return 0; |
| 653 | } |