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.

71 lines
2.6 KiB

5 years ago
  1. #include "framebufferobject.h"
  2. FrameBufferObject::FrameBufferObject()
  3. {
  4. glGenFramebuffers(1, &mFrameBufferObject);
  5. }
  6. void FrameBufferObject::AttachColorBuffer(const char*bufferName, GLenum attachment, int width, int height) {
  7. //���Դ�������һ���ռ䣬���ÿ�
  8. GLuint colorBuffer;
  9. glGenTextures(1, &colorBuffer);
  10. glBindTexture(GL_TEXTURE_2D, colorBuffer);
  11. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  12. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  13. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  14. glBindTexture(GL_TEXTURE_2D, 0);
  15. //�����������Ŀռ���frame buffer ����
  16. glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject);
  17. glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, colorBuffer, 0);
  18. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  19. mDrawBuffers.push_back(attachment);
  20. mBuffers.insert(std::pair<std::string, GLuint>(bufferName, colorBuffer));
  21. }
  22. void FrameBufferObject::AttachDepthBuffer(const char*bufferName, int width, int height) {
  23. GLuint depthMap;
  24. glGenTextures(1, &depthMap);
  25. glBindTexture(GL_TEXTURE_2D, depthMap);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  30. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  31. glBindTexture(GL_TEXTURE_2D, 0);
  32. glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject);
  33. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
  34. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  35. mBuffers.insert(std::pair<std::string, GLuint>(bufferName, depthMap));
  36. }
  37. void FrameBufferObject::Finish() {
  38. int nCount = (int)mDrawBuffers.size();
  39. if (nCount > 0) {
  40. GLenum *buffers = new GLenum[nCount];
  41. int i = 0;
  42. while (i<nCount) {
  43. buffers[i] = mDrawBuffers[i];
  44. i++;
  45. }
  46. glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject);
  47. glDrawBuffers(nCount, buffers);
  48. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  49. }
  50. }
  51. void FrameBufferObject::Bind() {
  52. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mPrevFrameBuffer);//����ǰ��¼һ��buffer��Ϊ���󷵻�����buffer��׼��
  53. glBindFramebuffer(GL_FRAMEBUFFER, mFrameBufferObject);
  54. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  55. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  56. }
  57. void FrameBufferObject::Unbind() {
  58. glBindFramebuffer(GL_FRAMEBUFFER, mPrevFrameBuffer);
  59. }
  60. GLuint FrameBufferObject::GetBuffer(const char*bufferName) {
  61. auto iter = mBuffers.find(bufferName);
  62. if (iter != mBuffers.end()) {
  63. return iter->second;
  64. }
  65. return 0;
  66. }