blob: 72b4cf3683dbd3fd2dd10a28b9efd846a3efde4a [file] [log] [blame]
Brian Paul4c460cf2009-03-18 10:23:31 -06001/* Framebuffer object test */
Keith Whitwellb1399a52007-11-25 15:06:54 +00002
3
José Fonseca479ea7d2009-01-23 14:35:36 +00004#include <GL/glew.h>
Keith Whitwellb1399a52007-11-25 15:06:54 +00005#include <GL/glut.h>
6#include <assert.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <math.h>
11
12/* For debug */
13
14
15static int Win = 0;
16static int Width = 512, Height = 512;
17
18static GLenum TexTarget = GL_TEXTURE_2D;
19static int TexWidth = 512, TexHeight = 512;
20
21static GLuint MyFB;
22static GLuint TexObj;
23static GLboolean Anim = GL_FALSE;
24static GLfloat Rot = 0.0;
25static GLuint TextureLevel = 0; /* which texture level to render to */
26static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
27
28
29static void
30CheckError(int line)
31{
32 GLenum err = glGetError();
33 if (err) {
34 printf("GL Error 0x%x at line %d\n", (int) err, line);
35 }
36}
37
38
39static void
40Idle(void)
41{
42 Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
43 glutPostRedisplay();
44}
45
46
47static void
48RenderTexture(void)
49{
50 GLenum status;
51
52 glMatrixMode(GL_PROJECTION);
53 glLoadIdentity();
54 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
55 glMatrixMode(GL_MODELVIEW);
56 glLoadIdentity();
57 glTranslatef(0.0, 0.0, -15.0);
58
59 if (1) {
60 /* draw to texture image */
61 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
62
63 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
64 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
65 printf("Framebuffer incomplete!!!\n");
66 }
67
68 glViewport(0, 0, TexWidth, TexHeight);
69
70 glClearColor(0.5, 0.5, 1.0, 0.0);
71 glClear(GL_COLOR_BUFFER_BIT);
72
73 CheckError(__LINE__);
74
75 glBegin(GL_POLYGON);
76 glColor3f(1, 0, 0);
77 glVertex2f(-1, -1);
78 glColor3f(0, 1, 0);
79 glVertex2f(1, -1);
80 glColor3f(0, 0, 1);
81 glVertex2f(0, 1);
82 glEnd();
83
84 /* Bind normal framebuffer */
85 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
86 }
87 else {
88 }
89
90 CheckError(__LINE__);
91}
92
93
94
95static void
96Display(void)
97{
98 float ar = (float) Width / (float) Height;
99
100 RenderTexture();
101
102 /* draw textured quad in the window */
103 glMatrixMode(GL_PROJECTION);
104 glLoadIdentity();
105 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
106 glMatrixMode(GL_MODELVIEW);
107 glLoadIdentity();
108 glTranslatef(0.0, 0.0, -7.0);
109
110 glViewport(0, 0, Width, Height);
111
112 glClearColor(0.25, 0.25, 0.25, 0);
113 glClear(GL_COLOR_BUFFER_BIT);
114
115 glPushMatrix();
116 glRotatef(Rot, 0, 1, 0);
117 glEnable(TexTarget);
118 glBindTexture(TexTarget, TexObj);
119
120 {
121 glBegin(GL_POLYGON);
122 glColor3f(0.25, 0.25, 0.25);
123 glTexCoord2f(0, 0);
124 glVertex2f(-1, -1);
125 glTexCoord2f(1, 0);
126 glVertex2f(1, -1);
127 glColor3f(1.0, 1.0, 1.0);
128 glTexCoord2f(1, 1);
129 glVertex2f(1, 1);
130 glTexCoord2f(0, 1);
131 glVertex2f(-1, 1);
132 glEnd();
133 }
134
135 glPopMatrix();
136 glDisable(TexTarget);
137
138 glutSwapBuffers();
139 CheckError(__LINE__);
140}
141
142
143static void
144Reshape(int width, int height)
145{
146 glViewport(0, 0, width, height);
147 Width = width;
148 Height = height;
149}
150
151
152static void
153CleanUp(void)
154{
155 glDeleteFramebuffersEXT(1, &MyFB);
156
157 glDeleteTextures(1, &TexObj);
158
159 glutDestroyWindow(Win);
160
161 exit(0);
162}
163
164
165static void
166Key(unsigned char key, int x, int y)
167{
168 (void) x;
169 (void) y;
170 switch (key) {
171 case 'a':
172 Anim = !Anim;
173 if (Anim)
174 glutIdleFunc(Idle);
175 else
176 glutIdleFunc(NULL);
177 break;
178 case 's':
179 Rot += 2.0;
180 break;
181 case 27:
182 CleanUp();
183 break;
184 }
185 glutPostRedisplay();
186}
187
188
189static void
190Init(int argc, char *argv[])
191{
192 static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 };
193 GLint i;
194
195 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
196 printf("GL_EXT_framebuffer_object not found!\n");
197 exit(0);
198 }
199
200 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
201
202
203 /* Make texture object/image */
204 glGenTextures(1, &TexObj);
205 glBindTexture(TexTarget, TexObj);
206 glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
207 glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
208 glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
209 glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
210
211 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
212 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
213
214
215 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
216
217
218
219
220 /* gen framebuffer id, delete it, do some assertions, just for testing */
221 glGenFramebuffersEXT(1, &MyFB);
222 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
223 assert(glIsFramebufferEXT(MyFB));
224
225
226 CheckError(__LINE__);
227
228 /* Render color to texture */
229 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
230 TexTarget, TexObj, TextureLevel);
231
232
233
234 CheckError(__LINE__);
235
236 /* bind regular framebuffer */
237 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
238
239
240}
241
242
243int
244main(int argc, char *argv[])
245{
246 glutInit(&argc, argv);
247 glutInitWindowPosition(0, 0);
248 glutInitWindowSize(Width, Height);
249 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
250 Win = glutCreateWindow(argv[0]);
José Fonseca479ea7d2009-01-23 14:35:36 +0000251 glewInit();
Keith Whitwellb1399a52007-11-25 15:06:54 +0000252 glutReshapeFunc(Reshape);
253 glutKeyboardFunc(Key);
254 glutDisplayFunc(Display);
255 if (Anim)
256 glutIdleFunc(Idle);
257 Init(argc, argv);
258 glutMainLoop();
259 return 0;
260}