RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
clnode.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 "staticfuncs.h"
31 #include "misc/btcustom.h"
32 #include "misc/stringhelper.hpp"
34 #include "clengine/clpolygon.h"
35 #include "clengine/clnode.h"
36 
37 //-----------------------------------------------------------------------------
38 
39 CLNode::CLNode(const CLNode& original_node) : CObject() {
40  copyValuesFromObject(original_node);
41 }
42 
43 //-----------------------------------------------------------------------------
44 
46  m_instanceGeometries = NULL;
47  m_defaultSize = btCustom::zero_vector();
50  m_visible = true;
51 }
52 
53 //-----------------------------------------------------------------------------
54 
55 void CLNode::copyValuesFromObject(const CLNode& original_obj) {
56  m_id = original_obj.id();
57  m_transform = Transform(original_obj.constTransform());
58  m_visible = true;
59  m_size = original_obj.size();
60  m_defaultSize = ( original_obj.defaultSize() != btCustom::zero_vector() ?
61  btVector3(original_obj.defaultSize()) :
63 
64  m_instanceGeometries = (CArray<CLInstanceGeometry>*)original_obj.getInstanceGeometries()->retain();
65 }
66 
67 //-----------------------------------------------------------------------------
68 
70  return m_transform;
71 }
72 
73 //-----------------------------------------------------------------------------
74 
76  return m_transform;
77 }
78 
79 //-----------------------------------------------------------------------------
80 
81 void CLNode::setTransform(const Transform& transform) {
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 bool CLNode::visible() const {
88  return m_visible;
89 }
90 
91 //-----------------------------------------------------------------------------
92 
93 void CLNode::setVisible(bool visible) {
95 }
96 
97 //-----------------------------------------------------------------------------
98 
99 const std::string& CLNode::id() const {
100  return m_id;
101 }
102 
103 //-----------------------------------------------------------------------------
104 
105 void CLNode::setId(const std::string &id) {
106  m_id = id;
107 }
108 
109 //-----------------------------------------------------------------------------
110 
111 CLNode& CLNode::operator=(const CLNode& original_obj) {
112  if (this == &original_obj) {
113  return *this;
114  }
115 
116  copyValuesFromObject(original_obj);
117 
118  return *this;
119 }
120 
121 //-----------------------------------------------------------------------------
122 
124  m_instanceGeometries = ( node->getInstanceGeometries() != NULL ?
125  static_cast<CArray<CLInstanceGeometry>*>(node->getInstanceGeometries()->retain()) :
126  NULL );
127  m_transform = Transform(node->transform());
128  m_visible = node->visible();
129  m_id = node->id();
130  m_size = node->size();
131  m_defaultSize = ( node->defaultSize() != btCustom::zero_vector() ?
132  btVector3(node->defaultSize()) :
134 }
135 
136 //-----------------------------------------------------------------------------
137 
139  CLNode* copy_node = new CLNode(*this);
140  return copy_node;
141 }
142 
143 //-----------------------------------------------------------------------------
144 
146  RE_ASSERT(_array);
147  SAFE_RELEASE(m_instanceGeometries);
148  SAFE_RETAIN(_array);
149  m_instanceGeometries = _array;
150  calcSize();
151 }
152 
153 //-----------------------------------------------------------------------------
154 
156  return m_instanceGeometries;
157 }
158 
159 //-----------------------------------------------------------------------------
160 
161 QList<CLInstanceGeometry> CLNode::instanceGeometries() {
162  QList<CLInstanceGeometry> vec = QList<CLInstanceGeometry>();
163  for (uint i = 0; i < m_instanceGeometries->size(); i++) {
164  vec.append(CLInstanceGeometry(m_instanceGeometries->objectAtIndex(i)));
165  }
166  return vec;
167 }
168 
169 //-----------------------------------------------------------------------------
170 
171 void CLNode::setInstanceGeometries(QList<CLInstanceGeometry> vec) {
172  SAFE_RELEASE(m_instanceGeometries);
173  std::vector<CLInstanceGeometry> std_vec = std::vector<CLInstanceGeometry>();
174  for (int i = 0; i < vec.count(); i++) {
175  std_vec.push_back(CLInstanceGeometry(vec.at(i)));
176  }
178 }
179 
180 //-----------------------------------------------------------------------------
181 
182 const btVector3& CLNode::size() const {
183  return m_size;
184 }
185 
186 //-----------------------------------------------------------------------------
187 
188 const btVector3& CLNode::defaultSize() const {
189  return m_defaultSize;
190 }
191 
192 //-----------------------------------------------------------------------------
193 
194 const btVector3& CLNode::calcSize() {
195  if(m_defaultSize == btCustom::zero_vector()) {
196  QVector<float> x_values, y_values, z_values;
197  for (unsigned int i = 0; i < getInstanceGeometries()->size(); i++) {
198  CLInstanceGeometry* instance_geometry = &(getInstanceGeometries()->data()[i]);
199  RE_ASSERT(instance_geometry);
200  std::string geometry_id = instance_geometry->getMeshURL();
201  geometry_id = StringHelper::removeOccurrencesOfChar(geometry_id, '#');
202  CLGeometry* geometry_object = CLASSETSREPOSITORY->getGeometryObjHavingId(geometry_id);
203  if (!geometry_object) {
204  return btVector3(1, 1, 1);
205  }
206 
207  for (unsigned int i = 0; i < geometry_object->getPolygons()->size(); i++) {
208  CArray<btVector3>* triangles_vertices = static_cast<CArray<btVector3>*>(geometry_object->getPolygons()->objectAtIndex(i).getTriangleVertices()->retain());
209  for (uint j = 0; j < triangles_vertices->size(); j++) {
210  btVector3 vec = triangles_vertices->objectAtIndex(j);
211  x_values.append(vec.x());
212  y_values.append(vec.y());
213  z_values.append(vec.z());
214  }
215  triangles_vertices->release();
216  }
217  }
218 
219  qSort(x_values.begin(), x_values.end(), valueLessThan);
220  qSort(y_values.begin(), y_values.end(), valueLessThan);
221  qSort(z_values.begin(), z_values.end(), valueLessThan);
222 
223  float x_min = x_values.first();
224  float y_min = y_values.first();
225  float z_min = z_values.first();
226 
227  float x_max = x_values.last();
228  float y_max = y_values.last();
229  float z_max = z_values.last();
230 
231  float Lx = fabs(x_max - x_min);
232  float Ly = fabs(y_max - y_min);
233  float Lz = fabs(z_max - z_min);
234 
235  m_defaultSize = btVector3(Lx, Ly, Lz);
236  }
237  m_size = m_defaultSize * transform().localScale();
238  return m_size;
239 }
240 
241 //-----------------------------------------------------------------------------
242 
244  SAFE_RELEASE(m_instanceGeometries);
245 }
CArray< CLPolygon > * getPolygons() const
Definition: clgeometry.cpp:80
void setId(const std::string &id)
Definition: clnode.cpp:105
void copyNodeValuesFromNode(CLNode *node)
Definition: clnode.cpp:123
virtual const btVector3 & calcSize()
Definition: clnode.cpp:194
const btVector3 & defaultSize() const
Definition: clnode.cpp:188
CArray< CLInstanceGeometry > * getInstanceGeometries() const
Definition: clnode.cpp:155
Transform m_transform
Definition: clnode.h:77
btVector3 zero_vector()
Definition: btcustom.cpp:26
CArray< btVector3 > * getTriangleVertices() const
Definition: clpolygon.cpp:119
Definition: clnode.h:37
static std::string & removeOccurrencesOfChar(std::string &original_str, char char_to_remove)
T * data() const
Definition: carray.h:95
bool valueLessThan(const float &p1, const float &p2)
valueLessThan Used by qSort method
Definition: staticfuncs.cpp:32
CLNode & operator=(const CLNode &node)
Definition: clnode.cpp:111
const Transform & constTransform() const
Definition: clnode.cpp:75
unsigned int release() const
Definition: cobject.cpp:74
void setInstanceGeometries(QList< CLInstanceGeometry > vec)
Definition: clnode.cpp:171
bool m_visible
Definition: clnode.h:80
CLNode * getCopy()
Definition: clnode.cpp:138
const btVector3 & size() const
Definition: clnode.cpp:182
CLNode(void)
Definition: clnode.cpp:45
const std::string & id() const
Definition: clnode.cpp:99
T objectAtIndex(unsigned int index) const
Definition: carray.h:83
#define SAFE_RELEASE(x)
Definition: cobject.h:36
void setVisible(bool visible)
Definition: clnode.cpp:93
btVector3 m_size
Definition: clnode.h:78
btVector3 localScale() const
Definition: transform.cpp:123
#define SAFE_RETAIN(x)
Definition: cobject.h:35
#define RE_ASSERT
Definition: macro.h:57
const std::string & getMeshURL() const
QList< CLInstanceGeometry > instanceGeometries()
Definition: clnode.cpp:161
~CLNode(void)
Definition: clnode.cpp:243
#define CLASSETSREPOSITORY
unsigned int size() const
Definition: carray.h:104
void setTransform(const Transform &transform)
Definition: clnode.cpp:81
virtual void copyValuesFromObject(const CLNode &node)
Definition: clnode.cpp:55
Transform & transform()
Definition: clnode.cpp:69
bool visible() const
Definition: clnode.cpp:87
CObject * retain()
Definition: cobject.cpp:66
std::string m_id
Definition: clnode.h:79