Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Rendering to cube map

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 1.96 KB  |  views: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // depth cube map
  2. glGenTextures(1, &tDepthCubeMap);
  3. glBindTexture(GL_TEXTURE_CUBE_MAP, tDepthCubeMap);
  4. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  5. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  6. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  7. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  8. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  9. for (uint face = 0; face < 6; face++) {
  10.     glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_DEPTH_COMPONENT24,
  11.         width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, null);
  12. }
  13.  
  14. // color cube map
  15. glGenTextures(1, &tColorCubeMap);
  16. glBindTexture(GL_TEXTURE_CUBE_MAP, tColorCubeMap);
  17. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  18. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  19. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  20. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  21. glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  22. for (uint face = 0; face < 6; face++) {
  23.     glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA,
  24.         width, height, 0, GL_RGBA, GL_FLOAT, null);
  25. }
  26.  
  27. // framebuffer object
  28. glGenFramebuffersEXT(1, &fbo);
  29. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  30. glFramebufferTextureARB(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, tDepthCubeMap, 0);
  31. glFramebufferTextureARB(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, tColorCubeMap, 0);
  32.  
  33. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  34.  
  35. if (!isValidFBO()) {
  36.     glDeleteFramebuffersEXT(1, &fbo);
  37.     fbo = 0;
  38. }
  39.        
  40. #version 120
  41. #extension GL_EXT_geometry_shader4 : enable
  42.  
  43. void main(void) {
  44.     int i, layer;
  45.     for (layer = 0; layer < 6; layer++) {
  46.         gl_Layer = layer;
  47.         for (i = 0; i < 3; i++) {
  48.             gl_Position = gl_PositionIn[i];
  49.             EmitVertex();
  50.         }
  51.         EndPrimitive();
  52.     }
  53. }