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