opengl使用FBO离屏渲染图片

  1. #include

  2. #include

  3. #include

  4. #include

  5.  
  6. #pragma comment(lib, "glew32.lib")

  7.  
  8. GLuint imageFBO;

  9. GLuint imageID;

  10. GLuint depthTextureID;

  11. void SetFrameBufferObject(int fbowidth, int fboheight)

  12. {

  13. // 创建FBO对象

  14. glGenFramebuffersEXT(1, &imageFBO);

  15. // 启用FBO

  16. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, imageFBO);

  17.  
  18. // 创建纹理

  19. glGenTextures(1, &imageID);

  20. // 启用纹理

  21. glBindTexture(GL_TEXTURE_2D, imageID);

  22. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, fbowidth, fboheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  27. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, imageID, 0);

  28.  
  29. //

  30. glGenRenderbuffersEXT(1, &depthTextureID);

  31. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthTextureID);

  32. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,GL_DEPTH_COMPONENT, fbowidth, fboheight);

  33. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthTextureID);

  34.  
  35. GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);

  36. if(status != GL_FRAMEBUFFER_COMPLETE_EXT)

  37. printf( "FBO Initialization Failed.");

  38.  
  39. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

  40. }

  41.  
  42. int IMAGE_WIDTH = 250;

  43. int IMAGE_HEIGHT = 250;

  44.  
  45. void GenImage()

  46. {

  47. glPushMatrix();

  48. glViewport(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);

  49. glMatrixMode(GL_PROJECTION);

  50. glLoadIdentity();

  51.  
  52. glMatrixMode(GL_MODELVIEW);

  53.  
  54. glShadeModel(GL_SMOOTH);

  55. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, imageFBO);

  56. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);

  57. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  58. glBegin(GL_POLYGON);

  59. glColor3f(1.0,0.0,0.0); glVertex3f(-1.0, -1.0, 0.0);

  60. glColor3f(0.0,1.0,0.0); glVertex3f(-1.0, 1.0, 0.0);

  61. glColor3f(0.0,0.0,1.0); glVertex3f(1.0, 1.0, 0.0);

  62. glColor3f(1.0,0.0,0.0); glVertex3f(1.0, -1.0, 0.0);

  63. glEnd();

  64.  
  65. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

  66. glPopMatrix();

  67. }

  68.  
  69. static GLuint texName;

  70.  
  71. void display(void)

  72. {

  73. // 离屏渲染纹理

  74. GenImage();

  75.  
  76. glViewport(0,0,250,250);

  77. glClear(GL_COLOR_BUFFER_BIT);

  78. glEnable(GL_TEXTURE_2D);

  79. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

  80. //切换屏幕

  81. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);

  82. // 启用离屏渲染的纹理影像

  83. glBindTexture(GL_TEXTURE_2D,imageID);

  84. // 创建Mipmap(多重细节层)

  85. glGenerateMipmapEXT(GL_TEXTURE_2D);

  86.  
  87. // 绘制纹理影像

  88. glBegin(GL_QUADS);

  89. glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0);

  90. glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, 0.0);

  91. glTexCoord2f(1.0, 1.0); glVertex3f(1.0, 1.0, 0.0);

  92. glTexCoord2f(1.0, 0.0); glVertex3f(1.0, -1.0, 0.0);

  93. glEnd();

  94.  
  95. glutSwapBuffers();

  96. glDisable(GL_TEXTURE_2D);

  97. }

  98.  
  99. void reshape(int w, int h)

  100. {

  101. glViewport(0, 0, (GLsizei) w, (GLsizei) h);

  102. glMatrixMode(GL_PROJECTION);

  103. glLoadIdentity();

  104. }

  105.  
  106. void keyboard (unsigned char key, int x, int y)

  107. {

  108. switch (key) {

  109. case 27:

  110. exit(0);

  111. break;

  112. default:

  113. break;

  114. }

  115. }

  116.  
  117. int main(int argc, char** argv)

  118. {

  119. glutInit(&argc, argv);

  120. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

  121. glutInitWindowSize(250, 250);

  122. glutInitWindowPosition(100, 100);

  123. glutCreateWindow(argv[0]);

  124. glewInit();

  125. glutDisplayFunc(display);

  126. glutReshapeFunc(reshape);

  127. glutKeyboardFunc(keyboard);

  128. // 设置创建虚拟屏幕

  129. SetFrameBufferObject(IMAGE_WIDTH, IMAGE_HEIGHT);

  130.  
  131. glutMainLoop();

  132. return 0;

  133. }

  134.  

你可能感兴趣的:(opengl,opengl)