1 /***********************************************************************
2     created:    Tue Feb 17 2009
3     author:     Paul D Turner
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2011 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 _CEGUIOgreGeometryBuffer_h_
28 #define _CEGUIOgreGeometryBuffer_h_
29 
30 #include "CEGUI/GeometryBuffer.h"
31 #include "CEGUI/RendererModules/Ogre/Renderer.h"
32 #include "CEGUI/Rect.h"
33 #include "CEGUI/Quaternion.h"
34 
35 #include <OgreMatrix4.h>
36 #include <OgreColourValue.h>
37 #include <OgreRenderOperation.h>
38 #include <OgreTexture.h>
39 
40 #include <utility>
41 #include <vector>
42 
43 #ifdef CEGUI_USE_OGRE_HLMS
44 #include <OgreRenderable.h>
45 #endif
46 
47 // Ogre forward refs
48 namespace Ogre
49 {
50 class RenderSystem;
51 }
52 
53 // Start of CEGUI namespace section
54 namespace CEGUI
55 {
56 //! Implementation of CEGUI::GeometryBuffer for the Ogre engine
57 class OGRE_GUIRENDERER_API OgreGeometryBuffer : public GeometryBuffer
58 {
59 public:
60     //! Constructor
61     OgreGeometryBuffer(OgreRenderer& owner, Ogre::RenderSystem& rs);
62     //! Destructor
63     virtual ~OgreGeometryBuffer();
64 
65     //! return the transformation matrix used for this buffer.
66     const Ogre::Matrix4& getMatrix() const;
67 
68     // implement CEGUI::GeometryBuffer interface.
69     virtual void draw() const;
70     virtual void setTranslation(const Vector3f& v);
71     virtual void setRotation(const Quaternion& r);
72     virtual void setPivot(const Vector3f& p);
73     virtual void setClippingRegion(const Rectf& region);
74     virtual void appendVertex(const Vertex& vertex);
75     virtual void appendGeometry(const Vertex* const vbuff, uint vertex_count);
76     virtual void setActiveTexture(Texture* texture);
77     virtual void reset();
78     virtual Texture* getActiveTexture() const;
79     virtual uint getVertexCount() const;
80     virtual uint getBatchCount() const;
81     virtual void setRenderEffect(RenderEffect* effect);
82     virtual RenderEffect* getRenderEffect();
83     void setClippingActive(const bool active);
84     bool isClippingActive() const;
85 
86 protected:
87     //! convert CEGUI::colour into something Ogre can use
88     Ogre::RGBA colourToOgre(const Colour& col) const;
89     //! update cached matrix
90     void updateMatrix() const;
91     //! Synchronise data in the hardware buffer with what's been added
92     void syncHardwareBuffer() const;
93     //! set up texture related states
94     void initialiseTextureStates() const;
95 
96     //! vertex structure used internally and also by Ogre.
97     struct OgreVertex
98     {
99         float x, y, z;
100         Ogre::RGBA diffuse;
101         float u, v;
102     };
103 
104     //! type to track info for per-texture sub batches of geometry
105     struct BatchInfo
106     {
107         Ogre::TexturePtr texture;
108         uint vertexCount;
109         bool clip;
110     };
111 
112     //! Renderer object that owns this GeometryBuffer
113     OgreRenderer& d_owner;
114     //! Ogre render system we're to use.
115     Ogre::RenderSystem& d_renderSystem;
116     //! Texture that is set as active
117     OgreTexture* d_activeTexture;
118     //! rectangular clip region
119     Rectf d_clipRect;
120     //! whether clipping will be active for the current batch
121     bool d_clippingActive;
122     //! translation vector
123     Vector3f d_translation;
124     //! rotation quaternion
125     Quaternion d_rotation;
126     //! pivot point for rotation
127     Vector3f d_pivot;
128     //! RenderEffect that will be used by the GeometryBuffer
129     RenderEffect* d_effect;
130     //! offset to be applied to all geometry
131     Vector2f d_texelOffset;
132     //! model matrix cache
133     mutable Ogre::Matrix4 d_matrix;
134     //! true when d_matrix is valid and up to date
135     mutable bool d_matrixValid;
136 #ifdef CEGUI_USE_OGRE_HLMS
137     //! Render operation for this buffer.
138     mutable Ogre::v1::RenderOperation d_renderOp;
139     //! H/W buffer where the vertices are rendered from.
140     mutable Ogre::v1::HardwareVertexBufferSharedPtr d_hwBuffer;
141 #else
142     //! Render operation for this buffer.
143     mutable Ogre::RenderOperation d_renderOp;
144     //! H/W buffer where the vertices are rendered from.
145     mutable Ogre::HardwareVertexBufferSharedPtr d_hwBuffer;
146 #endif
147     //! whether the h/w buffer is in sync with the added geometry
148     mutable bool d_sync;
149     //! type of container that tracks BatchInfos.
150     typedef std::vector<BatchInfo> BatchList;
151     //! list of texture batches added to the geometry buffer
152     BatchList d_batches;
153     //! type of container used to queue the geometry
154     typedef std::vector<OgreVertex> VertexList;
155     //! container where added geometry is stored.
156     VertexList d_vertices;
157 };
158 
159 
160 } // End of  CEGUI namespace section
161 
162 #endif  // end of guard _CEGUIOgreGeometryBuffer_h_
163