RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
clpolygon.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include "constants.h"
31 #include "cengine/carray.h"
32 #include "clengine/clpolygon.h"
33 
34 //-----------------------------------------------------------------------------
35 
37  m_triangleVertices = NULL;
38  m_triangleNormals = NULL;
39  m_textCoords = NULL;
40  m_triangleVerticesConstData = NULL;
41  m_triangleMapsConstData = NULL;
42 }
43 
44 //-----------------------------------------------------------------------------
45 
46 CLPolygon::CLPolygon(const CLPolygon& original_obj) : CObject() {
47  copyValuesFromObject(original_obj);
48 }
49 
50 //-----------------------------------------------------------------------------
51 
52 void CLPolygon::copyValuesFromObject(const CLPolygon& original_obj) {
53  if (original_obj.getTriangleMaps()) {
54  m_textCoords = static_cast<CArray<TextureCoord>*>(original_obj.getTriangleMaps()->retain());
55  } else {
56  m_textCoords = NULL;
57  }
58 
59  m_triangleNormals = static_cast<CArray<btVector3>*>(original_obj.getTriangleNormals()->retain());
60  m_triangleVertices = static_cast<CArray<btVector3>*>(original_obj.getTriangleVertices()->retain());
61  m_triangleVerticesConstData = NULL;
62  m_triangleMapsConstData = NULL;
63  m_materialId = original_obj.getMaterialId();
64 }
65 
66 //-----------------------------------------------------------------------------
67 
68 CLPolygon& CLPolygon::operator=(const CLPolygon& original_obj) {
69  if (this == &original_obj) {
70  return *this;
71  }
72 
73  copyValuesFromObject(original_obj);
74 
75  return *this;
76 }
77 
78 //-----------------------------------------------------------------------------
79 
80 void CLPolygon::setMaterialId(const std::string& material_id) {
81  m_materialId = material_id;
82 }
83 
84 //-----------------------------------------------------------------------------
85 
86 const std::string& CLPolygon::getMaterialId() const {
87  return m_materialId;
88 }
89 
90 //-----------------------------------------------------------------------------
91 
93  RE_ASSERT(triangles_vertices);
94  SAFE_RELEASE(m_triangleVertices);
95  SAFE_RETAIN(triangles_vertices);
96  m_triangleVertices = triangles_vertices;
97 }
98 
99 //-----------------------------------------------------------------------------
100 
102  RE_ASSERT(triangles_normals);
103  SAFE_RELEASE(m_triangleNormals);
104  SAFE_RETAIN(triangles_normals);
105  m_triangleNormals = triangles_normals;
106 }
107 
108 //-----------------------------------------------------------------------------
109 
110 void CLPolygon::setTriangleMaps(CArray<TextureCoord>* triangles_texture_coords) {
111  // RE_ASSERT(triangles_texture_coords); m_texCoords can be NULL
112  SAFE_RELEASE(m_textCoords);
113  SAFE_RETAIN(triangles_texture_coords);
114  m_textCoords = triangles_texture_coords;
115 }
116 
117 //-----------------------------------------------------------------------------
118 
120  return m_triangleVertices;
121 }
122 
123 //-----------------------------------------------------------------------------
124 
126  if(m_triangleVerticesConstData) {
127  return m_triangleVerticesConstData;
128  }
129 
130  int count = m_triangleVertices->size() * 3;
131  unsigned int index = 0;
132  m_triangleVerticesConstData = new float[count];
133 
134  for (unsigned int i = 0; i < m_triangleVertices->size(); i++) {
135  btVector3* vec = &m_triangleVertices->objectAtIndex(i);
136  (*vec) *= k_QGLGameEditorViewport_SizeFactor;
137  m_triangleVerticesConstData[index++] = vec->x();
138  m_triangleVerticesConstData[index++] = vec->y();
139  m_triangleVerticesConstData[index++] = vec->z();
140  }
141 
142  return m_triangleVerticesConstData;
143 }
144 
145 //-----------------------------------------------------------------------------
146 
148  return m_triangleNormals;
149 }
150 
151 //-----------------------------------------------------------------------------
152 
154  return m_textCoords;
155 }
156 
157 //-----------------------------------------------------------------------------
158 
160  if (!m_textCoords) {
161  return NULL;
162  }
163 
164  if(m_triangleMapsConstData) {
165  return m_triangleMapsConstData;
166  }
167 
168  int count = m_textCoords->size() * 2;
169  unsigned int index = 0;
170  m_triangleMapsConstData = new float[count];
171 
172  for (unsigned int i = 0; i < m_textCoords->size(); i++) {
173  m_triangleMapsConstData[index++] = m_textCoords->objectAtIndex(i).s;
174  m_triangleMapsConstData[index++] = 1 - m_textCoords->objectAtIndex(i).t;
175  }
176 
177  return m_triangleMapsConstData;
178 }
179 
180 //-----------------------------------------------------------------------------
181 
183  SAFE_RELEASE(m_textCoords);
184  SAFE_RELEASE(m_triangleNormals);
185  SAFE_RELEASE(m_triangleVertices);
186  SAFE_RELEASE_ARRAY(m_triangleVerticesConstData);
187  SAFE_RELEASE_ARRAY(m_triangleMapsConstData);
188 }
CLPolygon & operator=(const CLPolygon &polygon)
Definition: clpolygon.cpp:68
float t
Definition: typedefs.h:46
void setTriangleMaps(CArray< TextureCoord > *maps)
Definition: clpolygon.cpp:110
float * getTriangleMapsConstData()
Definition: clpolygon.cpp:159
CArray< TextureCoord > * getTriangleMaps() const
Definition: clpolygon.cpp:153
CArray< btVector3 > * getTriangleVertices() const
Definition: clpolygon.cpp:119
CArray< btVector3 > * getTriangleNormals() const
Definition: clpolygon.cpp:147
virtual void copyValuesFromObject(const CLPolygon &polygon)
Definition: clpolygon.cpp:52
float * getTriangleVerticesConstData()
Definition: clpolygon.cpp:125
T objectAtIndex(unsigned int index) const
Definition: carray.h:83
float s
Definition: typedefs.h:45
void setMaterialId(const std::string &material_id)
Definition: clpolygon.cpp:80
#define SAFE_RELEASE(x)
Definition: cobject.h:36
~CLPolygon(void)
Definition: clpolygon.cpp:182
#define SAFE_RETAIN(x)
Definition: cobject.h:35
#define RE_ASSERT
Definition: macro.h:57
#define SAFE_RELEASE_ARRAY(x)
Definition: cobject.h:37
void setTriangleNormals(CArray< btVector3 > *normals)
Definition: clpolygon.cpp:101
const std::string & getMaterialId() const
Definition: clpolygon.cpp:86
unsigned int size() const
Definition: carray.h:104
CLPolygon(void)
Definition: clpolygon.cpp:36
CObject * retain()
Definition: cobject.cpp:66
void setTriangleVertices(CArray< btVector3 > *vertices)
Definition: clpolygon.cpp:92