frameBuffer.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "frameBuffer.h"
  2. namespace pika
  3. {
  4. namespace GL
  5. {
  6. void PikaFramebuffer::createFramebuffer(unsigned int w, unsigned int h, bool hasDepth)
  7. {
  8. this->w = w;
  9. this->h = h;
  10. glGenFramebuffers(1, &fbo);
  11. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  12. glGenTextures(1, &texture);
  13. glBindTexture(GL_TEXTURE_2D, texture);
  14. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  15. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  16. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  17. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
  18. //glDrawBuffer(GL_COLOR_ATTACHMENT0); //todo look into this function
  19. if (hasDepth)
  20. {
  21. glGenTextures(1, &depthTexture); //todo add depth stuff
  22. glBindTexture(GL_TEXTURE_2D, depthTexture);
  23. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  26. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
  27. }
  28. glBindTexture(GL_TEXTURE_2D, 0);
  29. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  30. }
  31. void PikaFramebuffer::deleteFramebuffer()
  32. {
  33. if (fbo)
  34. {
  35. glDeleteFramebuffers(1, &fbo);
  36. fbo = 0;
  37. }
  38. if (texture)
  39. {
  40. glDeleteTextures(1, &texture);
  41. texture = 0;
  42. }
  43. if (depthTexture)
  44. {
  45. glDeleteTextures(1, &depthTexture);
  46. depthTexture = 0;
  47. }
  48. }
  49. void PikaFramebuffer::resizeFramebuffer(unsigned int w, unsigned int h)
  50. {
  51. if (this->w != w || this->h != h)
  52. {
  53. this->w = w;
  54. this->h = h;
  55. glBindTexture(GL_TEXTURE_2D, texture);
  56. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  57. if (depthTexture)
  58. {
  59. glBindTexture(GL_TEXTURE_2D, depthTexture);
  60. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
  61. }
  62. glBindTexture(GL_TEXTURE_2D, 0);
  63. }
  64. }
  65. void PikaFramebuffer::clear()
  66. {
  67. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  68. //glClearColor(1, 1, 1, 0);
  69. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70. //glClearColor(0, 0, 0, 0);
  71. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  72. }
  73. }
  74. }