NGL  6.5
The NCCA Graphics Library
Vec2.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 VEC2_H_
18 #define VEC2_H_
19 //----------------------------------------------------------------------------------------------------------------------
22 //----------------------------------------------------------------------------------------------------------------------
23 
24 // must include types.h first for Real and GLEW if required
25 #include "Types.h"
26 #include <array>
27 //----------------------------------------------------------------------------------------------------------------------
36 //----------------------------------------------------------------------------------------------------------------------
37 
38 
39 namespace ngl
40 {
41 
42 //----------------------------------------------------------------------------------------------------------------------
46 //----------------------------------------------------------------------------------------------------------------------
47 
48 class Vec4;
50 {
51 
52 public:
53 
54  //----------------------------------------------------------------------------------------------------------------------
58  //----------------------------------------------------------------------------------------------------------------------
59  void set( Real _x, Real _y ) noexcept;
60 
61  //----------------------------------------------------------------------------------------------------------------------
64  //----------------------------------------------------------------------------------------------------------------------
65  void set(const Vec2& _v ) noexcept;
66  //----------------------------------------------------------------------------------------------------------------------
69  //----------------------------------------------------------------------------------------------------------------------
70  void set(const Vec4& _v) noexcept;
71 
72  //----------------------------------------------------------------------------------------------------------------------
75  //----------------------------------------------------------------------------------------------------------------------
76  void set( const Vec2* _v) noexcept;
77  //----------------------------------------------------------------------------------------------------------------------
80  //----------------------------------------------------------------------------------------------------------------------
81  Vec2( const Vec2& _v) noexcept :
82  m_x(_v.m_x),
83  m_y(_v.m_y){;}
84 
85  //----------------------------------------------------------------------------------------------------------------------
90  //----------------------------------------------------------------------------------------------------------------------
91  Vec2(Real _x=0.0, Real _y=0.0 ) noexcept:
92  m_x(_x),
93  m_y(_y){;}
94  //----------------------------------------------------------------------------------------------------------------------
97  //----------------------------------------------------------------------------------------------------------------------
98  Vec2( Real _x ) noexcept:
99  m_x(_x),
100  m_y(_x){;}
101 
102  //----------------------------------------------------------------------------------------------------------------------
104  //----------------------------------------------------------------------------------------------------------------------
105  void null() noexcept;
106  //----------------------------------------------------------------------------------------------------------------------
109  //----------------------------------------------------------------------------------------------------------------------
110  Real& operator[]( int _i ) noexcept;
111  //----------------------------------------------------------------------------------------------------------------------
114  //----------------------------------------------------------------------------------------------------------------------
115 
116  const Real& operator[]( int _i ) const noexcept{ return m_openGL[_i]; }
117 
118 
119  //----------------------------------------------------------------------------------------------------------------------
122  //----------------------------------------------------------------------------------------------------------------------
123  void operator+=(const Vec2 &_v ) noexcept;
124 
125  //----------------------------------------------------------------------------------------------------------------------
128  //----------------------------------------------------------------------------------------------------------------------
129  void operator-=( const Vec2& _v ) noexcept;
130 
131  //----------------------------------------------------------------------------------------------------------------------
135  //----------------------------------------------------------------------------------------------------------------------
136  Vec2 operator *(const Real &_i )const noexcept;
137  //----------------------------------------------------------------------------------------------------------------------
140  //----------------------------------------------------------------------------------------------------------------------
141  Real length() const noexcept;
142  //----------------------------------------------------------------------------------------------------------------------
145  //----------------------------------------------------------------------------------------------------------------------
146  Real lengthSquared() const noexcept;
147 
148 
149  //----------------------------------------------------------------------------------------------------------------------
153  //----------------------------------------------------------------------------------------------------------------------
154  Vec2 operator +( const Vec2 &_v )const noexcept;
155 
156  //----------------------------------------------------------------------------------------------------------------------
160  //----------------------------------------------------------------------------------------------------------------------
161  Vec2 operator/( Real _v )const noexcept;
162 
163  //----------------------------------------------------------------------------------------------------------------------
167  //----------------------------------------------------------------------------------------------------------------------
168  void operator/=( Real _v ) noexcept;
169  //----------------------------------------------------------------------------------------------------------------------
173  //----------------------------------------------------------------------------------------------------------------------
174  void operator*=( Real _v ) noexcept;
175  //----------------------------------------------------------------------------------------------------------------------
179  //----------------------------------------------------------------------------------------------------------------------
180  Vec2 operator-( const Vec2& _v )const noexcept;
181 
182  //----------------------------------------------------------------------------------------------------------------------
186  //----------------------------------------------------------------------------------------------------------------------
187  Vec2 operator*( const Vec2 &_v )const noexcept;
188 
189  //----------------------------------------------------------------------------------------------------------------------
193  //----------------------------------------------------------------------------------------------------------------------
194  Vec2 &operator =( const Vec2 &_v ) noexcept;
195 
196  //----------------------------------------------------------------------------------------------------------------------
198  //----------------------------------------------------------------------------------------------------------------------
199  Vec2 operator-() const noexcept;
200  //----------------------------------------------------------------------------------------------------------------------
204  //----------------------------------------------------------------------------------------------------------------------
205  bool operator==( const Vec2 &_v )const noexcept;
206  //----------------------------------------------------------------------------------------------------------------------
210  //----------------------------------------------------------------------------------------------------------------------
211  bool operator!=( const Vec2 &_v )const noexcept;
212  //----------------------------------------------------------------------------------------------------------------------
216  //----------------------------------------------------------------------------------------------------------------------
217  Vec2 operator/( const Vec2& _v )const noexcept;
218  //----------------------------------------------------------------------------------------------------------------------
222  //----------------------------------------------------------------------------------------------------------------------
223  Vec2 operator *( Real _i )const noexcept;
224  //----------------------------------------------------------------------------------------------------------------------
228  //----------------------------------------------------------------------------------------------------------------------
229  void normalize() noexcept;
230  //----------------------------------------------------------------------------------------------------------------------
234  //----------------------------------------------------------------------------------------------------------------------
235  Real dot( const Vec2 &_b )const noexcept;
236  //----------------------------------------------------------------------------------------------------------------------
238  //----------------------------------------------------------------------------------------------------------------------
239  Real* openGL() noexcept{return &m_openGL[0];}
240 
244 public :
245 #pragma pack(push,1)
246 
247 #ifndef BUILDING_DOCS
248  union
249  {
250  struct
251  {
252 #endif
253  //----------------------------------------------------------------------------------------------------------------------
255  //----------------------------------------------------------------------------------------------------------------------
257  //----------------------------------------------------------------------------------------------------------------------
259  //----------------------------------------------------------------------------------------------------------------------
261 
262 #ifndef BUILDING_DOCS
263  };
264 #endif
265 #pragma pack(pop)
266  //----------------------------------------------------------------------------------------------------------------------
271  //----------------------------------------------------------------------------------------------------------------------
272 
273  std::array <Real,2> m_openGL;
274 #ifndef BUILDING_DOCS
275  };
276 #endif
277 
278 };
279 
280 //----------------------------------------------------------------------------------------------------------------------
285 //----------------------------------------------------------------------------------------------------------------------
286 inline Vec2 operator *(Real _k, const Vec2 &_v) noexcept
287 {
288  return Vec2(_k*_v.m_x, _k*_v.m_y);
289 }
290 
291 
292 
293 } // end namespace ngl
294 #endif
295 
296 //----------------------------------------------------------------------------------------------------------------------
bool operator==(const ngl::Mat3 &_m1, const ngl::Mat3 &_m2)
Definition: Mat3.h:297
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, 2 > m_openGL
array of four floats mapped to the x,y,z,w components of the Vec2 useful for openGL fv data types thi...
Definition: Vec2.h:273
#define NGL_DLLEXPORT
Definition: Types.h:65
Real m_x
x component of the Vec2
Definition: Vec2.h:256
Vec2(Real _x=0.0, Real _y=0.0) noexcept
initialise the constructor from 3 or 4 Real
Definition: Vec2.h:91
implementation files for RibExport class
Definition: AABB.cpp:22
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_y
y component of the Vec2
Definition: Vec2.h:260
Vec2(const Vec2 &_v) noexcept
copy ctor
Definition: Vec2.h:81
Vec2(Real _x) noexcept
ctor using a single float all components are set to the value _x
Definition: Vec2.h:98
Vec2 operator*(Real _k, const Vec2 &_v) noexcept
scalar * vector operator
Definition: Vec2.h:286
Real * openGL() noexcept
accesor to the m_openGL matrix returns the address of the 0th element
Definition: Vec2.h:239
const Real & operator[](int _i) const noexcept
[] index operator to access the index component of the Vec2
Definition: Vec2.h:116