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
= 0; /* 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");
68 glViewport(0, 0, TexWidth
, TexHeight
);
70 glClearColor(0.5, 0.5, 1.0, 0.0);
71 glClear(GL_COLOR_BUFFER_BIT
);
84 /* Bind normal framebuffer */
85 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
98 float ar
= (float) Width
/ (float) Height
;
102 /* draw textured quad in the window */
103 glMatrixMode(GL_PROJECTION
);
105 glFrustum(-ar
, ar
, -1.0, 1.0, 5.0, 25.0);
106 glMatrixMode(GL_MODELVIEW
);
108 glTranslatef(0.0, 0.0, -7.0);
110 glViewport(0, 0, Width
, Height
);
112 glClearColor(0.25, 0.25, 0.25, 0);
113 glClear(GL_COLOR_BUFFER_BIT
);
116 glRotatef(Rot
, 0, 1, 0);
118 glBindTexture(TexTarget
, TexObj
);
122 glColor3f(0.25, 0.25, 0.25);
127 glColor3f(1.0, 1.0, 1.0);
136 glDisable(TexTarget
);
139 CheckError(__LINE__
);
144 Reshape(int width
, int height
)
146 glViewport(0, 0, width
, height
);
155 glDeleteFramebuffersEXT(1, &MyFB
);
157 glDeleteTextures(1, &TexObj
);
159 glutDestroyWindow(Win
);
166 Key(unsigned char key
, int x
, int y
)
190 Init(int argc
, char *argv
[])
192 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
193 printf("GL_EXT_framebuffer_object not found!\n");
197 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
200 /* Make texture object/image */
201 glGenTextures(1, &TexObj
);
202 glBindTexture(TexTarget
, TexObj
);
203 glTexParameteri(TexTarget
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
204 glTexParameteri(TexTarget
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
205 glTexParameteri(TexTarget
, GL_TEXTURE_BASE_LEVEL
, TextureLevel
);
206 glTexParameteri(TexTarget
, GL_TEXTURE_MAX_LEVEL
, TextureLevel
);
208 glTexImage2D(TexTarget
, 0, TexIntFormat
, TexWidth
, TexHeight
, 0,
209 GL_RGBA
, GL_UNSIGNED_BYTE
, NULL
);
212 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
217 /* gen framebuffer id, delete it, do some assertions, just for testing */
218 glGenFramebuffersEXT(1, &MyFB
);
219 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, MyFB
);
220 assert(glIsFramebufferEXT(MyFB
));
223 CheckError(__LINE__
);
225 /* Render color to texture */
226 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
, GL_COLOR_ATTACHMENT0_EXT
,
227 TexTarget
, TexObj
, TextureLevel
);
231 CheckError(__LINE__
);
233 /* bind regular framebuffer */
234 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
241 main(int argc
, char *argv
[])
243 glutInit(&argc
, argv
);
244 glutInitWindowPosition(0, 0);
245 glutInitWindowSize(Width
, Height
);
246 glutInitDisplayMode(GLUT_RGB
| GLUT_DOUBLE
);
247 Win
= glutCreateWindow(argv
[0]);
249 glutReshapeFunc(Reshape
);
250 glutKeyboardFunc(Key
);
251 glutDisplayFunc(Display
);