OgreRenderTarget.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __RenderTarget_H__
  25. #define __RenderTarget_H__
  26. #include "OgrePrerequisites.h"
  27. #include "OgreString.h"
  28. #include "OgrePixelFormat.h"
  29. #include "OgreViewport.h"
  30. /* Define the number of priority groups for the render system's render targets. */
  31. #ifndef OGRE_NUM_RENDERTARGET_GROUPS
  32. #define OGRE_NUM_RENDERTARGET_GROUPS 10
  33. #define OGRE_DEFAULT_RT_GROUP 4
  34. #define OGRE_REND_TO_TEX_RT_GROUP 2
  35. #endif
  36. namespace Ogre {
  37. /** \addtogroup Core
  38. * @{
  39. */
  40. /** \addtogroup RenderSystem
  41. * @{
  42. */
  43. /** A 'canvas' which can receive the results of a rendering
  44. operation.
  45. @remarks
  46. This abstract class defines a common root to all targets of rendering operations. A
  47. render target could be a window on a screen, or another
  48. offscreen surface like a texture or bump map etc.
  49. @author
  50. Steven Streeting
  51. @version
  52. 1.0
  53. */
  54. class _OgreExport RenderTarget
  55. {
  56. public:
  57. enum FrameBuffer
  58. {
  59. FB_FRONT,
  60. FB_BACK,
  61. FB_AUTO
  62. };
  63. RenderTarget();
  64. virtual ~RenderTarget();
  65. /// Retrieve target's name.
  66. virtual const String& getName(void) const;
  67. /// Retrieve information about the render target.
  68. virtual void getMetrics(unsigned int& width, unsigned int& height, unsigned int& colourDepth);
  69. virtual unsigned int getWidth(void) const;
  70. virtual unsigned int getHeight(void) const;
  71. virtual unsigned int getColourDepth(void) const;
  72. /** Tells the target to update it's contents.
  73. @remarks
  74. If OGRE is not running in an automatic rendering loop
  75. (started using Root::startRendering),
  76. the user of the library is responsible for asking each render
  77. target to refresh. This is the method used to do this. It automatically
  78. re-renders the contents of the target using whatever cameras have been
  79. pointed at it (using Camera::setRenderTarget).
  80. @par
  81. This allows OGRE to be used in multi-windowed utilities
  82. and for contents to be refreshed only when required, rather than
  83. constantly as with the automatic rendering loop.
  84. @param swapBuffers For targets that support double-buffering, if set
  85. to true, the target will immediately
  86. swap it's buffers after update. Otherwise, the buffers are
  87. not swapped, and you have to call swapBuffers yourself sometime
  88. later. You might want to do this on some rendersystems which
  89. pause for queued rendering commands to complete before accepting
  90. swap buffers calls - so you could do other CPU tasks whilst the
  91. queued commands complete. Or, you might do this if you want custom
  92. control over your windows, such as for externally created windows.
  93. */
  94. virtual void update(bool swapBuffers = true);
  95. /** Swaps the frame buffers to display the next frame.
  96. @remarks
  97. For targets that are double-buffered so that no
  98. 'in-progress' versions of the scene are displayed
  99. during rendering. Once rendering has completed (to
  100. an off-screen version of the window) the buffers
  101. are swapped to display the new frame.
  102. @param
  103. waitForVSync If true, the system waits for the
  104. next vertical blank period (when the CRT beam turns off
  105. as it travels from bottom-right to top-left at the
  106. end of the pass) before flipping. If false, flipping
  107. occurs no matter what the beam position. Waiting for
  108. a vertical blank can be slower (and limits the
  109. framerate to the monitor refresh rate) but results
  110. in a steadier image with no 'tearing' (a flicker
  111. resulting from flipping buffers when the beam is
  112. in the progress of drawing the last frame).
  113. */
  114. virtual void swapBuffers(bool waitForVSync = true)
  115. { (void)waitForVSync; }
  116. /** Adds a viewport to the rendering target.
  117. @remarks
  118. A viewport is the rectangle into which rendering output is sent. This method adds
  119. a viewport to the render target, rendering from the supplied camera. The
  120. rest of the parameters are only required if you wish to add more than one viewport
  121. to a single rendering target. Note that size information passed to this method is
  122. passed as a parametric, i.e. it is relative rather than absolute. This is to allow
  123. viewports to automatically resize along with the target.
  124. @param
  125. ZOrder The relative order of the viewport with others on the target (allows overlapping
  126. viewports i.e. picture-in-picture). Higher ZOrders are on top of lower ones. The actual number
  127. is irrelevant, only the relative ZOrder matters (you can leave gaps in the numbering)
  128. @param
  129. left The relative position of the left of the viewport on the target, as a value between 0 and 1.
  130. @param
  131. top The relative position of the top of the viewport on the target, as a value between 0 and 1.
  132. @param
  133. width The relative width of the viewport on the target, as a value between 0 and 1.
  134. @param
  135. height The relative height of the viewport on the target, as a value between 0 and 1.
  136. */
  137. virtual Viewport* addViewport(int ZOrder = 0, float left = 0.0f, float top = 0.0f ,
  138. float width = 1.0f, float height = 1.0f);
  139. /** Returns the number of viewports attached to this target.*/
  140. virtual unsigned short getNumViewports(void) const;
  141. /** Retrieves a pointer to the viewport with the given index. */
  142. virtual Viewport* getViewport(unsigned short index);
  143. /** Removes a viewport at a given ZOrder.
  144. */
  145. virtual void removeViewport(int ZOrder);
  146. /** Removes all viewports on this target.
  147. */
  148. virtual void removeAllViewports(void);
  149. /** Gets a custom (maybe platform-specific) attribute.
  150. @remarks
  151. This is a nasty way of satisfying any API's need to see platform-specific details.
  152. It horrid, but D3D needs this kind of info. At least it's abstracted.
  153. @param
  154. name The name of the attribute.
  155. @param
  156. pData Pointer to memory of the right kind of structure to receive the info.
  157. */
  158. virtual void getCustomAttribute(const String& name, void* pData);
  159. /** Sets the priority of this render target in relation to the others.
  160. @remarks
  161. This can be used in order to schedule render target updates. Lower
  162. priorities will be rendered first. Note that the priority must be set
  163. at the time the render target is attached to the render system, changes
  164. afterwards will not affect the ordering.
  165. */
  166. virtual void setPriority( uchar priority ) { mPriority = priority; }
  167. /** Gets the priority of a render target. */
  168. virtual uchar getPriority() const { return mPriority; }
  169. /** Used to retrieve or set the active state of the render target.
  170. */
  171. virtual bool isActive() const;
  172. /** Used to set the active state of the render target.
  173. */
  174. virtual void setActive( bool state );
  175. /** Sets whether this target should be automatically updated if Ogre's rendering
  176. loop or Root::_updateAllRenderTargets is being used.
  177. @remarks
  178. By default, if you use Ogre's own rendering loop (Root::startRendering)
  179. or call Root::_updateAllRenderTargets, all render targets are updated
  180. automatically. This method allows you to control that behaviour, if
  181. for example you have a render target which you only want to update periodically.
  182. @param autoupdate If true, the render target is updated during the automatic render
  183. loop or when Root::_updateAllRenderTargets is called. If false, the
  184. target is only updated when its update() method is called explicitly.
  185. */
  186. virtual void setAutoUpdated(bool autoupdate);
  187. /** Gets whether this target is automatically updated if Ogre's rendering
  188. loop or Root::_updateAllRenderTargets is being used.
  189. */
  190. virtual bool isAutoUpdated(void) const;
  191. /** Copies the current contents of the render target to a pixelbox.
  192. @remarks See suggestPixelFormat for a tip as to the best pixel format to
  193. extract into, although you can use whatever format you like and the
  194. results will be converted.
  195. */
  196. virtual void copyContentsToMemory(const PixelBox &dst, FrameBuffer buffer = FB_AUTO) = 0;
  197. /** Suggests a pixel format to use for extracting the data in this target,
  198. when calling copyContentsToMemory.
  199. */
  200. virtual PixelFormat suggestPixelFormat() const { return PF_BYTE_RGBA; }
  201. virtual bool requiresTextureFlipping() const = 0;
  202. /** Indicates whether this target is the primary window. The
  203. primary window is special in that it is destroyed when
  204. ogre is shut down, and cannot be destroyed directly.
  205. This is the case because it holds the context for vertex,
  206. index buffers and textures.
  207. */
  208. virtual bool isPrimary(void) const;
  209. /** Indicates whether on rendering, linear colour space is converted to
  210. sRGB gamma colour space. This is the exact opposite conversion of
  211. what is indicated by Texture::isHardwareGammaEnabled, and can only
  212. be enabled on creation of the render target. For render windows, it's
  213. enabled through the 'gamma' creation misc parameter. For textures,
  214. it is enabled through the hwGamma parameter to the create call.
  215. */
  216. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  217. /** Indicates whether multisampling is performed on rendering and at what level.
  218. */
  219. virtual uint getFSAA() const { return mFSAA; }
  220. /** Gets the FSAA hint (@see Root::createRenderWindow)
  221. */
  222. virtual const String& getFSAAHint() const { return mFSAAHint; }
  223. /** RenderSystem specific interface for a RenderTarget;
  224. this should be subclassed by RenderSystems.
  225. */
  226. class Impl
  227. {
  228. protected:
  229. ~Impl() { }
  230. };
  231. /** Get rendersystem specific interface for this RenderTarget.
  232. This is used by the RenderSystem to (un)bind this target,
  233. and to get specific information like surfaces
  234. and framebuffer objects.
  235. */
  236. virtual Impl *_getImpl();
  237. /** Method for manual management of rendering : fires 'preRenderTargetUpdate'
  238. and initialises statistics etc.
  239. @remarks
  240. <ul>
  241. <li>_beginUpdate resets statistics and fires 'preRenderTargetUpdate'.</li>
  242. <li>_updateViewport renders the given viewport (even if it is not autoupdated),
  243. fires preViewportUpdate and postViewportUpdate and manages statistics.</li>
  244. <li>_updateAutoUpdatedViewports renders only viewports that are auto updated,
  245. fires preViewportUpdate and postViewportUpdate and manages statistics.</li>
  246. <li>_endUpdate() ends statistics calculation and fires postRenderTargetUpdate.</li>
  247. </ul>
  248. you can use it like this for example :
  249. <pre>
  250. renderTarget->_beginUpdate();
  251. renderTarget->_updateViewport(1); // which is not auto updated
  252. renderTarget->_updateViewport(2); // which is not auto updated
  253. renderTarget->_updateAutoUpdatedViewports();
  254. renderTarget->_endUpdate();
  255. renderTarget->swapBuffers(true);
  256. </pre>
  257. Please note that in that case, the zorder may not work as you expect,
  258. since you are responsible for calling _updateViewport in the correct order.
  259. */
  260. virtual void _beginUpdate();
  261. /** Method for manual management of rendering - renders the given
  262. viewport (even if it is not autoupdated)
  263. @remarks
  264. This also fires preViewportUpdate and postViewportUpdate, and manages statistics.
  265. You should call it between _beginUpdate() and _endUpdate().
  266. @see _beginUpdate for more details.
  267. @param zorder The zorder of the viewport to update.
  268. @param updateStatistics Whether you want to update statistics or not.
  269. */
  270. virtual void _updateViewport(int zorder, bool updateStatistics = true);
  271. /** Method for manual management of rendering - renders the given viewport (even if it is not autoupdated)
  272. @remarks
  273. This also fires preViewportUpdate and postViewportUpdate, and manages statistics
  274. if needed. You should call it between _beginUpdate() and _endUpdate().
  275. @see _beginUpdate for more details.
  276. @param viewport The viewport you want to update, it must be bound to the rendertarget.
  277. @param updateStatistics Whether you want to update statistics or not.
  278. */
  279. virtual void _updateViewport(Viewport* viewport, bool updateStatistics = true);
  280. /** Method for manual management of rendering - renders only viewports that are auto updated
  281. @remarks
  282. This also fires preViewportUpdate and postViewportUpdate, and manages statistics.
  283. You should call it between _beginUpdate() and _endUpdate().
  284. See _beginUpdate for more details.
  285. @param updateStatistics Whether you want to update statistics or not.
  286. @see _beginUpdate()
  287. */
  288. virtual void _updateAutoUpdatedViewports(bool updateStatistics = true);
  289. /** Method for manual management of rendering - finishes statistics calculation
  290. and fires 'postRenderTargetUpdate'.
  291. @remarks
  292. You should call it after a _beginUpdate
  293. @see _beginUpdate for more details.
  294. */
  295. virtual void _endUpdate();
  296. protected:
  297. /// The name of this target.
  298. String mName;
  299. /// The priority of the render target.
  300. uchar mPriority;
  301. unsigned int mWidth;
  302. unsigned int mHeight;
  303. unsigned int mColourDepth;
  304. bool mIsDepthBuffered;
  305. bool mActive;
  306. bool mAutoUpdate;
  307. // Hardware sRGB gamma conversion done on write?
  308. bool mHwGamma;
  309. // FSAA performed?
  310. uint mFSAA;
  311. String mFSAAHint;
  312. typedef map<int, Viewport*>::type ViewportList;
  313. /// List of viewports, map on Z-order
  314. ViewportList mViewportList;
  315. /// Internal implementation of update()
  316. virtual void updateImpl();
  317. };
  318. /** @} */
  319. /** @} */
  320. } // Namespace
  321. #endif