Brian Paul | 28bc3cd | 2006-04-14 16:25:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Test GL_EXT_framebuffer_object |
| 3 | * |
| 4 | * Brian Paul |
| 5 | * 19 Mar 2006 |
| 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 | |
| 16 | static int Width = 400, Height = 400; |
| 17 | static GLuint MyFB, ColorRb, DepthRb; |
| 18 | static GLboolean Animate = GL_TRUE; |
| 19 | static GLfloat Rotation = 0.0; |
| 20 | |
| 21 | |
| 22 | static void |
| 23 | CheckError(int line) |
| 24 | { |
| 25 | GLenum err = glGetError(); |
| 26 | if (err) { |
| 27 | printf("fbotest2: GL Error 0x%x at line %d\n", (int) err, line); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | |
| 32 | static void |
| 33 | Display( void ) |
| 34 | { |
| 35 | GLubyte *buffer = malloc(Width * Height * 4); |
| 36 | GLenum status; |
| 37 | |
| 38 | CheckError(__LINE__); |
| 39 | |
| 40 | /* draw to user framebuffer */ |
| 41 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 42 | glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 43 | glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 44 | |
| 45 | status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
| 46 | if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 47 | printf("fbotest2: Error: Framebuffer is incomplete!!!\n"); |
| 48 | } |
| 49 | |
| 50 | CheckError(__LINE__); |
| 51 | |
| 52 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 53 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 54 | |
| 55 | glEnable(GL_DEPTH_TEST); |
| 56 | glEnable(GL_LIGHTING); |
| 57 | glEnable(GL_LIGHT0); |
| 58 | |
| 59 | glPushMatrix(); |
| 60 | glRotatef(30.0, 1, 0, 0); |
| 61 | glRotatef(Rotation, 0, 1, 0); |
| 62 | glutSolidTeapot(2.0); |
| 63 | glPopMatrix(); |
| 64 | |
| 65 | /* read from user framebuffer */ |
| 66 | glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 67 | |
| 68 | /* draw to window */ |
| 69 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 70 | glWindowPos2iARB(0, 0); |
| 71 | glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 72 | |
| 73 | free(buffer); |
| 74 | glutSwapBuffers(); |
| 75 | CheckError(__LINE__); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static void |
| 80 | Reshape( int width, int height ) |
| 81 | { |
| 82 | float ar = (float) width / (float) height; |
| 83 | |
| 84 | glViewport( 0, 0, width, height ); |
| 85 | glMatrixMode( GL_PROJECTION ); |
| 86 | glLoadIdentity(); |
| 87 | glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 ); |
| 88 | |
| 89 | glMatrixMode( GL_MODELVIEW ); |
| 90 | glLoadIdentity(); |
| 91 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 92 | |
| 93 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ColorRb); |
| 94 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, width, height); |
| 95 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRb); |
| 96 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, |
| 97 | width, height); |
| 98 | |
| 99 | Width = width; |
| 100 | Height = height; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static void |
| 105 | CleanUp(void) |
| 106 | { |
| 107 | glDeleteFramebuffersEXT(1, &MyFB); |
| 108 | glDeleteRenderbuffersEXT(1, &ColorRb); |
| 109 | glDeleteRenderbuffersEXT(1, &DepthRb); |
| 110 | assert(!glIsFramebufferEXT(MyFB)); |
| 111 | assert(!glIsRenderbufferEXT(ColorRb)); |
| 112 | assert(!glIsRenderbufferEXT(DepthRb)); |
| 113 | exit(0); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static void |
| 118 | Idle(void) |
| 119 | { |
| 120 | Rotation = glutGet(GLUT_ELAPSED_TIME) * 0.1; |
| 121 | glutPostRedisplay(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | static void |
| 126 | Key( unsigned char key, int x, int y ) |
| 127 | { |
| 128 | (void) x; |
| 129 | (void) y; |
| 130 | switch (key) { |
| 131 | case 'a': |
| 132 | Animate = !Animate; |
| 133 | if (Animate) |
| 134 | glutIdleFunc(Idle); |
| 135 | else |
| 136 | glutIdleFunc(NULL); |
| 137 | break; |
| 138 | case 27: |
| 139 | CleanUp(); |
| 140 | break; |
| 141 | } |
| 142 | glutPostRedisplay(); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static void |
| 147 | Init( void ) |
| 148 | { |
| 149 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 150 | printf("fbotest2: GL_EXT_framebuffer_object not found!\n"); |
| 151 | exit(0); |
| 152 | } |
| 153 | printf("fbotest2: GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 154 | |
| 155 | glGenFramebuffersEXT(1, &MyFB); |
| 156 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB); |
| 157 | assert(glIsFramebufferEXT(MyFB)); |
| 158 | |
| 159 | /* set color buffer */ |
| 160 | glGenRenderbuffersEXT(1, &ColorRb); |
| 161 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ColorRb); |
| 162 | assert(glIsRenderbufferEXT(ColorRb)); |
| 163 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, |
| 164 | GL_RENDERBUFFER_EXT, ColorRb); |
| 165 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 166 | |
| 167 | /* setup depth buffer */ |
| 168 | glGenRenderbuffersEXT(1, &DepthRb); |
| 169 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRb); |
| 170 | assert(glIsRenderbufferEXT(DepthRb)); |
| 171 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, |
| 172 | GL_RENDERBUFFER_EXT, DepthRb); |
| 173 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, Width, Height); |
| 174 | |
| 175 | CheckError(__LINE__); |
| 176 | |
| 177 | /* restore to default */ |
| 178 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 179 | CheckError(__LINE__); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | int |
| 184 | main( int argc, char *argv[] ) |
| 185 | { |
| 186 | glutInit( &argc, argv ); |
| 187 | glutInitWindowPosition( 0, 0 ); |
| 188 | glutInitWindowSize(Width, Height); |
| 189 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE ); |
| 190 | glutCreateWindow(argv[0]); |
| 191 | glutReshapeFunc( Reshape ); |
| 192 | glutKeyboardFunc( Key ); |
| 193 | glutDisplayFunc( Display ); |
| 194 | if (Animate) |
| 195 | glutIdleFunc(Idle); |
| 196 | Init(); |
| 197 | glutMainLoop(); |
| 198 | return 0; |
| 199 | } |