SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLBox.cpp
Go to the documentation of this file.
1 /**
2  * \file SLBox.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 <SLBox.h>
11 #include <SLMaterial.h>
12 
13 //-----------------------------------------------------------------------------
14 //! SLBox::SLBox ctor with min. & max. coords. of the axis aligned box
16  SLfloat minx,
17  SLfloat miny,
18  SLfloat minz,
19  SLfloat maxx,
20  SLfloat maxy,
21  SLfloat maxz,
22  const SLstring& name,
23  SLMaterial* mat) : SLMesh(assetMgr, name)
24 {
25  _min.set(minx, miny, minz);
26  _max.set(maxx, maxy, maxz);
27  SLVec3f diag(_max - _min);
28  assert(diag.length() > FLT_EPSILON && "Box has no size!");
29  buildMesh(mat);
30 }
31 //-----------------------------------------------------------------------------
32 //! SLBox::SLBox ctor with min. & max. vectors of the axis aligned box
34  const SLVec3f& min,
35  const SLVec3f& max,
36  const SLstring& name,
37  SLMaterial* mat) : SLMesh(assetMgr, name)
38 {
39  _min.set(min);
40  _max.set(max);
41  SLVec3f diag(_max - _min);
42  assert(diag.length() > FLT_EPSILON && "Box has no size!");
43  buildMesh(mat);
44 }
45 //-----------------------------------------------------------------------------
46 //! SLBox::buildMesh fills in the underlying arrays from the SLMesh object
48 {
49  deleteData();
50 
51  // allocate new vectors of SLMesh
52  P.resize(24); // 6 sides with 4 vertices
53  I16.resize(12 * 3); // 6 sides with 2 triangles * 3 indices
54  N.resize(P.size());
55 
56  // Set one default material index
57  mat(material);
58 
59  SLuint p = 0, i = 0;
60 
61  // predefined normals
62  SLVec3f NX = SLVec3f(1, 0, 0);
63  SLVec3f NY = SLVec3f(0, 1, 0);
64  SLVec3f NZ = SLVec3f(0, 0, 1);
65 
66  // clang-format off
67  // towards +x
68  P[p].x=_max.x; P[p].y=_max.y; P[p].z=_min.z; N[p]= NX; p++; // 0
69  P[p].x=_max.x; P[p].y=_max.y; P[p].z=_max.z; N[p]= NX; p++; // 1
70  P[p].x=_max.x; P[p].y=_min.y; P[p].z=_max.z; N[p]= NX; p++; // 2
71  P[p].x=_max.x; P[p].y=_min.y; P[p].z=_min.z; N[p]= NX; p++; // 3
72  I16[i++] = 0; I16[i++] = 1; I16[i++] = 2;
73  I16[i++] = 0; I16[i++] = 2; I16[i++] = 3;
74 
75  // towards -z
76  P[p].x=_min.x; P[p].y=_max.y; P[p].z=_min.z; N[p]=-NZ; p++; // 4
77  P[p].x=_max.x; P[p].y=_max.y; P[p].z=_min.z; N[p]=-NZ; p++; // 5
78  P[p].x=_max.x; P[p].y=_min.y; P[p].z=_min.z; N[p]=-NZ; p++; // 6
79  P[p].x=_min.x; P[p].y=_min.y; P[p].z=_min.z; N[p]=-NZ; p++; // 7
80  I16[i++] = 4; I16[i++] = 5; I16[i++] = 6;
81  I16[i++] = 4; I16[i++] = 6; I16[i++] = 7;
82 
83  // towards -x
84  P[p].x=_min.x; P[p].y=_max.y; P[p].z=_max.z; N[p]=-NX; p++; // 8
85  P[p].x=_min.x; P[p].y=_max.y; P[p].z=_min.z; N[p]=-NX; p++; // 9
86  P[p].x=_min.x; P[p].y=_min.y; P[p].z=_min.z; N[p]=-NX; p++; // 10
87  P[p].x=_min.x; P[p].y=_min.y; P[p].z=_max.z; N[p]=-NX; p++; // 11
88  I16[i++] = 8; I16[i++] = 9; I16[i++] = 10;
89  I16[i++] = 8; I16[i++] = 10; I16[i++] = 11;
90 
91  // towards +z
92  P[p].x=_max.x; P[p].y=_max.y; P[p].z=_max.z; N[p]= NZ; p++; // 12
93  P[p].x=_min.x; P[p].y=_max.y; P[p].z=_max.z; N[p]= NZ; p++; // 13
94  P[p].x=_min.x; P[p].y=_min.y; P[p].z=_max.z; N[p]= NZ; p++; // 14
95  P[p].x=_max.x; P[p].y=_min.y; P[p].z=_max.z; N[p]= NZ; p++; // 15
96  I16[i++] = 12; I16[i++] = 13; I16[i++] = 14;
97  I16[i++] = 12; I16[i++] = 14; I16[i++] = 15;
98 
99  // towards +y
100  P[p].x=_max.x; P[p].y=_max.y; P[p].z=_max.z; N[p]= NY; p++; // 16
101  P[p].x=_max.x; P[p].y=_max.y; P[p].z=_min.z; N[p]= NY; p++; // 17
102  P[p].x=_min.x; P[p].y=_max.y; P[p].z=_min.z; N[p]= NY; p++; // 18
103  P[p].x=_min.x; P[p].y=_max.y; P[p].z=_max.z; N[p]= NY; p++; // 19
104  I16[i++] = 16; I16[i++] = 17; I16[i++] = 18;
105  I16[i++] = 16; I16[i++] = 18; I16[i++] = 19;
106 
107  // towards -y
108  P[p].x=_min.x; P[p].y=_min.y; P[p].z=_max.z; N[p]=-NY; p++; // 20
109  P[p].x=_min.x; P[p].y=_min.y; P[p].z=_min.z; N[p]=-NY; p++; // 21
110  P[p].x=_max.x; P[p].y=_min.y; P[p].z=_min.z; N[p]=-NY; p++; // 22
111  P[p].x=_max.x; P[p].y=_min.y; P[p].z=_max.z; N[p]=-NY; // 23
112  I16[i++] = 20; I16[i++] = 21; I16[i++] = 22;
113  I16[i++] = 20; I16[i++] = 22; I16[i] = 23;
114 
115  UV[0].resize(6ull * 4ull);
116  for (SLint i = 0; i < 6; i++)
117  {
118  UV[0][4 * i + 0] = SLVec2f(1, 1);
119  UV[0][4 * i + 1] = SLVec2f(0, 1);
120  UV[0][4 * i + 2] = SLVec2f(0, 0);
121  UV[0][4 * i + 3] = SLVec2f(1, 0);
122  }
123 }
124 //-----------------------------------------------------------------------------
125 
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
Toplevel holder of the assets meshes, materials, textures and shaders.
SLBox(SLAssetManager *assetMgr, SLfloat minx=0, SLfloat miny=0, SLfloat minz=0, SLfloat maxx=1, SLfloat maxy=1, SLfloat maxz=1, const SLstring &name="box mesh", SLMaterial *mat=nullptr)
SLBox::SLBox ctor with min. & max. coords. of the axis aligned box.
Definition: SLBox.cpp:15
SLVec3f _max
maximum corner
Definition: SLBox.h:52
void buildMesh(SLMaterial *mat)
SLBox::buildMesh fills in the underlying arrays from the SLMesh object.
Definition: SLBox.cpp:47
SLVec3f _min
minimal corner
Definition: SLBox.h:51
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
SLVushort I16
Vector of vertex indices 16 bit.
Definition: SLMesh.h:214
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
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T length() const
Definition: SLVec3.h:122
void set(const T X, const T Y, const T Z)
Definition: SLVec3.h:59
T z
Definition: SLVec3.h:43