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.

77 lines
2.2 KiB

4 years ago
  1. #include "FBO.h"
  2. FBO::FBO()
  3. {
  4. glGenFramebuffers(1, &mFBO);
  5. }
  6. void FBO::AttachColorBuffer(const char*bufferName, GLenum attachment, GLenum dataType, int width, int height)
  7. {
  8. GLuint colorBuffer;
  9. glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  10. glGenTextures(1, &colorBuffer);
  11. glBindTexture(GL_TEXTURE_2D, colorBuffer);
  12. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  13. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  14. glTexImage2D(GL_TEXTURE_2D, 0, dataType, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  15. glBindTexture(GL_TEXTURE_2D, 0);
  16. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0);
  17. mDrawBuffers.push(attachment);
  18. mBuffers.insert(std::pair<std::string,GLuint>(bufferName,colorBuffer));
  19. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  20. }
  21. void FBO::AttachDepthBuffer(const char*bufferName, int width, int height)
  22. {
  23. GLuint depthMap;
  24. glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  25. glGenTextures(1, &depthMap);
  26. glBindTexture(GL_TEXTURE_2D, depthMap);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  30. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  31. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  32. glBindTexture(GL_TEXTURE_2D, 0);
  33. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
  34. mBuffers.insert(std::pair<std::string, GLuint>(bufferName, depthMap));
  35. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  36. }
  37. void FBO::Finish()
  38. {
  39. int nCount = (int)mDrawBuffers.size();
  40. if (nCount>0)
  41. {
  42. GLenum *buffers = new GLenum[nCount];
  43. int i = 0;
  44. while (!mDrawBuffers.empty())
  45. {
  46. buffers[i++] = mDrawBuffers.top();
  47. mDrawBuffers.pop();
  48. }
  49. glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  50. glDrawBuffers(nCount, buffers);
  51. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  52. }
  53. }
  54. void FBO::Bind()
  55. {
  56. glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  57. }
  58. void FBO::Unbind()
  59. {
  60. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  61. }
  62. GLuint FBO::GetBuffer(const char*bufferName)
  63. {
  64. auto iter = mBuffers.find(bufferName);
  65. if (iter!=mBuffers.end())
  66. {
  67. return iter->second;
  68. }
  69. return 0;
  70. }