NGL  6.5
The NCCA Graphics Library
Util.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 UTIL_H_
18 #define UTIL_H_
19 // must include types.h first for Real and GLEW if required
20 #include "Types.h"
21 #include "Vec4.h"
22 #include <cmath>
23 #include <string>
24 //----------------------------------------------------------------------------------------------------------------------
27 //----------------------------------------------------------------------------------------------------------------------
28 
29 namespace ngl
30 {
31 //----------------------------------------------------------------------------------------------------------------------
37 //----------------------------------------------------------------------------------------------------------------------
39 //----------------------------------------------------------------------------------------------------------------------
40 constexpr Real TWO_PI= Real(2*M_PI); //6.28318530717958647692 //360
41 //----------------------------------------------------------------------------------------------------------------------
43 //----------------------------------------------------------------------------------------------------------------------
44 constexpr Real PI=Real(M_PI); //3.14159265358979323846 //180
45 //----------------------------------------------------------------------------------------------------------------------
47 //----------------------------------------------------------------------------------------------------------------------
48 constexpr Real PI2=Real(M_PI/2.0); //1.57079632679489661923 //90
49 //----------------------------------------------------------------------------------------------------------------------
51 //----------------------------------------------------------------------------------------------------------------------
52 constexpr Real PI4=Real(M_PI/4.0); //0.785398163397448309615 //45
53 //----------------------------------------------------------------------------------------------------------------------
59 //----------------------------------------------------------------------------------------------------------------------
60 extern NGL_DLLEXPORT Vec3 calcNormal( const Vec4 &_p1, const Vec4 &_p2, const Vec4 &_p3 ) noexcept;
61 //----------------------------------------------------------------------------------------------------------------------
67 //----------------------------------------------------------------------------------------------------------------------
68 extern NGL_DLLEXPORT Vec3 calcNormal( const Vec3 &_p1, const Vec3 &_p2, const Vec3 &_p3 ) noexcept;
69 //----------------------------------------------------------------------------------------------------------------------
76 //----------------------------------------------------------------------------------------------------------------------
77 
78 NGL_DLLEXPORT Mat4 perspective(Real _fovy,Real _aspect, Real _zNear, Real _zFar) noexcept;
79 
80 //----------------------------------------------------------------------------------------------------------------------
88 //----------------------------------------------------------------------------------------------------------------------
89 NGL_DLLEXPORT Mat4 perspectiveFov(Real const & _fov, Real const & _width, Real const & _height, Real const & _zNear, Real const & _zFar) noexcept;
90 //----------------------------------------------------------------------------------------------------------------------
96 //----------------------------------------------------------------------------------------------------------------------
97 NGL_DLLEXPORT Mat4 infinitePerspective(Real _fovy, Real _aspect, Real _zNear) noexcept;
98 
99 
100 //----------------------------------------------------------------------------------------------------------------------
106 //----------------------------------------------------------------------------------------------------------------------
107 NGL_DLLEXPORT Mat4 lookAt(const Vec3 & _eye,const Vec3 & _center,const Vec3 & _up) noexcept;
108 
109 //----------------------------------------------------------------------------------------------------------------------
118 //----------------------------------------------------------------------------------------------------------------------
119 NGL_DLLEXPORT Mat4 ortho(Real _left, Real _right, Real _bottom, Real _top, Real _zNear, Real _zFar) noexcept;
120 
121 //----------------------------------------------------------------------------------------------------------------------
128 //----------------------------------------------------------------------------------------------------------------------
129 NGL_DLLEXPORT Mat4 ortho(Real _left, Real _right, Real _bottom, Real _top) noexcept;
130 
131 //----------------------------------------------------------------------------------------------------------------------
140 //----------------------------------------------------------------------------------------------------------------------
141 NGL_DLLEXPORT Mat4 frustum(Real _left, Real _right, Real _bottom, Real _top, Real _nearVal, Real _farVal) noexcept;
142 //----------------------------------------------------------------------------------------------------------------------
149 //----------------------------------------------------------------------------------------------------------------------
150 NGL_DLLEXPORT Vec3 unProject(const Vec3 &_win, const Mat4 &_model, const Mat4 &_project, const Vec4 &_viewport ) noexcept;
151 
152 NGL_DLLEXPORT Vec3 project(const Vec3 &_pos, const Mat4 &_model, const Mat4 &_project, const Vec4 &_viewport ) noexcept;
153 
154 //----------------------------------------------------------------------------------------------------------------------
158 //----------------------------------------------------------------------------------------------------------------------
159 extern NGL_DLLEXPORT Real radians(const Real _deg ) noexcept;
160 //----------------------------------------------------------------------------------------------------------------------
164 //----------------------------------------------------------------------------------------------------------------------
165 extern NGL_DLLEXPORT Real degrees( const Real _rad ) noexcept;
166 
167 //----------------------------------------------------------------------------------------------------------------------
171 //----------------------------------------------------------------------------------------------------------------------
172 extern NGL_DLLEXPORT bool isPowerOfTwo (unsigned int _x) noexcept;
173 //----------------------------------------------------------------------------------------------------------------------
177 //----------------------------------------------------------------------------------------------------------------------
178 
179 extern NGL_DLLEXPORT unsigned int nextPow2(unsigned int _x) noexcept;
180 
181 
182 //----------------------------------------------------------------------------------------------------------------------
186 //----------------------------------------------------------------------------------------------------------------------
187 extern NGL_DLLEXPORT void NGLCheckGLError(const std::string &_file, const int _line ) noexcept;
188 //----------------------------------------------------------------------------------------------------------------------
195 //----------------------------------------------------------------------------------------------------------------------
196 template <typename T> T lerp(T _a, T _b, Real _t) noexcept
197 {
198  T p;
199  p=_a+(_b-_a)*_t;
200  return p;
201 }
202 
203 template <typename T> T trigInterp(T _a, T _b, Real _t ) noexcept
204 {
205  Real angle=radians(90.0f*_t);
206  return _a*cos(angle)*cos(angle)+_b*sin(angle)*sin(angle);
207 }
208 
209 template <typename T> T cubic( T _a, T _b,Real _t) noexcept
210 {
211  Real v1=(2.0f*_t*_t*_t)-3.0f*(_t*_t)+1.0f;
212  Real v2=-(2.0f*_t*_t*_t)+3.0f*(_t*_t);
213  return (_a*v1)+(_b*v2);
214 }
215 
216 
217 
218 }
219 #endif
220 
221 //----------------------------------------------------------------------------------------------------------------------
simple Vector class for OpenGL graphics, contains overloaded operators for most math functions...
Definition: Vec4.h:57
main definition of types and namespace
NGL_DLLEXPORT Vec3 calcNormal(const Vec4 &_p1, const Vec4 &_p2, const Vec4 &_p3) noexcept
calculates the normal from 3 points and return the new normal as a Vector
Definition: Util.cpp:31
constexpr Real PI
pre-compute the value for value for PI based on system M_PI
Definition: Util.h:44
#define NGL_DLLEXPORT
Definition: Types.h:65
T trigInterp(T _a, T _b, Real _t) noexcept
Definition: Util.h:203
constexpr Real PI2
pre-compute the value for value for PI/2.0
Definition: Util.h:48
GLfloat GLfloat v1
Definition: glew.h:1855
GLfloat GLfloat GLfloat v2
Definition: glew.h:1859
NGL_DLLEXPORT Mat4 perspectiveFov(Real const &_fov, Real const &_width, Real const &_height, Real const &_zNear, Real const &_zFar) noexcept
computer a perspective projection matrix similar to the one from the GLM library this is to help make...
Definition: Util.cpp:162
simple Vec3 encapsulates a 3 float object like glsl vec3 but not maths use the Vec3 class for maths a...
Definition: Vec3.h:51
NGL_DLLEXPORT Mat4 frustum(Real _left, Real _right, Real _bottom, Real _top, Real _nearVal, Real _farVal) noexcept
calculate frustum matrix similar to the one from the GLM library this is to help make porting glm cod...
Definition: Util.cpp:251
NGL_DLLEXPORT bool isPowerOfTwo(unsigned int _x) noexcept
returns if value is a power of 2
Definition: Util.cpp:301
implementation files for RibExport class
Definition: AABB.cpp:22
PRECISION Real
create a variable called Real which is the main data type we use (GLfloat for most cases) ...
Definition: Types.h:127
encapsulates a 4d Homogenous Point / Vector object
NGL_DLLEXPORT Mat4 lookAt(const Vec3 &_eye, const Vec3 &_center, const Vec3 &_up) noexcept
calculate a look at matrix similar to the one from the GLM library this is to help make porting glm c...
Definition: Util.cpp:201
NGL_DLLEXPORT Mat4 ortho(Real _left, Real _right, Real _bottom, Real _top, Real _zNear, Real _zFar) noexcept
calculate an ortho graphic projection at matrix similar to the one from the GLM library this is to he...
Definition: Util.cpp:228
T cubic(T _a, T _b, Real _t) noexcept
Definition: Util.h:209
constexpr Real TWO_PI
pre-compute the value for value for 2*PI convert to float to suppress windows warning as well ...
Definition: Util.h:40
NGL_DLLEXPORT Vec3 project(const Vec3 &_pos, const Mat4 &_model, const Mat4 &_project, const Vec4 &_viewport) noexcept
Definition: Util.cpp:284
GLfloat GLfloat p
Definition: glew.h:16654
NGL_DLLEXPORT Real degrees(const Real _rad) noexcept
converts Radians to Degrees
Definition: Util.cpp:95
NGL_DLLEXPORT Real radians(const Real _deg)
converts Degrees to Radians
Definition: Util.cpp:89
Matrix Class to do simple matrix operations included operator overloaded functions for maths and matr...
Definition: Mat4.h:58
GLdouble angle
Definition: glew.h:9175
NGL_DLLEXPORT Mat4 perspective(Real _fovy, Real _aspect, Real _zNear, Real _zFar) noexcept
computer a perspective projection matrix similar to the one from the GLM library this is to help make...
Definition: Util.cpp:133
NGL_DLLEXPORT Vec3 unProject(const Vec3 &_win, const Mat4 &_model, const Mat4 &_project, const Vec4 &_viewport) noexcept
unproject points similar to the one from the GLM library this is to help make porting glm code easier...
Definition: Util.cpp:265
NGL_DLLEXPORT void NGLCheckGLError(const std::string &_file, const int _line) noexcept
check for openGL errors and print out.
Definition: Util.cpp:101
GLsizei const GLchar *const * string
Definition: glew.h:1847
NGL_DLLEXPORT Mat4 infinitePerspective(Real _fovy, Real _aspect, Real _zNear) noexcept
computer a perspective projection matrix similar to the one from the GLM library this is to help make...
Definition: Util.cpp:180
NGL_DLLEXPORT unsigned int nextPow2(unsigned int _x) noexcept
rounds up the value to next power of 2
Definition: Util.cpp:312
T lerp(T _a, T _b, Real _t) noexcept
a simple template function for Linear Interpolation requires that any classes have ...
Definition: Util.h:196
constexpr Real PI4
pre-compute the value for value for PI/4.0
Definition: Util.h:52
GLclampf f
Definition: glew.h:3511