Clone of mesa.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 = 400, Height = 400;
  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. if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
  18. printf("GL_EXT_framebuffer_object not found!\n");
  19. exit(0);
  20. }
  21. glGenFramebuffersEXT(1, &MyFB);
  22. glGenRenderbuffersEXT(1, &MyRB);
  23. {
  24. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  25. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, MyRB);
  26. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT,
  27. GL_RENDERBUFFER_EXT, MyRB);
  28. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGB, Width, Height);
  29. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  30. }
  31. glClearColor(0.0, 0.0, 1.0, 0.0);
  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. return;
  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. glClearColor(0.5, 0.5, 1.0, 0.0);
  62. glClear(GL_COLOR_BUFFER_BIT);
  63. CheckError();
  64. glBegin(GL_TRIANGLES);
  65. glColor3f(0,0,.7);
  66. glVertex3f( 0.9, -0.9, -30.0);
  67. glColor3f(.8,0,0);
  68. glVertex3f( 0.9, 0.9, -30.0);
  69. glColor3f(0,.9,0);
  70. glVertex3f(-0.9, 0.0, -30.0);
  71. glEnd();
  72. {
  73. GLubyte *buffer = malloc(Width * Height * 4);
  74. /* read from user framebuffer */
  75. glReadPixels(0, 0, Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  76. CheckError();
  77. /* draw to window */
  78. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  79. /* Try to clear the window, but will overwrite:
  80. */
  81. glClearColor(0.8, 0.8, 0, 0.0);
  82. glClear(GL_COLOR_BUFFER_BIT);
  83. glWindowPos2iARB(30, 30);
  84. glDrawPixels(Width-60, Height-60, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  85. free(buffer);
  86. }
  87. /* Bind normal framebuffer */
  88. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  89. if (1) {
  90. glBegin(GL_TRIANGLES);
  91. glColor3f(0,.7,0);
  92. glVertex3f( 0.5, -0.5, -30.0);
  93. glColor3f(0,0,.8);
  94. glVertex3f( 0.5, 0.5, -30.0);
  95. glColor3f(.9,0,0);
  96. glVertex3f(-0.5, 0.0, -30.0);
  97. glEnd();
  98. }
  99. if (doubleBuffer)
  100. glutSwapBuffers();
  101. else
  102. glFinish();
  103. CheckError();
  104. }
  105. static GLenum Args(int argc, char **argv)
  106. {
  107. GLint i;
  108. doubleBuffer = GL_FALSE;
  109. for (i = 1; i < argc; i++) {
  110. if (strcmp(argv[i], "-sb") == 0) {
  111. doubleBuffer = GL_FALSE;
  112. } else if (strcmp(argv[i], "-db") == 0) {
  113. doubleBuffer = GL_TRUE;
  114. } else {
  115. fprintf(stderr, "%s (Bad option).\n", argv[i]);
  116. return GL_FALSE;
  117. }
  118. }
  119. return GL_TRUE;
  120. }
  121. int
  122. main( int argc, char *argv[] )
  123. {
  124. GLenum type;
  125. glutInit(&argc, argv);
  126. if (Args(argc, argv) == GL_FALSE) {
  127. exit(1);
  128. }
  129. glutInitWindowPosition(100, 0); glutInitWindowSize( Width, Height );
  130. type = GLUT_RGB;
  131. type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  132. glutInitDisplayMode(type);
  133. if (glutCreateWindow(argv[0]) == GL_FALSE) {
  134. exit(1);
  135. }
  136. glewInit();
  137. Init();
  138. glutReshapeFunc(Reshape);
  139. glutKeyboardFunc(Key);
  140. glutDisplayFunc(Draw);
  141. glutMainLoop();
  142. return 0;
  143. }