Merge commit 'origin/gallium-draw-retval'
[mesa.git] / progs / trivial / tri-fbo-tex.c
1 /* Framebuffer object test */
2
3
4 #include <GL/glew.h>
5 #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
15 static int Win = 0;
16 static int Width = 512, Height = 512;
17
18 static GLenum TexTarget = GL_TEXTURE_2D;
19 static int TexWidth = 512, TexHeight = 512;
20
21 static GLuint MyFB;
22 static GLuint TexObj;
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 */
27
28
29 static void
30 CheckError(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
39 static void
40 Idle(void)
41 {
42 Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
43 glutPostRedisplay();
44 }
45
46
47 static void
48 RenderTexture(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
95 static void
96 Display(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
143 static void
144 Reshape(int width, int height)
145 {
146 glViewport(0, 0, width, height);
147 Width = width;
148 Height = height;
149 }
150
151
152 static void
153 CleanUp(void)
154 {
155 glDeleteFramebuffersEXT(1, &MyFB);
156
157 glDeleteTextures(1, &TexObj);
158
159 glutDestroyWindow(Win);
160
161 exit(0);
162 }
163
164
165 static void
166 Key(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
189 static void
190 Init(int argc, char *argv[])
191 {
192 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
193 printf("GL_EXT_framebuffer_object not found!\n");
194 exit(0);
195 }
196
197 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
198
199
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);
207
208 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
209 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
210
211
212 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
213
214
215
216
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));
221
222
223 CheckError(__LINE__);
224
225 /* Render color to texture */
226 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
227 TexTarget, TexObj, TextureLevel);
228
229
230
231 CheckError(__LINE__);
232
233 /* bind regular framebuffer */
234 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
235
236
237 }
238
239
240 int
241 main(int argc, char *argv[])
242 {
243 glutInit(&argc, argv);
244 glutInitWindowPosition(0, 0);
245 glutInitWindowSize(Width, Height);
246 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
247 Win = glutCreateWindow(argv[0]);
248 glewInit();
249 glutReshapeFunc(Reshape);
250 glutKeyboardFunc(Key);
251 glutDisplayFunc(Display);
252 if (Anim)
253 glutIdleFunc(Idle);
254 Init(argc, argv);
255 glutMainLoop();
256 return 0;
257 }