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 |
| 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <math.h> |
| 17 | #include <GL/glut.h> |
| 18 | |
| 19 | static int Width = 400, Height = 400; |
| 20 | static int TexWidth = 512, TexHeight = 512; |
| 21 | static GLuint MyFB; |
| 22 | static GLuint TexObj; |
| 23 | static GLuint DepthRB, StencilRB; |
| 24 | static GLboolean Anim = GL_FALSE; |
| 25 | static GLfloat Rot = 0.0; |
| 26 | |
| 27 | |
| 28 | static void |
| 29 | CheckError(int line) |
| 30 | { |
| 31 | GLenum err = glGetError(); |
| 32 | if (err) { |
| 33 | printf("GL Error 0x%x at line %d\n", (int) err, line); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | static void |
| 39 | Idle(void) |
| 40 | { |
| 41 | Rot = glutGet(GLUT_ELAPSED_TIME) * 0.05; |
| 42 | glutPostRedisplay(); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | static void |
| 47 | RenderTexture(void) |
| 48 | { |
| 49 | GLint level = 0; |
| 50 | GLenum status; |
| 51 | |
| 52 | glMatrixMode(GL_PROJECTION); |
| 53 | glLoadIdentity(); |
| 54 | glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0); |
| 55 | glMatrixMode(GL_MODELVIEW); |
| 56 | glLoadIdentity(); |
| 57 | glTranslatef(0.0, 0.0, -15.0); |
| 58 | |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame^] | 59 | /* draw to texture image */ |
| 60 | glBindTexture(GL_TEXTURE_2D, 0); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 61 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 62 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 63 | GL_TEXTURE_2D, TexObj, level); |
| 64 | |
| 65 | status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 66 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 67 | printf("Framebuffer incomplete!!!\n"); |
| 68 | } |
| 69 | |
| 70 | glViewport(0, 0, TexWidth, TexHeight); |
| 71 | |
| 72 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 73 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 74 | |
| 75 | glEnable(GL_DEPTH_TEST); |
| 76 | glEnable(GL_STENCIL_TEST); |
| 77 | glStencilFunc(GL_NEVER, 1, ~0); |
| 78 | glStencilOp(GL_REPLACE, GL_KEEP, GL_REPLACE); |
| 79 | |
| 80 | /* draw diamond-shaped stencil pattern */ |
| 81 | glColor3f(0, 1, 0); |
| 82 | glBegin(GL_POLYGON); |
| 83 | glVertex2f(-0.2, 0.0); |
| 84 | glVertex2f( 0.0, -0.2); |
| 85 | glVertex2f( 0.2, 0.0); |
| 86 | glVertex2f( 0.0, 0.2); |
| 87 | glEnd(); |
| 88 | |
| 89 | /* draw teapot where stencil != 1 */ |
| 90 | glStencilFunc(GL_NOTEQUAL, 1, ~0); |
| 91 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 92 | |
| 93 | #if 0 |
| 94 | glBegin(GL_POLYGON); |
| 95 | glColor3f(1, 0, 0); |
| 96 | glVertex2f(-1, -1); |
| 97 | glColor3f(0, 1, 0); |
| 98 | glVertex2f(1, -1); |
| 99 | glColor3f(0, 0, 1); |
| 100 | glVertex2f(0, 1); |
| 101 | glEnd(); |
| 102 | #else |
| 103 | glEnable(GL_LIGHTING); |
| 104 | glEnable(GL_LIGHT0); |
| 105 | glPushMatrix(); |
| 106 | glRotatef(0.5 * Rot, 1.0, 0.0, 0.0); |
| 107 | glutSolidTeapot(0.5); |
| 108 | glPopMatrix(); |
| 109 | glDisable(GL_LIGHTING); |
| 110 | #endif |
| 111 | glDisable(GL_DEPTH_TEST); |
| 112 | glDisable(GL_STENCIL_TEST); |
| 113 | |
| 114 | /* Bind normal framebuffer */ |
| 115 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 116 | |
| 117 | CheckError(__LINE__); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | static void |
| 123 | Display(void) |
| 124 | { |
| 125 | float ar = (float) Width / (float) Height; |
| 126 | |
| 127 | RenderTexture(); |
| 128 | |
| 129 | /* draw textured quad in the window */ |
| 130 | |
| 131 | glMatrixMode(GL_PROJECTION); |
| 132 | glLoadIdentity(); |
| 133 | glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0); |
| 134 | glMatrixMode(GL_MODELVIEW); |
| 135 | glLoadIdentity(); |
| 136 | glTranslatef(0.0, 0.0, -7.0); |
| 137 | |
| 138 | glViewport(0, 0, Width, Height); |
| 139 | |
| 140 | glClearColor(0.25, 0.25, 0.25, 0); |
| 141 | glClear(GL_COLOR_BUFFER_BIT); |
| 142 | |
| 143 | glPushMatrix(); |
| 144 | glRotatef(Rot, 0, 1, 0); |
| 145 | glEnable(GL_TEXTURE_2D); |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame^] | 146 | glBindTexture(GL_TEXTURE_2D, TexObj); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 147 | glBegin(GL_POLYGON); |
| 148 | glColor3f(0.25, 0.25, 0.25); |
| 149 | glTexCoord2f(0, 0); |
| 150 | glVertex2f(-1, -1); |
| 151 | glTexCoord2f(1, 0); |
| 152 | glVertex2f(1, -1); |
| 153 | glColor3f(1.0, 1.0, 1.0); |
| 154 | glTexCoord2f(1, 1); |
| 155 | glVertex2f(1, 1); |
| 156 | glTexCoord2f(0, 1); |
| 157 | glVertex2f(-1, 1); |
| 158 | glEnd(); |
| 159 | glPopMatrix(); |
| 160 | glDisable(GL_TEXTURE_2D); |
| 161 | |
| 162 | glutSwapBuffers(); |
| 163 | CheckError(__LINE__); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | static void |
| 168 | Reshape(int width, int height) |
| 169 | { |
| 170 | glViewport(0, 0, width, height); |
| 171 | Width = width; |
| 172 | Height = height; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static void |
| 177 | Key(unsigned char key, int x, int y) |
| 178 | { |
| 179 | (void) x; |
| 180 | (void) y; |
| 181 | switch (key) { |
| 182 | case 'a': |
| 183 | Anim = !Anim; |
| 184 | if (Anim) |
| 185 | glutIdleFunc(Idle); |
| 186 | else |
| 187 | glutIdleFunc(NULL); |
| 188 | break; |
| 189 | case 27: |
| 190 | exit(0); |
| 191 | break; |
| 192 | } |
| 193 | glutPostRedisplay(); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static void |
| 198 | Init(void) |
| 199 | { |
| 200 | GLint i; |
| 201 | |
| 202 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 203 | printf("GL_EXT_framebuffer_object not found!\n"); |
| 204 | exit(0); |
| 205 | } |
| 206 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 207 | |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame^] | 208 | /* gen framebuffer id, delete it, do some assertions, just for testing */ |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 209 | glGenFramebuffersEXT(1, &MyFB); |
| 210 | assert(MyFB); |
Brian Paul | 9e920fb | 2005-10-04 15:01:51 +0000 | [diff] [blame] | 211 | assert(!glIsFramebufferEXT(MyFB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 212 | glDeleteFramebuffersEXT(1, &MyFB); |
| 213 | assert(!glIsFramebufferEXT(MyFB)); |
| 214 | /* Note, continue to use MyFB below */ |
| 215 | |
| 216 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 217 | assert(glIsFramebufferEXT(MyFB)); |
| 218 | glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i); |
| 219 | assert(i == MyFB); |
| 220 | |
| 221 | /* make depth renderbuffer */ |
| 222 | glGenRenderbuffersEXT(1, &DepthRB); |
| 223 | assert(DepthRB); |
Brian Paul | 9e920fb | 2005-10-04 15:01:51 +0000 | [diff] [blame] | 224 | assert(!glIsRenderbufferEXT(DepthRB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 225 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRB); |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame^] | 226 | assert(glIsRenderbufferEXT(DepthRB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 227 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, |
| 228 | TexWidth, TexHeight); |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 229 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 230 | GL_RENDERBUFFER_DEPTH_SIZE_EXT, &i); |
| 231 | printf("Depth renderbuffer size = %d bits\n", i); |
| 232 | assert(i > 0); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 233 | |
| 234 | /* make stencil renderbuffer */ |
| 235 | glGenRenderbuffersEXT(1, &StencilRB); |
| 236 | assert(StencilRB); |
Brian Paul | 9e920fb | 2005-10-04 15:01:51 +0000 | [diff] [blame] | 237 | assert(!glIsRenderbufferEXT(StencilRB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 238 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, StencilRB); |
Brian Paul | 7edf1e8 | 2005-10-04 15:16:27 +0000 | [diff] [blame^] | 239 | assert(glIsRenderbufferEXT(StencilRB)); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 240 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, |
| 241 | TexWidth, TexHeight); |
Brian Paul | 58a9573 | 2005-07-01 01:34:29 +0000 | [diff] [blame] | 242 | glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT, |
| 243 | GL_RENDERBUFFER_STENCIL_SIZE_EXT, &i); |
| 244 | printf("Stencil renderbuffer size = %d bits\n", i); |
| 245 | assert(i > 0); |
Brian Paul | e4b2356 | 2005-05-04 20:11:35 +0000 | [diff] [blame] | 246 | |
| 247 | /* attach DepthRB to MyFB */ |
| 248 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, |
| 249 | GL_RENDERBUFFER_EXT, DepthRB); |
| 250 | |
| 251 | /* attach StencilRB to MyFB */ |
| 252 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, |
| 253 | GL_RENDERBUFFER_EXT, StencilRB); |
| 254 | |
| 255 | |
| 256 | /* bind regular framebuffer */ |
| 257 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 258 | |
| 259 | /* Make texture object/image */ |
| 260 | glGenTextures(1, &TexObj); |
| 261 | glBindTexture(GL_TEXTURE_2D, TexObj); |
| 262 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0, |
| 263 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 264 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 265 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 266 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 267 | |
| 268 | CheckError(__LINE__); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | int |
| 273 | main(int argc, char *argv[]) |
| 274 | { |
| 275 | glutInit(&argc, argv); |
| 276 | glutInitWindowPosition(0, 0); |
| 277 | glutInitWindowSize(Width, Height); |
| 278 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); |
| 279 | glutCreateWindow(argv[0]); |
| 280 | glutReshapeFunc(Reshape); |
| 281 | glutKeyboardFunc(Key); |
| 282 | glutDisplayFunc(Display); |
| 283 | if (Anim) |
| 284 | glutIdleFunc(Idle); |
| 285 | Init(); |
| 286 | glutMainLoop(); |
| 287 | return 0; |
| 288 | } |