FrameBuffer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 FrameBuffer with a single RenderTarget of the specified width and height,
  23. * and adds the FrameBuffer to the list of available FrameBuffers.
  24. * You can additionally add a DepthStencilTarget using FrameBuffer::setDepthStencilTarget.
  25. *
  26. * @param id The ID of the new FrameBuffer. Uniqueness is recommended but not enforced.
  27. * @param width The width of the RenderTarget to be created and attached.
  28. * @param height The height of the RenderTarget to be created and attached.
  29. *
  30. * @return A newly created FrameBuffer.
  31. */
  32. static FrameBuffer* create(const char* id, unsigned int width, unsigned int height);
  33. /**
  34. * Get a named FrameBuffer from its ID.
  35. *
  36. * @param id The ID of the FrameBuffer to search for.
  37. *
  38. * @return The FrameBuffer with the specified ID, or NULL if one was not found.
  39. */
  40. static FrameBuffer* getFrameBuffer(const char* id);
  41. /**
  42. * Get the ID of this FrameBuffer.
  43. *
  44. * @return The ID of this FrameBuffer.
  45. */
  46. const char* getID() const;
  47. /**
  48. * Gets the width of the frame buffer.
  49. *
  50. * @return The width of the frame buffer.
  51. */
  52. unsigned int getWidth() const;
  53. /**
  54. * Gets the height of the frame buffer.
  55. *
  56. * @return The height of the frame buffer.
  57. */
  58. unsigned int getHeight() const;
  59. /**
  60. * Get the number of color attachments available on the current hardware.
  61. *
  62. * @return The number of color attachments available on the current hardware.
  63. */
  64. static unsigned int getMaxRenderTargets();
  65. /**
  66. * Set a RenderTarget on this FrameBuffer's color attachment at the specified index.
  67. *
  68. * @param target The RenderTarget to set.
  69. * @param index The index of the color attachment to set.
  70. */
  71. void setRenderTarget(RenderTarget* target, unsigned int index = 0);
  72. /**
  73. * Get the RenderTarget attached to the FrameBuffer's color attachment at the specified index.
  74. *
  75. * @param index The index of the color attachment to retrieve a RenderTarget from.
  76. *
  77. * @return The RenderTarget attached at the specified index.
  78. */
  79. RenderTarget* getRenderTarget(unsigned int index = 0) const;
  80. /**
  81. * Set this FrameBuffer's DepthStencilTarget.
  82. *
  83. * @param target The DepthStencilTarget to set on this FrameBuffer.
  84. */
  85. void setDepthStencilTarget(DepthStencilTarget* target);
  86. /**
  87. * Get this FrameBuffer's DepthStencilTarget.
  88. *
  89. * @return This FrameBuffer's DepthStencilTarget.
  90. */
  91. DepthStencilTarget* getDepthStencilTarget() const;
  92. /**
  93. * Binds this FrameBuffer for off-screen rendering.
  94. */
  95. void bind();
  96. /**
  97. * Binds the default FrameBuffer for rendering to the display.
  98. */
  99. static void bindDefault();
  100. private:
  101. /**
  102. * Constructor.
  103. */
  104. FrameBuffer(const char* id, unsigned int width, unsigned int height);
  105. /**
  106. * Destructor.
  107. */
  108. ~FrameBuffer();
  109. static void initialize();
  110. static bool isPowerOfTwo(unsigned int value);
  111. std::string _id;
  112. unsigned int _width;
  113. unsigned int _height;
  114. FrameBufferHandle _handle;
  115. RenderTarget** _renderTargets;
  116. DepthStencilTarget* _depthStencilTarget;
  117. };
  118. }
  119. #endif