NGL  6.5
The NCCA Graphics Library
Vec3.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 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 #ifndef VEC3_H_
18 #define VEC3_H_
19 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 // must include types.h first for Real and GLEW if required
24 #include "Types.h"
25 #include <array>
26 //----------------------------------------------------------------------------------------------------------------------
35 //----------------------------------------------------------------------------------------------------------------------
36 
37 
38 namespace ngl
39 {
40 // pre-declare the vector class
41 class Vec4;
42 class Mat3;
43 
44 //----------------------------------------------------------------------------------------------------------------------
48 //----------------------------------------------------------------------------------------------------------------------
49 
50 
52 {
53 
54 public:
55 
56  //----------------------------------------------------------------------------------------------------------------------
58  //----------------------------------------------------------------------------------------------------------------------
59  Vec3() : m_x(0.0f),m_y(0.0f),m_z(0.0f) {}
60  //----------------------------------------------------------------------------------------------------------------------
62  //----------------------------------------------------------------------------------------------------------------------
63  Vec3(const Vec3& _v)=default;
64  //----------------------------------------------------------------------------------------------------------------------
69  //----------------------------------------------------------------------------------------------------------------------
70  Vec3(Real _x, Real _y, Real _z) noexcept:
71  m_x(_x),m_y(_y),m_z(_z){}
72  //----------------------------------------------------------------------------------------------------------------------
77  //----------------------------------------------------------------------------------------------------------------------
78  void set( Real _x, Real _y, Real _z) noexcept;
79  //----------------------------------------------------------------------------------------------------------------------
82  //----------------------------------------------------------------------------------------------------------------------
83  void set( const Vec3& _v) noexcept;
84  //----------------------------------------------------------------------------------------------------------------------
87  //----------------------------------------------------------------------------------------------------------------------
88  void set( const Vec4& _v) noexcept;
89  //----------------------------------------------------------------------------------------------------------------------
93  //----------------------------------------------------------------------------------------------------------------------
94  Real dot(const Vec3 &_b )const noexcept;
95  //----------------------------------------------------------------------------------------------------------------------
97  //----------------------------------------------------------------------------------------------------------------------
98  void null() noexcept;
99  //----------------------------------------------------------------------------------------------------------------------
102  //----------------------------------------------------------------------------------------------------------------------
103  Real& operator[]( size_t & _i ) noexcept;
104  //----------------------------------------------------------------------------------------------------------------------
107  //----------------------------------------------------------------------------------------------------------------------
108  const Real& operator[]( size_t & _i ) const noexcept{ return m_openGL[_i]; }
109 
110  //----------------------------------------------------------------------------------------------------------------------
115  //----------------------------------------------------------------------------------------------------------------------
116  void normalize() noexcept;
117  //----------------------------------------------------------------------------------------------------------------------
121  //----------------------------------------------------------------------------------------------------------------------
122  Real inner(const Vec3& _v)const noexcept;
123  //----------------------------------------------------------------------------------------------------------------------
127  //----------------------------------------------------------------------------------------------------------------------
128 
129  Mat3 outer(const Vec3 &_v) const noexcept;
130  //----------------------------------------------------------------------------------------------------------------------
133  //----------------------------------------------------------------------------------------------------------------------
134  Real length() const noexcept;
135  //----------------------------------------------------------------------------------------------------------------------
138  //----------------------------------------------------------------------------------------------------------------------
139  Real lengthSquared() const noexcept;
140  //----------------------------------------------------------------------------------------------------------------------
143  //----------------------------------------------------------------------------------------------------------------------
144  void operator+=(const Vec3& _v ) noexcept;
145  //----------------------------------------------------------------------------------------------------------------------
148  //----------------------------------------------------------------------------------------------------------------------
149  void operator-=( const Vec3& _v ) noexcept;
150  //----------------------------------------------------------------------------------------------------------------------
154  //----------------------------------------------------------------------------------------------------------------------
155  Vec3 operator *( Real _i )const noexcept;
156  //----------------------------------------------------------------------------------------------------------------------
160  //----------------------------------------------------------------------------------------------------------------------
161  Vec3 operator +(const Vec3 &_v )const noexcept;
162  //----------------------------------------------------------------------------------------------------------------------
166  //----------------------------------------------------------------------------------------------------------------------
167  Vec3 operator/(Real _v )const noexcept;
168  //----------------------------------------------------------------------------------------------------------------------
172  //----------------------------------------------------------------------------------------------------------------------
173  void operator/=( Real _v ) noexcept;
174  //----------------------------------------------------------------------------------------------------------------------
178  //----------------------------------------------------------------------------------------------------------------------
179  void operator*=( Real _v ) noexcept;
180  //----------------------------------------------------------------------------------------------------------------------
184  //----------------------------------------------------------------------------------------------------------------------
185  Vec3 operator-(const Vec3 &_v )const noexcept;
186  //----------------------------------------------------------------------------------------------------------------------
190  //----------------------------------------------------------------------------------------------------------------------
191  Vec3 operator*( const Vec3 &_v )const noexcept;
192  //----------------------------------------------------------------------------------------------------------------------
196  //----------------------------------------------------------------------------------------------------------------------
197  Vec3 &operator =( const Vec3 &_v )=default;
198  //----------------------------------------------------------------------------------------------------------------------
202  //----------------------------------------------------------------------------------------------------------------------
203  Vec3 &operator =(const Vec4 &_v ) noexcept;
204  //----------------------------------------------------------------------------------------------------------------------
208  //----------------------------------------------------------------------------------------------------------------------
209  Vec3 &operator =(Real _v ) noexcept;
210 
211  //----------------------------------------------------------------------------------------------------------------------
214  //----------------------------------------------------------------------------------------------------------------------
215  Vec3 operator*(const Mat3 &_m )const noexcept;
216  //----------------------------------------------------------------------------------------------------------------------
218  //----------------------------------------------------------------------------------------------------------------------
219  Vec3 operator-() const noexcept;
220  //----------------------------------------------------------------------------------------------------------------------
224  //----------------------------------------------------------------------------------------------------------------------
225  bool operator==( const Vec3 &_v )const noexcept;
226  //----------------------------------------------------------------------------------------------------------------------
230  //----------------------------------------------------------------------------------------------------------------------
231  bool operator!=( const Vec3 &_v )const noexcept;
232  //----------------------------------------------------------------------------------------------------------------------
236  //----------------------------------------------------------------------------------------------------------------------
237  Vec3 operator/( const Vec3& _v )const noexcept;
238  //----------------------------------------------------------------------------------------------------------------------
242  //----------------------------------------------------------------------------------------------------------------------
243  void cross(const Vec3& _v1, const Vec3& _v2 ) noexcept;
244  //----------------------------------------------------------------------------------------------------------------------
248  //----------------------------------------------------------------------------------------------------------------------
249  Vec3 cross(const Vec3& _b )const noexcept;
250  //----------------------------------------------------------------------------------------------------------------------
254  //----------------------------------------------------------------------------------------------------------------------
255  void clamp(float _min, float _max) noexcept;
256  //----------------------------------------------------------------------------------------------------------------------
259  //----------------------------------------------------------------------------------------------------------------------
260  void clamp(float _max) noexcept;
261  //----------------------------------------------------------------------------------------------------------------------
265  //----------------------------------------------------------------------------------------------------------------------
266  Vec3 reflect(const Vec3 & _n) const noexcept;
267  //----------------------------------------------------------------------------------------------------------------------
269  //----------------------------------------------------------------------------------------------------------------------
270  inline Real* openGL() noexcept{return &m_openGL[0];}
271  //----------------------------------------------------------------------------------------------------------------------
273  //----------------------------------------------------------------------------------------------------------------------
274  static Vec3 up() {return Vec3(0.0f,1.0f,0.0f); }
275  //----------------------------------------------------------------------------------------------------------------------
277  //----------------------------------------------------------------------------------------------------------------------
278  static Vec3 down() {return Vec3(0.0f,-1.0f,0.0f); }
279  //----------------------------------------------------------------------------------------------------------------------
281  //----------------------------------------------------------------------------------------------------------------------
282  static Vec3 left() {return Vec3(-1.0f,0.0f,0.0f); }
283  //----------------------------------------------------------------------------------------------------------------------
285  //----------------------------------------------------------------------------------------------------------------------
286  static Vec3 right() {return Vec3(1.0f,0.0f,0.0f); }
287  //----------------------------------------------------------------------------------------------------------------------
289  //----------------------------------------------------------------------------------------------------------------------
290  static Vec3 in() {return Vec3(0.0f,0.0f,1.0f); }
291  //----------------------------------------------------------------------------------------------------------------------
293  //----------------------------------------------------------------------------------------------------------------------
294  static Vec3 out() {return Vec3(0.0f,0.0f,-1.0f); }
295  //----------------------------------------------------------------------------------------------------------------------
297  //----------------------------------------------------------------------------------------------------------------------
298  static Vec3 zero() {return Vec3(0.0f,0.0f,0.0f); }
299 
303 public :
304 #pragma pack(push,1)
305 
306  union
307  {
308  struct
309  {
313 
314  };
315  struct
316  {
320 
321  };
322 #pragma pack(pop)
323  //----------------------------------------------------------------------------------------------------------------------
329  //----------------------------------------------------------------------------------------------------------------------
330  // we have to set the values here rather than above due to C++ spec
331  // see section implicitly declared as defaulted (8.4).
332  std::array<Real,3> m_openGL;
333  };
334 };
335 //----------------------------------------------------------------------------------------------------------------------
340 //----------------------------------------------------------------------------------------------------------------------
341 inline Vec3 operator *(Real _k, const Vec3 &_v) noexcept
342 {
343  return Vec3(_k*_v.m_x, _k*_v.m_y, _k*_v.m_z);
344 }
345 
346 
347 } // end namespace ngl
348 #endif
349 
350 //----------------------------------------------------------------------------------------------------------------------
bool operator==(const ngl::Mat3 &_m1, const ngl::Mat3 &_m2)
Definition: Mat3.h:297
Mat3 basic 3x3 matrix for certain glsl ops.
Definition: Mat3.h:43
static Vec3 right()
simple static method to return X right vector
Definition: Vec3.h:286
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
main definition of types and namespace
#define NGL_DLLEXPORT
Definition: Types.h:65
static Vec3 in()
simple static method to return Z out vector
Definition: Vec3.h:290
Vec3()
default ctor use default and set to (0.0f,0.0f,0.0f) as attributes are initialised ...
Definition: Vec3.h:59
Real m_r
r component
Definition: Vec3.h:317
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
implementation files for RibExport class
Definition: AABB.cpp:22
Real * openGL() noexcept
accesor to the m_openGL array returns the address of the 0th element
Definition: Vec3.h:270
GLuint GLsizei GLsizei * length
Definition: glew.h:1828
GLenum clamp
Definition: glew.h:2169
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
Real m_y
y component
Definition: Vec3.h:311
std::array< Real, 3 > m_openGL
array of four floats mapped to the x,y,z,w components of the Vec3 useful for openGL fv data types thi...
Definition: Vec3.h:332
Real m_b
b component
Definition: Vec3.h:319
Real m_x
x component
Definition: Vec3.h:310
static Vec3 left()
simple static method to return X left vector
Definition: Vec3.h:282
static Vec3 up()
simple static method to return Y up vector
Definition: Vec3.h:274
Vec2 operator*(Real _k, const Vec2 &_v) noexcept
scalar * vector operator
Definition: Vec2.h:286
const Real & operator[](size_t &_i) const noexcept
[] index operator to access the index component of the Vec3
Definition: Vec3.h:108
static Vec3 zero()
simple static method to return zero vector
Definition: Vec3.h:298
Real m_g
g component
Definition: Vec3.h:318
static Vec3 out()
simple static method to return Z in vector
Definition: Vec3.h:294
static Vec3 down()
simple static method to return Y down vector
Definition: Vec3.h:278
Vec3(Real _x, Real _y, Real _z) noexcept
initialise the constructor from 3 floats
Definition: Vec3.h:70
Real m_z
z component
Definition: Vec3.h:312
GLclampf f
Definition: glew.h:3511