Merge commit 'origin/gallium-master-merge'
[mesa.git] / progs / trivial / clear-fbo-tex.c
1
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <math.h>
6 #include <string.h>
7
8 #include <GL/glew.h>
9 #include <GL/glut.h>
10
11
12
13 static GLenum TexTarget = GL_TEXTURE_2D;
14 static int TexWidth = 512, TexHeight = 512;
15 static GLenum TexIntFormat = GL_RGBA; /* either GL_RGB or GL_RGBA */
16
17 static int Width = 512, Height = 512;
18 static GLuint MyFB, TexObj;
19
20
21 #define CheckError() assert(glGetError() == 0)
22
23 GLenum doubleBuffer;
24
25 static void Init(void)
26 {
27 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
28 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
29 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
30
31
32 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
33 printf("GL_EXT_framebuffer_object not found!\n");
34 exit(0);
35 }
36
37
38 glGenFramebuffersEXT(1, &MyFB);
39 glGenTextures(1, &TexObj);
40
41 /* Make texture object/image */
42 glBindTexture(TexTarget, TexObj);
43 glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
44 glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
45 glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, 0);
46 glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, 0);
47
48 glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
49 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
50
51
52 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
53
54
55
56 {
57 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
58
59 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
60 TexTarget, TexObj, 0);
61
62
63 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
64 }
65
66 }
67
68
69
70 static void
71 Reshape( int width, int height )
72 {
73 glViewport( 0, 0, width, height );
74 glMatrixMode( GL_PROJECTION );
75 glLoadIdentity();
76 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
77 glMatrixMode( GL_MODELVIEW );
78
79 Width = width;
80 Height = height;
81 }
82
83 static void Key(unsigned char key, int x, int y)
84 {
85
86 switch (key) {
87 case 27:
88 exit(1);
89 default:
90 return;
91 }
92
93 glutPostRedisplay();
94 }
95
96
97
98 static void Draw( void )
99 {
100
101
102 /* draw to texture image */
103 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
104 // glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
105 // glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
106
107
108 glViewport(0, 0, TexWidth, TexHeight);
109 CheckError();
110
111 glClearColor(0.5, 0.5, 1.0, 0.0);
112 glClear(GL_COLOR_BUFFER_BIT);
113 CheckError();
114
115 if (0) {
116 glBegin(GL_TRIANGLES);
117 glColor3f(0,0,.7);
118 glVertex3f( 0.9, -0.9, -30.0);
119 glColor3f(.8,0,0);
120 glVertex3f( 0.9, 0.9, -30.0);
121 glColor3f(0,.9,0);
122 glVertex3f(-0.9, 0.0, -30.0);
123 glEnd();
124 }
125
126 {
127 GLubyte *buffer = malloc(Width * Height * 4);
128
129 /* read from user framebuffer */
130 glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
131 CheckError();
132
133 /* draw to window */
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
135 glViewport(0, 0, Width, Height);
136
137 /* Try to clear the window, but will overwrite:
138 */
139 glClearColor(0.8, 0.8, 0, 0.0);
140 glClear(GL_COLOR_BUFFER_BIT);
141
142 glWindowPos2iARB(30, 30);
143 glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
144
145 free(buffer);
146 }
147
148 /* Bind normal framebuffer */
149 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
150 glViewport(0, 0, Width, Height);
151
152 if (0) {
153 glBegin(GL_TRIANGLES);
154 glColor3f(0,.7,0);
155 glVertex3f( 0.5, -0.5, -30.0);
156 glColor3f(0,0,.8);
157 glVertex3f( 0.5, 0.5, -30.0);
158 glColor3f(.9,0,0);
159 glVertex3f(-0.5, 0.0, -30.0);
160 glEnd();
161 }
162
163 if (doubleBuffer) {
164 glutSwapBuffers();
165 }
166
167 CheckError();
168 }
169
170
171 static GLenum Args(int argc, char **argv)
172 {
173 GLint i;
174
175 doubleBuffer = GL_FALSE;
176
177 for (i = 1; i < argc; i++) {
178 if (strcmp(argv[i], "-sb") == 0) {
179 doubleBuffer = GL_FALSE;
180 } else if (strcmp(argv[i], "-db") == 0) {
181 doubleBuffer = GL_TRUE;
182 } else {
183 fprintf(stderr, "%s (Bad option).\n", argv[i]);
184 return GL_FALSE;
185 }
186 }
187 return GL_TRUE;
188 }
189
190
191
192 int
193 main( int argc, char *argv[] )
194 {
195 GLenum type;
196
197 glutInit(&argc, argv);
198
199 if (Args(argc, argv) == GL_FALSE) {
200 exit(1);
201 }
202
203 glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
204
205 type = GLUT_RGB;
206 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
207 glutInitDisplayMode(type);
208
209 if (glutCreateWindow(argv[0]) == GL_FALSE) {
210 exit(1);
211 }
212
213 glewInit();
214
215 Init();
216
217 glutReshapeFunc(Reshape);
218 glutKeyboardFunc(Key);
219 glutDisplayFunc(Draw);
220 glutMainLoop();
221 return 0;
222 }