1 /* Framebuffer object test */
16 static int Width
= 512, Height
= 512;
18 static GLenum TexTarget
= GL_TEXTURE_2D
;
19 static int TexWidth
= 512, TexHeight
= 512;
23 static GLboolean Anim
= GL_FALSE
;
24 static GLfloat Rot
= 0.0;
25 static GLuint TextureLevel
= 4; /* which texture level to render to */
26 static GLenum TexIntFormat
= GL_RGB
; /* either GL_RGB or GL_RGBA */
32 GLenum err
= glGetError();
34 printf("GL Error 0x%x at line %d\n", (int) err
, line
);
42 Rot
= glutGet(GLUT_ELAPSED_TIME
) * 0.1;
52 glMatrixMode(GL_PROJECTION
);
54 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
55 glMatrixMode(GL_MODELVIEW
);
57 glTranslatef(0.0, 0.0, -15.0);
60 /* draw to texture image */
61 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
63 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
64 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
65 printf("Framebuffer incomplete!!!\n");
69 TexWidth
/ (1 << TextureLevel
),
70 TexHeight
/ (1 << TextureLevel
));
71 glClearColor(0.5, 0.5, 1.0, 0.0);
72 glClear(GL_COLOR_BUFFER_BIT
);
85 /* Bind normal framebuffer */
86 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
99 float ar
= (float) Width
/ (float) Height
;
103 /* draw textured quad in the window */
104 glMatrixMode(GL_PROJECTION
);
106 glFrustum(-ar
, ar
, -1.0, 1.0, 5.0, 25.0);
107 glMatrixMode(GL_MODELVIEW
);
109 glTranslatef(0.0, 0.0, -7.0);
111 glViewport(0, 0, Width
, Height
);
113 glClearColor(0.25, 0.25, 0.25, 0);
114 glClear(GL_COLOR_BUFFER_BIT
);
117 glRotatef(Rot
, 0, 1, 0);
119 glBindTexture(TexTarget
, TexObj
);
123 glColor3f(0.25, 0.25, 0.25);
128 glColor3f(1.0, 1.0, 1.0);
137 glDisable(TexTarget
);
140 CheckError(__LINE__
);
145 Reshape(int width
, int height
)
147 glViewport(0, 0, width
, height
);
156 glDeleteFramebuffersEXT(1, &MyFB
);
158 glDeleteTextures(1, &TexObj
);
160 glutDestroyWindow(Win
);
167 Key(unsigned char key
, int x
, int y
)
191 Init(int argc
, char *argv
[])
193 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
194 printf("GL_EXT_framebuffer_object not found!\n");
198 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
201 /* Make texture object/image */
202 glGenTextures(1, &TexObj
);
203 glBindTexture(TexTarget
, TexObj
);
204 glTexParameteri(TexTarget
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
205 glTexParameteri(TexTarget
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
206 glTexParameteri(TexTarget
, GL_TEXTURE_BASE_LEVEL
, TextureLevel
);
207 glTexParameteri(TexTarget
, GL_TEXTURE_MAX_LEVEL
, TextureLevel
);
209 glTexImage2D(TexTarget
, 0, TexIntFormat
, TexWidth
, TexHeight
, 0,
210 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
211 glTexImage2D(TexTarget
, TextureLevel
, TexIntFormat
,
212 TexWidth
/ (1 << TextureLevel
), TexHeight
/ (1 << TextureLevel
), 0,
213 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
216 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
221 /* gen framebuffer id, delete it, do some assertions, just for testing */
222 glGenFramebuffersEXT(1, &MyFB
);
223 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
224 assert(glIsFramebufferEXT(MyFB
));
227 CheckError(__LINE__
);
229 /* Render color to texture */
230 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
231 TexTarget
, TexObj
, TextureLevel
);
235 CheckError(__LINE__
);
237 /* bind regular framebuffer */
238 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
245 main(int argc
, char *argv
[])
247 glutInit(&argc
, argv
);
248 glutInitWindowPosition(0, 0);
249 glutInitWindowSize(Width
, Height
);
250 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
251 Win
= glutCreateWindow(argv
[0]);
253 glutReshapeFunc(Reshape
);
254 glutKeyboardFunc(Key
);
255 glutDisplayFunc(Display
);