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.

tri-fbo-tex.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Framebuffer object test */
  2. #include <GL/glew.h>
  3. #include <GL/glut.h>
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9. /* For debug */
  10. static int Win = 0;
  11. static int Width = 512, Height = 512;
  12. static GLenum TexTarget = GL_TEXTURE_2D;
  13. static int TexWidth = 512, TexHeight = 512;
  14. static GLuint MyFB;
  15. static GLuint TexObj;
  16. static GLboolean Anim = GL_FALSE;
  17. static GLfloat Rot = 0.0;
  18. static GLuint TextureLevel = 0; /* which texture level to render to */
  19. static GLenum TexIntFormat = GL_RGB; /* either GL_RGB or GL_RGBA */
  20. static void
  21. CheckError(int line)
  22. {
  23. GLenum err = glGetError();
  24. if (err) {
  25. printf("GL Error 0x%x at line %d\n", (int) err, line);
  26. }
  27. }
  28. static void
  29. Idle(void)
  30. {
  31. Rot = glutGet(GLUT_ELAPSED_TIME) * 0.1;
  32. glutPostRedisplay();
  33. }
  34. static void
  35. RenderTexture(void)
  36. {
  37. GLenum status;
  38. glMatrixMode(GL_PROJECTION);
  39. glLoadIdentity();
  40. glOrtho(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  41. glMatrixMode(GL_MODELVIEW);
  42. glLoadIdentity();
  43. glTranslatef(0.0, 0.0, -15.0);
  44. if (1) {
  45. /* draw to texture image */
  46. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  47. status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  48. if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  49. printf("Framebuffer incomplete!!!\n");
  50. }
  51. glViewport(0, 0, TexWidth, TexHeight);
  52. glClearColor(0.5, 0.5, 1.0, 0.0);
  53. glClear(GL_COLOR_BUFFER_BIT);
  54. CheckError(__LINE__);
  55. glBegin(GL_POLYGON);
  56. glColor3f(1, 0, 0);
  57. glVertex2f(-1, -1);
  58. glColor3f(0, 1, 0);
  59. glVertex2f(1, -1);
  60. glColor3f(0, 0, 1);
  61. glVertex2f(0, 1);
  62. glEnd();
  63. /* Bind normal framebuffer */
  64. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  65. }
  66. else {
  67. }
  68. CheckError(__LINE__);
  69. }
  70. static void
  71. Display(void)
  72. {
  73. float ar = (float) Width / (float) Height;
  74. RenderTexture();
  75. /* draw textured quad in the window */
  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, -7.0);
  82. glViewport(0, 0, Width, Height);
  83. glClearColor(0.25, 0.25, 0.25, 0);
  84. glClear(GL_COLOR_BUFFER_BIT);
  85. glPushMatrix();
  86. glRotatef(Rot, 0, 1, 0);
  87. glEnable(TexTarget);
  88. glBindTexture(TexTarget, TexObj);
  89. {
  90. glBegin(GL_POLYGON);
  91. glColor3f(0.25, 0.25, 0.25);
  92. glTexCoord2f(0, 0);
  93. glVertex2f(-1, -1);
  94. glTexCoord2f(1, 0);
  95. glVertex2f(1, -1);
  96. glColor3f(1.0, 1.0, 1.0);
  97. glTexCoord2f(1, 1);
  98. glVertex2f(1, 1);
  99. glTexCoord2f(0, 1);
  100. glVertex2f(-1, 1);
  101. glEnd();
  102. }
  103. glPopMatrix();
  104. glDisable(TexTarget);
  105. glutSwapBuffers();
  106. CheckError(__LINE__);
  107. }
  108. static void
  109. Reshape(int width, int height)
  110. {
  111. glViewport(0, 0, width, height);
  112. Width = width;
  113. Height = height;
  114. }
  115. static void
  116. CleanUp(void)
  117. {
  118. glDeleteFramebuffersEXT(1, &MyFB);
  119. glDeleteTextures(1, &TexObj);
  120. glutDestroyWindow(Win);
  121. exit(0);
  122. }
  123. static void
  124. Key(unsigned char key, int x, int y)
  125. {
  126. (void) x;
  127. (void) y;
  128. switch (key) {
  129. case 'a':
  130. Anim = !Anim;
  131. if (Anim)
  132. glutIdleFunc(Idle);
  133. else
  134. glutIdleFunc(NULL);
  135. break;
  136. case 's':
  137. Rot += 2.0;
  138. break;
  139. case 27:
  140. CleanUp();
  141. break;
  142. }
  143. glutPostRedisplay();
  144. }
  145. static void
  146. Init(int argc, char *argv[])
  147. {
  148. static const GLfloat mat[4] = { 1.0, 0.5, 0.5, 1.0 };
  149. GLint i;
  150. if (!glutExtensionSupported("GL_EXT_framebuffer_object")) {
  151. printf("GL_EXT_framebuffer_object not found!\n");
  152. exit(0);
  153. }
  154. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  155. /* Make texture object/image */
  156. glGenTextures(1, &TexObj);
  157. glBindTexture(TexTarget, TexObj);
  158. glTexParameteri(TexTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  159. glTexParameteri(TexTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  160. glTexParameteri(TexTarget, GL_TEXTURE_BASE_LEVEL, TextureLevel);
  161. glTexParameteri(TexTarget, GL_TEXTURE_MAX_LEVEL, TextureLevel);
  162. glTexImage2D(TexTarget, 0, TexIntFormat, TexWidth, TexHeight, 0,
  163. GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  164. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  165. /* gen framebuffer id, delete it, do some assertions, just for testing */
  166. glGenFramebuffersEXT(1, &MyFB);
  167. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, MyFB);
  168. assert(glIsFramebufferEXT(MyFB));
  169. CheckError(__LINE__);
  170. /* Render color to texture */
  171. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  172. TexTarget, TexObj, TextureLevel);
  173. CheckError(__LINE__);
  174. /* bind regular framebuffer */
  175. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  176. }
  177. int
  178. main(int argc, char *argv[])
  179. {
  180. glutInit(&argc, argv);
  181. glutInitWindowPosition(0, 0);
  182. glutInitWindowSize(Width, Height);
  183. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  184. Win = glutCreateWindow(argv[0]);
  185. glewInit();
  186. glutReshapeFunc(Reshape);
  187. glutKeyboardFunc(Key);
  188. glutDisplayFunc(Display);
  189. if (Anim)
  190. glutIdleFunc(Idle);
  191. Init(argc, argv);
  192. glutMainLoop();
  193. return 0;
  194. }