2 * Test GL_EXT_framebuffer_object render-to-texture
4 * Draw a teapot into a texture image with stenciling.
5 * Then draw a textured quad using that texture.
24 static int Width
= 512, Height
= 512;
26 static GLenum TexTarget
= GL_TEXTURE_2D
;
27 static int TexWidth
= 512, TexHeight
= 512;
31 static GLboolean Anim
= GL_FALSE
;
32 static GLfloat Rot
= 0.0;
33 static GLuint TextureLevel
= 0; /* which texture level to render to */
34 static GLenum TexIntFormat
= GL_RGB
; /* either GL_RGB or GL_RGBA */
40 GLenum err
= glGetError();
42 printf("GL Error 0x%x at line %d\n", (int) err
, line
);
50 Rot
= glutGet(GLUT_ELAPSED_TIME
) * 0.1;
60 glMatrixMode(GL_PROJECTION
);
62 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
63 glMatrixMode(GL_MODELVIEW
);
65 glTranslatef(0.0, 0.0, -15.0);
68 /* draw to texture image */
69 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
71 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
72 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
73 printf("Framebuffer incomplete!!!\n");
76 glViewport(0, 0, TexWidth
, TexHeight
);
78 glClearColor(0.5, 0.5, 1.0, 0.0);
79 glClear(GL_COLOR_BUFFER_BIT
);
92 /* Bind normal framebuffer */
93 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
106 float ar
= (float) Width
/ (float) Height
;
110 /* draw textured quad in the window */
111 glMatrixMode(GL_PROJECTION
);
113 glFrustum(-ar
, ar
, -1.0, 1.0, 5.0, 25.0);
114 glMatrixMode(GL_MODELVIEW
);
116 glTranslatef(0.0, 0.0, -7.0);
118 glViewport(0, 0, Width
, Height
);
120 glClearColor(0.25, 0.25, 0.25, 0);
121 glClear(GL_COLOR_BUFFER_BIT
);
124 glRotatef(Rot
, 0, 1, 0);
126 glBindTexture(TexTarget
, TexObj
);
130 glColor3f(0.25, 0.25, 0.25);
135 glColor3f(1.0, 1.0, 1.0);
144 glDisable(TexTarget
);
147 CheckError(__LINE__
);
152 Reshape(int width
, int height
)
154 glViewport(0, 0, width
, height
);
163 glDeleteFramebuffersEXT(1, &MyFB
);
165 glDeleteTextures(1, &TexObj
);
167 glutDestroyWindow(Win
);
174 Key(unsigned char key
, int x
, int y
)
198 Init(int argc
, char *argv
[])
200 static const GLfloat mat
[4] = { 1.0, 0.5, 0.5, 1.0 };
203 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
204 printf("GL_EXT_framebuffer_object not found!\n");
208 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
211 /* Make texture object/image */
212 glGenTextures(1, &TexObj
);
213 glBindTexture(TexTarget
, TexObj
);
214 glTexParameteri(TexTarget
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
215 glTexParameteri(TexTarget
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
216 glTexParameteri(TexTarget
, GL_TEXTURE_BASE_LEVEL
, TextureLevel
);
217 glTexParameteri(TexTarget
, GL_TEXTURE_MAX_LEVEL
, TextureLevel
);
219 glTexImage2D(TexTarget
, 0, TexIntFormat
, TexWidth
, TexHeight
, 0,
220 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
223 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
228 /* gen framebuffer id, delete it, do some assertions, just for testing */
229 glGenFramebuffersEXT(1, &MyFB
);
230 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
231 assert(glIsFramebufferEXT(MyFB
));
234 CheckError(__LINE__
);
236 /* Render color to texture */
237 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
238 TexTarget
, TexObj
, TextureLevel
);
242 CheckError(__LINE__
);
244 /* bind regular framebuffer */
245 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
252 main(int argc
, char *argv
[])
254 glutInit(&argc
, argv
);
255 glutInitWindowPosition(0, 0);
256 glutInitWindowSize(Width
, Height
);
257 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
258 Win
= glutCreateWindow(argv
[0]);
260 glutReshapeFunc(Reshape
);
261 glutKeyboardFunc(Key
);
262 glutDisplayFunc(Display
);