1 /* Framebuffer object test */
14 static int Width
= 512, Height
= 512;
16 static GLenum TexTarget
= GL_TEXTURE_2D
;
17 static int TexWidth
= 512, TexHeight
= 512;
21 static GLboolean Anim
= GL_FALSE
;
22 static GLfloat Rot
= 0.0;
23 static GLuint TextureLevel
= 4; /* which texture level to render to */
24 static GLenum TexIntFormat
= GL_RGB
; /* either GL_RGB or GL_RGBA */
30 GLenum err
= glGetError();
32 printf("GL Error 0x%x at line %d\n", (int) err
, line
);
40 Rot
= glutGet(GLUT_ELAPSED_TIME
) * 0.1;
50 glMatrixMode(GL_PROJECTION
);
52 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
53 glMatrixMode(GL_MODELVIEW
);
55 glTranslatef(0.0, 0.0, -15.0);
58 /* draw to texture image */
59 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
61 status
= glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT
);
62 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
63 printf("Framebuffer incomplete!!!\n");
67 TexWidth
/ (1 << TextureLevel
),
68 TexHeight
/ (1 << TextureLevel
));
69 glClearColor(0.5, 0.5, 1.0, 0.0);
70 glClear(GL_COLOR_BUFFER_BIT
);
83 /* Bind normal framebuffer */
84 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
97 float ar
= (float) Width
/ (float) Height
;
101 /* draw textured quad in the window */
102 glMatrixMode(GL_PROJECTION
);
104 glFrustum(-ar
, ar
, -1.0, 1.0, 5.0, 25.0);
105 glMatrixMode(GL_MODELVIEW
);
107 glTranslatef(0.0, 0.0, -7.0);
109 glViewport(0, 0, Width
, Height
);
111 glClearColor(0.25, 0.25, 0.25, 0);
112 glClear(GL_COLOR_BUFFER_BIT
);
115 glRotatef(Rot
, 0, 1, 0);
117 glBindTexture(TexTarget
, TexObj
);
121 glColor3f(0.25, 0.25, 0.25);
126 glColor3f(1.0, 1.0, 1.0);
135 glDisable(TexTarget
);
138 CheckError(__LINE__
);
143 Reshape(int width
, int height
)
145 glViewport(0, 0, width
, height
);
154 glDeleteFramebuffersEXT(1, &MyFB
);
156 glDeleteTextures(1, &TexObj
);
158 glutDestroyWindow(Win
);
165 Key(unsigned char key
, int x
, int y
)
189 Init(int argc
, char *argv
[])
191 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
192 printf("GL_EXT_framebuffer_object not found!\n");
196 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
199 /* Make texture object/image */
200 glGenTextures(1, &TexObj
);
201 glBindTexture(TexTarget
, TexObj
);
202 glTexParameteri(TexTarget
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
203 glTexParameteri(TexTarget
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
204 glTexParameteri(TexTarget
, GL_TEXTURE_BASE_LEVEL
, TextureLevel
);
205 glTexParameteri(TexTarget
, GL_TEXTURE_MAX_LEVEL
, TextureLevel
);
207 glTexImage2D(TexTarget
, 0, TexIntFormat
, TexWidth
, TexHeight
, 0,
208 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
209 glTexImage2D(TexTarget
, TextureLevel
, TexIntFormat
,
210 TexWidth
/ (1 << TextureLevel
), TexHeight
/ (1 << TextureLevel
), 0,
211 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
214 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
219 /* gen framebuffer id, delete it, do some assertions, just for testing */
220 glGenFramebuffersEXT(1, &MyFB
);
221 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
222 assert(glIsFramebufferEXT(MyFB
));
225 CheckError(__LINE__
);
227 /* Render color to texture */
228 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
229 TexTarget
, TexObj
, TextureLevel
);
233 CheckError(__LINE__
);
235 /* bind regular framebuffer */
236 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
243 main(int argc
, char *argv
[])
245 glutInit(&argc
, argv
);
246 glutInitWindowPosition(0, 0);
247 glutInitWindowSize(Width
, Height
);
248 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
249 Win
= glutCreateWindow(argv
[0]);
251 glutReshapeFunc(Reshape
);
252 glutKeyboardFunc(Key
);
253 glutDisplayFunc(Display
);