RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
clmesh.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 "staticfuncs.h"
32 #include "misc/btcustom.h"
33 #include "misc/genericshelper.hpp"
34 #include "cengine/carray.h"
35 #include "clengine/clpolygon.h"
36 #include "clengine/cltriangles.h"
37 #include "clengine/clmesh.h"
38 
39 //-----------------------------------------------------------------------------
40 
42  m_positions = NULL;
43  m_normals = NULL;
44  m_textureCoords = NULL;
45  m_triangles = NULL;
46 }
47 
48 //-----------------------------------------------------------------------------
49 
50 CLMesh& CLMesh::operator=(const CLMesh& original_obj) {
51  if (this == &original_obj) {
52  return *this;
53  }
54 
55  copyValuesFromObject(original_obj);
56 
57  return *this;
58 }
59 
60 //-----------------------------------------------------------------------------
61 
62 CLMesh::CLMesh(const CLMesh& original_obj) : CObject() {
63  copyValuesFromObject(original_obj);
64 }
65 
66 //-----------------------------------------------------------------------------
67 
68 void CLMesh::copyValuesFromObject(const CLMesh& original_obj) {
69  m_positions = (CArray<btVector3>*)original_obj.positions()->retain();
70  m_normals = (CArray<btVector3>*)original_obj.normals()->retain();
71  m_textureCoords = (CArray<TextureCoord>*)original_obj.mapArray()->retain();
72  m_triangles = (CArray<CLTriangles>*)original_obj.triangles()->retain();
73 }
74 
75 //-----------------------------------------------------------------------------
76 
77 void CLMesh::setTriangles(CArray<CLTriangles>* triangles_array) {
78  RE_ASSERT(triangles_array);
79  SAFE_RELEASE(m_triangles);
80  SAFE_RETAIN(triangles_array);
81  m_triangles = triangles_array;
82 }
83 
84 //-----------------------------------------------------------------------------
85 
87  return m_triangles;
88 }
89 
90 //-----------------------------------------------------------------------------
91 
93  RE_ASSERT(positions);
94  SAFE_RELEASE(m_positions);
95  SAFE_RETAIN(positions);
96  m_positions = positions;
97 }
98 
99 //-----------------------------------------------------------------------------
100 
102  return m_positions;
103 }
104 
105 //-----------------------------------------------------------------------------
106 
108  RE_ASSERT(normals);
109  SAFE_RELEASE(m_normals);
110  SAFE_RETAIN(normals);
111  m_normals = normals;
112 }
113 
114 //-----------------------------------------------------------------------------
115 
117  return m_normals;
118 }
119 
120 //-----------------------------------------------------------------------------
121 
123  RE_ASSERT(map_array);
124  SAFE_RELEASE(m_textureCoords);
125  SAFE_RETAIN(map_array);
126  m_textureCoords = map_array;
127 }
128 
129 //-----------------------------------------------------------------------------
130 
132  return m_textureCoords;
133 }
134 
135 //-----------------------------------------------------------------------------
136 
138  RE_ASSERT(index < m_triangles->size());
139 
140  CLTriangles triangles_obj = m_triangles->objectAtIndex(index);
141 
142  CLPolygon polygon_obj = CLPolygon();
143 
144  CArray<btVector3>* triangles_vertices = NULL;
145  CArray<btVector3>* triangles_normals = NULL;
146  CArray<TextureCoord>* triangles_texture_coords = NULL;
147 
148  triangles_vertices = getLocationsArrayHavingIndicesArray<btVector3>(m_positions, triangles_obj.getVerticesIndices());
149  triangles_normals = getLocationsArrayHavingIndicesArray<btVector3>(m_normals, triangles_obj.getNormalsIndices());
150  triangles_texture_coords = getLocationsArrayHavingIndicesArray<TextureCoord>(m_textureCoords, triangles_obj.getTextureCoordIndices());
151 
152  polygon_obj.setTriangleMaps(triangles_texture_coords);
153  polygon_obj.setTriangleNormals(triangles_normals);
154  polygon_obj.setTriangleVertices(triangles_vertices);
155  polygon_obj.setMaterialId(triangles_obj.getMaterialId());
156 
157  return polygon_obj;
158 }
159 
160 //-----------------------------------------------------------------------------
161 
163  std::vector<CLPolygon>* polygons_array = new std::vector<CLPolygon>();
164 
165  for (unsigned int i = 0; i < m_triangles->size(); i++) {
166  polygons_array->push_back(getPolygonForTrianglesIndex(i));
167  }
168 
169  auto array = fixPolygonsArray(polygons_array);
170  SAFE_RELEASE(polygons_array);
171 
172  return array;
173 }
174 
175 //-----------------------------------------------------------------------------
176 
180 CArray<CLPolygon>* CLMesh::fixPolygonsArray( std::vector<CLPolygon>* polygons_array ) {
181  QVector<float> x_values, y_values, z_values;
182  for (std::vector<CLPolygon>::iterator it = polygons_array->begin();
183  it != polygons_array->end();
184  ++it) {
185  for (uint j = 0; j < (*it).getTriangleVertices()->size(); j++) {
186  btVector3 vec = (*it).getTriangleVertices()->objectAtIndex(j);
187  x_values.append(vec.x());
188  y_values.append(vec.y());
189  z_values.append(vec.z());
190  }
191  }
192 
193  qSort(x_values.begin(), x_values.end(), valueLessThan);
194  qSort(y_values.begin(), y_values.end(), valueLessThan);
195  qSort(z_values.begin(), z_values.end(), valueLessThan);
196 
197  float x_min = x_values.first();
198  float y_min = y_values.first();
199  float z_min = z_values.first();
200 
201  float x_max = x_values.last();
202  float y_max = y_values.last();
203  float z_max = z_values.last();
204 
205  float Lx = fabs(x_max - x_min);
206  float Ly = fabs(y_max - y_min);
207  float Lz = fabs(z_max - z_min);
208 
209  // calculate the centroid position
210  float Cx = x_min + fabs(x_max - x_min) / 2.0f;
211  float Cy = y_min + fabs(y_max - y_min) / 2.0f;
212  float Cz = z_min + fabs(z_max - z_min) / 2.0f;
213 
214  btVector3 centroid = btVector3(Cx, Cy, Cz);
215 
216  float max_axis_length = qMax(Lx, qMax(Ly, Lz));
217  float scale_down_factor = 1.0f;
218  if (max_axis_length > k_CLMesh_MaxAxisLength) {
219  scale_down_factor = k_CLMesh_MaxAxisLength / max_axis_length;
220  }
221 
222  std::vector<CLPolygon>* fixed_polygons = new std::vector<CLPolygon>();
223  for (std::vector<CLPolygon>::iterator it = polygons_array->begin();
224  it != polygons_array->end();
225  ++it) {
226  std::vector<btVector3> vec_array = std::vector<btVector3>();
227  CLPolygon polygon = *it;
228  for (uint j = 0; j < polygon.getTriangleVertices()->size(); j++) {
229  btVector3 vec = polygon.getTriangleVertices()->objectAtIndex(j);
230  btVector3 fixed_vec = vec - centroid;
231  fixed_vec *= scale_down_factor;
232  vec_array.push_back(fixed_vec);
233  }
234  auto triangleVerticesArray = CArray<btVector3>::fromStdVector(&vec_array);
235  polygon.setTriangleVertices(triangleVerticesArray);
236  fixed_polygons->push_back(polygon);
237  vec_array.clear();
238  }
239  auto polygons_array = CArray<CLPolygon>::fromStdVector(fixed_polygons);
240  fixed_polygons->clear();
241  SAFE_RELEASE(fixed_polygons);
242 
243  return polygons_array;
244 }
245 
246 //-----------------------------------------------------------------------------
247 
249  SAFE_RELEASE(m_positions);
250  SAFE_RELEASE(m_normals);
251  SAFE_RELEASE(m_textureCoords);
252  SAFE_RELEASE(m_triangles);
253 }
Definition: clmesh.h:47
void setTriangleMaps(CArray< TextureCoord > *maps)
Definition: clpolygon.cpp:110
CArray< btVector3 > * normals() const
Definition: clmesh.cpp:116
CArray< int > * getVerticesIndices() const
Definition: cltriangles.cpp:83
CArray< btVector3 > * getTriangleVertices() const
Definition: clpolygon.cpp:119
CArray< btVector3 > * positions() const
Definition: clmesh.cpp:101
void setTriangles(CArray< CLTriangles > *triangles_array)
Definition: clmesh.cpp:77
CArray< int > * getTextureCoordIndices() const
Definition: cltriangles.cpp:95
void setNormals(CArray< btVector3 > *normals)
Definition: clmesh.cpp:107
CArray< CLTriangles > * triangles() const
Definition: clmesh.cpp:86
bool valueLessThan(const float &p1, const float &p2)
valueLessThan Used by qSort method
Definition: staticfuncs.cpp:32
void setMapArray(CArray< TextureCoord > *map_array)
Definition: clmesh.cpp:122
CLMesh(void)
Definition: clmesh.cpp:41
~CLMesh(void)
Definition: clmesh.cpp:248
void setPositions(CArray< btVector3 > *positions)
Definition: clmesh.cpp:92
CArray< CLPolygon > * getPolygonsArray()
Definition: clmesh.cpp:162
T objectAtIndex(unsigned int index) const
Definition: carray.h:83
void setMaterialId(const std::string &material_id)
Definition: clpolygon.cpp:80
#define SAFE_RELEASE(x)
Definition: cobject.h:36
CLMesh & operator=(const CLMesh &mesh)
Definition: clmesh.cpp:50
CArray< TextureCoord > * mapArray() const
Definition: clmesh.cpp:131
CArray< int > * getNormalsIndices() const
Definition: cltriangles.cpp:89
virtual void copyValuesFromObject(const CLMesh &mesh)
Definition: clmesh.cpp:68
#define SAFE_RETAIN(x)
Definition: cobject.h:35
#define RE_ASSERT
Definition: macro.h:57
CArray< CLPolygon > * fixPolygonsArray(std::vector< CLPolygon > *polygons_array)
Definition: clmesh.cpp:180
CLPolygon getPolygonForTrianglesIndex(unsigned int index)
Definition: clmesh.cpp:137
const std::string & getMaterialId() const
Definition: cltriangles.cpp:77
void setTriangleNormals(CArray< btVector3 > *normals)
Definition: clpolygon.cpp:101
unsigned int size() const
Definition: carray.h:104
static CArray * fromStdVector(std::vector< T > *vec)
Definition: carray.h:129
CObject * retain()
Definition: cobject.cpp:66
void setTriangleVertices(CArray< btVector3 > *vertices)
Definition: clpolygon.cpp:92