Convert libGL and DRI drivers to require libdrm.
[mesa-demos.git] / src / tests / fbotest1.c
blob6cacaeb4b9da1ed2e786eaddbd626f6498937687
1 /*
2 * Test GL_EXT_framebuffer_object
4 * Brian Paul
5 * 7 Feb 2005
6 */
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>
16 static int Width = 400, Height = 400;
17 static GLuint MyFB;
20 static void
21 CheckError(int line)
23 GLenum err = glGetError();
24 if (err) {
25 printf("GL Error 0x%x at line %d\n", (int) err, line);
30 static void
31 Display( void )
33 GLubyte *buffer = malloc(Width * Height * 4);
34 GLenum status;
36 /* draw to user framebuffer */
37 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
38 glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
39 glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
41 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
42 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
43 printf("Framebuffer incomplete!!!\n");
46 glClearColor(0.5, 0.5, 1.0, 0.0);
47 glClear( GL_COLOR_BUFFER_BIT );
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();
58 /* read from user framebuffer */
59 glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
61 /* draw to window */
62 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
63 glWindowPos2iARB(0, 0);
64 glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
66 free(buffer);
67 glutSwapBuffers();
71 static void
72 Reshape( int width, int height )
74 float ar = (float) width / (float) height;
75 glViewport( 0, 0, width, height );
76 glMatrixMode( GL_PROJECTION );
77 glLoadIdentity();
78 #if 0
79 glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
80 #else
81 glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
82 #endif
83 glMatrixMode( GL_MODELVIEW );
84 glLoadIdentity();
85 glTranslatef( 0.0, 0.0, -15.0 );
86 Width = width;
87 Height = height;
88 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
92 static void
93 Key( unsigned char key, int x, int y )
95 (void) x;
96 (void) y;
97 switch (key) {
98 case 27:
99 exit(0);
100 break;
102 glutPostRedisplay();
106 static void
107 Init( void )
109 GLuint rb;
110 GLint i;
112 if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
113 printf("GL_EXT_framebuffer_object not found!\n");
114 /*exit(0);*/
116 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
118 glGenFramebuffersEXT(1, &MyFB);
119 assert(MyFB);
120 assert(glIsFramebufferEXT(MyFB));
121 glDeleteFramebuffersEXT(1, &MyFB);
122 assert(!glIsFramebufferEXT(MyFB));
123 /* Note, continue to use MyFB below */
125 glGenRenderbuffersEXT(1, &rb);
126 assert(rb);
127 assert(glIsRenderbufferEXT(rb));
128 glDeleteRenderbuffersEXT(1, &rb);
129 assert(!glIsRenderbufferEXT(rb));
130 rb = 42; /* an arbitrary ID */
132 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
133 assert(glIsFramebufferEXT(MyFB));
134 glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
135 assert(glIsRenderbufferEXT(rb));
137 glGetIntegerv(GL_RENDERBUFFER_BINDING_EXT, &i);
138 assert(i == rb);
140 glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &i);
141 assert(i == MyFB);
143 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
144 GL_RENDERBUFFER_EXT, rb);
146 glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
149 GLint r, g, b, a;
150 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
151 GL_RENDERBUFFER_RED_SIZE_EXT, &r);
152 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
153 GL_RENDERBUFFER_GREEN_SIZE_EXT, &g);
154 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
155 GL_RENDERBUFFER_BLUE_SIZE_EXT, &b);
156 glGetRenderbufferParameterivEXT(GL_RENDERBUFFER_EXT,
157 GL_RENDERBUFFER_ALPHA_SIZE_EXT, &a);
158 printf("renderbuffer RGBA sizes = %d %d %d %d\n", r, g, b, a);
161 CheckError(__LINE__);
163 /* restore to default */
164 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
169 main( int argc, char *argv[] )
171 glutInit( &argc, argv );
172 glutInitWindowPosition( 0, 0 );
173 glutInitWindowSize(Width, Height);
174 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
175 glutCreateWindow(argv[0]);
176 glutReshapeFunc( Reshape );
177 glutKeyboardFunc( Key );
178 glutDisplayFunc( Display );
179 Init();
180 glutMainLoop();
181 return 0;