1 /***********************************************************************
2     created:    Tue Feb 17 2009
3     author:     Paul D Turner
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2013 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #ifndef _CEGUIOgreRenderer_h_
28 #define _CEGUIOgreRenderer_h_
29 
30 #include "../../Renderer.h"
31 #include "../../Size.h"
32 #include "../../Vector.h"
33 #include "CEGUI/Config.h"
34 
35 #include <vector>
36 
37 #if (defined( __WIN32__ ) || defined( _WIN32 )) && !defined(CEGUI_STATIC)
38 #   ifdef CEGUIOGRERENDERER_EXPORTS
39 #       define OGRE_GUIRENDERER_API __declspec(dllexport)
40 #   else
41 #       define OGRE_GUIRENDERER_API __declspec(dllimport)
42 #   endif
43 #else
44 #   define OGRE_GUIRENDERER_API
45 #endif
46 
47 #if defined(_MSC_VER)
48 #   pragma warning(push)
49 #   pragma warning(disable : 4251)
50 #endif
51 
52 namespace Ogre
53 {
54 class Root;
55 class RenderSystem;
56 class RenderTarget;
57 #if (CEGUI_OGRE_VERSION < ((1 << 16) | (9 << 8) | 0))
58 class TexturePtr;
59 #else
60 template<typename T> class SharedPtr;
61 class Texture;
62 typedef SharedPtr<Texture> TexturePtr;
63 #endif
64 class Matrix4;
65 }
66 
67 #if (CEGUI_OGRE_VERSION >= (2 << 16))
68 // The new Ogre Compositor2 system has to be used since ViewPorts
69 // no longer have the required functionality
70 #define CEGUI_USE_OGRE_COMPOSITOR2
71 #endif
72 
73 #if (CEGUI_OGRE_VERSION >= ((2 << 16) | (1 << 8) | 0))
74 // The HLMS has to be used since fixed pipeline is disabled
75 #define CEGUI_USE_OGRE_HLMS
76 #include <OgreRenderOperation.h>
77 #include <OgreHlmsSamplerblock.h>
78 #endif
79 
80 // Start of CEGUI namespace section
81 namespace CEGUI
82 {
83 class OgreGeometryBuffer;
84 class OgreTexture;
85 class OgreResourceProvider;
86 class OgreImageCodec;
87 class OgreWindowTarget;
88 struct OgreRenderer_impl;
89 
90 //! CEGUI::Renderer implementation for the Ogre engine.
91 class OGRE_GUIRENDERER_API OgreRenderer : public Renderer
92 {
93 public:
94 #if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
95     /*!
96     \brief
97         Convenience function that creates all the Ogre specific objects and
98         then initialises the CEGUI system with them.
99 
100         The created Renderer will use the default Ogre rendering window as the
101         default output surface.
102 
103         This will create and initialise the following objects for you:
104         - CEGUI::OgreRenderer
105         - CEGUI::OgreResourceProvider
106         - CEGUI::OgreImageCodec
107         - CEGUI::System
108 
109     \param abi
110         This must be set to CEGUI_VERSION_ABI
111 
112     \return
113         Reference to the CEGUI::OgreRenderer object that was created.
114 
115     \note
116         For this to succeed you must have initialised Ogre to auto create the
117         rendering window.  If you have not done this, then you'll be wanting to
118         use the overload that takes an Ogre::RenderTarget as input.
119     */
120     static OgreRenderer& bootstrapSystem(const int abi = CEGUI_VERSION_ABI);
121 #endif
122     /*!
123     \brief
124         Convenience function that creates all the Ogre specific objects and
125         then initialises the CEGUI system with them.
126 
127         The create Renderer will use the specified Ogre::RenderTarget as the
128         default output surface.
129 
130         This will create and initialise the following objects for you:
131         - CEGUI::OgreRenderer
132         - CEGUI::OgreResourceProvider
133         - CEGUI::OgreImageCodec
134         - CEGUI::System
135 
136     \param target
137         Reference to the Ogre::RenderTarget object that the created OgreRenderer
138         will use as the default rendering root.
139 
140     \param abi
141         This must be set to CEGUI_VERSION_ABI
142 
143     \return
144         Reference to the CEGUI::OgreRenderer object that was created.
145     */
146     static OgreRenderer& bootstrapSystem(Ogre::RenderTarget& target,
147                                          const int abi = CEGUI_VERSION_ABI);
148 
149     /*!
150     \brief
151         Convenience function to cleanup the CEGUI system and related objects
152         that were created by calling the bootstrapSystem function.
153 
154         This function will destroy the following objects for you:
155         - CEGUI::System
156         - CEGUI::OgreImageCodec
157         - CEGUI::OgreResourceProvider
158         - CEGUI::OgreRenderer
159 
160     \note
161         If you did not initialise CEGUI by calling the bootstrapSystem function,
162         you should \e not call this, but rather delete any objects you created
163         manually.
164     */
165     static void destroySystem();
166 
167 #if !defined(CEGUI_USE_OGRE_COMPOSITOR2)
168     /*!
169     \brief
170         Create an OgreRenderer object that uses the default Ogre rendering
171         window as the default output surface.
172 
173     \note
174         For this to succeed you must have initialised Ogre to auto create the
175         rendering window.  If you have not done this, then you'll be wanting to
176         use the overload that takes an Ogre::RenderTarget as input.
177     */
178     static OgreRenderer& create(const int abi = CEGUI_VERSION_ABI);
179 #endif
180 
181     /*!
182     \brief
183         Create an OgreRenderer object that uses the specified Ogre::RenderTarget
184         as the default output surface.
185     */
186     static OgreRenderer& create(Ogre::RenderTarget& target,
187                                 const int abi = CEGUI_VERSION_ABI);
188 
189     //! destory an OgreRenderer object.
190     static void destroy(OgreRenderer& renderer);
191 
192     //! function to create a CEGUI::OgreResourceProvider object
193     static OgreResourceProvider& createOgreResourceProvider();
194 
195     //! function to destroy a CEGUI::OgreResourceProvider object
196     static void destroyOgreResourceProvider(OgreResourceProvider& rp);
197 
198     //! function to create a CEGUI::OgreImageCodec object.
199     static OgreImageCodec& createOgreImageCodec();
200 
201     //! function to destroy a CEGUI::OgreImageCodec object.
202     static void destroyOgreImageCodec(OgreImageCodec& ic);
203 
204     //! set whether CEGUI rendering will occur
205     void setRenderingEnabled(const bool enabled);
206 
207     //! return whether CEGUI rendering is enabled.
208     bool isRenderingEnabled() const;
209 
210     /*!
211     \brief
212         Create a CEGUI::Texture that wraps an existing Ogre texture.
213 
214     \param name
215         The name for tne new texture being created.
216 
217     \param tex
218         Ogre::TexturePtr for the texture that will be used by the created
219         CEGUI::Texture.
220 
221     \param take_ownership
222         - true if the created Texture will assume ownership of \a tex and
223         thus destroy \a tex when the Texture is destroyed.
224         - false if ownership of \a tex remains with the client app, and so
225         no attempt will be made to destroy \a tex when the Texture is destroyed.
226     */
227     Texture& createTexture(const String& name, Ogre::TexturePtr& tex,
228                            bool take_ownership = false);
229 
230     //! set the render states for the specified BlendMode.
231     void setupRenderingBlendMode(const BlendMode mode,
232                                  const bool force = false);
233 
234     /*!
235     \brief
236         Controls whether rendering done by CEGUI will be wrapped with calls to
237         Ogre::RenderSystem::_beginFrame and Ogre::RenderSystem::_endFrame.
238 
239         This defaults to enabled and is required when using the default hook
240         that automatically calls CEGUI::System::renderGUI via a frame listener.
241         If you disable this setting, the automated rendering will also be
242         disabled, which is useful when you wish to perform your own calls to the
243         CEGUI::System::renderGUI function (and is the sole purpose for this
244         setting).
245 
246     \param enabled
247         - true if _beginFrame and _endFrame should be called.
248         - false if _beginFrame and _endFrame should not be called (also disables
249           default renderGUI call).
250     */
251     void setFrameControlExecutionEnabled(const bool enabled);
252 
253     /*!
254     \brief
255         Returns whether rendering done by CEGUI will be wrapped with calls to
256         Ogre::RenderSystem::_beginFrame and Ogre::RenderSystem::_endFrame.
257 
258         This defaults to enabled and is required when using the default hook
259         that automatically calls CEGUI::System::renderGUI via a frame listener.
260         If you disable this setting, the automated rendering will also be
261         disabled, which is useful when you wish to perform your own calls to the
262         CEGUI::System::renderGUI function (and is the sole purpose for this
263         setting).
264 
265     \return
266         - true if _beginFrame and _endFrame will be called.
267         - false if _beginFrame and _endFrame will not be called (also means
268           default renderGUI call will not be made).
269     */
270     bool isFrameControlExecutionEnabled() const;
271 
272     /*!
273     \brief
274         Sets all the required render states needed for CEGUI rendering.
275 
276         This is a low-level function intended for certain advanced concepts; in
277         general it will not be required to call this function directly, since it
278         is called automatically by the system when rendering is done.
279     */
280     void initialiseRenderStateSettings();
281 
282     /*!
283     \brief
284         Sets the Ogre::RenderTarget that should be targetted by the default
285         GUIContext.
286 
287     \param target
288         Reference to the Ogre::RenderTarget object that is to be used as the
289         target for output from the default GUIContext.
290     */
291     void setDefaultRootRenderTarget(Ogre::RenderTarget& target);
292 
293     /*!
294     \brief
295         Returns whether the OgreRenderer is currently set to use shaders when
296         doing its rendering operations.
297 
298     \return
299         - true if rendering is being done using shaders.
300         - false if rendering is being done using the fixed function pipeline
301     */
302     bool isUsingShaders() const;
303 
304     /*!
305     \brief
306         Set whether the OgreRenderer shound use shaders when performing its
307         rendering operations.
308 
309     \param use_shaders
310         - true if rendering shaders should be used to perform rendering.
311         - false if the fixed function pipeline should be used to perform
312           rendering.
313 
314     \note
315         When compiled against Ogre 1.8 or later, shaders will automatically
316         be enabled if the render sytem does not support the fixed function
317         pipeline (such as with Direct3D 11). If you are compiling against
318         earlier releases of Ogre, you must explicity enable the use of
319         shaders by calling this function - if you are unsure what you'll be
320         compiling against, it is safe to call this function anyway.
321     */
322     void setUsingShaders(const bool use_shaders);
323 
324     /*!
325     \brief
326         Perform required operations to bind shaders (or unbind them) depending
327         on whether shader based rendering is currently enabled.
328 
329         Normally you would not need to call this function directly, although
330         that might be required if you are using RenderEffect objects that
331         also use shaders.
332     */
333     void bindShaders();
334 
335     /*!
336     \brief
337         Updates the shader constant parameters (i.e. uniforms).
338 
339         You do not normally need to call this function directly. Some may need
340         to call this function if you're doing non-standard or advanced things.
341     */
342     void updateShaderParams() const;
343 
344     //! Set the current world matrix to the given matrix.
345     void setWorldMatrix(const Ogre::Matrix4& m);
346     //! Set the current view matrix to the given matrix.
347     void setViewMatrix(const Ogre::Matrix4& m);
348     //! Set the current projection matrix to the given matrix.
349     void setProjectionMatrix(const Ogre::Matrix4& m);
350     //! return a const reference to the current world matrix.
351     const Ogre::Matrix4& getWorldMatrix() const;
352     //! return a const reference to the current view matrix.
353     const Ogre::Matrix4& getViewMatrix() const;
354     //! return a const reference to the current projection matrix.
355     const Ogre::Matrix4& getProjectionMatrix() const;
356 
357     /*!
358     \brief
359         Return a const reference to the final transformation matrix that
360         should be used when transforming geometry.
361 
362     \note
363         The projection used when building this matrix is correctly adjusted
364         according to whether the current Ogre::RenderTarget requires textures
365         to be flipped (i.e it does the right thing for both D3D and OpenGL).
366     */
367     const Ogre::Matrix4& getWorldViewProjMatrix() const;
368 
369     /*!
370     \brief
371         Returns if the texture coordinate system is vertically flipped or not. The original of a
372         texture coordinate system is typically located either at the the top-left or the bottom-left.
373         CEGUI, Direct3D and most rendering engines assume it to be on the top-left. OpenGL assumes it to
374         be at the bottom left.
375 
376         This function is intended to be used when generating geometry for rendering the TextureTarget
377         onto another surface. It is also intended to be used when trying to use a custom texture (RTT)
378         inside CEGUI using the Image class, in order to determine the Image coordinates correctly.
379 
380     \return
381         - true if flipping is required: the texture coordinate origin is at the bottom left
382         - false if flipping is not required: the texture coordinate origin is at the top left
383     */
isTexCoordSystemFlipped()384     bool isTexCoordSystemFlipped() const { return false; }
385 
386 #ifdef CEGUI_USE_OGRE_HLMS
387     Ogre::RenderTarget* getOgreRenderTarget();
388     const Ogre::HlmsSamplerblock* getHlmsSamplerblock();
389 #endif
390 
391     // implement CEGUI::Renderer interface
392     RenderTarget& getDefaultRenderTarget();
393     GeometryBuffer& createGeometryBuffer();
394     void destroyGeometryBuffer(const GeometryBuffer& buffer);
395     void destroyAllGeometryBuffers();
396     TextureTarget* createTextureTarget();
397     void destroyTextureTarget(TextureTarget* target);
398     void destroyAllTextureTargets();
399     Texture& createTexture(const String& name);
400     Texture& createTexture(const String& name,
401                            const String& filename,
402                            const String& resourceGroup);
403     Texture& createTexture(const String& name, const Sizef& size);
404     void destroyTexture(Texture& texture);
405     void destroyTexture(const String& name);
406     void destroyAllTextures();
407     Texture& getTexture(const String& name) const;
408     bool isTextureDefined(const String& name) const;
409     void beginRendering();
410     void endRendering();
411     void setDisplaySize(const Sizef& sz);
412     const Sizef& getDisplaySize() const;
413     const Vector2f& getDisplayDPI() const;
414     uint getMaxTextureSize() const;
415     const String& getIdentifierString() const;
416 
417 protected:
418     //! default constructor.
419     OgreRenderer();
420     //! constructor takin the Ogre::RenderTarget to use as the default root.
421     OgreRenderer(Ogre::RenderTarget& target);
422     //! destructor.
423     virtual ~OgreRenderer();
424 
425     //! checks Ogre initialisation.  throws exceptions if an issue is detected.
426     void checkOgreInitialised();
427     //! helper to throw exception if name is already used.
428     void throwIfNameExists(const String& name) const;
429     //! helper to safely log the creation of a named texture
430     static void logTextureCreation(const String& name);
431     //! helper to safely log the destruction of a named texture
432     static void logTextureDestruction(const String& name);
433 
434     //! common parts of constructor
435     void constructor_impl(Ogre::RenderTarget& target);
436     //! helper that creates and sets up shaders
437     void initialiseShaders();
438     //! helper to clean up shaders
439     void cleanupShaders();
440 
441     //! Pointer to the hidden implementation data
442     OgreRenderer_impl* d_pimpl;
443 };
444 
445 
446 } // End of  CEGUI namespace section
447 
448 #if defined(_MSC_VER)
449 #   pragma warning(pop)
450 #endif
451 
452 #endif  // end of guard _CEGUIOgreRenderer_h_
453