OSDN Git Service

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