NGL  6.5
The NCCA Graphics Library
BBox.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Jon Macey
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 //----------------------------------------------------------------------------------------------------------------------
20 //----------------------------------------------------------------------------------------------------------------------
21 #include "BBox.h"
22 #include "VAOFactory.h"
23 #include "SimpleIndexVAO.h"
24 #include <iostream>
25 namespace ngl
26 {
27 constexpr GLubyte indices[]= {
28  0,1,5,0,4,5, // back
29  3,2,6,7,6,3, // front
30  0,1,2,3,2,0, // top
31  4,5,6,7,6,4, // bottom
32  0,3,4,4,7,3,
33  1,5,2,2,6,5
34 
35  };
36 // indices for line loop
37 constexpr GLubyte lindices[]= {
38  0,3,7,4,0,
39  0,1,5,4,0,
40  1,2,6,5,1, // right
41  0,1,2,3,0,
42  4,5,6,7,4
43  };
44 
45 //----------------------------------------------------------------------------------------------------------------------
46 BBox::BBox( const Vec3& _center, Real _width, Real _height, Real _depth ) noexcept
47 {
48  // Calculate the Vertices based on the w,h,d params passed in the box is asumed
49  // to be centered on the _center with equal w / h / d
50  // -x +y -z
51 
52  m_vert[0].m_x=_center.m_x-(_width/2.0f); m_vert[0].m_y=_center.m_y+(_height/2.0f); m_vert[0].m_z=_center.m_z-(_depth/2.0f);
53  // + x -y -z
54  m_vert[1].m_x=_center.m_x+(_width/2.0f); m_vert[1].m_y=_center.m_y+(_height/2.0f); m_vert[1].m_z=_center.m_z-(_depth/2.0f);
55  m_vert[2].m_x=_center.m_x+(_width/2.0f); m_vert[2].m_y=_center.m_y+(_height/2.0f); m_vert[2].m_z=_center.m_z+(_depth/2.0f);
56  m_vert[3].m_x=_center.m_x-(_width/2.0f); m_vert[3].m_y=_center.m_y+(_height/2.0f); m_vert[3].m_z=_center.m_z+(_depth/2.0f);
57  m_vert[4].m_x=_center.m_x-(_width/2.0f); m_vert[4].m_y=_center.m_y-(_height/2.0f); m_vert[4].m_z=_center.m_z-(_depth/2.0f);
58  m_vert[5].m_x=_center.m_x+(_width/2.0f); m_vert[5].m_y=_center.m_y-(_height/2.0f); m_vert[5].m_z=_center.m_z-(_depth/2.0f);
59  m_vert[6].m_x=_center.m_x+(_width/2.0f); m_vert[6].m_y=_center.m_y-(_height/2.0f); m_vert[6].m_z=_center.m_z+(_depth/2.0f);
60  m_vert[7].m_x=_center.m_x-(_width/2.0f); m_vert[7].m_y=_center.m_y-(_height/2.0f); m_vert[7].m_z=_center.m_z+(_depth/2.0f);
61 
62  //Store the _center
63  m_center=_center;
64  // Setup the Plane Normals for Collision Detection
65  m_norm[0].set(0.0f,1.0f,0.0f);
66  m_norm[1].set(0.0f,-1.0f,0.0f);
67  m_norm[2].set(1.0f,0.0f,0.0f);
68  m_norm[3].set(-1.0f,0.0f,0.0f);
69  m_norm[4].set(0.0f,0.0f,1.0f);
70  m_norm[5].set(0.0f,0.0f,-1.0f);
71  // store width height and depth
72  m_width=_width;
73  m_height=_height;
74  m_depth=_depth;
75  #ifdef USINGIOS_
76  m_drawMode=GL_LINE_LOOP;
77  #else
78  m_drawMode=GL_LINE;
79  #endif
80  m_vao=0;
81  setVAO();
82 }
83 
84 
85 
86 
87 //----------------------------------------------------------------------------------------------------------------------
88 BBox::BBox() noexcept
89 {
90  //default constructor creates a unit BBox
92  #ifdef USINGIOS_
94  #else
96  #endif
97  m_width=2.0;
98  m_height=2.0;
99  m_depth=2.0;
100  m_vao=nullptr;
101  setVAO();
102 }
103 
104 BBox::BBox(const BBox &_b) noexcept
105 {
106  m_center=_b.m_center;
107  m_width=_b.m_width;
108  m_height=_b.m_height;
109  m_depth=_b.m_depth;
110  m_drawMode=_b.m_drawMode;
111  m_minX=_b.m_minX;
112  m_maxX=_b.m_maxX;
113  m_minY=_b.m_minY;
114  m_maxY=_b.m_maxY;
115  m_minZ=_b.m_minZ;
116  m_maxZ=_b.m_maxZ;
117  m_vao=0;
118  recalculate();
119 }
120 
122 {
123 
124  m_center=_b.m_center;
125  m_width=_b.m_width;
126  m_height=_b.m_height;
127  m_depth=_b.m_depth;
129  m_minX=_b.m_minX;
130  m_maxX=_b.m_maxX;
131  m_minY=_b.m_minY;
132  m_maxY=_b.m_maxY;
133  m_minZ=_b.m_minZ;
134  m_maxZ=_b.m_maxZ;
135  m_vao.reset(_b.m_vao.get());
136  return *this;
137 }
138 
139 
140 //----------------------------------------------------------------------------------------------------------------------
141 BBox::BBox( Real _minX, Real _maxX, Real _minY, Real _maxY, Real _minZ, Real _maxZ ) noexcept
142 {
143 
144  m_minX=_minX;
145  m_maxX=_maxX;
146  m_minY=_minY;
147  m_maxY=_maxY;
148  m_minZ=_minZ;
149  m_maxZ=_maxZ;
150 
151 
152  m_center.set(0,0,0);
153 
154  m_vert[0].m_x=_minX; m_vert[0].m_y=_maxY; m_vert[0].m_z=_minZ;
155  m_vert[1].m_x=_maxX; m_vert[1].m_y=_maxY; m_vert[1].m_z=_minZ;
156  m_vert[2].m_x=_maxX; m_vert[2].m_y=_maxY; m_vert[2].m_z=_maxZ;
157  m_vert[3].m_x=_minX; m_vert[3].m_y=_maxY; m_vert[3].m_z=_maxZ;
158 
159  m_vert[4].m_x=_minX; m_vert[4].m_y=_minY; m_vert[4].m_z=_minZ;
160  m_vert[5].m_x=_maxX; m_vert[5].m_y=_minY; m_vert[5].m_z=_minZ;
161  m_vert[6].m_x=_maxX; m_vert[6].m_y=_minY; m_vert[6].m_z=_maxZ;
162  m_vert[7].m_x=_minX; m_vert[7].m_y=_minY; m_vert[7].m_z=_maxZ;
163  #ifdef USINGIOS_
165  #else
167  #endif
171  m_vao=0;
172  setVAO();
173 
174 }
175 
176 //----------------------------------------------------------------------------------------------------------------------
177 void BBox::setDrawMode( GLenum _mode) noexcept
178 {
179  m_drawMode=_mode;
180  setVAO();
181 }
182 
184 {
185  // if were not doing line drawing then use tris
186  #ifdef USINGIOS_
188  #else
189  if(m_drawMode !=GL_LINE)
190  #endif
191  {
192  m_vao.reset( VAOFactory::createVAO("simpleIndexVAO",GL_TRIANGLES) );
193 
194  // now we have our data add it to the VAO, we need to tell the VAO the following
195  // how much (in bytes) data we are copying
196  // a pointer to the first element of data (in this case the address of the first element of the
197  // std::vector
198  m_vao->bind();
199  m_vao->setData(SimpleIndexVAO::VertexData(8*sizeof(Vec3),m_vert[0].m_x,sizeof(indices),&indices[0],GL_UNSIGNED_BYTE,GL_STATIC_DRAW));
200  m_vao->setVertexAttributePointer(0,3,GL_FLOAT,sizeof(Vec3),0);
201 
202  m_vao->setNumIndices(sizeof(indices));
203  // finally we have finished for now so time to unbind the VAO
204  m_vao->unbind();
205  }
206  else // we just need to draw the lines
207  {
208  m_vao.reset( VAOFactory::createVAO("simpleIndexVAO",GL_LINE_LOOP));
209 
210  // now we have our data add it to the VAO, we need to tell the VAO the following
211  // how much (in bytes) data we are copying
212  // a pointer to the first element of data (in this case the address of the first element of the
213 
214 
215  m_vao->bind();
216  m_vao->setData( SimpleIndexVAO::VertexData(8*sizeof(Vec3),m_vert[0].m_x,sizeof(lindices),&lindices[0],GL_UNSIGNED_BYTE,GL_STATIC_DRAW));
217  m_vao->setVertexAttributePointer(0,3,GL_FLOAT,sizeof(Vec3),0);
218 
219  m_vao->setNumIndices(sizeof(lindices));
220  // finally we have finished for now so time to unbind the VAO
221  m_vao->unbind();
222  }
223 
224 
225 }
226 
227 
228 //----------------------------------------------------------------------------------------------------------------------
229 void BBox::draw() const noexcept
230 {
231 #ifndef USINGIOS_
233  m_vao->bind();
234  m_vao->draw();
235  m_vao->unbind();
237 #else
238  m_vao->bind();
239  m_vao->draw();
240  m_vao->unbind();
241 #endif
242 }
243 //----------------------------------------------------------------------------------------------------------------------
244 
245 void BBox::setCenter(const Vec3 &_center, bool _recalc) noexcept
246 {
247  // Calculate the Vertices based on the w,h,d params passed in the box is asumed
248  // to be centered on the _center with equal w / h / d
249  // -x +y -z
250  m_center=_center;
251  m_vert[0].m_x=_center.m_x-(m_width/2.0f); m_vert[0].m_y=_center.m_y+(m_height/2.0f); m_vert[0].m_z=_center.m_z-(m_depth/2.0f);
252  // + x -y -z
253  m_vert[1].m_x=_center.m_x+(m_width/2.0f); m_vert[1].m_y=_center.m_y+(m_height/2.0f); m_vert[1].m_z=_center.m_z-(m_depth/2.0f);
254  m_vert[2].m_x=_center.m_x+(m_width/2.0f); m_vert[2].m_y=_center.m_y+(m_height/2.0f); m_vert[2].m_z=_center.m_z+(m_depth/2.0f);
255  m_vert[3].m_x=_center.m_x-(m_width/2.0f); m_vert[3].m_y=_center.m_y+(m_height/2.0f); m_vert[3].m_z=_center.m_z+(m_depth/2.0f);
256  m_vert[4].m_x=_center.m_x-(m_width/2.0f); m_vert[4].m_y=_center.m_y-(m_height/2.0f); m_vert[4].m_z=_center.m_z-(m_depth/2.0f);
257  m_vert[5].m_x=_center.m_x+(m_width/2.0f); m_vert[5].m_y=_center.m_y-(m_height/2.0f); m_vert[5].m_z=_center.m_z-(m_depth/2.0f);
258  m_vert[6].m_x=_center.m_x+(m_width/2.0f); m_vert[6].m_y=_center.m_y-(m_height/2.0f); m_vert[6].m_z=_center.m_z+(m_depth/2.0f);
259  m_vert[7].m_x=_center.m_x-(m_width/2.0f); m_vert[7].m_y=_center.m_y-(m_height/2.0f); m_vert[7].m_z=_center.m_z+(m_depth/2.0f);
260  if(_recalc)
261  {
262  recalculate();
263  }
264 }
265 
266 void BBox::width(Real _w, bool _recalc) noexcept
267 {
268  m_width=_w;
269  if(_recalc)
270  {
271  recalculate();
272  }
273 }
274 
275 void BBox::height(Real _h, bool _recalc) noexcept
276 {
277  m_height=_h;
278  if(_recalc)
279  {
280  recalculate();
281  }
282 }
283 
284 void BBox::depth(Real _d, bool _recalc) noexcept
285 {
286  m_depth=_d;
287  if(_recalc)
288  {
289  recalculate();
290  }
291 }
292 
293 void BBox::recalculate() noexcept
294 {
295  // Calculate the Vertices based on the w,h,d params passed in the box is asumed
296  // to be centered on the _center with equal w / h / d
297  // -x +y -z
299  // + x -y -z
307  setVAO();
308 }
309 
310 //----------------------------------------------------------------------------------------------------------------------
311 
312 BBox::~BBox() noexcept
313 {
314  m_vao->removeVAO();
315 }
316 //----------------------------------------------------------------------------------------------------------------------
317 
318 } // end namespace ngl
319 
320 
321 
constexpr GLubyte lindices[]
Definition: BBox.cpp:37
Vec3 m_center
This is the center of the BBox stored for caluculations in other classes s.
Definition: BBox.h:206
Real depth() const noexcept
Depth of the BBox.
Definition: BBox.h:140
Vec3 m_vert[8]
Contains the 8 vertices for the BBox aranged from v[0] = Left-top-Max Z and then rotating clock wise ...
Definition: BBox.h:178
static AbstractVAO * createVAO(const std::string &_type, GLenum _mode=GL_TRIANGLES)
Definition: VAOFactory.cpp:19
#define GL_STATIC_DRAW
Definition: glew.h:1671
Real m_maxX
the max x value of the bbox
Definition: BBox.h:186
void setDrawMode(GLenum _mode) noexcept
reset the draw mode for the BBox
Definition: BBox.cpp:177
#define GL_FRONT_AND_BACK
Definition: glew.h:374
Real m_minZ
the min z value of the bbox
Definition: BBox.h:198
#define GL_TRIANGLES
Definition: glew.h:330
Real m_width
Width of the BBox.
Definition: BBox.h:214
~BBox() noexcept
dtor no dynamic memory so empty
Definition: BBox.cpp:312
Real m_height
Height of the BBox.
Definition: BBox.h:218
#define GL_UNSIGNED_BYTE
Definition: glew.h:637
#define GL_FILL
Definition: glew.h:687
unsigned int GLenum
Definition: glew.h:278
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
Real m_depth
Depth of the BBox.
Definition: BBox.h:222
implementation files for RibExport class
Definition: AABB.cpp:22
Real width() const noexcept
accessor for the width of the BBox
Definition: BBox.h:120
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
GLuint GLuint GLsizei GLenum const void * indices
Definition: glew.h:1256
BBox() noexcept
Default constructor will create a BBox centered at point 0,0,0 With Unit length width and height (== ...
Definition: BBox.cpp:88
Real m_minX
the min x value of the bbox
Definition: BBox.h:182
Real m_y
y component
Definition: Vec3.h:311
void set(Real _x, Real _y, Real _z) noexcept
sets the Vec3 component from 3 values
Definition: Vec3.cpp:33
Real m_maxY
the max y value of the bbox
Definition: BBox.h:194
std::unique_ptr< AbstractVAO > m_vao
a pointer to the VAO buffer used for drawing the bbox
Definition: BBox.h:226
Real height() const noexcept
Height of the BBox.
Definition: BBox.h:130
Real m_x
x component
Definition: Vec3.h:310
void recalculate() noexcept
recalculate the bbox values once things have been changed this will also re-do the VAO ...
Definition: BBox.cpp:293
#define GL_LINE_LOOP
Definition: glew.h:325
GLuint m_drawMode
sets the draw mode for the BBox Faces, set to GL_LINE for line faces and GL_FILL for filled ...
Definition: BBox.h:231
Real m_maxZ
the max z value of the bbox
Definition: BBox.h:202
void draw() const noexcept
Draw Method draws the BBox using OpenGL.
Definition: BBox.cpp:229
#define GL_LINE
Definition: glew.h:686
GLAPI void GLAPIENTRY glPolygonMode(GLenum face, GLenum mode)
Simple Bounding box class used in various parts of ngl and other example programs.
Definition: BBox.h:40
a simple bounding box class
#define GL_FLOAT
Definition: glew.h:642
Real m_minY
the min y value of the bbox
Definition: BBox.h:190
void setVAO()
method used to set the vao
Definition: BBox.cpp:183
unsigned char GLubyte
Definition: glew.h:286
Real m_z
z component
Definition: Vec3.h:312
void setCenter(const Vec3 &_center, bool _recalc=true) noexcept
set the center of the BBox and re-calculate the extents
Definition: BBox.cpp:245
GLclampf f
Definition: glew.h:3511
BBox & operator=(const BBox &_other)
Definition: BBox.cpp:121