blob: 8f4569ff3b4f466cb4b3d71e36fd1de438bfccb6 [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
Brian2282d812007-03-06 16:33:00 -070016static int Win;
Brian Paule4b23562005-05-04 20:11:35 +000017static int Width = 400, Height = 400;
Brian Paulf81c78f82005-11-17 02:08:10 +000018static GLuint MyFB, MyRB;
Brian Paule4b23562005-05-04 20:11:35 +000019
20
21static void
22CheckError(int line)
23{
24 GLenum err = glGetError();
25 if (err) {
26 printf("GL Error 0x%x at line %d\n", (int) err, line);
27 }
28}
29
30
31static void
32Display( void )
33{
34 GLubyte *buffer = malloc(Width * Height * 4);
35 GLenum status;
36
37 /* draw to user framebuffer */
38 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
39 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
40 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
41
42 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
43 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
44 printf("Framebuffer incomplete!!!\n");
45 }
46
47 glClearColor(0.5, 0.5, 1.0, 0.0);
48 glClear( GL_COLOR_BUFFER_BIT );
49
50 glBegin(GL_POLYGON);
51 glColor3f(1, 0, 0);
52 glVertex2f(-1, -1);
53 glColor3f(0, 1, 0);
54 glVertex2f(1, -1);
55 glColor3f(0, 0, 1);
56 glVertex2f(0, 1);
57 glEnd();
58
59 /* read from user framebuffer */
60 glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
61
62 /* draw to window */
63 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
64 glWindowPos2iARB(0, 0);
65 glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
66
67 free(buffer);
68 glutSwapBuffers();
Brian Paulf81c78f82005-11-17 02:08:10 +000069 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +000070}
71
72
73static void
74Reshape( int width, int height )
75{
Brian Paulcf085db2005-10-08 14:42:51 +000076#if 0
Brian Paule4b23562005-05-04 20:11:35 +000077 float ar = (float) width / (float) height;
Brian Paulcf085db2005-10-08 14:42:51 +000078#endif
Brian Paule4b23562005-05-04 20:11:35 +000079 glViewport( 0, 0, width, height );
80 glMatrixMode( GL_PROJECTION );
81 glLoadIdentity();
82#if 0
83 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
84#else
85 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
86#endif
87 glMatrixMode( GL_MODELVIEW );
88 glLoadIdentity();
89 glTranslatef( 0.0, 0.0, -15.0 );
90 Width = width;
91 Height = height;
92 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
93}
94
95
96static void
Brian Paulf81c78f82005-11-17 02:08:10 +000097CleanUp(void)
98{
99 glDeleteFramebuffersEXT(1, &MyFB);
100 glDeleteRenderbuffersEXT(1, &MyRB);
101 assert(!glIsFramebufferEXT(MyFB));
102 assert(!glIsRenderbufferEXT(MyRB));
Brian2282d812007-03-06 16:33:00 -0700103 glutDestroyWindow(Win);
Brian Paulf81c78f82005-11-17 02:08:10 +0000104 exit(0);
105}
106
107
108static void
Brian Paule4b23562005-05-04 20:11:35 +0000109Key( unsigned char key, int x, int y )
110{
111 (void) x;
112 (void) y;
113 switch (key) {
114 case 27:
Brian Paulf81c78f82005-11-17 02:08:10 +0000115 CleanUp();
Brian Paule4b23562005-05-04 20:11:35 +0000116 break;
117 }
118 glutPostRedisplay();
119}
120
121
122static void
123Init( void )
124{
Brian Paule4b23562005-05-04 20:11:35 +0000125 GLint i;
126
127 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
128 printf("GL_EXT_framebuffer_object not found!\n");
129 /*exit(0);*/
130 }
131 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
132
133 glGenFramebuffersEXT(1, &MyFB);
134 assert(MyFB);
Brian Paul9e920fb2005-10-04 15:01:51 +0000135 assert(!glIsFramebufferEXT(MyFB));
Brian Paule4b23562005-05-04 20:11:35 +0000136 glDeleteFramebuffersEXT(1, &MyFB);
137 assert(!glIsFramebufferEXT(MyFB));
138 /* Note, continue to use MyFB below */
139
Brian Paulf81c78f82005-11-17 02:08:10 +0000140 glGenRenderbuffersEXT(1, &MyRB);
141 assert(MyRB);
142 assert(!glIsRenderbufferEXT(MyRB));
143 glDeleteRenderbuffersEXT(1, &MyRB);
144 assert(!glIsRenderbufferEXT(MyRB));
145 MyRB = 42; /* an arbitrary ID */
Brian Paule4b23562005-05-04 20:11:35 +0000146
147 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
148 assert(glIsFramebufferEXT(MyFB));
Brian Paulf81c78f82005-11-17 02:08:10 +0000149 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
150 assert(glIsRenderbufferEXT(MyRB));
Brian Paule4b23562005-05-04 20:11:35 +0000151
152 glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, &i);
Brian Paulf81c78f82005-11-17 02:08:10 +0000153 assert(i == MyRB);
Brian Paule4b23562005-05-04 20:11:35 +0000154
155 glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i);
156 assert(i == MyFB);
157
Brian Paulf81c78f82005-11-17 02:08:10 +0000158 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +0000159 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
Brian Paulf81c78f82005-11-17 02:08:10 +0000160 GL_RENDERBUFFER_EXT, MyRB);
Brian Paule4b23562005-05-04 20:11:35 +0000161
162 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
163
Brian Paulf81c78f82005-11-17 02:08:10 +0000164 CheckError(__LINE__);
165
Brian Paul58a95732005-07-01 01:34:29 +0000166 {
167 GLint r, g, b, a;
168 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
169 GL_RENDERBUFFER_RED_SIZE_EXT, &r);
170 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
171 GL_RENDERBUFFER_GREEN_SIZE_EXT, &g);
172 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
173 GL_RENDERBUFFER_BLUE_SIZE_EXT, &b);
174 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
175 GL_RENDERBUFFER_ALPHA_SIZE_EXT, &a);
Brian Paulf81c78f82005-11-17 02:08:10 +0000176 CheckError(__LINE__);
Brian Paul58a95732005-07-01 01:34:29 +0000177 printf("renderbuffer RGBA sizes = %d %d %d %d\n", r, g, b, a);
Brian Paulcf085db2005-10-08 14:42:51 +0000178
179 glGetIntegerv(GL_RED_BITS, &r);
180 glGetIntegerv(GL_GREEN_BITS, &g);
181 glGetIntegerv(GL_BLUE_BITS, &b);
182 glGetIntegerv(GL_ALPHA_BITS, &a);
183 printf("Visual RGBA sizes = %d %d %d %d\n", r, g, b, a);
Brian Paul58a95732005-07-01 01:34:29 +0000184 }
185
Brian Paule4b23562005-05-04 20:11:35 +0000186 /* restore to default */
187 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
Brian Paulf81c78f82005-11-17 02:08:10 +0000188 CheckError(__LINE__);
Brian Paule4b23562005-05-04 20:11:35 +0000189}
190
191
192int
193main( int argc, char *argv[] )
194{
195 glutInit( &argc, argv );
196 glutInitWindowPosition( 0, 0 );
197 glutInitWindowSize(Width, Height);
198 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
Brian2282d812007-03-06 16:33:00 -0700199 Win = glutCreateWindow(argv[0]);
Brian Paule4b23562005-05-04 20:11:35 +0000200 glutReshapeFunc( Reshape );
201 glutKeyboardFunc( Key );
202 glutDisplayFunc( Display );
203 Init();
204 glutMainLoop();
205 return 0;
206}