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 | |
| 12 | #define GL_GLEXT_PROTOTYPES |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 13 | #include <GL/glut.h> |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 17 | #include <string.h> |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 18 | #include <math.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 | |
| 29 | static GLenum TexTarget = GL_TEXTURE_2D; /*GL_TEXTURE_RECTANGLE_ARB;*/ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 30 | static int TexWidth = 512, TexHeight = 512; |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 31 | /*static int TexWidth = 600, TexHeight = 600;*/ |
| 32 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 33 | static GLuint MyFB; |
| 34 | static GLuint TexObj; |
| 35 | static GLuint DepthRB, StencilRB; |
| 36 | static GLboolean Anim = GL_FALSE; |
| 37 | static GLfloat Rot = 0.0; |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 38 | static GLboolean UsePackedDepthStencil = GL_FALSE; |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 39 | static GLuint TextureLevel = 1; /* which texture level to render to */ |
| 40 | static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */ |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 41 | static GLboolean Cull = GL_FALSE; |
| 42 | static GLboolean Wireframe = GL_FALSE; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | static void |
| 46 | CheckError(int line) |
| 47 | { |
| 48 | GLenum err = glGetError(); |
| 49 | if (err) { |
| 50 | printf("GL Error 0x%x at line %d\n", (int) err, line); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static void |
| 56 | Idle(void) |
| 57 | { |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 58 | Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 59 | glutPostRedisplay(); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static void |
| 64 | RenderTexture(void) |
| 65 | { |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 66 | GLenum status; |
| 67 | |
| 68 | glMatrixMode(GL_PROJECTION); |
| 69 | glLoadIdentity(); |
| 70 | glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 71 | glMatrixMode(GL_MODELVIEW); |
| 72 | glLoadIdentity(); |
| 73 | glTranslatef(0.0, 0.0, -15.0); |
| 74 | |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame] | 75 | /* draw to texture image */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 76 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 77 | |
| 78 | status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 79 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 80 | printf("Framebuffer incomplete!!!\n"); |
| 81 | } |
| 82 | |
| 83 | glViewport(0, 0, TexWidth, TexHeight); |
| 84 | |
| 85 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 86 | 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] | 87 | CheckError(__LINE__); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 88 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 89 | #if DEPTH |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 90 | glEnable(GL_DEPTH_TEST); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | #if STENCIL |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 94 | glEnable(GL_STENCIL_TEST); |
| 95 | glStencilFunc(GL_NEVER, 1, ~0); |
| 96 | glStencilOp(GL_REPLACE, GL_KEEP, GL_REPLACE); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 97 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 98 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 99 | CheckError(__LINE__); |
| 100 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 101 | #if DEPTH || STENCIL |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 102 | /* draw diamond-shaped stencil pattern */ |
| 103 | glColor3f(0, 1, 0); |
| 104 | glBegin(GL_POLYGON); |
| 105 | glVertex2f(-0.2, 0.0); |
| 106 | glVertex2f( 0.0, -0.2); |
| 107 | glVertex2f( 0.2, 0.0); |
| 108 | glVertex2f( 0.0, 0.2); |
| 109 | glEnd(); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 110 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 111 | |
| 112 | /* draw teapot where stencil != 1 */ |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 113 | #if STENCIL |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 114 | glStencilFunc(GL_NOTEQUAL, 1, ~0); |
| 115 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 116 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 117 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 118 | CheckError(__LINE__); |
| 119 | |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 120 | if (Wireframe) { |
| 121 | glPolygonMode(GL_FRONT, GL_LINE); |
| 122 | } |
| 123 | else { |
| 124 | glPolygonMode(GL_FRONT, GL_FILL); |
| 125 | } |
| 126 | |
| 127 | if (Cull) { |
| 128 | /* cull back */ |
| 129 | glCullFace(GL_BACK); |
| 130 | glEnable(GL_CULL_FACE); |
| 131 | } |
| 132 | else { |
| 133 | glDisable(GL_CULL_FACE); |
| 134 | } |
| 135 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 136 | #if 0 |
| 137 | glBegin(GL_POLYGON); |
| 138 | glColor3f(1, 0, 0); |
| 139 | glVertex2f(-1, -1); |
| 140 | glColor3f(0, 1, 0); |
| 141 | glVertex2f(1, -1); |
| 142 | glColor3f(0, 0, 1); |
| 143 | glVertex2f(0, 1); |
| 144 | glEnd(); |
| 145 | #else |
| 146 | glEnable(GL_LIGHTING); |
| 147 | glEnable(GL_LIGHT0); |
| 148 | glPushMatrix(); |
| 149 | glRotatef(0.5 * Rot, 1.0, 0.0, 0.0); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 150 | glFrontFace(GL_CW); /* Teapot patches backward */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 151 | glutSolidTeapot(0.5); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 152 | glFrontFace(GL_CCW); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 153 | glPopMatrix(); |
| 154 | glDisable(GL_LIGHTING); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 155 | /* |
| 156 | PrintStencilHistogram(TexWidth, TexHeight); |
| 157 | */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 158 | #endif |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 159 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 160 | glDisable(GL_DEPTH_TEST); |
| 161 | glDisable(GL_STENCIL_TEST); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 162 | glDisable(GL_CULL_FACE); |
| 163 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 164 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 165 | #if DRAW |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 166 | /* Bind normal framebuffer */ |
| 167 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 168 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 169 | |
| 170 | CheckError(__LINE__); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | static void |
| 176 | Display(void) |
| 177 | { |
| 178 | float ar = (float) Width / (float) Height; |
| 179 | |
| 180 | RenderTexture(); |
| 181 | |
| 182 | /* draw textured quad in the window */ |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 183 | #if DRAW |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 184 | glMatrixMode(GL_PROJECTION); |
| 185 | glLoadIdentity(); |
| 186 | glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0); |
| 187 | glMatrixMode(GL_MODELVIEW); |
| 188 | glLoadIdentity(); |
| 189 | glTranslatef(0.0, 0.0, -7.0); |
| 190 | |
| 191 | glViewport(0, 0, Width, Height); |
| 192 | |
| 193 | glClearColor(0.25, 0.25, 0.25, 0); |
| 194 | glClear(GL_COLOR_BUFFER_BIT); |
| 195 | |
| 196 | glPushMatrix(); |
| 197 | glRotatef(Rot, 0, 1, 0); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 198 | glEnable(TexTarget); |
| 199 | glBindTexture(TexTarget, TexObj); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 200 | glBegin(GL_POLYGON); |
| 201 | glColor3f(0.25, 0.25, 0.25); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 202 | if (TexTarget == GL_TEXTURE_2D) { |
| 203 | glTexCoord2f(0, 0); |
| 204 | glVertex2f(-1, -1); |
| 205 | glTexCoord2f(1, 0); |
| 206 | glVertex2f(1, -1); |
| 207 | glColor3f(1.0, 1.0, 1.0); |
| 208 | glTexCoord2f(1, 1); |
| 209 | glVertex2f(1, 1); |
| 210 | glTexCoord2f(0, 1); |
| 211 | glVertex2f(-1, 1); |
| 212 | } |
| 213 | else { |
| 214 | assert(TexTarget == GL_TEXTURE_RECTANGLE_ARB); |
| 215 | glTexCoord2f(0, 0); |
| 216 | glVertex2f(-1, -1); |
| 217 | glTexCoord2f(TexWidth, 0); |
| 218 | glVertex2f(1, -1); |
| 219 | glColor3f(1.0, 1.0, 1.0); |
| 220 | glTexCoord2f(TexWidth, TexHeight); |
| 221 | glVertex2f(1, 1); |
| 222 | glTexCoord2f(0, TexHeight); |
| 223 | glVertex2f(-1, 1); |
| 224 | } |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 225 | glEnd(); |
| 226 | glPopMatrix(); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 227 | glDisable(TexTarget); |
| 228 | #endif |
| 229 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 230 | glutSwapBuffers(); |
| 231 | CheckError(__LINE__); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static void |
| 236 | Reshape(int width, int height) |
| 237 | { |
| 238 | glViewport(0, 0, width, height); |
| 239 | Width = width; |
| 240 | Height = height; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | static void |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 245 | CleanUp(void) |
| 246 | { |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 247 | #if DEPTH |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 248 | glDeleteRenderbuffersEXT(1, &DepthRB); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 249 | #endif |
| 250 | #if STENCIL |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 251 | if (!UsePackedDepthStencil) |
| 252 | glDeleteRenderbuffersEXT(1, &StencilRB); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 253 | #endif |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 254 | glDeleteFramebuffersEXT(1, &MyFB); |
| 255 | |
| 256 | glDeleteTextures(1, &TexObj); |
| 257 | |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 258 | glutDestroyWindow(Win); |
| 259 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 260 | exit(0); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | static void |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 265 | Key(unsigned char key, int x, int y) |
| 266 | { |
| 267 | (void) x; |
| 268 | (void) y; |
| 269 | switch (key) { |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 270 | case 'a': |
| 271 | Anim = !Anim; |
| 272 | if (Anim) |
| 273 | glutIdleFunc(Idle); |
| 274 | else |
| 275 | glutIdleFunc(NULL); |
| 276 | break; |
| 277 | case 'c': |
| 278 | Cull = !Cull; |
| 279 | break; |
| 280 | case 'w': |
| 281 | Wireframe = !Wireframe; |
| 282 | break; |
| 283 | case 's': |
| 284 | Rot += 2.0; |
| 285 | break; |
| 286 | case 'S': |
| 287 | Rot -= 2.0; |
| 288 | break; |
| 289 | case 27: |
| 290 | CleanUp(); |
| 291 | break; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 292 | } |
| 293 | glutPostRedisplay(); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | static void |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 298 | Usage(void) |
| 299 | { |
| 300 | printf("Usage:\n"); |
| 301 | printf(" a Toggle animation\n"); |
| 302 | printf(" s/s Step/rotate\n"); |
| 303 | printf(" c Toggle back-face culling\n"); |
| 304 | printf(" w Toggle wireframe mode (front-face only)\n"); |
| 305 | printf(" Esc Exit\n"); |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static void |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 310 | Init(int argc, char *argv[]) |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 311 | { |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 312 | static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 }; |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 313 | GLint i; |
| 314 | |
| 315 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 316 | printf("GL_EXT_framebuffer_object not found!\n"); |
| 317 | exit(0); |
| 318 | } |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 319 | |
| 320 | if (argc > 1 && strcmp(argv[1], "-ds") == 0) { |
| 321 | if (!glutExtensionSupported("GL_EXT_packed_depth_stencil")) { |
| 322 | printf("GL_EXT_packed_depth_stencil not found!\n"); |
| 323 | exit(0); |
| 324 | } |
| 325 | UsePackedDepthStencil = GL_TRUE; |
| 326 | printf("Using GL_EXT_packed_depth_stencil\n"); |
| 327 | } |
| 328 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 329 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 330 | |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame] | 331 | /* gen framebuffer id, delete it, do some assertions, just for testing */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 332 | glGenFramebuffersEXT(1, &MyFB); |
| 333 | assert(MyFB); |
Brian Paul | 9e920fb | 2005-10-04 15:01:51 +0000 | [diff] [blame] | 334 | assert(!glIsFramebufferEXT(MyFB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 335 | glDeleteFramebuffersEXT(1, &MyFB); |
| 336 | assert(!glIsFramebufferEXT(MyFB)); |
| 337 | /* Note, continue to use MyFB below */ |
| 338 | |
| 339 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 340 | assert(glIsFramebufferEXT(MyFB)); |
| 341 | glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i); |
| 342 | assert(i == MyFB); |
| 343 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 344 | /* Make texture object/image */ |
| 345 | glGenTextures(1, &TexObj); |
| 346 | glBindTexture(TexTarget, TexObj); |
| 347 | /* make two image levels */ |
| 348 | glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0, |
| 349 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 350 | glTexImage2D(TexTarget, 1, TexIntFormat, TexWidth/2, TexHeight/2, 0, |
| 351 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 352 | TexWidth = TexWidth >> TextureLevel; |
| 353 | TexHeight = TexHeight >> TextureLevel; |
| 354 | |
| 355 | glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 356 | glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 357 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 358 | glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel); |
| 359 | glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel); |
| 360 | |
| 361 | CheckError(__LINE__); |
| 362 | |
| 363 | /* Render color to texture */ |
| 364 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 365 | TexTarget, TexObj, TextureLevel); |
| 366 | |
| 367 | |
| 368 | #if DEPTH |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 369 | /* make depth renderbuffer */ |
| 370 | glGenRenderbuffersEXT(1, &DepthRB); |
| 371 | assert(DepthRB); |
Brian Paul | 9e920fb | 2005-10-04 15:01:51 +0000 | [diff] [blame] | 372 | assert(!glIsRenderbufferEXT(DepthRB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 373 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRB); |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame] | 374 | assert(glIsRenderbufferEXT(DepthRB)); |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 375 | if (UsePackedDepthStencil) |
| 376 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, |
| 377 | TexWidth, TexHeight); |
| 378 | else |
| 379 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, |
| 380 | TexWidth, TexHeight); |
| 381 | CheckError(__LINE__); |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 382 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 383 | GL_RENDERBUFFER_DEPTH_SIZE_EXT, &i); |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 384 | CheckError(__LINE__); |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 385 | printf("Depth renderbuffer size = %d bits\n", i); |
| 386 | assert(i > 0); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 387 | |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 388 | /* attach DepthRB to MyFB */ |
| 389 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, |
| 390 | GL_RENDERBUFFER_EXT, DepthRB); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 391 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 392 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 393 | CheckError(__LINE__); |
| 394 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 395 | #if STENCIL |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 396 | if (UsePackedDepthStencil) { |
| 397 | /* DepthRb is a combined depth/stencil renderbuffer */ |
| 398 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, |
| 399 | GL_STENCIL_ATTACHMENT_EXT, |
| 400 | GL_RENDERBUFFER_EXT, DepthRB); |
| 401 | } |
| 402 | else { |
| 403 | /* make stencil renderbuffer */ |
| 404 | glGenRenderbuffersEXT(1, &StencilRB); |
| 405 | assert(StencilRB); |
| 406 | assert(!glIsRenderbufferEXT(StencilRB)); |
| 407 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, StencilRB); |
| 408 | assert(glIsRenderbufferEXT(StencilRB)); |
| 409 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, |
| 410 | TexWidth, TexHeight); |
| 411 | /* attach StencilRB to MyFB */ |
| 412 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, |
| 413 | GL_STENCIL_ATTACHMENT_EXT, |
| 414 | GL_RENDERBUFFER_EXT, StencilRB); |
| 415 | } |
| 416 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 417 | GL_RENDERBUFFER_STENCIL_SIZE_EXT, &i); |
| 418 | CheckError(__LINE__); |
| 419 | printf("Stencil renderbuffer size = %d bits\n", i); |
| 420 | assert(i > 0); |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 421 | #endif |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 422 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 423 | CheckError(__LINE__); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 424 | |
| 425 | /* bind regular framebuffer */ |
| 426 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 427 | |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 428 | |
Brian Paul | 6494541 | 2006-03-24 23:17:06 +0000 | [diff] [blame] | 429 | /* lighting */ |
| 430 | glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
| 434 | int |
| 435 | main(int argc, char *argv[]) |
| 436 | { |
| 437 | glutInit(&argc, argv); |
| 438 | glutInitWindowPosition(0, 0); |
| 439 | glutInitWindowSize(Width, Height); |
| 440 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); |
Brian | 2282d81 | 2007-03-06 16:33:00 -0700 | [diff] [blame] | 441 | Win = glutCreateWindow(argv[0]); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 442 | glutReshapeFunc(Reshape); |
| 443 | glutKeyboardFunc(Key); |
| 444 | glutDisplayFunc(Display); |
| 445 | if (Anim) |
| 446 | glutIdleFunc(Idle); |
Brian Paul | 32fe233 | 2005-11-16 14:48:11 +0000 | [diff] [blame] | 447 | Init(argc, argv); |
Brian | df198d2 | 2007-12-04 10:48:05 -0700 | [diff] [blame^] | 448 | Usage(); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 449 | glutMainLoop(); |
| 450 | return 0; |
| 451 | } |