Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

fbotest3.c 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Test GL_EXT_framebuffer_object
  3. * Like fbotest2.c but use a texture for the Z buffer / renderbuffer.
  4. * Note: the Z texture is never resized so that limits what can be
  5. * rendered if the window is resized.
  6. *
  7. * This tests a bug reported by Christoph Bumiller on 1 Feb 2010
  8. * on mesa3d-dev.
  9. *
  10. * XXX this should be made into a piglit test.
  11. *
  12. * Brian Paul
  13. * 1 Feb 2010
  14. */
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include <GL/glew.h>
  20. #include <GL/glut.h>
  21. static int Win = 0;
  22. static int Width = 400, Height = 400;
  23. static GLuint Tex = 0;
  24. static GLuint MyFB, ColorRb, DepthRb;
  25. static GLboolean Animate = GL_FALSE;
  26. static GLfloat Rotation = 0.0;
  27. static void
  28. CheckError(int line)
  29. {
  30. GLenum err = glGetError();
  31. if (err) {
  32. printf("fbotest3: GL Error 0x%x at line %d\n", (int) err, line);
  33. }
  34. }
  35. static void
  36. Display( void )
  37. {
  38. GLubyte *buffer = malloc(Width * Height * 4);
  39. GLenum status;
  40. CheckError(__LINE__);
  41. /* draw to user framebuffer */
  42. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  43. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  44. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  45. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  46. if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  47. printf("fbotest3: Error: Framebuffer is incomplete!!!\n");
  48. }
  49. CheckError(__LINE__);
  50. glClearColor(0.5, 0.5, 1.0, 0.0);
  51. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  52. glEnable(GL_DEPTH_TEST);
  53. glEnable(GL_LIGHTING);
  54. glEnable(GL_LIGHT0);
  55. glPushMatrix();
  56. glRotatef(30.0, 1, 0, 0);
  57. glRotatef(Rotation, 0, 1, 0);
  58. glutSolidTeapot(2.0);
  59. glPopMatrix();
  60. /* read from user framebuffer */
  61. glReadPixels(0, 0, Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  62. /* draw to window */
  63. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  64. glDisable(GL_DEPTH_TEST); /* in case window has depth buffer */
  65. glWindowPos2iARB(0, 0);
  66. glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  67. free(buffer);
  68. glutSwapBuffers();
  69. CheckError(__LINE__);
  70. }
  71. static void
  72. Reshape( int width, int height )
  73. {
  74. float ar = (float) width / (float) height;
  75. glViewport( 0, 0, width, height );
  76. glMatrixMode( GL_PROJECTION );
  77. glLoadIdentity();
  78. glFrustum( -ar, ar, -1.0, 1.0, 5.0, 25.0 );
  79. glMatrixMode( GL_MODELVIEW );
  80. glLoadIdentity();
  81. glTranslatef( 0.0, 0.0, -15.0 );
  82. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ColorRb);
  83. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, width, height);
  84. Width = width;
  85. Height = height;
  86. }
  87. static void
  88. CleanUp(void)
  89. {
  90. glDeleteFramebuffersEXT(1, &MyFB);
  91. glDeleteRenderbuffersEXT(1, &ColorRb);
  92. glDeleteRenderbuffersEXT(1, &DepthRb);
  93. glDeleteTextures(1, &Tex);
  94. assert(!glIsFramebufferEXT(MyFB));
  95. assert(!glIsRenderbufferEXT(ColorRb));
  96. assert(!glIsRenderbufferEXT(DepthRb));
  97. glutDestroyWindow(Win);
  98. exit(0);
  99. }
  100. static void
  101. Idle(void)
  102. {
  103. Rotation = glutGet(GLUT_ELAPSED_TIME) * 0.1;
  104. glutPostRedisplay();
  105. }
  106. static void
  107. Key( unsigned char key, int x, int y )
  108. {
  109. (void) x;
  110. (void) y;
  111. switch (key) {
  112. case 'a':
  113. Animate = !Animate;
  114. if (Animate)
  115. glutIdleFunc(Idle);
  116. else
  117. glutIdleFunc(NULL);
  118. break;
  119. case 27:
  120. CleanUp();
  121. break;
  122. }
  123. glutPostRedisplay();
  124. }
  125. static void
  126. Init( void )
  127. {
  128. if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
  129. printf("fbotest3: GL_EXT_framebuffer_object not found!\n");
  130. exit(0);
  131. }
  132. printf("fbotest3: GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  133. /* create initial tex obj as an RGBA texture */
  134. glGenTextures(1, &Tex);
  135. glBindTexture(GL_TEXTURE_2D, Tex);
  136. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0,
  137. GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  138. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  139. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  140. glEnable(GL_TEXTURE_2D);
  141. /* draw something to make sure the texture is used */
  142. glBegin(GL_POINTS);
  143. glVertex2f(0, 0);
  144. glEnd();
  145. /* done w/ texturing */
  146. glDisable(GL_TEXTURE_2D);
  147. /* Create my Framebuffer Object */
  148. glGenFramebuffersEXT(1, &MyFB);
  149. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  150. assert(glIsFramebufferEXT(MyFB));
  151. /* Setup color renderbuffer */
  152. glGenRenderbuffersEXT(1, &ColorRb);
  153. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, ColorRb);
  154. assert(glIsRenderbufferEXT(ColorRb));
  155. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  156. GL_RENDERBUFFER_EXT, ColorRb);
  157. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
  158. /* Setup depth renderbuffer (a texture) */
  159. glGenRenderbuffersEXT(1, &DepthRb);
  160. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthRb);
  161. assert(glIsRenderbufferEXT(DepthRb));
  162. /* replace RGBA texture with Z texture */
  163. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, Width, Height, 0,
  164. GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL);
  165. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  166. GL_TEXTURE_2D, Tex, 0);
  167. CheckError(__LINE__);
  168. /* restore to default */
  169. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  170. CheckError(__LINE__);
  171. }
  172. int
  173. main( int argc, char *argv[] )
  174. {
  175. glutInit( &argc, argv );
  176. glutInitWindowPosition( 0, 0 );
  177. glutInitWindowSize(Width, Height);
  178. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  179. Win = glutCreateWindow(argv[0]);
  180. glewInit();
  181. glutReshapeFunc( Reshape );
  182. glutKeyboardFunc( Key );
  183. glutDisplayFunc( Display );
  184. if (Animate)
  185. glutIdleFunc(Idle);
  186. Init();
  187. glutMainLoop();
  188. return 0;
  189. }