blob: ed72ab16dd8b95ab9f062bc0c1da4e1bb4956d48 [file] [log] [blame]
Brian Paule4b23562005-05-04 20:11:35 +00001/*
2 * Test GL_EXT_framebuffer_object
3 *
4 * Brian Paul
5 * 7 Feb 2005
6 */
7
8
9#define GL_GLEXT_PROTOTYPES
10#include <assert.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <math.h>
14#include <GL/glut.h>
15
16static int Width = 400, Height = 400;
Brian Paulf81c78f82005-11-17 02:08:10 +000017static GLuint MyFB, MyRB;
Brian Paule4b23562005-05-04 20:11:35 +000018
19
20static void
21CheckError(int line)
22{
23 GLenum err = glGetError();
24 if (err) {
25 printf("GL Error 0x%x at line %d\n", (int) err, line);
26 }
27}
28
29
30static void
31Display( void )
32{
33 GLubyte *buffer = malloc(Width * Height * 4);
34 GLenum status;
35
36 /* draw to user framebuffer */
37 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
38 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
39 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
40
41 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
42 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
43 printf("Framebuffer incomplete!!!\n");
44 }
45
46 glClearColor(0.5, 0.5, 1.0, 0.0);
47 glClear( GL_COLOR_BUFFER_BIT );
48
49 glBegin(GL_POLYGON);
50 glColor3f(1, 0, 0);
51 glVertex2f(-1, -1);
52 glColor3f(0, 1, 0);
53 glVertex2f(1, -1);
54 glColor3f(0, 0, 1);
55 glVertex2f(0, 1);
56 glEnd();
57
58 /* read from user framebuffer */
59 glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
60
61 /* draw to window */
62 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
63 glWindowPos2iARB(0, 0);
64 glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
65
66 free(buffer);
67 glutSwapBuffers();
Brian Paulf81c78f82005-11-17 02:08:10 +000068 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +000069}
70
71
72static void
73Reshape( int width, int height )
74{
Brian Paulcf085db2005-10-08 14:42:51 +000075#if 0
Brian Paule4b23562005-05-04 20:11:35 +000076 float ar = (float) width / (float) height;
Brian Paulcf085db2005-10-08 14:42:51 +000077#endif
Brian Paule4b23562005-05-04 20:11:35 +000078 glViewport( 0, 0, width, height );
79 glMatrixMode( GL_PROJECTION );
80 glLoadIdentity();
81#if 0
82 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
83#else
84 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
85#endif
86 glMatrixMode( GL_MODELVIEW );
87 glLoadIdentity();
88 glTranslatef( 0.0, 0.0, -15.0 );
89 Width = width;
90 Height = height;
91 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
92}
93
94
95static void
Brian Paulf81c78f82005-11-17 02:08:10 +000096CleanUp(void)
97{
98 glDeleteFramebuffersEXT(1, &MyFB);
99 glDeleteRenderbuffersEXT(1, &MyRB);
100 assert(!glIsFramebufferEXT(MyFB));
101 assert(!glIsRenderbufferEXT(MyRB));
102 exit(0);
103}
104
105
106static void
Brian Paule4b23562005-05-04 20:11:35 +0000107Key( unsigned char key, int x, int y )
108{
109 (void) x;
110 (void) y;
111 switch (key) {
112 case 27:
Brian Paulf81c78f82005-11-17 02:08:10 +0000113 CleanUp();
Brian Paule4b23562005-05-04 20:11:35 +0000114 break;
115 }
116 glutPostRedisplay();
117}
118
119
120static void
121Init( void )
122{
Brian Paule4b23562005-05-04 20:11:35 +0000123 GLint i;
124
125 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
126 printf("GL_EXT_framebuffer_object not found!\n");
127 /*exit(0);*/
128 }
129 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
130
131 glGenFramebuffersEXT(1, &MyFB);
132 assert(MyFB);
Brian Paul9e920fb2005-10-04 15:01:51 +0000133 assert(!glIsFramebufferEXT(MyFB));
Brian Paule4b23562005-05-04 20:11:35 +0000134 glDeleteFramebuffersEXT(1, &MyFB);
135 assert(!glIsFramebufferEXT(MyFB));
136 /* Note, continue to use MyFB below */
137
Brian Paulf81c78f82005-11-17 02:08:10 +0000138 glGenRenderbuffersEXT(1, &MyRB);
139 assert(MyRB);
140 assert(!glIsRenderbufferEXT(MyRB));
141 glDeleteRenderbuffersEXT(1, &MyRB);
142 assert(!glIsRenderbufferEXT(MyRB));
143 MyRB = 42; /* an arbitrary ID */
Brian Paule4b23562005-05-04 20:11:35 +0000144
145 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
146 assert(glIsFramebufferEXT(MyFB));
Brian Paulf81c78f82005-11-17 02:08:10 +0000147 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
148 assert(glIsRenderbufferEXT(MyRB));
Brian Paule4b23562005-05-04 20:11:35 +0000149
150 glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, &i);
Brian Paulf81c78f82005-11-17 02:08:10 +0000151 assert(i == MyRB);
Brian Paule4b23562005-05-04 20:11:35 +0000152
153 glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i);
154 assert(i == MyFB);
155
Brian Paulf81c78f82005-11-17 02:08:10 +0000156 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +0000157 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
Brian Paulf81c78f82005-11-17 02:08:10 +0000158 GL_RENDERBUFFER_EXT, MyRB);
Brian Paule4b23562005-05-04 20:11:35 +0000159
160 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
161
Brian Paulf81c78f82005-11-17 02:08:10 +0000162 CheckError(__LINE__);
163
Brian Paul58a95732005-07-01 01:34:29 +0000164 {
165 GLint r, g, b, a;
166 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
167 GL_RENDERBUFFER_RED_SIZE_EXT, &r);
168 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
169 GL_RENDERBUFFER_GREEN_SIZE_EXT, &g);
170 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
171 GL_RENDERBUFFER_BLUE_SIZE_EXT, &b);
172 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
173 GL_RENDERBUFFER_ALPHA_SIZE_EXT, &a);
Brian Paulf81c78f82005-11-17 02:08:10 +0000174 CheckError(__LINE__);
Brian Paul58a95732005-07-01 01:34:29 +0000175 printf("renderbuffer RGBA sizes = %d %d %d %d\n", r, g, b, a);
Brian Paulcf085db2005-10-08 14:42:51 +0000176
177 glGetIntegerv(GL_RED_BITS, &r);
178 glGetIntegerv(GL_GREEN_BITS, &g);
179 glGetIntegerv(GL_BLUE_BITS, &b);
180 glGetIntegerv(GL_ALPHA_BITS, &a);
181 printf("Visual RGBA sizes = %d %d %d %d\n", r, g, b, a);
Brian Paul58a95732005-07-01 01:34:29 +0000182 }
183
Brian Paule4b23562005-05-04 20:11:35 +0000184 /* restore to default */
185 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
Brian Paulf81c78f82005-11-17 02:08:10 +0000186 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +0000187}
188
189
190int
191main( int argc, char *argv[] )
192{
193 glutInit( &argc, argv );
194 glutInitWindowPosition( 0, 0 );
195 glutInitWindowSize(Width, Height);
196 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
197 glutCreateWindow(argv[0]);
198 glutReshapeFunc( Reshape );
199 glutKeyboardFunc( Key );
200 glutDisplayFunc( Display );
201 Init();
202 glutMainLoop();
203 return 0;
204}