Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS08/src/orxonox/gui/OgreCEGUIRenderer.h @ 10241

Last change on this file since 10241 was 1846, checked in by rgrieder, 16 years ago

Merged Revisions 1831 - 1845 to tutorial. This is an update from the trunk.

  • Property svn:eol-style set to native
File size: 15.7 KB
Line 
1/************************************************************************
2    filename:   OgreCEGUIRenderer.h
3    created:    11/5/2004
4    author:             Paul D Turner
5
6    purpose:    Interface for main Ogre GUI renderer class
7*************************************************************************/
8/*************************************************************************
9    Crazy Eddie's GUI System (http://www.cegui.org.uk)
10    Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
11
12    This library is free software; you can redistribute it and/or
13    modify it under the terms of the GNU Lesser General Public
14    License as published by the Free Software Foundation; either
15    version 2.1 of the License, or (at your option) any later version.
16
17    This library is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    Lesser General Public License for more details.
21
22    You should have received a copy of the GNU Lesser General Public
23    License along with this library; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25*************************************************************************/
26/*************************************************************************
27    This file contains code that is specific to Ogre (http://www.ogre3d.org)
28*************************************************************************/
29#ifndef _OgreCEGUIRenderer_H__
30#define _OgreCEGUIRenderer_H__
31
32#include <CEGUIBase.h>
33#include <CEGUIRenderer.h>
34#include <CEGUITexture.h>
35
36#include <OgreRenderQueueListener.h>
37#include <OgreSceneManagerEnumerator.h>
38#include <OgreTextureUnitState.h>
39
40/** CHANGES MADE BY ORXONOX TO FIT DLL IMPORT/EXPORT **/
41#if (OGRE_PLATFORM == OGRE_PLATFORM_WIN32) && !(defined(ORXONOX_STATIC_BUILD) || defined(ORXONOX_NO_EXPORTS))
42#   ifdef ORXONOX_SHARED_BUILD
43#       define OGRE_GUIRENDERER_API __declspec(dllexport)
44#   else
45#       if defined(__MINGW32__)
46#           define OGRE_GUIRENDERER_API
47#       else
48#           define OGRE_GUIRENDERER_API __declspec(dllimport)
49#       endif
50#   endif
51#elif defined ( OGRE_GCC_VISIBILITY )
52#    define OGRE_GUIRENDERER_API  __attribute__ ((visibility("default")))
53#else
54#    define OGRE_GUIRENDERER_API
55#endif
56
57
58// Start of CEGUI namespace section
59namespace CEGUI
60{
61/*************************************************************************
62    Forward refs
63*************************************************************************/
64class OgreCEGUITexture;
65class OgreCEGUIRenderer;
66
67
68/*!
69\brief
70    RenderQueueListener based class used to hook into the ogre rendering system
71*/
72class _OgrePrivate CEGUIRQListener : public Ogre::RenderQueueListener
73{
74public:
75    CEGUIRQListener(OgreCEGUIRenderer* renderer, Ogre::uint8 queue_id, bool post_queue)
76    {
77        d_renderer              = renderer;
78        d_queue_id              = queue_id;
79        d_post_queue    = post_queue;
80    }
81
82    virtual ~CEGUIRQListener() {}
83
84    virtual void        renderQueueStarted(Ogre::uint8 id, const Ogre::String& invocation, bool& skipThisQueue);
85    virtual void        renderQueueEnded(Ogre::uint8 id, const Ogre::String& invocation, bool& repeatThisQueue);
86
87    // methods for adjusting target queue settings
88    void        setTargetRenderQueue(Ogre::uint8 queue_id)              {d_queue_id = queue_id;}
89    void        setPostRenderQueue(bool post_queue)             {d_post_queue = post_queue;}
90
91private:
92    /*************************************************************************
93        Implementation Data
94    *************************************************************************/
95    OgreCEGUIRenderer*                          d_renderer;             //!< CEGUI renderer object for Ogre.
96    Ogre::uint8 d_queue_id;             //!< ID of the queue that we are hooked into
97    bool                                                d_post_queue;   //!< true if we render after everything else in our queue.
98};
99
100
101/*!
102\brief
103    Renderer class to interface with Ogre engine.
104*/
105class OGRE_GUIRENDERER_API OgreCEGUIRenderer : public Renderer
106{
107public:
108    /*!
109    \brief
110        Constructor for renderer class that uses Ogre for rendering. Note that if
111        you use this option you must call setTargetSceneManager before rendering.
112
113    \param window
114        Pointer to an Ogre::RenderWindow object.
115
116    \param queue_id
117        Ogre::uint8 value that specifies where the GUI should appear in the ogre rendering output.
118
119    \param post_queue
120        set to true to have GUI rendered after render queue \a queue_id, or false to have the GUI rendered before render queue
121        \a queue_id.
122
123    \param max_quads
124        Obsolete.  Set to 0.
125
126    */
127    OgreCEGUIRenderer(Ogre::RenderWindow* window,
128        Ogre::uint8 queue_id = Ogre::RENDER_QUEUE_OVERLAY,
129        bool post_queue = false, uint max_quads = 0);
130
131
132    /*!
133    \brief
134        Constructor for renderer class that uses Ogre for rendering.
135
136    \param window
137        Pointer to an Ogre::RenderWindow object.
138
139    \param queue_id
140        Ogre::uint8 value that specifies where the GUI should appear in the ogre rendering output.
141
142    \param post_queue
143        set to true to have GUI rendered after render queue \a queue_id, or false to have the GUI rendered before render queue
144        \a queue_id.
145
146    \param max_quads
147        Obsolete.  Set to 0.
148
149    \param scene_manager
150        Pointer to an Ogre::SceneManager object that is to be used for GUI rendering.
151    */
152    OgreCEGUIRenderer(Ogre::RenderWindow* window, Ogre::uint8 queue_id, bool post_queue, uint max_quads, Ogre::SceneManager* scene_manager);
153
154
155    /*!
156    \brief
157        Destructor for Ogre renderer.
158    */
159    virtual ~OgreCEGUIRenderer(void);
160
161
162
163    // add's a quad to the list to be rendered
164    virtual     void    addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
165
166    // perform final rendering for all queued renderable quads.
167    virtual     void    doRender(void);
168
169    // clear the queue
170    virtual     void    clearRenderList(void);
171
172
173    /*!
174    \brief
175        Enable or disable the queueing of quads from this point on.
176
177        This only affects queueing.  If queueing is turned off, any calls to addQuad will cause the quad to be rendered directly.  Note that
178        disabling queueing will not cause currently queued quads to be rendered, nor is the queue cleared - at any time the queue can still
179        be drawn by calling doRender, and the list can be cleared by calling clearRenderList.  Re-enabling the queue causes subsequent quads
180        to be added as if queueing had never been disabled.
181
182    \param setting
183        true to enable queueing, or false to disable queueing (see notes above).
184
185    \return
186        Nothing
187    */
188    virtual void        setQueueingEnabled(bool setting)                {d_queueing = setting;}
189
190
191    // create an empty texture
192    virtual     Texture*        createTexture(void);
193
194    // create a texture and load it with the specified file.
195    virtual     Texture*        createTexture(const String& filename, const String& resourceGroup = "General");
196
197    // create a texture and set it to the specified size
198    virtual     Texture*        createTexture(float size);
199
200    // create an OGRE resource provider.
201    virtual ResourceProvider* createResourceProvider(void);
202
203    // destroy the given texture
204    virtual     void            destroyTexture(Texture* texture);
205
206    // destroy all textures still active
207    virtual void                destroyAllTextures(void);
208
209
210    /*!
211    \brief
212        Return whether queueing is enabled.
213
214    \return
215        true if queueing is enabled, false if queueing is disabled.
216    */
217    virtual bool        isQueueingEnabled(void) const   {return d_queueing;}
218
219
220    /*!
221    \brief
222    Return the current width of the display in pixels
223
224    \return
225    float value equal to the current width of the display in pixels.
226    */
227    virtual float       getWidth(void) const            {return d_display_area.getWidth();}
228
229
230    /*!
231    \brief
232    Return the current height of the display in pixels
233
234    \return
235    float value equal to the current height of the display in pixels.
236    */
237    virtual float       getHeight(void) const           {return d_display_area.getHeight();}
238
239
240    /*!
241    \brief
242    Return the size of the display in pixels
243
244    \return
245    Size object describing the dimensions of the current display.
246    */
247    virtual Size        getSize(void) const                     {return d_display_area.getSize();}
248
249
250    /*!
251    \brief
252    Return a Rect describing the screen
253
254    \return
255    A Rect object that describes the screen area.  Typically, the top-left values are always 0, and the size of the area described is
256    equal to the screen resolution.
257    */
258    virtual Rect        getRect(void) const                     {return d_display_area;}
259
260
261    /*!
262    \brief
263        Return the maximum texture size available
264
265    \return
266        Size of the maximum supported texture in pixels (textures are always assumed to be square)
267    */
268    virtual     uint    getMaxTextureSize(void) const           {return 2048;}          // TODO: Change to proper value
269
270
271    /*!
272    \brief
273        Return the horizontal display resolution dpi
274
275    \return
276        horizontal resolution of the display in dpi.
277    */
278    virtual     uint    getHorzScreenDPI(void) const    {return 96;}
279
280
281    /*!
282    \brief
283        Return the vertical display resolution dpi
284
285    \return
286        vertical resolution of the display in dpi.
287    */
288    virtual     uint    getVertScreenDPI(void) const    {return 96;}
289
290
291    /*!
292    \brief
293        Set the scene manager to be used for rendering the GUI.
294
295        The GUI system will be unhooked from the current scene manager and attached to what ever
296        is specified here.
297
298    \param scene_manager
299        Pointer to an Ogre::SceneManager object that is the new target Ogre::SceneManager to be
300        used for GUI rendering.
301
302    \return
303        Nothing.
304    */
305    void        setTargetSceneManager(Ogre::SceneManager* scene_manager);
306
307
308    /*!
309    \brief
310        Set the target render queue for GUI rendering.
311
312    \param queue_id
313        Ogre::uint8 value specifying the render queue that the GUI system should attach to.
314
315    \param post_queue
316        - true to specify that the GUI should render after everything else in render queue \a queue_id.
317        - false to specify the GUI should render before everything else in render queue \a queue_id.
318
319    \return
320        Nothing.
321    */
322    void        setTargetRenderQueue(Ogre::uint8 queue_id, bool post_queue);
323
324
325    /*!
326    \brief
327        Create a texture from an existing Ogre::TexturePtr object.
328
329    \note
330        If you want to use an Ogre::RenderTexture (for putting rendered output onto Gui elements or other
331        advanced techniques), you can get the Ogre::TexturePtr to be used by calling Ogre::TextureManager::getByName()
332        passing the name returned from Ogre::RenderTexture::getName() (and casting the result as necessary).
333
334    \param texture
335        pointer to an Ogre::TexturePtr object to be used as the basis for the new CEGUI::Texture
336
337    \return
338        Pointer to the newly created CEGUI::TexturePtr object.
339    */
340    Texture*    createTexture(Ogre::TexturePtr& texture);
341
342
343    /*!
344    \brief
345    Set the size of the display in pixels.
346
347    You do not have to call this method under normal operation as the system
348    will automatically extract the size from the current view port.
349
350    \note
351    This method will cause the EventDisplaySizeChanged event to fire if the
352    display size has changed.
353
354    \param sz
355    Size object describing the size of the display.
356
357    \return
358    Nothing.
359    */
360    void        setDisplaySize(const Size& sz);
361
362
363private:
364    /************************************************************************
365        Implementation Constants
366    ************************************************************************/
367    static const size_t    VERTEX_PER_QUAD;                                              //!< number of vertices per quad
368    static const size_t    VERTEX_PER_TRIANGLE;                                  //!< number of vertices for a triangle
369    static const size_t    VERTEXBUFFER_INITIAL_CAPACITY;                //!< initial capacity of the allocated vertex buffer
370    static const size_t    UNDERUSED_FRAME_THRESHOLD;            //!< number of frames to wait before shrinking buffer
371
372    /*************************************************************************
373        Implementation Structs & classes
374    *************************************************************************/
375    /*!
376    \brief
377        structure used for all vertices.
378    */
379    struct QuadVertex {
380        float x, y, z;                  //!< The position for the vertex.
381        Ogre::RGBA diffuse;             //!< colour of the vertex
382        float tu1, tv1;                 //!< texture coordinates
383    };
384
385    /*!
386    \brief
387        structure holding details about a quad to be drawn
388    */
389    struct QuadInfo
390    {
391        Ogre::TexturePtr                texture;
392        Rect                            position;
393        float                           z;
394        Rect                            texPosition;
395        uint32                  topLeftCol;
396        uint32                  topRightCol;
397        uint32                  bottomLeftCol;
398        uint32                  bottomRightCol;
399
400        QuadSplitMode           splitMode;
401
402        bool operator<(const QuadInfo& other) const
403        {
404            // this is intentionally reversed.
405            return z > other.z;
406        }
407    };
408
409
410    /*************************************************************************
411        Implementation Methods
412    *************************************************************************/
413    // setup states etc
414    void        initRenderStates(void);
415
416    // sort quads list according to texture
417    void        sortQuads(void);
418
419    // render a quad directly to the display
420    void        renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
421
422    // convert colour value to whatever the Ogre render system is expecting.
423    uint32    colourToOgre(const colour& col) const;
424
425    // perform main work of the constructor.  This does everything except the final hook into the render system.
426    void        constructor_impl(Ogre::RenderWindow* window, Ogre::uint8 queue_id, bool post_queue, uint max_quads);
427
428
429    /*************************************************************************
430        Implementation Data
431    *************************************************************************/
432    Rect                                d_display_area;
433
434    typedef std::multiset<QuadInfo>             QuadList;
435    QuadList d_quadlist;
436    bool         d_queueing;            //!< setting for queueing control.
437
438    // Ogre specific bits.
439    Ogre::Root*                                 d_ogre_root;            //!< pointer to the Ogre root object that we attach to
440    Ogre::RenderSystem*                 d_render_sys;           //!< Pointer to the render system for Ogre.
441    Ogre::uint8 d_queue_id;                     //!< ID of the queue that we are hooked into
442    Ogre::TexturePtr                    d_currTexture;          //!< currently set texture;
443    Ogre::RenderOperation               d_render_op;            //!< Ogre render operation we use to do our stuff.
444    Ogre::HardwareVertexBufferSharedPtr d_buffer;       //!< vertex buffer to queue sprite rendering
445    size_t d_underused_framecount;                  //!< Number of frames elapsed since buffer utilization was above half the capacity
446    Ogre::RenderOperation               d_direct_render_op;             //!< Renderop for cursor
447    Ogre::HardwareVertexBufferSharedPtr d_direct_buffer;        //!< Renderop for cursor
448    Ogre::SceneManager*                 d_sceneMngr;            //!< The scene manager we are hooked into.
449    Ogre::LayerBlendModeEx              d_colourBlendMode;      //!< Controls colour blending mode used.
450    Ogre::LayerBlendModeEx              d_alphaBlendMode;       //!< Controls alpha blending mode used.
451    Ogre::TextureUnitState::UVWAddressingMode d_uvwAddressMode;
452
453    CEGUIRQListener*                    d_ourlistener;
454    bool                                                d_post_queue;           //!< true if we render after everything else in our queue.
455    size_t                                              d_bufferPos;            //!< index into buffer where next vertex should be put.
456    bool                                                d_sorted;                       //!< true when data in quad list is sorted.
457    Point                                               d_texelOffset;          //!< Offset required for proper texel mapping.
458
459    std::list<OgreCEGUITexture*> d_texturelist;         //!< List used to track textures.
460};
461
462} // End of  CEGUI namespace section
463
464
465#endif  // end of guard _OgreCEGUIRenderer_H__
Note: See TracBrowser for help on using the repository browser.