blob: 3faa55eb907c0ffff7ad3701b1dc66224109d190 [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;
17static GLuint MyFB;
18
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();
68}
69
70
71static void
72Reshape( int width, int height )
73{
Brian Paulcf085db2005-10-08 14:42:51 +000074#if 0
Brian Paule4b23562005-05-04 20:11:35 +000075 float ar = (float) width / (float) height;
Brian Paulcf085db2005-10-08 14:42:51 +000076#endif
Brian Paule4b23562005-05-04 20:11:35 +000077 glViewport( 0, 0, width, height );
78 glMatrixMode( GL_PROJECTION );
79 glLoadIdentity();
80#if 0
81 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
82#else
83 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
84#endif
85 glMatrixMode( GL_MODELVIEW );
86 glLoadIdentity();
87 glTranslatef( 0.0, 0.0, -15.0 );
88 Width = width;
89 Height = height;
90 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
91}
92
93
94static void
95Key( unsigned char key, int x, int y )
96{
97 (void) x;
98 (void) y;
99 switch (key) {
100 case 27:
101 exit(0);
102 break;
103 }
104 glutPostRedisplay();
105}
106
107
108static void
109Init( void )
110{
111 GLuint rb;
112 GLint i;
113
114 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
115 printf("GL_EXT_framebuffer_object not found!\n");
116 /*exit(0);*/
117 }
118 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
119
120 glGenFramebuffersEXT(1, &MyFB);
121 assert(MyFB);
Brian Paul9e920fb2005-10-04 15:01:51 +0000122 assert(!glIsFramebufferEXT(MyFB));
Brian Paule4b23562005-05-04 20:11:35 +0000123 glDeleteFramebuffersEXT(1, &MyFB);
124 assert(!glIsFramebufferEXT(MyFB));
125 /* Note, continue to use MyFB below */
126
127 glGenRenderbuffersEXT(1, &rb);
128 assert(rb);
Brian Paul9e920fb2005-10-04 15:01:51 +0000129 assert(!glIsRenderbufferEXT(rb));
Brian Paule4b23562005-05-04 20:11:35 +0000130 glDeleteRenderbuffersEXT(1, &rb);
131 assert(!glIsRenderbufferEXT(rb));
132 rb = 42; /* an arbitrary ID */
133
134 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
135 assert(glIsFramebufferEXT(MyFB));
136 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
137 assert(glIsRenderbufferEXT(rb));
138
139 glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, &i);
140 assert(i == rb);
141
142 glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i);
143 assert(i == MyFB);
144
145 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
146 GL_RENDERBUFFER_EXT, rb);
147
148 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
149
Brian Paul58a95732005-07-01 01:34:29 +0000150 {
151 GLint r, g, b, a;
152 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
153 GL_RENDERBUFFER_RED_SIZE_EXT, &r);
154 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
155 GL_RENDERBUFFER_GREEN_SIZE_EXT, &g);
156 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
157 GL_RENDERBUFFER_BLUE_SIZE_EXT, &b);
158 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
159 GL_RENDERBUFFER_ALPHA_SIZE_EXT, &a);
160 printf("renderbuffer RGBA sizes = %d %d %d %d\n", r, g, b, a);
Brian Paulcf085db2005-10-08 14:42:51 +0000161
162 glGetIntegerv(GL_RED_BITS, &r);
163 glGetIntegerv(GL_GREEN_BITS, &g);
164 glGetIntegerv(GL_BLUE_BITS, &b);
165 glGetIntegerv(GL_ALPHA_BITS, &a);
166 printf("Visual RGBA sizes = %d %d %d %d\n", r, g, b, a);
Brian Paul58a95732005-07-01 01:34:29 +0000167 }
168
Brian Paule4b23562005-05-04 20:11:35 +0000169 CheckError(__LINE__);
170
171 /* restore to default */
172 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
173}
174
175
176int
177main( int argc, char *argv[] )
178{
179 glutInit( &argc, argv );
180 glutInitWindowPosition( 0, 0 );
181 glutInitWindowSize(Width, Height);
182 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
183 glutCreateWindow(argv[0]);
184 glutReshapeFunc( Reshape );
185 glutKeyboardFunc( Key );
186 glutDisplayFunc( Display );
187 Init();
188 glutMainLoop();
189 return 0;
190}