GLC_lib  2.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
glc_box.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 
3  This file is part of the GLC-lib library.
4  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
5  http://glc-lib.sourceforge.net
6 
7  GLC-lib is free software; you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  GLC-lib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with GLC-lib; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21 *****************************************************************************/
22 
24 
25 #include "glc_box.h"
26 #include "../glc_openglexception.h"
27 #include "../glc_state.h"
28 
30 // Constructor Destructor
32 
33 GLC_Box::GLC_Box(double dLx, double dLy, double dlz)
34 :GLC_Mesh()
35 , m_dLgX(dLx)
36 , m_dLgY(dLy)
37 , m_dLgZ(dlz)
38 {
40 }
41 // Copy constructor
43 :GLC_Mesh(box)
44 , m_dLgX(box.m_dLgX)
45 , m_dLgY(box.m_dLgY)
46 , m_dLgZ(box.m_dLgZ)
47 {
49 }
51 {
52 
53 }
54 
56 // Get Functions
58 
59 // return the box bounding box
61 {
62  if (GLC_Mesh::isEmpty())
63  {
65  }
66  return GLC_Mesh::boundingBox();
67 }
68 
69 // Return a copy of the current geometry
71 {
72  return new GLC_Box(*this);
73 }
74 
75 
77 // Set Functions
79 
80 // Set X length
81 void GLC_Box::setLgX(double LgX)
82 {
83  Q_ASSERT(LgX > 0);
84  m_dLgX= LgX;
85 
87 }
88 
89 // Set Y length
90 void GLC_Box::setLgY(double LgY)
91 {
92  Q_ASSERT(LgY > 0);
93  m_dLgY= LgY;
94 
96 }
97 
98 // Set Z length
99 void GLC_Box::setLgZ(double LgZ)
100 {
101  Q_ASSERT(LgZ > 0);
102  m_dLgZ= LgZ;
103 
105 }
106 
108 // Private OpenGL functions
110 
111 // Box Set Up
112 void GLC_Box::glDraw(const GLC_RenderProperties& renderProperties)
113 {
114  if (GLC_Mesh::isEmpty())
115  {
117  }
118 
119  GLC_Mesh::glDraw(renderProperties);
120 }
121 // Create the box mesh
123 {
124  Q_ASSERT(GLC_Mesh::isEmpty());
125  createWire();
126 
127  const GLfloat lgX= static_cast<const GLfloat>(m_dLgX / 2.0);
128  const GLfloat lgY= static_cast<const GLfloat>(m_dLgY / 2.0);
129  const GLfloat lgZ= static_cast<const GLfloat>(m_dLgZ / 2.0);
130 
131  GLfloatVector verticeVector;
132  GLfloatVector normalsVector;
134 
135  // Face 1
136  verticeVector << -lgX; verticeVector << -lgY; verticeVector << lgZ;
137  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
138  texelVector << 0.0f; texelVector << 0.0f;
139 
140  verticeVector << lgX; verticeVector << -lgY; verticeVector << lgZ;
141  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
142  texelVector << 1.0f; texelVector << 0.0f;
143 
144  verticeVector << lgX; verticeVector << lgY; verticeVector << lgZ;
145  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
146  texelVector << 1.0f; texelVector << 1.0f;
147 
148  verticeVector << -lgX; verticeVector << lgY; verticeVector << lgZ;
149  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << 1.0f;
150  texelVector << 0.0f; texelVector << 1.0f;
151 
152  // Face 2
153  verticeVector << lgX; verticeVector << -lgY; verticeVector << lgZ;
154  normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
155  texelVector << 0.0f; texelVector << 0.0f;
156 
157  verticeVector << lgX; verticeVector << -lgY; verticeVector << -lgZ;
158  normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
159  texelVector << 1.0f; texelVector << 0.0f;
160 
161  verticeVector << lgX; verticeVector << lgY; verticeVector << -lgZ;
162  normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
163  texelVector << 1.0f; texelVector << 1.0f;
164 
165  verticeVector << lgX; verticeVector << lgY; verticeVector << lgZ;
166  normalsVector << 1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
167  texelVector << 0.0f; texelVector << 1.0f;
168 
169  // Face 3
170  verticeVector << -lgX; verticeVector << -lgY; verticeVector << -lgZ;
171  normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
172  texelVector << 0.0f; texelVector << 0.0f;
173 
174  verticeVector << -lgX; verticeVector << -lgY; verticeVector << lgZ;
175  normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
176  texelVector << 1.0f; texelVector << 0.0f;
177 
178  verticeVector << -lgX; verticeVector << lgY; verticeVector << lgZ;
179  normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
180  texelVector << 1.0f; texelVector << 1.0f;
181 
182  verticeVector << -lgX; verticeVector << lgY; verticeVector << -lgZ;
183  normalsVector << -1.0f; normalsVector << 0.0f; normalsVector << 0.0f;
184  texelVector << 0.0f; texelVector << 1.0f;
185 
186  // Face 4
187  verticeVector << lgX; verticeVector << -lgY; verticeVector << -lgZ;
188  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
189  texelVector << 0.0f; texelVector << 0.0f;
190 
191  verticeVector << -lgX; verticeVector << -lgY; verticeVector << -lgZ;
192  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
193  texelVector << 1.0f; texelVector << 0.0f;
194 
195  verticeVector << -lgX; verticeVector << lgY; verticeVector << -lgZ;
196  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
197  texelVector << 1.0f; texelVector << 1.0f;
198 
199  verticeVector << lgX; verticeVector << lgY; verticeVector << -lgZ;
200  normalsVector << 0.0f; normalsVector << 0.0f; normalsVector << -1.0f;
201  texelVector << 0.0f; texelVector << 1.0f;
202 
203  // Face 5
204  verticeVector << -lgX; verticeVector << lgY; verticeVector << lgZ;
205  normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
206  texelVector << 0.0f; texelVector << 0.0f;
207 
208  verticeVector << lgX; verticeVector << lgY; verticeVector << lgZ;
209  normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
210  texelVector << 1.0f; texelVector << 0.0f;
211 
212  verticeVector << lgX; verticeVector << lgY; verticeVector << -lgZ;
213  normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
214  texelVector << 1.0f; texelVector << 1.0f;
215 
216  verticeVector << -lgX; verticeVector << lgY; verticeVector << -lgZ;
217  normalsVector << 0.0f; normalsVector << 1.0f; normalsVector << 0.0f;
218  texelVector << 0.0f; texelVector << 1.0f;
219 
220  // Face 6
221  verticeVector << -lgX; verticeVector << -lgY; verticeVector << -lgZ;
222  normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
223  texelVector << 0.0f; texelVector << 0.0f;
224 
225  verticeVector << lgX; verticeVector << -lgY; verticeVector << -lgZ;
226  normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
227  texelVector << 1.0f; texelVector << 0.0f;
228 
229  verticeVector << lgX; verticeVector << -lgY; verticeVector << lgZ;
230  normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
231  texelVector << 1.0f; texelVector << 1.0f;
232 
233  verticeVector << -lgX; verticeVector << -lgY; verticeVector << lgZ;
234  normalsVector << 0.0f; normalsVector << -1.0f; normalsVector << 0.0f;
235  texelVector << 0.0f; texelVector << 1.0f;
236 
237  // Add bulk data in to the mesh
238  GLC_Mesh::addVertice(verticeVector);
239  GLC_Mesh::addNormals(normalsVector);
240  GLC_Mesh::addTexels(texelVector);
241 
242  // Set the material to use
243  GLC_Material* pMaterial;
244  if (hasMaterial())
245  {
246  pMaterial= this->firstMaterial();
247  }
248  else
249  {
250  pMaterial= new GLC_Material();
251  }
252 
253  IndexList index;
254  // Face 1
255  index << 0 << 1 << 3 << 2;
256  GLC_Mesh::addTrianglesStrip(pMaterial, index);
257  index.clear();
258  // Face 2
259  index << 4 << 5 << 7 << 6;
260  GLC_Mesh::addTrianglesStrip(pMaterial, index);
261  index.clear();
262  // Face 3
263  index << 8 << 9 << 11 << 10;
264  GLC_Mesh::addTrianglesStrip(pMaterial, index);
265  index.clear();
266  // Face 4
267  index << 12 << 13 << 15 << 14;
268  GLC_Mesh::addTrianglesStrip(pMaterial, index);
269  index.clear();
270  // Face 5
271  index << 16 << 17 << 19 << 18;
272  GLC_Mesh::addTrianglesStrip(pMaterial, index);
273  index.clear();
274  // Face 6
275  index << 20 << 21 << 23 << 22;
276  GLC_Mesh::addTrianglesStrip(pMaterial, index);
277  index.clear();
278 
280 }
281 
282 // Create the wire of the mesh
284 {
285  Q_ASSERT(m_WireData.isEmpty());
286 
287  const GLfloat lgX= static_cast<const GLfloat>(m_dLgX / 2.0);
288  const GLfloat lgY= static_cast<const GLfloat>(m_dLgY / 2.0);
289  const GLfloat lgZ= static_cast<const GLfloat>(m_dLgZ / 2.0);
290 
291  // Float vector
292  GLfloatVector floatVector;
293  floatVector << lgX << lgY << lgZ;
294  floatVector << lgX << lgY << -lgZ;
295  floatVector << -lgX << lgY << -lgZ;
296  floatVector << -lgX << lgY << lgZ;
297  floatVector << lgX << lgY << lgZ;
298  GLC_Geometry::addVerticeGroup(floatVector);
299  floatVector.clear();
300 
301  floatVector << lgX << -lgY << lgZ;
302  floatVector << lgX << -lgY << -lgZ;
303  floatVector << -lgX << -lgY << -lgZ;
304  floatVector << -lgX << -lgY << lgZ;
305  floatVector << lgX << -lgY << lgZ;
306  GLC_Geometry::addVerticeGroup(floatVector);
307  floatVector.clear();
308 
309  floatVector << lgX << lgY << lgZ;
310  floatVector << lgX << -lgY << lgZ;
311  GLC_Geometry::addVerticeGroup(floatVector);
312  floatVector.clear();
313 
314  floatVector << lgX << lgY << -lgZ;
315  floatVector << lgX << -lgY << -lgZ;
316  GLC_Geometry::addVerticeGroup(floatVector);
317  floatVector.clear();
318 
319  floatVector << -lgX << lgY << -lgZ;
320  floatVector << -lgX << -lgY << -lgZ;
321  GLC_Geometry::addVerticeGroup(floatVector);
322  floatVector.clear();
323 
324  floatVector << -lgX << lgY << lgZ;
325  floatVector << -lgX << -lgY << lgZ;
326  GLC_Geometry::addVerticeGroup(floatVector);
327  floatVector.clear();
328 }
329 

©2005-2013 Laurent Ribon