SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLRectangle.cpp
Go to the documentation of this file.
1 /**
2  * \file SLRectangle.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 <SLRectangle.h>
11 #include <limits.h>
12 #include <utility>
13 
14 //-----------------------------------------------------------------------------
15 //! SLRectangle ctor with min & max corners and its resolutions
17  const SLVec2f& min,
18  const SLVec2f& max,
19  SLuint resX,
20  SLuint resY,
21  const SLstring& name,
22  SLMaterial* mat) : SLMesh(assetMgr, name)
23 {
24  assert(min != max);
25  assert(resX > 0);
26  assert(resY > 0);
27  assert(!name.empty());
28  _min = min;
29  _max = max;
30  _uv_min.set(0, 0);
31  _uv_max.set(1, 1);
32  _resX = resX;
33  _resY = resY;
34  _isVolume = true;
35  buildMesh(mat);
36 }
37 //-----------------------------------------------------------------------------
38 //! SLRectangle ctor with min & max corners and its resolutions
40  const SLVec2f& min,
41  const SLVec2f& max,
42  const SLVec2f& uv_min,
43  const SLVec2f& uv_max,
44  SLuint resX,
45  SLuint resY,
46  const SLstring& name,
47  SLMaterial* mat) : SLMesh(assetMgr, name)
48 {
49  assert(min != max);
50  assert(uv_min != uv_max);
51  assert(resX > 0);
52  assert(resY > 0);
53  assert(!name.empty());
54  _min = min;
55  _max = max;
56  _uv_min = uv_min;
57  _uv_max = uv_max;
58  _resX = resX;
59  _resY = resY;
60  _isVolume = true;
61  buildMesh(mat);
62 }
63 //-----------------------------------------------------------------------------
64 //! SLRectangle::buildMesh fills in the underlying arrays from the SLMesh object
66 {
67  deleteData();
68 
69  // Check max. allowed no. of vertices
70  SLuint uIntNumV64 = (_resX + 1) * (_resY + 1);
71  if (uIntNumV64 > UINT_MAX)
72  SL_EXIT_MSG("SLMesh supports max. 2^32 vertices.");
73 
74  // allocate vectors of SLMesh
75  P.clear();
76  P.resize((_resX + 1) * (_resY + 1));
77  N.clear();
78  N.resize(P.size());
79  UV[0].clear();
80  UV[0].resize(P.size());
81  UV[1].clear();
82 
83  if (uIntNumV64 < 65535)
84  {
85  I16.clear();
86  I16.resize(_resX * _resY * 2 * 3);
87  }
88  else
89  {
90  I32.clear();
91  I32.resize(_resX * _resY * 2 * 3);
92  }
93 
94  // Calculate normal from the first 3 corners
95  SLVec3f maxmin(_max.x, _min.y, 0);
96  SLVec3f minmax(_min.x, _max.y, 0);
97  SLVec3f e1(maxmin - _min);
98  SLVec3f e2(minmax - _min);
99  SLVec3f curN(e1 ^ e2);
100  curN.normalize();
101 
102  // Set one default material index
103  mat(material);
104 
105  // define delta vectors dX & dY and deltas for tex. coord. dU,dV
106  SLVec3f dX = e1 / (SLfloat)_resX;
107  SLVec3f dY = e2 / (SLfloat)_resY;
108  SLfloat dU = (_uv_max.x - _uv_min.x) / (SLfloat)_resX;
109  SLfloat dV = (_uv_max.y - _uv_min.y) / (SLfloat)_resY;
110 
111  // Build vertex data
112  SLuint i = 0;
113  for (SLuint y = 0; y <= _resY; ++y)
114  {
115  SLVec3f curV = _min;
116  SLVec2f curT = _uv_min;
117  curV += (SLfloat)y * dY;
118  curT.y += (SLfloat)y * dV;
119 
120  for (SLuint x = 0; x <= _resX; ++x, ++i)
121  {
122  P[i] = curV;
123  UV[0][i] = curT;
124  N[i] = curN;
125  curV += dX;
126  curT.x += dU;
127  }
128  }
129 
130  // Build face vertex indices
131  if (!I16.empty())
132  {
133  SLushort v = 0, i = 0; // index for vertices and indices
134  for (SLuint y = 0; y < _resY; ++y)
135  {
136  for (SLuint x = 0; x < _resX; ++x, ++v)
137  { // triangle 1
138  I16[i++] = v;
139  I16[i++] = v + (SLushort)_resX + 2;
140  I16[i++] = v + (SLushort)_resX + 1;
141 
142  // triangle 2
143  I16[i++] = v;
144  I16[i++] = v + 1;
145  I16[i++] = v + (SLushort)_resX + 2;
146  }
147  v++;
148  }
149  }
150  else
151  {
152  SLuint v = 0, i = 0; // index for vertices and indices
153  for (SLuint y = 0; y < _resY; ++y)
154  {
155  for (SLuint x = 0; x < _resX; ++x, ++v)
156  { // triangle 1
157  I32[i++] = v;
158  I32[i++] = v + _resX + 2;
159  I32[i++] = v + _resX + 1;
160 
161  // triangle 2
162  I32[i++] = v;
163  I32[i++] = v + 1;
164  I32[i++] = v + _resX + 2;
165  }
166  v++;
167  }
168  }
169 }
170 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
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
Toplevel holder of the assets meshes, materials, textures and shaders.
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
SLVVec3f N
Vector for vertex normals (opt.) layout (location = 1)
Definition: SLMesh.h:204
virtual void deleteData()
SLMesh::deleteData deletes all mesh data and vbo's.
Definition: SLMesh.cpp:88
SLVVec2f UV[2]
Array of 2 Vectors for tex. coords. (opt.) layout (location = 2)
Definition: SLMesh.h:205
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
SLuint _resX
resolution in x direction
Definition: SLRectangle.h:58
SLVec3f _max
max corner
Definition: SLRectangle.h:55
SLVec3f _min
min corner
Definition: SLRectangle.h:54
void buildMesh(SLMaterial *mat)
SLRectangle::buildMesh fills in the underlying arrays from the SLMesh object.
Definition: SLRectangle.cpp:65
SLuint _resY
resolution in y direction
Definition: SLRectangle.h:59
SLRectangle(SLAssetManager *assetMgr, const SLVec2f &min, const SLVec2f &max, SLuint resX, SLuint resY, const SLstring &name="rectangle mesh", SLMaterial *mat=nullptr)
ctor for rectangle w. min & max corner
Definition: SLRectangle.cpp:16
SLVec2f _uv_min
min corner tex.coord.
Definition: SLRectangle.h:56
SLVec2f _uv_max
max corner tex.coord.
Definition: SLRectangle.h:57
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30
void set(const T X, const T Y)
Definition: SLVec2.h:40
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
SLVec3 & normalize()
Definition: SLVec3.h:124