ARB prog parser: fix parameter binding type
[mesa.git] / progs / trivial / tri-fbo.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 int Width = 400, Height = 400;
14 static GLuint MyFB, MyRB;
15
16
17 #define CheckError() assert(glGetError() == 0)
18
19 GLenum doubleBuffer;
20
21 static void Init(void)
22 {
23 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
24 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
25 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
26 fflush(stderr);
27
28
29 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
30 printf("GL_EXT_framebuffer_object not found!\n");
31 exit(0);
32 }
33
34
35 glGenFramebuffersEXT(1, &MyFB);
36 glGenRenderbuffersEXT(1, &MyRB);
37
38 {
39 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
40
41 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
42
43 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
44 GL_RENDERBUFFER_EXT, MyRB);
45
46 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
47
48 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
49 }
50
51 glClearColor(0.0, 0.0, 1.0, 0.0);
52 }
53
54
55
56 static void
57 Reshape( int width, int height )
58 {
59 glViewport( 0, 0, width, height );
60 glMatrixMode( GL_PROJECTION );
61 glLoadIdentity();
62 glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
63 glMatrixMode( GL_MODELVIEW );
64
65 Width = width;
66 Height = height;
67 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
68 }
69
70
71 static void Key(unsigned char key, int x, int y)
72 {
73
74 switch (key) {
75 case 27:
76 exit(1);
77 default:
78 break;
79 }
80
81 glutPostRedisplay();
82 }
83
84
85
86 static void Draw( void )
87 {
88
89 /* draw to user framebuffer */
90 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
91 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
92 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
93
94 glClearColor(0.5, 0.5, 1.0, 0.0);
95 glClear(GL_COLOR_BUFFER_BIT);
96 CheckError();
97
98 glBegin(GL_TRIANGLES);
99 glColor3f(0,0,.7);
100 glVertex3f( 0.9, -0.9, -30.0);
101 glColor3f(.8,0,0);
102 glVertex3f( 0.9, 0.9, -30.0);
103 glColor3f(0,.9,0);
104 glVertex3f(-0.9, 0.0, -30.0);
105 glEnd();
106
107
108 {
109 GLubyte *buffer = malloc(Width * Height * 4);
110
111 /* read from user framebuffer */
112 glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
113 CheckError();
114
115 /* draw to window */
116 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
117 /* Try to clear the window, but will overwrite:
118 */
119 glClearColor(0.8, 0.8, 0, 0.0);
120 glClear(GL_COLOR_BUFFER_BIT);
121
122 glWindowPos2iARB(30, 30);
123 glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
124
125
126 free(buffer);
127 }
128
129 /* Bind normal framebuffer */
130 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
131
132 if (1) {
133 glBegin(GL_TRIANGLES);
134 glColor3f(0,.7,0);
135 glVertex3f( 0.5, -0.5, -30.0);
136 glColor3f(0,0,.8);
137 glVertex3f( 0.5, 0.5, -30.0);
138 glColor3f(.9,0,0);
139 glVertex3f(-0.5, 0.0, -30.0);
140 glEnd();
141 }
142
143 if (doubleBuffer)
144 glutSwapBuffers();
145 else
146 glFinish();
147
148 CheckError();
149 }
150
151
152 static GLenum Args(int argc, char **argv)
153 {
154 GLint i;
155
156 doubleBuffer = GL_FALSE;
157
158 for (i = 1; i < argc; i++) {
159 if (strcmp(argv[i], "-sb") == 0) {
160 doubleBuffer = GL_FALSE;
161 } else if (strcmp(argv[i], "-db") == 0) {
162 doubleBuffer = GL_TRUE;
163 } else {
164 fprintf(stderr, "%s (Bad option).\n", argv[i]);
165 return GL_FALSE;
166 }
167 }
168 return GL_TRUE;
169 }
170
171
172
173 int
174 main( int argc, char *argv[] )
175 {
176 GLenum type;
177
178 glutInit(&argc, argv);
179
180 if (Args(argc, argv) == GL_FALSE) {
181 exit(1);
182 }
183
184 glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
185
186 type = GLUT_RGB;
187 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
188 glutInitDisplayMode(type);
189
190 if (glutCreateWindow(argv[0]) == GL_FALSE) {
191 exit(1);
192 }
193
194 glewInit();
195
196 Init();
197
198 glutReshapeFunc(Reshape);
199 glutKeyboardFunc(Key);
200 glutDisplayFunc(Draw);
201 glutMainLoop();
202 return 0;
203 }