NGL  6.5
The NCCA Graphics Library
Vec4.h
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 #ifndef VEC4_H_
18 #define VEC4_H_
19 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 
24 // must include types.h first for Real and GLEW if required
25 #include "Types.h"
26 #include "Vec2.h"
27 #include "Vec3.h"
28 #include <array>
29 //----------------------------------------------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------------------------------------------
41 
42 
43 namespace ngl
44 {
45 // pre-declare the matrix class
46 
47 class Mat4;
48 
49 //----------------------------------------------------------------------------------------------------------------------
54 //----------------------------------------------------------------------------------------------------------------------
55 
56 
58 {
59 // added after clang++ build
60 friend class Mat4;
61 friend class VBOPrimitives;
62 friend class Transformation;
63 friend class Quaternion;
64 friend class Obj;
65 
66 public:
67  Vec4() : m_x(0.0f),m_y(0.0f),m_z(0.0f),m_w(1.0f){}
68  //----------------------------------------------------------------------------------------------------------------------
71  //----------------------------------------------------------------------------------------------------------------------
72  Vec4(const Vec4& _v)=default;
73  //----------------------------------------------------------------------------------------------------------------------
76  //----------------------------------------------------------------------------------------------------------------------
77  Vec4(const Vec3& _v, float _w=1.0f) noexcept:
78  m_x(_v.m_x),
79  m_y(_v.m_y),
80  m_z(_v.m_z),
81  m_w(_w){;}
82 
83  //----------------------------------------------------------------------------------------------------------------------
89  //----------------------------------------------------------------------------------------------------------------------
90  Vec4( Real _x, Real _y, Real _z, Real _w=1.0f ) noexcept:
91  m_x(_x),
92  m_y(_y),
93  m_z(_z),
94  m_w(_w){;}
95 
96  //----------------------------------------------------------------------------------------------------------------------
100  //----------------------------------------------------------------------------------------------------------------------
101  Real dot( const Vec4 &_b )const noexcept;
102  //----------------------------------------------------------------------------------------------------------------------
108  //----------------------------------------------------------------------------------------------------------------------
109  void set(Real _x, Real _y, Real _z, Real _w=1.0) noexcept;
110 
111  //----------------------------------------------------------------------------------------------------------------------
114  //----------------------------------------------------------------------------------------------------------------------
115  void set(const Vec4& _v ) noexcept;
116  //----------------------------------------------------------------------------------------------------------------------
119  //----------------------------------------------------------------------------------------------------------------------
120  void set( const Vec3 &_v ) noexcept;
121  //----------------------------------------------------------------------------------------------------------------------
123  //----------------------------------------------------------------------------------------------------------------------
124  void null() noexcept;
125  //----------------------------------------------------------------------------------------------------------------------
127  //----------------------------------------------------------------------------------------------------------------------
128  Vec3 toVec3() const noexcept{ return Vec3(m_x,m_y,m_z);}
129 
130  //----------------------------------------------------------------------------------------------------------------------
132  //----------------------------------------------------------------------------------------------------------------------
133  Vec2 toVec2() const noexcept{ return Vec2(m_x,m_y);}
134  //----------------------------------------------------------------------------------------------------------------------
137  //----------------------------------------------------------------------------------------------------------------------
138  Real& operator[]( int _i ) noexcept;
139  const Real& operator[]( int _i ) const noexcept{ return m_openGL[_i]; }
140 
141  //----------------------------------------------------------------------------------------------------------------------
144  //----------------------------------------------------------------------------------------------------------------------
145  Real length() const noexcept;
146  //----------------------------------------------------------------------------------------------------------------------
151  //----------------------------------------------------------------------------------------------------------------------
152  Vec4 &normalize() noexcept;
153  //----------------------------------------------------------------------------------------------------------------------
157  //----------------------------------------------------------------------------------------------------------------------
158  void cross(const Vec4& _v1, const Vec4& _v2) noexcept;
159  //----------------------------------------------------------------------------------------------------------------------
163  //----------------------------------------------------------------------------------------------------------------------
164  Vec4 cross(const Vec4& _b)const noexcept;
165  //----------------------------------------------------------------------------------------------------------------------
168  //----------------------------------------------------------------------------------------------------------------------
169  void operator+=( const Vec4& _v) noexcept;
170 
171  //----------------------------------------------------------------------------------------------------------------------
174  //----------------------------------------------------------------------------------------------------------------------
175  void operator-=(const Vec4& _v ) noexcept;
176 
177  //----------------------------------------------------------------------------------------------------------------------
181  //----------------------------------------------------------------------------------------------------------------------
182  Vec4 operator *(Real _i)const noexcept;
183 
184  //----------------------------------------------------------------------------------------------------------------------
188  //----------------------------------------------------------------------------------------------------------------------
189  Vec4 operator +(const Vec4 &_v)const noexcept;
190 
191  //----------------------------------------------------------------------------------------------------------------------
195  //----------------------------------------------------------------------------------------------------------------------
196  Vec4 operator/(Real _v)const noexcept;
197 
198  //----------------------------------------------------------------------------------------------------------------------
202  //----------------------------------------------------------------------------------------------------------------------
203  void operator/=(Real _v) noexcept;
204  //----------------------------------------------------------------------------------------------------------------------
208  //----------------------------------------------------------------------------------------------------------------------
209  void operator*=( Real _v) noexcept;
210  //----------------------------------------------------------------------------------------------------------------------
214  //----------------------------------------------------------------------------------------------------------------------
215  Vec4 operator-(const Vec4& _v)const noexcept;
216 
217  //----------------------------------------------------------------------------------------------------------------------
221  //----------------------------------------------------------------------------------------------------------------------
222  Vec4 operator*( const Vec4 &_v)const noexcept;
223 
224  //----------------------------------------------------------------------------------------------------------------------
228  //----------------------------------------------------------------------------------------------------------------------
229  Vec4 &operator =( const Vec4 &_v) noexcept;
230  //----------------------------------------------------------------------------------------------------------------------
234  //----------------------------------------------------------------------------------------------------------------------
235  Vec4 &operator =(const Vec3 &_v) noexcept;
236 
237  //----------------------------------------------------------------------------------------------------------------------
241  //----------------------------------------------------------------------------------------------------------------------
242  Vec4 &operator =( Real _v) noexcept;
243 
244  //----------------------------------------------------------------------------------------------------------------------
246  //----------------------------------------------------------------------------------------------------------------------
247  Vec4 &operator-() noexcept;
248  //----------------------------------------------------------------------------------------------------------------------
252  //----------------------------------------------------------------------------------------------------------------------
253  bool operator==( const Vec4 &_v)const noexcept;
254  //----------------------------------------------------------------------------------------------------------------------
258  //----------------------------------------------------------------------------------------------------------------------
259  bool operator!=( const Vec4 &_v)const noexcept;
260  //----------------------------------------------------------------------------------------------------------------------
264  //----------------------------------------------------------------------------------------------------------------------
265  Vec4 operator/( const Vec4& _v)const noexcept;
266  //----------------------------------------------------------------------------------------------------------------------
270  //----------------------------------------------------------------------------------------------------------------------
271  Real angleBetween( const Vec4 &_v)const noexcept;
272  //----------------------------------------------------------------------------------------------------------------------
276  //----------------------------------------------------------------------------------------------------------------------
277  Real inner( const Vec4& _v)const noexcept;
278  //----------------------------------------------------------------------------------------------------------------------
282  //----------------------------------------------------------------------------------------------------------------------
283  Vec4 outer(const Vec4& _v)const noexcept;
284  //----------------------------------------------------------------------------------------------------------------------
287  //----------------------------------------------------------------------------------------------------------------------
288  Real lengthSquared() const noexcept;
289  //----------------------------------------------------------------------------------------------------------------------
292  //----------------------------------------------------------------------------------------------------------------------
293  Vec4 operator*( const Mat4 &_m)const noexcept;
294  //----------------------------------------------------------------------------------------------------------------------
296  //----------------------------------------------------------------------------------------------------------------------
297  Real* openGL() noexcept{return &m_openGL[0];}
298 
299  static Vec4 up() {return Vec4(0.0f,1.0f,0.0f,0.0f); }
300  static Vec4 down() {return Vec4(0.0f,-1.0f,0.0f,0.0f); }
301 
302  static Vec4 left() {return Vec4(-1.0f,0.0f,0.0f,0.0f); }
303  static Vec4 right() {return Vec4(1.0f,0.0f,0.0f,0.0f); }
304 
305  static Vec4 in() {return Vec4(0.0f,0.0f,1.0f,0.0f); }
306  static Vec4 out() {return Vec4(0.0f,0.0f,-1.0f,0.0f); }
307 
308  static Vec4 zero() {return Vec4(0.0f,0.0f,0.0f,0.0f); }
309 
310 
314 public :
315 #pragma pack(push,1)
316 
317  union
318  {
319  struct
320  {
325  };
326  struct
327  {
332  };
333 #pragma pack(pop)
334  //----------------------------------------------------------------------------------------------------------------------
341  //----------------------------------------------------------------------------------------------------------------------
342  std::array <Real,4> m_openGL;
343  };
344 
345 };
346 
347 
348 
349 //----------------------------------------------------------------------------------------------------------------------
354 //----------------------------------------------------------------------------------------------------------------------
355 inline Vec4 operator *(Real _k, const Vec4 &_v) noexcept
356 {
357  return Vec4(_k*_v.m_x, _k*_v.m_y, _k*_v.m_z,_v.m_w);
358 }
359 
360 }
361 #endif
362 
363 //----------------------------------------------------------------------------------------------------------------------
bool operator==(const ngl::Mat3 &_m1, const ngl::Mat3 &_m2)
Definition: Mat3.h:297
Real m_a
a component
Definition: Vec4.h:331
Vec4(Real _x, Real _y, Real _z, Real _w=1.0f) noexcept
initialise the constructor from 3 or 4 Real
Definition: Vec4.h:90
const Real & operator[](int _i) const noexcept
Definition: Vec4.h:139
simple Vec2 encapsulates a 3 float object like glsl Vec2 but not maths use the Vec2 class for maths a...
Definition: Vec2.h:49
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
main definition of types and namespace
std::array< Real, 4 > m_openGL
array of four floats mapped to the x,y,z,w components of the vector useful for openGL fv data types t...
Definition: Vec4.h:342
#define NGL_DLLEXPORT
Definition: Types.h:65
static Vec4 up()
Definition: Vec4.h:299
static Vec4 right()
Definition: Vec4.h:303
Real m_b
b component
Definition: Vec4.h:330
Vec3 toVec3() const noexcept
get as a Vec3 for glsl etc
Definition: Vec4.h:128
Vec4(const Vec3 &_v, float _w=1.0f) noexcept
copy ctor
Definition: Vec4.h:77
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
Vec4()
Definition: Vec4.h:67
GLuint GLsizei GLsizei * length
Definition: glew.h:1828
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_g
g component
Definition: Vec4.h:329
static Vec4 down()
Definition: Vec4.h:300
static Vec4 in()
Definition: Vec4.h:305
a simple 2 tuple container for compatibility with glsl
a simple 3 tuple container for compatibility with glsl
Real m_z
z component
Definition: Vec4.h:323
static Vec4 zero()
Definition: Vec4.h:308
static Vec4 out()
Definition: Vec4.h:306
Real m_x
x component
Definition: Vec4.h:321
Real m_r
r component
Definition: Vec4.h:328
Vec2 operator*(Real _k, const Vec2 &_v) noexcept
scalar * vector operator
Definition: Vec2.h:286
Transformation describes a transformation (translate, scale, rotation) modifed by j macey and include...
Real * openGL() noexcept
accesor to the m_openGL matrix returns the address of the 0th element
Definition: Vec4.h:297
used to load in an alias wave front obj format file and draw using open gl has been completly re-writ...
Definition: Obj.h:53
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
static Vec4 left()
Definition: Vec4.h:302
Real m_w
w component 0 for vector 1 for point
Definition: Vec4.h:324
Real m_y
y component
Definition: Vec4.h:322
Vec2 toVec2() const noexcept
get as a Vec2 for glsl etc
Definition: Vec4.h:133
GLclampf f
Definition: glew.h:3511