FrameBuffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef FRAMEBUFFER_H_
  2. #define FRAMEBUFFER_H_
  3. #include "Base.h"
  4. #include "RenderTarget.h"
  5. #include "DepthStencilTarget.h"
  6. namespace gameplay
  7. {
  8. /**
  9. * Defines a video output off all graphics buffer containing a complete frame of data.
  10. * This consists of a RenderTarget and DepthStencilTarget holding the color, depth and
  11. * stencil data in the rendering frame.
  12. *
  13. * to change the default Game framebuffer call Game::setFrameBuffer(myFrameBuffer);
  14. * To restore back to the default call Game::setFrameBuffer(NULL).
  15. * This is useful for rendering shadows and other post-processing effects.
  16. */
  17. class FrameBuffer : public Ref
  18. {
  19. friend class Game;
  20. public:
  21. /**
  22. * Creates a new, empty FrameBuffer object.
  23. *
  24. * The new FrameBuffer does not have any render targets or a depth/stencil target and these
  25. * must be added before it can be used. The FrameBuffer is added to the list of available
  26. * FrameBuffers.
  27. *
  28. * @param id The ID of the new FrameBuffer. Uniqueness is recommended but not enforced.
  29. *
  30. * @return A newly created FrameBuffer.
  31. * @script{create}
  32. */
  33. static FrameBuffer* create(const char* id);
  34. /**
  35. * Creates a new FrameBuffer with a single RenderTarget of the specified width and height,
  36. * and adds the FrameBuffer to the list of available FrameBuffers.
  37. *
  38. * If width and height are non-zero a default RenderTarget of type RGBA will be created
  39. * and added to the FrameBuffer, with the same ID. The ID of the render target can be
  40. * changed later via RenderTarget::setId(const char*).
  41. *
  42. * You can additionally add a DepthStencilTarget using FrameBuffer::setDepthStencilTarget.
  43. *
  44. * @param id The ID of the new FrameBuffer. Uniqueness is recommended but not enforced.
  45. * @param width The width of the RenderTarget to be created and attached.
  46. * @param height The height of the RenderTarget to be created and attached.
  47. *
  48. * @return A newly created FrameBuffer.
  49. * @script{create}
  50. */
  51. static FrameBuffer* create(const char* id, unsigned int width, unsigned int height);
  52. /**
  53. * Get a named FrameBuffer from its ID.
  54. *
  55. * @param id The ID of the FrameBuffer to search for.
  56. *
  57. * @return The FrameBuffer with the specified ID, or NULL if one was not found.
  58. */
  59. static FrameBuffer* getFrameBuffer(const char* id);
  60. /**
  61. * Get the ID of this FrameBuffer.
  62. *
  63. * @return The ID of this FrameBuffer.
  64. */
  65. const char* getId() const;
  66. /**
  67. * Gets the width of the frame buffer.
  68. *
  69. * @return The width of the frame buffer.
  70. */
  71. unsigned int getWidth() const;
  72. /**
  73. * Gets the height of the frame buffer.
  74. *
  75. * @return The height of the frame buffer.
  76. */
  77. unsigned int getHeight() const;
  78. /**
  79. * Get the number of color attachments available on the current hardware.
  80. *
  81. * @return The number of color attachments available on the current hardware.
  82. */
  83. static unsigned int getMaxRenderTargets();
  84. /**
  85. * Set a RenderTarget on this FrameBuffer's color attachment at the specified index.
  86. *
  87. * @param target The RenderTarget to set.
  88. * @param index The index of the color attachment to set.
  89. */
  90. void setRenderTarget(RenderTarget* target, unsigned int index = 0);
  91. /**
  92. * Get the RenderTarget attached to the FrameBuffer's color attachment at the specified index.
  93. *
  94. * @param index The index of the color attachment to retrieve a RenderTarget from.
  95. *
  96. * @return The RenderTarget attached at the specified index.
  97. */
  98. RenderTarget* getRenderTarget(unsigned int index = 0) const;
  99. /**
  100. * Set this FrameBuffer's DepthStencilTarget.
  101. *
  102. * @param target The DepthStencilTarget to set on this FrameBuffer.
  103. */
  104. void setDepthStencilTarget(DepthStencilTarget* target);
  105. /**
  106. * Get this FrameBuffer's DepthStencilTarget.
  107. *
  108. * @return This FrameBuffer's DepthStencilTarget.
  109. */
  110. DepthStencilTarget* getDepthStencilTarget() const;
  111. /**
  112. * Binds this FrameBuffer for off-screen rendering.
  113. */
  114. void bind();
  115. /**
  116. * Binds the default FrameBuffer for rendering to the display.
  117. */
  118. static void bindDefault();
  119. private:
  120. /**
  121. * Constructor.
  122. */
  123. FrameBuffer(const char* id, unsigned int width, unsigned int height);
  124. /**
  125. * Destructor.
  126. */
  127. ~FrameBuffer();
  128. /**
  129. * Hidden copy assignment operator.
  130. */
  131. FrameBuffer& operator=(const FrameBuffer&);
  132. static void initialize();
  133. static bool isPowerOfTwo(unsigned int value);
  134. std::string _id;
  135. unsigned int _width;
  136. unsigned int _height;
  137. FrameBufferHandle _handle;
  138. RenderTarget** _renderTargets;
  139. DepthStencilTarget* _depthStencilTarget;
  140. };
  141. }
  142. #endif