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 = 0; /* 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");
66
glViewport(0, 0, TexWidth, TexHeight);
68
glClearColor(0.5, 0.5, 1.0, 0.0);
69
glClear(GL_COLOR_BUFFER_BIT);
82
/* Bind normal framebuffer */
83
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
96
float ar = (float) Width / (float) Height;
100
/* draw textured quad in the window */
101
glMatrixMode(GL_PROJECTION);
103
glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
104
glMatrixMode(GL_MODELVIEW);
106
glTranslatef(0.0, 0.0, -7.0);
108
glViewport(0, 0, Width, Height);
110
glClearColor(0.25, 0.25, 0.25, 0);
111
glClear(GL_COLOR_BUFFER_BIT);
114
glRotatef(Rot, 0, 1, 0);
116
glBindTexture(TexTarget, TexObj);
120
glColor3f(0.25, 0.25, 0.25);
125
glColor3f(1.0, 1.0, 1.0);
134
glDisable(TexTarget);
137
CheckError(__LINE__);
142
Reshape(int width, int height)
144
glViewport(0, 0, width, height);
153
glDeleteFramebuffersEXT(1, &MyFB);
155
glDeleteTextures(1, &TexObj);
157
glutDestroyWindow(Win);
164
Key(unsigned char key, int x, int y)
188
Init(int argc, char *argv[])
190
if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
191
printf("GL_EXT_framebuffer_object not found!\n");
195
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
198
/* Make texture object/image */
199
glGenTextures(1, &TexObj);
200
glBindTexture(TexTarget, TexObj);
201
glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
202
glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
203
glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
204
glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
206
glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
207
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
210
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
215
/* gen framebuffer id, delete it, do some assertions, just for testing */
216
glGenFramebuffersEXT(1, &MyFB);
217
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
218
assert(glIsFramebufferEXT(MyFB));
221
CheckError(__LINE__);
223
/* Render color to texture */
224
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
225
TexTarget, TexObj, TextureLevel);
229
CheckError(__LINE__);
231
/* bind regular framebuffer */
232
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
239
main(int argc, char *argv[])
241
glutInit(&argc, argv);
242
glutInitWindowPosition(0, 0);
243
glutInitWindowSize(Width, Height);
244
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
245
Win = glutCreateWindow(argv[0]);
247
glutReshapeFunc(Reshape);
248
glutKeyboardFunc(Key);
249
glutDisplayFunc(Display);