Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clear-fbo.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <GL/glew.h>
  7. #include <GL/glut.h>
  8. static int Width = 512, Height = 512;
  9. static GLuint MyFB, MyRB;
  10. #define CheckError() assert(glGetError() == 0)
  11. GLenum doubleBuffer;
  12. static void Init(void)
  13. {
  14. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  15. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  16. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  17. fflush(stderr);
  18. if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
  19. printf("GL_EXT_framebuffer_object not found!\n");
  20. exit(0);
  21. }
  22. glGenFramebuffersEXT(1, &MyFB);
  23. glGenRenderbuffersEXT(1, &MyRB);
  24. {
  25. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  26. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
  27. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
  28. GL_RENDERBUFFER_EXT, MyRB);
  29. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
  30. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  31. }
  32. }
  33. static void
  34. Reshape( int width, int height )
  35. {
  36. glViewport( 0, 0, width, height );
  37. glMatrixMode( GL_PROJECTION );
  38. glLoadIdentity();
  39. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  40. glMatrixMode( GL_MODELVIEW );
  41. Width = width;
  42. Height = height;
  43. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
  44. }
  45. static void Key(unsigned char key, int x, int y)
  46. {
  47. switch (key) {
  48. case 27:
  49. exit(1);
  50. default:
  51. break;
  52. }
  53. glutPostRedisplay();
  54. }
  55. static void Draw( void )
  56. {
  57. /* draw to user framebuffer */
  58. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  59. glDrawBuffer(GL_COLOR_ATTACHMENT1_EXT);
  60. glReadBuffer(GL_COLOR_ATTACHMENT1_EXT);
  61. glViewport(0, 0, Width, Height);
  62. CheckError();
  63. glClearColor(0.5, 0.5, 1.0, 0.0);
  64. glClear(GL_COLOR_BUFFER_BIT);
  65. CheckError();
  66. if (0) {
  67. glBegin(GL_TRIANGLES);
  68. glColor3f(0,0,.7);
  69. glVertex3f( 0.9, -0.9, -30.0);
  70. glColor3f(.8,0,0);
  71. glVertex3f( 0.9, 0.9, -30.0);
  72. glColor3f(0,.9,0);
  73. glVertex3f(-0.9, 0.0, -30.0);
  74. glEnd();
  75. }
  76. {
  77. GLubyte *buffer = malloc(Width * Height * 4);
  78. /* read from user framebuffer */
  79. glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  80. CheckError();
  81. /* draw to window */
  82. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  83. glViewport(0, 0, Width, Height);
  84. /* Try to clear the window, but will overwrite:
  85. */
  86. glClearColor(0.8, 0.8, 0, 0.0);
  87. glClear(GL_COLOR_BUFFER_BIT);
  88. glWindowPos2iARB(30, 30);
  89. glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  90. free(buffer);
  91. }
  92. /* Bind normal framebuffer */
  93. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  94. glViewport(0, 0, Width, Height);
  95. if (0) {
  96. glBegin(GL_TRIANGLES);
  97. glColor3f(0,.7,0);
  98. glVertex3f( 0.5, -0.5, -30.0);
  99. glColor3f(0,0,.8);
  100. glVertex3f( 0.5, 0.5, -30.0);
  101. glColor3f(.9,0,0);
  102. glVertex3f(-0.5, 0.0, -30.0);
  103. glEnd();
  104. }
  105. if (doubleBuffer)
  106. glutSwapBuffers();
  107. else
  108. glFinish();
  109. CheckError();
  110. }
  111. static GLenum Args(int argc, char **argv)
  112. {
  113. GLint i;
  114. doubleBuffer = GL_FALSE;
  115. for (i = 1; i < argc; i++) {
  116. if (strcmp(argv[i], "-sb") == 0) {
  117. doubleBuffer = GL_FALSE;
  118. } else if (strcmp(argv[i], "-db") == 0) {
  119. doubleBuffer = GL_TRUE;
  120. } else {
  121. fprintf(stderr, "%s (Bad option).\n", argv[i]);
  122. return GL_FALSE;
  123. }
  124. }
  125. return GL_TRUE;
  126. }
  127. int
  128. main( int argc, char *argv[] )
  129. {
  130. GLenum type;
  131. glutInit(&argc, argv);
  132. if (Args(argc, argv) == GL_FALSE) {
  133. exit(1);
  134. }
  135. glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
  136. type = GLUT_RGB;
  137. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  138. glutInitDisplayMode(type);
  139. if (glutCreateWindow(argv[0]) == GL_FALSE) {
  140. exit(1);
  141. }
  142. glewInit();
  143. Init();
  144. glutReshapeFunc(Reshape);
  145. glutKeyboardFunc(Key);
  146. glutDisplayFunc(Draw);
  147. glutMainLoop();
  148. return 0;
  149. }