Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tri-fbo-tex.c 5.3KB

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