My Project
BlockModel.h
1 
2 #pragma once
3 
4 #include "ShapeAABB.h"
5 
6 namespace ParaEngine
7 {
8  class IBlockModelProvider;
9  class BlockModelManager;
10  class BlockTemplate;
11 
13  {
14  public:
15  float position[3]; //4byte
16  float normal[3]; //4byte
20  DWORD color; //4byte;
22  DWORD color2; //4byte;
23  float texcoord[2]; //8byte
24 
25  public:
27  :color(0xffffffff), color2(0xffffffff)
28  {
29  }
30 
31  inline void SetPosition(float x,float y,float z)
32  {
33  position[0] = x;
34  position[1] = y;
35  position[2] = z;
36  }
37 
38  inline void SetPosition(const Vector3& vPos)
39  {
40  position[0] = vPos.x;
41  position[1] = vPos.y;
42  position[2] = vPos.z;
43  }
44 
45  inline void SetPosition(const BlockVertexCompressed& r)
46  {
47  position[0] = r.position[0];
48  position[1] = r.position[1];
49  position[2] = r.position[2];
50  }
51 
52  void GetPosition(Vector3& vPos)
53  {
54  vPos.x = position[0];
55  vPos.y = position[1];
56  vPos.z = position[2];
57  }
58 
59  inline void SetNormal(float x,float y,float z)
60  {
61  normal[0] = x;
62  normal[1] = y;
63  normal[2] = z;
64  }
65 
66  inline void SetNormal(const Vector3& vNormal)
67  {
68  normal[0] = vNormal.x;
69  normal[1] = vNormal.y;
70  normal[2] = vNormal.z;
71  }
72 
73  void GetNormal(Vector3& vPos)
74  {
75  vPos.x = normal[0];
76  vPos.y = normal[1];
77  vPos.z = normal[2];
78  }
79 
80  Vector3 GetNormal()
81  {
82  return Vector3(normal[0], normal[1], normal[2]);
83  }
84 
85  inline void OffsetPosition(float dx,float dy,float dz)
86  {
87  position[0] += dx;
88  position[1] += dy;
89  position[2] += dz;
90  }
91 
92  inline void OffsetPosition(const Vector3& v)
93  {
94  position[0] += v.x;
95  position[1] += v.y;
96  position[2] += v.z;
97  }
98 
99  inline void SetHeightScale(float scale)
100  {
101  position[1] *= scale;
102  }
103 
104  inline void SetTexcoord(float u,float v)
105  {
106  texcoord[0] = u;
107  texcoord[1] = v;
108  }
109 
110  inline void SetTexcoord(const Vector2& v)
111  {
112  texcoord[0] = v.x;
113  texcoord[1] = v.y;
114  }
115 
116 
117  inline void GetTexcoord(float& u,float& v)
118  {
119  u = texcoord[0];
120  v = texcoord[1];
121  }
122 
124  inline void SetLightIntensity(float amount)
125  {
126  DWORD light = (DWORD)(amount * 255);
127  color = (color&0xff000000) | (light<<16 | light<<8 | light);
128  }
129 
131  inline void SetVertexLight(DWORD nBlockLight, DWORD nSunLight)
132  {
133  color = (color&0xff0000ff) | (nSunLight<<16 | nBlockLight<<8);
134  }
135 
137  inline void SetCategoryID(DWORD nBlockID)
138  {
139  color = (color&0xffffff00) | nBlockID;
140  }
141 
144  inline void SetShadow()
145  {
146  // color = (180<<24) | (color&0x00ffffff);
147  color = (((byte)(color>>24)-45)<<24) | (color&0x00ffffff);
148  }
149 
151  inline void SetColorStrength(byte strength)
152  {
153  color = (strength<<24) | (color&0x00ffffff);
154  }
155 
156 
157  inline void SetBlockColor(DWORD color)
158  {
159  color2 = color;
160  }
161  };
162 
164  {
165  public:
166  Vector3 position;
167  Vector2 texcoord;
168  };
169 
170 
171 
173  {
174  public:
175  friend class BlockModelManager;
176  enum EdgeVertexFlag
177  {
178  evf_none = 0,
179  //edge
180  evf_topFront = 0x001,
181  evf_topLeft = 0x002,
182  evf_topRight = 0x004,
183  evf_topBack = 0x008,
184  evf_LeftFront = 0x010,
185  evf_leftBack = 0x020,
186  evf_rightFont = 0x040,
187  evf_rightBack = 0x080,
188  evf_bottomFront = 0x100,
189  evf_bottomLeft = 0x200,
190  evf_bottomRight = 0x400,
191  evf_bottomBack = 0x800,
192 
193  //vertex
194  evf_xyz = 0x01000,
195  evf_xyNz = 0x02000,
196  evf_xNyz = 0x04000,
197  evf_xNyNz = 0x08000,
198 
199  evf_Nxyz = 0x10000,
200  evf_NxyNz = 0x20000,
201  evf_NxNyz = 0x40000,
202  evf_NxNyNz = 0x80000,
203  };
204 
205  BlockModel(int32_t texFaceNum=0);
206 
208  void CloneRenderData(const BlockModel& from_block);
209  void Clone(const BlockModel& from_block);
210 
212  void ClearVertices();
216  int AddVertex(const BlockVertexCompressed& vertex);
220  int AddVertex(const BlockModel& from_block, int32 nVertexIndex);
221  void CloneVertices(const BlockModel& from_block);
222 
224  std::vector<BlockVertexCompressed>& Vertices();
225 
226  void SetAOMask(uint32_t edges);
227 
228  void SetVerticalScale(EdgeVertexFlag vertexId,float scale);
229 
231  void TranslateVertices(float dx, float dy, float dz);
232 
235  inline void SetLightIntensity(uint32_t vertexId,float value)
236  {
237  m_Vertices[vertexId].SetLightIntensity(value);
238  }
239 
244  void SetVertexLight(uint32_t vertexId, uint8_t nBlockLight, uint8_t nSunLight)
245  {
246  m_Vertices[vertexId].SetVertexLight(nBlockLight, nSunLight);
247  }
248 
250  void SetCategoryID(DWORD nCategoryID);
251 
252 
254  void GetBoundingBoxVertices(Vector3 * pVertices, int* pNumber);
255  static void GetBoundingBoxVertices(CShapeAABB& aabb, Vector3 * pVertices, int* pNumber);
256 
258  bool IsCubeAABB();
259  void SetIsCubeAABB(bool bIsCube);
260 
262  void GetAABB(CShapeAABB* pOut) const;
263  const CShapeAABB& GetAABB() const;
264  void SetAABB(const Vector3& vMin, const Vector3& vMax);
265 
267  bool IsUseAmbientOcclusion(){return m_bUseAO;};
268  void SetUseAmbientOcclusion(bool bValue){m_bUseAO = bValue;};
269 
271  int GetFaceCount(){return m_nFaceCount;}
272  void SetFaceCount(int nFaceCount);
273  int IncrementFaceCount(int nDelta = 1);
275  int GetVerticesCount();
276  void ReserveVertices(int nReservedSize = 24);
277 
279  bool IsDisableFaceCulling(){return m_bDisableFaceCulling;};
280  void SetDisableFaceCulling(bool bDisabled){ m_bDisableFaceCulling = bDisabled; };
281 
283  bool IsUsingSelfLighting() {return m_bUseSelfLighting;}
284  void SetUsingSelfLighting(bool bEnable) {m_bUseSelfLighting = bEnable;}
285 
287  bool IsUniformLighting() const { return m_bUniformLighting; }
288  void SetUniformLighting(bool val) { m_bUniformLighting = val; }
289 
291  void Transform(const Matrix4& mat);
293  void Transform(const Vector3& vOffset, float fScaling);
294 
296  void RemoveFace(int nFirstIndex);
297 
299  BlockVertexCompressed* GetVertices();
300  const BlockVertexCompressed* GetVerticesConst() const;
301 
303  int GetTextureIndex() const;
304  void SetTextureIndex(int val);
306  void SetColor(DWORD color);
307 
309  void LoadModel(BlockTemplate* pTemplate, const std::string& filename, const Matrix4& mat, int nTextureIndex = 0);
310  void LoadModelByTexture(int32_t texFaceNum);
311  void LoadCubeModel();
313  void LoadModel(const std::string& sModelName);
317  void SetVertexShadow(int nIndex, unsigned char nShadowLevel);
318  void SetVertexShadowFromAOFlags(int nIndex, int nCubeIndex, uint32 aoFlags);
319  void SetVertexColor(int nIndex, DWORD color);
320 
322  unsigned char CalculateCubeVertexAOShadowLevel(int nIndex, uint32 aoFlags);
323 
324  void SetVertexHeightScale(int nIndex, float scale);
325 
326  //
327  // LT ----- RT
328  // | |
329  // | |
330  // LB ----- RB
331  //vertex indices
332  //top face
333  static const int32_t g_topLB = 0;
334  static const int32_t g_topLT = 1;
335  static const int32_t g_topRT = 2;
336  static const int32_t g_topRB = 3;
337 
338  //front face
339  static const int32_t g_frtLB = 4;
340  static const int32_t g_frtLT = 5;
341  static const int32_t g_frtRT = 6;
342  static const int32_t g_frtRB = 7;
343 
344  //bottom face
345  static const int32_t g_btmLB = 8;
346  static const int32_t g_btmLT = 9;
347  static const int32_t g_btmRT = 10;
348  static const int32_t g_btmRB = 11;
349 
350  //left face
351  static const int32_t g_leftLB = 12;
352  static const int32_t g_leftLT = 13;
353  static const int32_t g_leftRT = 14;
354  static const int32_t g_leftRB = 15;
355 
356  //right face
357  static const int32_t g_rightLB = 16;
358  static const int32_t g_rightLT = 17;
359  static const int32_t g_rightRT = 18;
360  static const int32_t g_rightRB = 19;
361 
362  //back face
363  static const int32_t g_bkLB = 20;
364  static const int32_t g_bkLT = 21;
365  static const int32_t g_bkRT = 22;
366  static const int32_t g_bkRB = 23;
367 
368  private:
370  std::vector<BlockVertexCompressed> m_Vertices;
371 
373  bool m_bUseAO;
375  bool m_bDisableFaceCulling;
376 
378  bool m_bUseSelfLighting;
380  bool m_bUniformLighting;
381 
383  bool m_bIsCubeAABB;
384 
386  int m_nFaceCount;
388  int m_nTextureIndex;
389 
391  CShapeAABB m_shapeAABB;
392 
393  friend class IBlockModelProvider;
394  };
395 }
396 
int GetFaceCount()
get face count.
Definition: BlockModel.h:271
void SetLightIntensity(float amount)
only used for fixed function rendering
Definition: BlockModel.h:124
void SetCategoryID(DWORD nBlockID)
used for shader rendering
Definition: BlockModel.h:137
Definition: BlockModel.h:163
void SetVertexLight(uint32_t vertexId, uint8_t nBlockLight, uint8_t nSunLight)
Definition: BlockModel.h:244
void SetShadow()
make it darker, each time this function is called.
Definition: BlockModel.h:144
DWORD color
for fixed function: ao_shadow, max_light, max_light, max_light for shader: ao_shadow, sun_light, block_light, block_id
Definition: BlockModel.h:20
bool IsUseAmbientOcclusion()
whether use ambient occlusion.
Definition: BlockModel.h:267
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: BlockModel.h:12
Definition: BlockModel.h:172
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
void SetVertexLight(DWORD nBlockLight, DWORD nSunLight)
used for shader rendering
Definition: BlockModel.h:131
Definition: BlockModelManager.h:11
AABB-related code.
Definition: ShapeAABB.h:11
bool IsUniformLighting() const
whether all vertices have same lighting value.
Definition: BlockModel.h:287
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Definition: enum_maker.hpp:46
void SetColorStrength(byte strength)
similar to SetShadow, except that it uses a user provided value [0,255]
Definition: BlockModel.h:151
bool IsUsingSelfLighting()
whether to disable block and sun lighting when rendering this block.
Definition: BlockModel.h:283
block template base class.
Definition: BlockTemplate.h:15
bool IsDisableFaceCulling()
Is disable face culling.
Definition: BlockModel.h:279
interface class for filtering block models.
Definition: BlockModelProvider.h:12
void SetLightIntensity(uint32_t vertexId, float value)
only used for fixed function pipeline.
Definition: BlockModel.h:235
DWORD color2
color of the block, default to white.
Definition: BlockModel.h:22