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