Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Test GL_ARB_draw_buffers, GL_EXT_framebuffer_object |
| 3 | * and GLSL's gl_FragData[]. |
| 4 | * |
| 5 | * Brian Paul |
| 6 | * 11 March 2007 |
| 7 | */ |
| 8 | |
| 9 | |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 10 | #include <assert.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <math.h> |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 14 | #include <GL/glew.h> |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 15 | #include <GL/glut.h> |
| 16 | #include "extfuncs.h" |
| 17 | |
| 18 | static int Win; |
| 19 | static int Width = 400, Height = 400; |
| 20 | static GLuint FBobject, RBobjects[3]; |
| 21 | static GLfloat Xrot = 0.0, Yrot = 0.0; |
Brian | 5e9cff0 | 2007-06-08 13:02:14 -0600 | [diff] [blame] | 22 | static GLuint Program; |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 23 | |
| 24 | |
| 25 | static void |
| 26 | CheckError(int line) |
| 27 | { |
| 28 | GLenum err = glGetError(); |
| 29 | if (err) { |
| 30 | printf("GL Error 0x%x at line %d\n", (int) err, line); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | static void |
| 36 | Display(void) |
| 37 | { |
| 38 | GLubyte *buffer = malloc(Width * Height * 4); |
| 39 | static const GLenum buffers[2] = { |
| 40 | GL_COLOR_ATTACHMENT0_EXT, |
| 41 | GL_COLOR_ATTACHMENT1_EXT |
| 42 | }; |
| 43 | |
Brian | 5e9cff0 | 2007-06-08 13:02:14 -0600 | [diff] [blame] | 44 | glUseProgram_func(Program); |
| 45 | |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 46 | /* draw to user framebuffer */ |
| 47 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBobject); |
| 48 | |
| 49 | /* Clear color buffer 0 (blue) */ |
| 50 | glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT); |
| 51 | glClearColor(0.5, 0.5, 1.0, 0.0); |
| 52 | glClear(GL_COLOR_BUFFER_BIT); |
| 53 | |
| 54 | /* Clear color buffer 1 (1 - blue) */ |
| 55 | glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT); |
| 56 | glClearColor(0.5, 0.5, 0.0, 0.0); |
| 57 | glClear(GL_COLOR_BUFFER_BIT); |
| 58 | |
| 59 | glClear(GL_DEPTH_BUFFER_BIT); |
| 60 | |
| 61 | /* draw to two buffers w/ fragment shader */ |
| 62 | glDrawBuffersARB(2, buffers); |
| 63 | |
| 64 | glPushMatrix(); |
| 65 | glRotatef(Xrot, 1, 0, 0); |
| 66 | glRotatef(Yrot, 0, 1, 0); |
| 67 | glutSolidTorus(0.75, 2.0, 10, 20); |
| 68 | glPopMatrix(); |
| 69 | |
| 70 | /* read from user framebuffer */ |
| 71 | /* bottom half = colorbuffer 0 */ |
| 72 | glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); |
| 73 | glReadPixels(0, 0, Width, Height / 2, GL_RGBA, GL_UNSIGNED_BYTE, |
| 74 | buffer); |
| 75 | /* top half = colorbuffer 1 */ |
| 76 | glReadBuffer(GL_COLOR_ATTACHMENT1_EXT); |
Brian | 39d221e | 2007-06-08 13:10:01 -0600 | [diff] [blame] | 77 | glReadPixels(0, Height/2, Width, Height - Height / 2, |
| 78 | GL_RGBA, GL_UNSIGNED_BYTE, |
| 79 | buffer + Width * (Height / 2) * 4); |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 80 | |
| 81 | /* draw to window */ |
Brian | 5e9cff0 | 2007-06-08 13:02:14 -0600 | [diff] [blame] | 82 | glUseProgram_func(0); |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 83 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 84 | glWindowPos2iARB(0, 0); |
| 85 | glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 86 | |
| 87 | free(buffer); |
| 88 | glutSwapBuffers(); |
| 89 | CheckError(__LINE__); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static void |
| 94 | Reshape(int width, int height) |
| 95 | { |
| 96 | float ar = (float) width / (float) height; |
| 97 | |
| 98 | glViewport(0, 0, width, height); |
| 99 | glMatrixMode(GL_PROJECTION); |
| 100 | glLoadIdentity(); |
| 101 | glFrustum(-ar, ar, -1.0, 1.0, 5.0, 35.0); |
| 102 | glMatrixMode(GL_MODELVIEW); |
| 103 | glLoadIdentity(); |
| 104 | glTranslatef(0.0, 0.0, -20.0); |
| 105 | |
| 106 | Width = width; |
| 107 | Height = height; |
| 108 | |
| 109 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBobjects[0]); |
| 110 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 111 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBobjects[1]); |
| 112 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 113 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBobjects[2]); |
| 114 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, |
| 115 | Width, Height); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static void |
| 120 | CleanUp(void) |
| 121 | { |
| 122 | glDeleteFramebuffersEXT(1, &FBobject); |
| 123 | glDeleteRenderbuffersEXT(3, RBobjects); |
| 124 | glutDestroyWindow(Win); |
| 125 | exit(0); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static void |
| 130 | Key(unsigned char key, int x, int y) |
| 131 | { |
| 132 | (void) x; |
| 133 | (void) y; |
| 134 | switch (key) { |
| 135 | case 'x': |
| 136 | Xrot += 5.0; |
| 137 | break; |
| 138 | case 'y': |
| 139 | Yrot += 5.0; |
| 140 | break; |
| 141 | case 27: |
| 142 | CleanUp(); |
| 143 | break; |
| 144 | } |
| 145 | glutPostRedisplay(); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static void |
| 150 | CheckExtensions(void) |
| 151 | { |
| 152 | const char *version = (const char *) glGetString(GL_VERSION); |
| 153 | GLint numBuf; |
| 154 | |
| 155 | if (!glutExtensionSupported("GL_EXT_framebuffer_object")) { |
| 156 | printf("Sorry, GL_EXT_framebuffer_object is required!\n"); |
| 157 | exit(1); |
| 158 | } |
| 159 | if (!glutExtensionSupported("GL_ARB_draw_buffers")) { |
| 160 | printf("Sorry, GL_ARB_draw_buffers is required!\n"); |
| 161 | exit(1); |
| 162 | } |
| 163 | if (version[0] != '2') { |
| 164 | printf("Sorry, OpenGL 2.0 is required!\n"); |
| 165 | exit(1); |
| 166 | } |
| 167 | |
| 168 | glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &numBuf); |
| 169 | printf("GL_MAX_DRAW_BUFFERS_ARB = %d\n", numBuf); |
| 170 | if (numBuf < 2) { |
| 171 | printf("Sorry, GL_MAX_DRAW_BUFFERS_ARB needs to be >= 2\n"); |
| 172 | exit(1); |
| 173 | } |
| 174 | |
| 175 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static void |
| 180 | SetupRenderbuffers(void) |
| 181 | { |
| 182 | glGenFramebuffersEXT(1, &FBobject); |
| 183 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBobject); |
| 184 | |
| 185 | glGenRenderbuffersEXT(3, RBobjects); |
| 186 | |
| 187 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBobjects[0]); |
| 188 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 189 | |
| 190 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBobjects[1]); |
| 191 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height); |
| 192 | |
| 193 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBobjects[2]); |
| 194 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, |
| 195 | Width, Height); |
| 196 | |
| 197 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 198 | GL_RENDERBUFFER_EXT, RBobjects[0]); |
| 199 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, |
| 200 | GL_RENDERBUFFER_EXT, RBobjects[1]); |
| 201 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, |
| 202 | GL_RENDERBUFFER_EXT, RBobjects[2]); |
| 203 | |
| 204 | CheckError(__LINE__); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | static GLuint |
| 209 | LoadAndCompileShader(GLenum target, const char *text) |
| 210 | { |
| 211 | GLint stat; |
| 212 | GLuint shader = glCreateShader_func(target); |
| 213 | glShaderSource_func(shader, 1, (const GLchar **) &text, NULL); |
| 214 | glCompileShader_func(shader); |
| 215 | glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat); |
| 216 | if (!stat) { |
| 217 | GLchar log[1000]; |
| 218 | GLsizei len; |
| 219 | glGetShaderInfoLog_func(shader, 1000, &len, log); |
| 220 | fprintf(stderr, "drawbuffers: problem compiling shader:\n%s\n", log); |
| 221 | exit(1); |
| 222 | } |
| 223 | return shader; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static void |
| 228 | CheckLink(GLuint prog) |
| 229 | { |
| 230 | GLint stat; |
| 231 | glGetProgramiv_func(prog, GL_LINK_STATUS, &stat); |
| 232 | if (!stat) { |
| 233 | GLchar log[1000]; |
| 234 | GLsizei len; |
| 235 | glGetProgramInfoLog_func(prog, 1000, &len, log); |
| 236 | fprintf(stderr, "drawbuffers: shader link error:\n%s\n", log); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static void |
| 242 | SetupShaders(void) |
| 243 | { |
| 244 | /* second color output = 1 - first color */ |
| 245 | static const char *fragShaderText = |
| 246 | "void main() {\n" |
| 247 | " gl_FragData[0] = gl_Color; \n" |
| 248 | " gl_FragData[1] = vec4(1.0) - gl_Color; \n" |
| 249 | "}\n"; |
| 250 | |
Brian | 5e9cff0 | 2007-06-08 13:02:14 -0600 | [diff] [blame] | 251 | GLuint fragShader; |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 252 | |
| 253 | fragShader = LoadAndCompileShader(GL_FRAGMENT_SHADER, fragShaderText); |
Brian | 5e9cff0 | 2007-06-08 13:02:14 -0600 | [diff] [blame] | 254 | Program = glCreateProgram_func(); |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 255 | |
Brian | 5e9cff0 | 2007-06-08 13:02:14 -0600 | [diff] [blame] | 256 | glAttachShader_func(Program, fragShader); |
| 257 | glLinkProgram_func(Program); |
| 258 | CheckLink(Program); |
| 259 | glUseProgram_func(Program); |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | static void |
| 264 | SetupLighting(void) |
| 265 | { |
| 266 | static const GLfloat frontMat[4] = { 1.0, 0.5, 0.5, 1.0 }; |
| 267 | static const GLfloat backMat[4] = { 1.0, 0.5, 0.5, 1.0 }; |
| 268 | |
| 269 | glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, frontMat); |
| 270 | glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, backMat); |
| 271 | glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1); |
| 272 | glEnable(GL_LIGHT0); |
| 273 | glEnable(GL_LIGHTING); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static void |
| 278 | Init(void) |
| 279 | { |
| 280 | CheckExtensions(); |
| 281 | GetExtensionFuncs(); |
| 282 | SetupRenderbuffers(); |
| 283 | SetupShaders(); |
| 284 | SetupLighting(); |
| 285 | glEnable(GL_DEPTH_TEST); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | int |
| 290 | main(int argc, char *argv[]) |
| 291 | { |
| 292 | glutInit(&argc, argv); |
| 293 | glutInitWindowPosition(0, 0); |
| 294 | glutInitWindowSize(Width, Height); |
| 295 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); |
| 296 | Win = glutCreateWindow(argv[0]); |
Keith Whitwell | a58065d | 2009-03-10 13:11:23 +0000 | [diff] [blame] | 297 | glewInit(); |
Brian | ccb80d7 | 2007-03-11 17:03:29 -0600 | [diff] [blame] | 298 | glutReshapeFunc(Reshape); |
| 299 | glutKeyboardFunc(Key); |
| 300 | glutDisplayFunc(Display); |
| 301 | Init(); |
| 302 | glutMainLoop(); |
| 303 | return 0; |
| 304 | } |