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.

82 lines
2.3 KiB

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