SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGrid.cpp
Go to the documentation of this file.
1 /**
2  * \file SLGrid.cpp
3  * \date July 2014
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #include <limits.h>
11 #include <SLGrid.h>
12 
13 //-----------------------------------------------------------------------------
14 //! SLGrid ctor with min & max corners and its resolutions
16  SLVec3f minXZ,
17  SLVec3f maxXZ,
18  SLuint resX,
19  SLuint resZ,
20  SLstring name,
21  SLMaterial* mat) : SLMesh(assetMgr, name)
22 {
23  assert(minXZ != maxXZ);
24  assert(resX > 0);
25  assert(resZ > 0);
26  assert(name != "");
27 
29  _min = minXZ;
30  _max = maxXZ;
31  _resX = resX;
32  _resZ = resZ;
33  _isVolume = false;
34 
35  buildMesh(mat);
36 }
37 //-----------------------------------------------------------------------------
38 //! SLGrid::buildMesh fills in the underlying arrays from the SLMesh object
40 {
41  deleteData();
42 
43  // Check max. allowed no. of verts
44  SLuint64 uIntNumV64 = (_resX + 1) * 2 + (_resZ + 1) * 2;
45  if (uIntNumV64 > UINT_MAX)
46  SL_EXIT_MSG("SLGrid supports max. 2^32 vertices.");
47 
48  // allocate vectors of SLMesh
49  P.clear();
50  P.resize((_resX + 1) * 2 + (_resZ + 1) * 2);
51 
52  if (uIntNumV64 < 65535)
53  {
54  I16.clear();
55  I16.resize(P.size());
56  }
57  else
58  {
59  I32.clear();
60  I32.resize(P.size());
61  }
62 
63  // Set one default material index
64  mat(material);
65 
66  // delta vector
67  SLVec3f d = _max - _min;
68  d.x /= (SLfloat)_resX;
69  d.z /= (SLfloat)_resZ;
70 
71  // Build vertex data
72  SLuint p = 0;
73  for (SLuint x = 0; x <= _resX; ++x)
74  {
75  P[p++].set(_min.x + x * d.x, 0, _min.z);
76  P[p++].set(_min.x + x * d.x, 0, _max.z);
77  }
78  for (SLuint z = 0; z <= _resZ; ++z)
79  {
80  P[p++].set(_min.x, 0, _min.z + z * d.z);
81  P[p++].set(_max.x, 0, _min.z + z * d.z);
82  }
83 
84  // Indexes
85  if (I16.size())
86  for (SLushort i = 0; i < P.size(); ++i)
87  I16[i] = i;
88  else
89  for (SLuint i = 0; i < P.size(); ++i)
90  I32[i] = i;
91 }
92 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
uint64_t SLuint64
Definition: SL.h:186
unsigned int SLuint
Definition: SL.h:171
unsigned short SLushort
Definition: SL.h:169
#define SL_EXIT_MSG(message)
Definition: SL.h:240
string SLstring
Definition: SL.h:158
@ PT_lines
Definition: SLGLEnums.h:32
Toplevel holder of the assets meshes, materials, textures and shaders.
void buildMesh(SLMaterial *mat)
SLGrid::buildMesh fills in the underlying arrays from the SLMesh object.
Definition: SLGrid.cpp:39
SLVec3f _max
max corner
Definition: SLGrid.h:44
SLGrid(SLAssetManager *assetMgr, SLVec3f minXZ, SLVec3f maxXZ, SLuint resX, SLuint resZ, SLstring name="grid mesh", SLMaterial *mat=nullptr)
ctor for rectangle w. min & max corner
Definition: SLGrid.cpp:15
SLVec3f _min
min corner
Definition: SLGrid.h:43
SLuint _resZ
resolution in z direction
Definition: SLGrid.h:46
SLuint _resX
resolution in x direction
Definition: SLGrid.h:45
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
An SLMesh object is a triangulated mesh, drawn with one draw call.
Definition: SLMesh.h:134
SLVuint I32
Vector of vertex indices 32 bit.
Definition: SLMesh.h:215
SLVushort I16
Vector of vertex indices 16 bit.
Definition: SLMesh.h:214
SLbool _isVolume
Flag for RT if mesh is a closed volume.
Definition: SLMesh.h:253
SLGLPrimitiveType _primitive
Primitive type (default triangles)
Definition: SLMesh.h:231
virtual void deleteData()
SLMesh::deleteData deletes all mesh data and vbo's.
Definition: SLMesh.cpp:88
SLVVec3f P
Vector for vertex positions layout (location = 0)
Definition: SLMesh.h:203
SLMaterial * mat() const
Definition: SLMesh.h:177
const SLstring & name() const
Definition: SLObject.h:38
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43