Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ModelRenderer.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Home to the ModelRenderer class, an abstract base class that manages
20  * a per-frame list of submitted models, as well as simple helper
21  * classes.
22  */
23 
24 #ifndef INCLUDED_MODELRENDERER
25 #define INCLUDED_MODELRENDERER
26 
27 #include <boost/shared_ptr.hpp>
28 
29 #include "graphics/MeshManager.h"
31 #include "graphics/SColor.h"
32 #include "renderer/VertexArray.h"
33 
35 typedef shared_ptr<RenderModifier> RenderModifierPtr;
36 
38 typedef shared_ptr<LitRenderModifier> LitRenderModifierPtr;
39 
41 typedef shared_ptr<ModelVertexRenderer> ModelVertexRendererPtr;
42 
44 typedef shared_ptr<ModelRenderer> ModelRendererPtr;
45 
46 class CModel;
47 class CShaderDefines;
48 
50 {
52 public:
54  virtual ~CModelFilter() {}
55  virtual bool Filter(CModel* model) = 0;
56 };
57 
58 /**
59  * Class CModelRData: Render data that is maintained per CModel.
60  * ModelRenderer implementations may derive from this class to store
61  * per-CModel data.
62  *
63  * The main purpose of this class over CRenderData is to track which
64  * ModelRenderer the render data belongs to (via the key that is passed
65  * to the constructor). When a model changes the renderer it uses
66  * (e.g. via run-time modification of the renderpath configuration),
67  * the old ModelRenderer's render data is supposed to be replaced by
68  * the new data.
69  */
70 class CModelRData : public CRenderData
71 {
72 public:
73  CModelRData(const void* key) : m_Key(key) { }
74 
75  /**
76  * GetKey: Retrieve the key that can be used to identify the
77  * ModelRenderer that created this data.
78  *
79  * @return The opaque key that was passed to the constructor.
80  */
81  const void* GetKey() const { return m_Key; }
82 
83 private:
84  /// The key for model renderer identification
85  const void* m_Key;
86 };
87 
88 
89 /**
90  * Class ModelRenderer: Abstract base class for all model renders.
91  *
92  * A ModelRenderer manages a per-frame list of models.
93  *
94  * It is supposed to be derived in order to create new ways in which
95  * the per-frame list of models can be managed (for batching, for
96  * transparent rendering, etc.) or potentially for rarely used special
97  * effects.
98  *
99  * A typical ModelRenderer will delegate vertex transformation/setup
100  * to a ModelVertexRenderer.
101  * It will delegate fragment stage setup to a RenderModifier.
102  *
103  * For most purposes, you should use a BatchModelRenderer with
104  * specialized ModelVertexRenderer and RenderModifier implementations.
105  *
106  * It is suggested that a derived class implement the provided generic
107  * Render function, however in some cases it may be necessary to supply
108  * a Render function with a different prototype.
109  *
110  * ModelRenderer also contains a number of static helper functions
111  * for building vertex arrays.
112  */
114 {
115 public:
117  virtual ~ModelRenderer() { }
118 
119  /**
120  * Initialise global settings.
121  * Should be called before using the class.
122  */
123  static void Init();
124 
125  /**
126  * Submit: Submit a model for rendering this frame.
127  *
128  * preconditions : The model must not have been submitted to any
129  * ModelRenderer in this frame. Submit may only be called
130  * after EndFrame and before PrepareModels.
131  *
132  * @param model The model that will be added to the list of models
133  * submitted this frame.
134  */
135  virtual void Submit(CModel* model) = 0;
136 
137  /**
138  * PrepareModels: Calculate renderer data for all previously
139  * submitted models.
140  *
141  * Must be called before any rendering calls and after all models
142  * for this frame have been submitted.
143  */
144  virtual void PrepareModels() = 0;
145 
146  /**
147  * EndFrame: Remove all models from the list of submitted
148  * models.
149  */
150  virtual void EndFrame() = 0;
151 
152  /**
153  * Render: Render submitted models, using the given RenderModifier to setup
154  * the fragment stage.
155  *
156  * @note It is suggested that derived model renderers implement and use
157  * this Render functions. However, a highly specialized model renderer
158  * may need to "disable" this function and provide its own Render function
159  * with a different prototype.
160  *
161  * preconditions : PrepareModels must be called after all models have been
162  * submitted and before calling Render.
163  *
164  * @param modifier The RenderModifier that specifies the fragment stage.
165  * @param flags If flags is 0, all submitted models are rendered.
166  * If flags is non-zero, only models that contain flags in their
167  * CModel::GetFlags() are rendered.
168  */
169  virtual void Render(const RenderModifierPtr& modifier, const CShaderDefines& context, int flags) = 0;
170 
171  /**
172  * Filter: Filter submitted models, setting the passed flags on any models
173  * that pass the filter, and clearing them from models that fail.
174  *
175  * @param filter Filter to select a subset of models.
176  * @param passed Flags to be set/cleared.
177  * @param flags If non-zero, only models that contain @p flags
178  * have the filter test applied.
179  */
180  virtual void Filter(CModelFilter& filter, int passed, int flags = 0) = 0;
181 
182  /**
183  * CopyPositionAndNormals: Copy unanimated object-space vertices and
184  * normals into the given vertex array.
185  *
186  * @param mdef The underlying CModelDef that contains mesh data.
187  * @param Position Points to the array that will receive
188  * position vectors. The array behind the iterator
189  * must be large enough to hold model->GetModelDef()->GetNumVertices()
190  * vertices.
191  * @param Normal Points to the array that will receive normal vectors.
192  * The array behind the iterator must be as large as the Position array.
193  */
194  static void CopyPositionAndNormals(
195  const CModelDefPtr& mdef,
196  const VertexArrayIterator<CVector3D>& Position,
197  const VertexArrayIterator<CVector3D>& Normal);
198 
199  /**
200  * BuildPositionAndNormals: Build animated vertices and normals,
201  * transformed into world space.
202  *
203  * @param model The model that is to be transformed.
204  * @param Position Points to the array that will receive
205  * transformed position vectors. The array behind the iterator
206  * must be large enough to hold model->GetModelDef()->GetNumVertices()
207  * vertices. It must allow 16 bytes to be written to each element
208  * (i.e. provide 4 bytes of padding after each CVector3D).
209  * @param Normal Points to the array that will receive transformed
210  * normal vectors. The array behind the iterator must be as large as
211  * the Position array.
212  */
213  static void BuildPositionAndNormals(
214  CModel* model,
215  const VertexArrayIterator<CVector3D>& Position,
216  const VertexArrayIterator<CVector3D>& Normal);
217 
218  /**
219  * BuildColor4ub: Build lighting colors for the given model,
220  * based on previously calculated world space normals.
221  *
222  * @param model The model that is to be lit.
223  * @param Normal Array of the model's normal vectors, animated and
224  * transformed into world space.
225  * @param Color Points to the array that will receive the lit vertex color.
226  * The array behind the iterator must large enough to hold
227  * model->GetModelDef()->GetNumVertices() vertices.
228  */
229  static void BuildColor4ub(
230  CModel* model,
231  const VertexArrayIterator<CVector3D>& Normal,
232  const VertexArrayIterator<SColor4ub>& Color);
233 
234  /**
235  * BuildUV: Copy UV coordinates into the given vertex array.
236  *
237  * @param mdef The model def.
238  * @param UV Points to the array that will receive UV coordinates.
239  * The array behind the iterator must large enough to hold
240  * mdef->GetNumVertices() vertices.
241  */
242  static void BuildUV(
243  const CModelDefPtr& mdef,
244  const VertexArrayIterator<float[2]>& UV,
245  int UVset);
246 
247  /**
248  * BuildIndices: Create the indices array for the given CModelDef.
249  *
250  * @param mdef The model definition object.
251  * @param Indices The index array, must be able to hold
252  * mdef->GetNumFaces()*3 elements.
253  */
254  static void BuildIndices(
255  const CModelDefPtr& mdef,
256  const VertexArrayIterator<u16>& Indices);
257 
258  /**
259  * GenTangents: Generate tangents for the given CModelDef.
260  *
261  * @param mdef The model definition object.
262  * @param newVertices An out vector of the unindexed vertices with tangents added.
263  * The new vertices cannot be used with existing face index and must be welded/reindexed.
264  */
265  static void GenTangents(const CModelDefPtr& mdef, std::vector<float>& newVertices, bool gpuSkinning);
266 };
267 
268 
270 
271 /**
272  * Implementation of ModelRenderer that loads the appropriate shaders for
273  * rendering each model, and that batches by shader (and by mesh and texture).
274  *
275  * Note that the term "Shader" is somewhat misleading, as this handled
276  * fixed-function rendering using the same API as real GLSL/ARB shaders.
277  */
279 {
281 
282 public:
284  virtual ~ShaderModelRenderer();
285 
286  // Batching implementations
287  virtual void Submit(CModel* model);
288  virtual void PrepareModels();
289  virtual void EndFrame();
290  virtual void Render(const RenderModifierPtr& modifier, const CShaderDefines& context, int flags);
291  virtual void Filter(CModelFilter& filter, int passed, int flags);
292 
293 private:
295 };
296 
297 #endif // INCLUDED_MODELRENDERER
virtual ~ShaderModelRenderer()
virtual void EndFrame()
EndFrame: Remove all models from the list of submitted models.
static void BuildUV(const CModelDefPtr &mdef, const VertexArrayIterator< float[2]> &UV, int UVset)
BuildUV: Copy UV coordinates into the given vertex array.
virtual void Submit(CModel *model)=0
Submit: Submit a model for rendering this frame.
ShaderModelRendererInternals * m
static void CopyPositionAndNormals(const CModelDefPtr &mdef, const VertexArrayIterator< CVector3D > &Position, const VertexArrayIterator< CVector3D > &Normal)
CopyPositionAndNormals: Copy unanimated object-space vertices and normals into the given vertex array...
virtual ~CModelFilter()
Definition: ModelRenderer.h:54
virtual void Filter(CModelFilter &filter, int passed, int flags=0)=0
Filter: Filter submitted models, setting the passed flags on any models that pass the filter...
Internal data of the ShaderModelRenderer.
shared_ptr< RenderModifier > RenderModifierPtr
Definition: ModelRenderer.h:34
virtual void Render(const RenderModifierPtr &modifier, const CShaderDefines &context, int flags)
Render: Render submitted models, using the given RenderModifier to setup the fragment stage...
virtual void EndFrame()=0
EndFrame: Remove all models from the list of submitted models.
Implementation of ModelRenderer that loads the appropriate shaders for rendering each model...
virtual void PrepareModels()
PrepareModels: Calculate renderer data for all previously submitted models.
static void BuildColor4ub(CModel *model, const VertexArrayIterator< CVector3D > &Normal, const VertexArrayIterator< SColor4ub > &Color)
BuildColor4ub: Build lighting colors for the given model, based on previously calculated world space ...
virtual void Render(const RenderModifierPtr &modifier, const CShaderDefines &context, int flags)=0
Render: Render submitted models, using the given RenderModifier to setup the fragment stage...
virtual bool Filter(CModel *model)=0
static void Init()
Initialise global settings.
NONCOPYABLE(CModelFilter)
shared_ptr< ModelRenderer > ModelRendererPtr
Definition: ModelRenderer.h:43
CModelRData(const void *key)
Definition: ModelRenderer.h:73
static void BuildPositionAndNormals(CModel *model, const VertexArrayIterator< CVector3D > &Position, const VertexArrayIterator< CVector3D > &Normal)
BuildPositionAndNormals: Build animated vertices and normals, transformed into world space...
shared_ptr< ModelVertexRenderer > ModelVertexRendererPtr
Definition: ModelRenderer.h:40
Class ModelRenderer: Abstract base class for all model renders.
pthread_key_t key
Definition: wpthread.cpp:140
static void BuildIndices(const CModelDefPtr &mdef, const VertexArrayIterator< u16 > &Indices)
BuildIndices: Create the indices array for the given CModelDef.
virtual void Submit(CModel *model)
Submit: Submit a model for rendering this frame.
virtual ~ModelRenderer()
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
const void * m_Key
The key for model renderer identification.
Definition: ModelRenderer.h:85
Class RenderModifier: Some ModelRenderer implementations provide vertex management behaviour but allo...
static void GenTangents(const CModelDefPtr &mdef, std::vector< float > &newVertices, bool gpuSkinning)
GenTangents: Generate tangents for the given CModelDef.
static size_t model
Definition: x86_x64.cpp:211
Class CModelRData: Render data that is maintained per CModel.
Definition: ModelRenderer.h:70
shared_ptr< LitRenderModifier > LitRenderModifierPtr
Definition: ModelRenderer.h:37
Class ModelVertexRenderer: Normal ModelRenderer implementations delegate vertex array management and ...
virtual void PrepareModels()=0
PrepareModels: Calculate renderer data for all previously submitted models.
const void * GetKey() const
GetKey: Retrieve the key that can be used to identify the ModelRenderer that created this data...
Definition: ModelRenderer.h:81
Definition: Model.h:50
virtual void Filter(CModelFilter &filter, int passed, int flags)
Filter: Filter submitted models, setting the passed flags on any models that pass the filter...
ShaderModelRenderer(ModelVertexRendererPtr vertexrender)
Class LitRenderModifier: Abstract base class for RenderModifiers that apply a shadow map...
boost::shared_ptr< CModelDef > CModelDefPtr
Definition: MeshManager.h:27