DepthStencilTarget.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * DepthStencilTarget.cpp
  3. */
  4. #include "Base.h"
  5. #include "DepthStencilTarget.h"
  6. namespace gameplay
  7. {
  8. static std::vector<DepthStencilTarget*> __depthStencilTargets;
  9. DepthStencilTarget::DepthStencilTarget(const char* id, Format format)
  10. : _id(id), _format(format), _depthTexture(NULL), _stencilBuffer(0)
  11. {
  12. }
  13. DepthStencilTarget::~DepthStencilTarget()
  14. {
  15. SAFE_RELEASE(_depthTexture);
  16. // Destroy GL resources.
  17. if (_stencilBuffer)
  18. {
  19. GL_ASSERT( glDeleteTextures(1, &_stencilBuffer) );
  20. }
  21. // Remove from vector.
  22. std::vector<DepthStencilTarget*>::iterator it = std::find(__depthStencilTargets.begin(), __depthStencilTargets.end(), this);
  23. if (it != __depthStencilTargets.end())
  24. {
  25. __depthStencilTargets.erase(it);
  26. }
  27. }
  28. DepthStencilTarget* DepthStencilTarget::create(const char* id, Format format, unsigned int width, unsigned int height)
  29. {
  30. // Create a backing texture buffer.
  31. Texture* depthTexture = Texture::create(Texture::DEPTH, width, height, NULL, false);
  32. if (depthTexture == NULL)
  33. {
  34. return NULL;
  35. }
  36. // Create stencil renderbuffer if format is DEPTH24_STENCIL8
  37. RenderBufferHandle stencilBuffer = 0;
  38. if (format == DEPTH24_STENCIL8)
  39. {
  40. // Backup the existing render buffer
  41. GLint currentRbo = 0;
  42. GL_ASSERT( glGetIntegerv(GL_RENDERBUFFER_BINDING, &currentRbo) );
  43. // Create the new render buffer
  44. GL_ASSERT( glGenRenderbuffers(1, &stencilBuffer) );
  45. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, stencilBuffer) );
  46. GL_ASSERT( glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height) );
  47. // Restore the old render buffer
  48. GL_ASSERT( glBindRenderbuffer(GL_RENDERBUFFER, currentRbo) );
  49. }
  50. // Create the depth stencil target
  51. DepthStencilTarget* depthStencilTarget = new DepthStencilTarget(id, format);
  52. depthStencilTarget->_depthTexture = depthTexture;
  53. depthStencilTarget->_stencilBuffer = stencilBuffer;
  54. // Add it to the cache
  55. __depthStencilTargets.push_back(depthStencilTarget);
  56. return depthStencilTarget;
  57. }
  58. DepthStencilTarget* DepthStencilTarget::getDepthStencilTarget(const char* id)
  59. {
  60. // Search the vector for a matching ID.
  61. std::vector<DepthStencilTarget*>::const_iterator it;
  62. for (it = __depthStencilTargets.begin(); it < __depthStencilTargets.end(); it++)
  63. {
  64. DepthStencilTarget* dst = *it;
  65. if (strcmp(id, dst->getID()) == 0)
  66. {
  67. return dst;
  68. }
  69. }
  70. return NULL;
  71. }
  72. const char* DepthStencilTarget::getID() const
  73. {
  74. return _id.c_str();
  75. }
  76. DepthStencilTarget::Format DepthStencilTarget::getFormat() const
  77. {
  78. return _format;
  79. }
  80. Texture* DepthStencilTarget::getTexture() const
  81. {
  82. return _depthTexture;
  83. }
  84. }