1 /***********************************************************************
2     created:    Mon Feb 9 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 _CEGUIDirect3D9GeometryBuffer_h_
28 #define _CEGUIDirect3D9GeometryBuffer_h_
29 
30 #include "../../GeometryBuffer.h"
31 #include "CEGUI/RendererModules/Direct3D9/Renderer.h"
32 #include "../../Rect.h"
33 #include "../../Quaternion.h"
34 #include <d3dx9.h>
35 #include <utility>
36 #include <vector>
37 
38 #if defined(_MSC_VER)
39 #   pragma warning(push)
40 #   pragma warning(disable : 4251)
41 #endif
42 
43 // Start of CEGUI namespace section
44 namespace CEGUI
45 {
46 class Direct3D9Texture;
47 
48 /*!
49 \brief
50     Direct3D9 based implementation of the GeometryBuffer interface.
51 */
52 class DIRECT3D9_GUIRENDERER_API Direct3D9GeometryBuffer : public GeometryBuffer
53 {
54 public:
55     //! Constructor
56     Direct3D9GeometryBuffer(Direct3D9Renderer& owner, LPDIRECT3DDEVICE9 device);
57 
58     //! return pointer to D3DXMATRIX used as world transform.
59     const D3DXMATRIX* getMatrix() const;
60 
61     // implementation of abstract members from GeometryBuffer
62     void draw() const;
63     void setTranslation(const Vector3f& t);
64     void setRotation(const Quaternion& r);
65     void setPivot(const Vector3f& p);
66     void setClippingRegion(const Rectf& region);
67     void appendVertex(const Vertex& vertex);
68     void appendGeometry(const Vertex* const vbuff, uint vertex_count);
69     void setActiveTexture(Texture* texture);
70     void reset();
71     Texture* getActiveTexture() const;
72     uint getVertexCount() const;
73     uint getBatchCount() const;
74     void setRenderEffect(RenderEffect* effect);
75     RenderEffect* getRenderEffect();
76     void setClippingActive(const bool active);
77     bool isClippingActive() const;
78 
79 protected:
80     //! perform batch management operations prior to adding new geometry.
81     void performBatchManagement();
82     //! update cached matrix
83     void updateMatrix() const;
84 
85     //! internal Vertex structure used for Direct3D based geometry.
86     struct D3DVertex
87     {
88         //! The transformed position for the vertex.
89         FLOAT x, y, z;
90         //! colour of the vertex.
91         DWORD diffuse;
92         //! texture coordinates.
93         float tu, tv;
94     };
95 
96     //! type to track info for per-texture sub batches of geometry
97     struct BatchInfo
98     {
99         LPDIRECT3DTEXTURE9 texture;
100         uint vertexCount;
101         bool clip;
102     };
103 
104     //! Owning Direct3D9Renderer object
105     Direct3D9Renderer& d_owner;
106     //! last texture that was set as active
107     Direct3D9Texture* d_activeTexture;
108     //! type of container that tracks BatchInfos.
109     typedef std::vector<BatchInfo> BatchList;
110     //! list of texture batches added to the geometry buffer
111     BatchList d_batches;
112     //! type of container used to queue the geometry
113     typedef std::vector<D3DVertex> VertexList;
114     //! container where added geometry is stored.
115     VertexList d_vertices;
116     //! rectangular clip region
117     Rectf d_clipRect;
118     //! whether clipping will be active for the current batch
119     bool d_clippingActive;
120     //! translation vector
121     Vector3f d_translation;
122     //! rotation vector
123     Quaternion d_rotation;
124     //! pivot point for rotation
125     Vector3f d_pivot;
126     //! RenderEffect that will be used by the GeometryBuffer
127     RenderEffect* d_effect;
128     //! The D3D Device
129     LPDIRECT3DDEVICE9 d_device;
130     //! model matrix cache
131     mutable D3DXMATRIX d_matrix;
132     //! true when d_matrix is valid and up to date
133     mutable bool d_matrixValid;
134 };
135 
136 } // End of  CEGUI namespace section
137 
138 #if defined(_MSC_VER)
139 #   pragma warning(pop)
140 #endif
141 
142 #endif  // end of guard _CEGUIDirect3D9GeometryBuffer_h_
143