SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLPolygon.cpp
Go to the documentation of this file.
1 /**
2  * \file SLPolygon.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 <SLPolygon.h>
11 
12 //-----------------------------------------------------------------------------
13 //! SLPolygon ctor with corner points vector
15  const SLVVec3f& corners,
16  const SLstring& name,
17  SLMaterial* mat)
18  : SLMesh(assetMgr, name)
19 {
20  assert(corners.size() > 2);
21  _corners = corners;
22  buildMesh(mat);
23 }
24 //-----------------------------------------------------------------------------
25 //! SLPolygon ctor with corner points and its texture coords vector
27  const SLVVec3f& corners,
28  const SLVVec2f& texCoords,
29  const SLstring& name,
30  SLMaterial* mat) : SLMesh(assetMgr, name)
31 {
32  assert(corners.size() > 2 && texCoords.size() == corners.size());
33  _corners = corners;
34  _uv1 = texCoords;
35  buildMesh(mat);
36 }
37 //-----------------------------------------------------------------------------
38 //! SLPolygon ctor for centered light rectangle in x/y-plane w. N=-z
40  SLfloat width,
41  SLfloat height,
42  const SLstring& name,
43  SLMaterial* mat) : SLMesh(assetMgr, name)
44 {
45  assert(width > 0 && height > 0);
46  SLfloat hw = width * 0.5f;
47  SLfloat hh = height * 0.5f;
48  _corners.push_back(SLVec3f(hw, hh));
49  _corners.push_back(SLVec3f(hw, -hh));
50  _corners.push_back(SLVec3f(-hw, -hh));
51  _corners.push_back(SLVec3f(-hw, hh));
52  buildMesh(mat);
53 }
54 //-----------------------------------------------------------------------------
55 //! SLPolygon::buildMesh fills in the underlying arrays from the SLMesh object
57 {
58  _isVolume = false;
59 
60  deleteData();
61 
62  // Check max. allowed no. of verts
63  if (_corners.size() >= 65535)
64  SL_EXIT_MSG("SLPolygon::buildMesh: NO. of vertices exceeds the maximum (65535) allowed.");
65 
66  // allocate vectors of SLMesh
67  P.clear();
68  P.resize(_corners.size());
69  N.clear();
70  N.resize(P.size());
71  if (!_uv1.empty())
72  {
73  UV[0].clear();
74  UV[0].resize(P.size());
75  }
76  UV[1].clear();
77  I16.clear();
78  I16.resize((P.size() - 2) * 3);
79 
80  // Calculate normal from the first 3 corners
81  SLVec3f v1(_corners[0] - _corners[1]);
82  SLVec3f v2(_corners[0] - _corners[2]);
83  SLVec3f n(v1 ^ v2);
84  n.normalize();
85 
86  // Set one default material index
87  mat(material);
88 
89  // Copy vertices and normals
90  for (SLushort i = 0; i < (SLushort)P.size(); ++i)
91  {
92  P[i] = _corners[i];
93  N[i] = n;
94  if (!UV[0].empty()) UV[0][i] = _uv1[i];
95  }
96 
97  // Build face vertex indices
98  for (SLushort f = 0; f < (SLushort)_corners.size() - 2; ++f)
99  {
100  SLuint i = f * 3;
101  I16[i] = 0;
102  I16[i + 1] = f + 1;
103  I16[i + 2] = f + 2;
104  }
105 }
106 //-----------------------------------------------------------------------------
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
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
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
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
SLVVec2f _uv1
texture coords for corners
Definition: SLPolygon.h:55
void buildMesh(SLMaterial *mat)
SLPolygon::buildMesh fills in the underlying arrays from the SLMesh object.
Definition: SLPolygon.cpp:56
SLVVec3f _corners
corners in ccw order
Definition: SLPolygon.h:54
SLPolygon(SLAssetManager *assetMgr, const SLVVec3f &corner, const SLstring &name="polygon mesh", SLMaterial *mat=nullptr)
ctor for generic convex polygon
Definition: SLPolygon.cpp:14
SLVec3 & normalize()
Definition: SLVec3.h:124