├── ClipMap.sln ├── bin32 ├── ClipMaps.exe └── glut32.dll ├── bin64 ├── ClipMaps.exe └── glut32.dll ├── ext ├── GL │ ├── glext.h │ ├── glut.def │ ├── glut.h │ ├── glxext.h │ └── wglext.h ├── glew.cpp ├── glew.h ├── glxew.h ├── mathlib │ ├── CVS │ │ ├── Entries │ │ ├── Entries.Extra │ │ ├── Entries.Extra.Old │ │ ├── Entries.Old │ │ ├── Repository │ │ └── Root │ ├── _matrix33.h │ ├── _matrix33_sse.h │ ├── _matrix44.h │ ├── _matrix44_sse.h │ ├── _vector2.h │ ├── _vector3.h │ ├── _vector3_sse.h │ ├── _vector4.h │ ├── _vector4_sse.h │ ├── bbox.h │ ├── envelopecurve.h │ ├── euler.h │ ├── eulerangles.h │ ├── line.h │ ├── matrix.h │ ├── matrixdefs.h │ ├── nmath.h │ ├── noise.h │ ├── ntypes.h │ ├── ntypes.h~ │ ├── pknorm.h │ ├── plane.h │ ├── polar.h │ ├── quaternion.h │ ├── rectangle.h │ ├── sphere.h │ ├── transform33.h │ ├── transform44.h │ ├── triangle.h │ ├── vector.h │ └── vector3envelopecurve.h └── wglew.h ├── lib32 └── glut32.lib ├── lib64 └── glut32.lib ├── shader ├── frag.txt └── vs.txt └── src ├── Bmp.cpp ├── Bmp.h ├── ClipMap.vcproj ├── ClipMap.vcxproj ├── ClipMap.vcxproj.filters ├── ClipMap.vcxproj.user ├── Main.cpp ├── core.h ├── glsl.h └── ogl.h /ClipMap.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 11.00 2 | # Visual C++ Express 2010 3 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ClipMap3D", "src\ClipMap.vcxproj", "{98864040-E723-43CF-9479-6FBCDF2F6C51}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Win32 = Debug|Win32 8 | Debug|x64 = Debug|x64 9 | Release|Win32 = Release|Win32 10 | Release|x64 = Release|x64 11 | EndGlobalSection 12 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 13 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Debug|Win32.ActiveCfg = Debug|Win32 14 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Debug|Win32.Build.0 = Debug|Win32 15 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Debug|x64.ActiveCfg = Debug|x64 16 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Debug|x64.Build.0 = Debug|x64 17 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Release|Win32.ActiveCfg = Release|Win32 18 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Release|Win32.Build.0 = Release|Win32 19 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Release|x64.ActiveCfg = Release|x64 20 | {98864040-E723-43CF-9479-6FBCDF2F6C51}.Release|x64.Build.0 = Release|x64 21 | EndGlobalSection 22 | GlobalSection(SolutionProperties) = preSolution 23 | HideSolutionNode = FALSE 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /bin32/ClipMaps.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Geometry-Clipmap-Tutorial-in-OpenGL/154218b1e405d0e98aba3de41d488b1822a4725d/bin32/ClipMaps.exe -------------------------------------------------------------------------------- /bin32/glut32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Geometry-Clipmap-Tutorial-in-OpenGL/154218b1e405d0e98aba3de41d488b1822a4725d/bin32/glut32.dll -------------------------------------------------------------------------------- /bin64/ClipMaps.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Geometry-Clipmap-Tutorial-in-OpenGL/154218b1e405d0e98aba3de41d488b1822a4725d/bin64/ClipMaps.exe -------------------------------------------------------------------------------- /bin64/glut32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Geometry-Clipmap-Tutorial-in-OpenGL/154218b1e405d0e98aba3de41d488b1822a4725d/bin64/glut32.dll -------------------------------------------------------------------------------- /ext/GL/glut.def: -------------------------------------------------------------------------------- 1 | DESCRIPTION 'OpenGL Utility Toolkit for Win32' 2 | 3 | VERSION 3.7 4 | 5 | EXPORTS 6 | 7 | glutAddMenuEntry 8 | glutAddSubMenu 9 | glutAttachMenu 10 | glutBitmapCharacter 11 | glutBitmapLength 12 | glutBitmapWidth 13 | glutButtonBoxFunc 14 | glutChangeToMenuEntry 15 | glutChangeToSubMenu 16 | glutCopyColormap 17 | glutCreateMenu 18 | __glutCreateMenuWithExit 19 | glutCreateSubWindow 20 | glutCreateWindow 21 | __glutCreateWindowWithExit 22 | glutDestroyMenu 23 | glutDestroyWindow 24 | glutDetachMenu 25 | glutDeviceGet 26 | glutDialsFunc 27 | glutDisplayFunc 28 | glutEnterGameMode 29 | glutEntryFunc 30 | glutEstablishOverlay 31 | glutExtensionSupported 32 | glutForceJoystickFunc 33 | glutFullScreen 34 | glutGameModeGet 35 | glutGameModeString 36 | glutGet 37 | glutGetColor 38 | glutGetMenu 39 | glutGetModifiers 40 | glutGetWindow 41 | glutHideOverlay 42 | glutHideWindow 43 | glutIconifyWindow 44 | glutIdleFunc 45 | glutIgnoreKeyRepeat 46 | glutInit 47 | __glutInitWithExit 48 | glutInitDisplayMode 49 | glutInitDisplayString 50 | glutInitWindowPosition 51 | glutInitWindowSize 52 | glutJoystickFunc 53 | glutKeyboardFunc 54 | glutKeyboardUpFunc 55 | glutLayerGet 56 | glutLeaveGameMode 57 | glutMainLoop 58 | glutMenuStateFunc 59 | glutMenuStatusFunc 60 | glutMotionFunc 61 | glutMouseFunc 62 | glutOverlayDisplayFunc 63 | glutPassiveMotionFunc 64 | glutPopWindow 65 | glutPositionWindow 66 | glutPostOverlayRedisplay 67 | glutPostRedisplay 68 | glutPostWindowOverlayRedisplay 69 | glutPostWindowRedisplay 70 | glutPushWindow 71 | glutRemoveMenuItem 72 | glutRemoveOverlay 73 | glutReportErrors 74 | glutReshapeFunc 75 | glutReshapeWindow 76 | glutSetColor 77 | glutSetCursor 78 | glutSetIconTitle 79 | glutSetKeyRepeat 80 | glutSetMenu 81 | glutSetWindow 82 | glutSetWindowTitle 83 | glutSetupVideoResizing 84 | glutShowOverlay 85 | glutShowWindow 86 | glutSolidCone 87 | glutSolidCube 88 | glutSolidDodecahedron 89 | glutSolidIcosahedron 90 | glutSolidOctahedron 91 | glutSolidSphere 92 | glutSolidTeapot 93 | glutSolidTetrahedron 94 | glutSolidTorus 95 | glutSpaceballButtonFunc 96 | glutSpaceballMotionFunc 97 | glutSpaceballRotateFunc 98 | glutSpecialFunc 99 | glutSpecialUpFunc 100 | glutStopVideoResizing 101 | glutStrokeCharacter 102 | glutStrokeLength 103 | glutStrokeWidth 104 | glutSwapBuffers 105 | glutTabletButtonFunc 106 | glutTabletMotionFunc 107 | glutTimerFunc 108 | glutUseLayer 109 | glutVideoPan 110 | glutVideoResize 111 | glutVideoResizeGet 112 | glutVisibilityFunc 113 | glutWarpPointer 114 | glutWindowStatusFunc 115 | glutWireCone 116 | glutWireCube 117 | glutWireDodecahedron 118 | glutWireIcosahedron 119 | glutWireOctahedron 120 | glutWireSphere 121 | glutWireTeapot 122 | glutWireTetrahedron 123 | glutWireTorus 124 | ; __glutSetFCB 125 | ; __glutGetFCB 126 | 127 | -------------------------------------------------------------------------------- /ext/mathlib/CVS/Entries: -------------------------------------------------------------------------------- 1 | /_matrix33.h/1.5/Mon Sep 20 17:14:36 2004// 2 | /_matrix33_sse.h/1.4/Mon Sep 20 17:14:36 2004// 3 | /_matrix44_sse.h/1.5/Mon Sep 20 17:14:36 2004// 4 | /_vector2.h/1.8/Wed Oct 13 08:25:53 2004// 5 | /_vector3.h/1.13/Mon Sep 20 17:14:36 2004// 6 | /_vector3_sse.h/1.6/Mon Sep 20 17:14:36 2004// 7 | /_vector4.h/1.11/Mon Sep 20 17:14:36 2004// 8 | /_vector4_sse.h/1.6/Mon Sep 20 17:14:36 2004// 9 | /envelopecurve.h/1.13/Mon Sep 20 17:14:36 2004// 10 | /euler.h/1.1/Thu Jan 23 14:48:29 2003// 11 | /eulerangles.h/1.4/Mon Sep 20 17:14:36 2004// 12 | /line.h/1.6/Thu Dec 2 12:32:32 2004// 13 | /matrix.h/1.4/Fri Jun 4 13:17:32 2004// 14 | /matrixdefs.h/1.1/Thu Jan 23 14:48:29 2003// 15 | /noise.h/1.2/Mon Sep 20 17:14:36 2004// 16 | /pknorm.h/1.4/Mon Sep 20 17:14:36 2004// 17 | /plane.h/1.5/Mon Sep 20 17:14:36 2004// 18 | /polar.h/1.4/Mon Sep 20 17:14:36 2004// 19 | /quaternion.h/1.4/Mon Sep 20 17:14:36 2004// 20 | /rectangle.h/1.5/Mon Sep 20 17:14:36 2004// 21 | /transform33.h/1.2/Mon Sep 20 17:14:36 2004// 22 | /triangle.h/1.4/Mon Sep 20 17:14:36 2004// 23 | /vector.h/1.3/Fri Jun 4 13:17:32 2004// 24 | /vector3envelopecurve.h/1.3/Mon Sep 20 17:14:36 2004// 25 | /transform44.h/1.5/Mon Jan 24 11:25:48 2005// 26 | /nmath.h/1.13/Mon Feb 7 10:12:23 2005// 27 | /_matrix44.h/1.11/Fri Mar 18 11:28:29 2005// 28 | /bbox.h/1.17/Fri Mar 18 11:28:29 2005// 29 | /sphere.h/1.6/Fri Mar 18 11:28:29 2005// 30 | D 31 | -------------------------------------------------------------------------------- /ext/mathlib/CVS/Entries.Extra: -------------------------------------------------------------------------------- 1 | /_matrix33.h/////// 2 | /_matrix33_sse.h/////// 3 | /_matrix44_sse.h/////// 4 | /_vector2.h/////// 5 | /_vector3.h/////// 6 | /_vector3_sse.h/////// 7 | /_vector4.h/////// 8 | /_vector4_sse.h/////// 9 | /envelopecurve.h/////// 10 | /euler.h/////// 11 | /eulerangles.h/////// 12 | /line.h/////// 13 | /matrix.h/////// 14 | /matrixdefs.h/////// 15 | /noise.h/////// 16 | /pknorm.h/////// 17 | /plane.h/////// 18 | /polar.h/////// 19 | /quaternion.h/////// 20 | /rectangle.h/////// 21 | /transform33.h/////// 22 | /triangle.h/////// 23 | /vector.h/////// 24 | /vector3envelopecurve.h/////// 25 | /transform44.h/////// 26 | /nmath.h/////// 27 | /_matrix44.h/////// 28 | /bbox.h/////// 29 | /sphere.h/////// 30 | -------------------------------------------------------------------------------- /ext/mathlib/CVS/Entries.Extra.Old: -------------------------------------------------------------------------------- 1 | /_matrix33.h//// 2 | /_matrix33_sse.h//// 3 | /_matrix44.h//// 4 | /_matrix44_sse.h//// 5 | /_vector2.h//// 6 | /_vector3.h//// 7 | /_vector3_sse.h//// 8 | /_vector4.h//// 9 | /_vector4_sse.h//// 10 | /bbox.h//// 11 | /envelopecurve.h//// 12 | /euler.h//// 13 | /eulerangles.h//// 14 | /line.h//// 15 | /matrix.h//// 16 | /matrixdefs.h//// 17 | /noise.h//// 18 | /pknorm.h//// 19 | /plane.h//// 20 | /polar.h//// 21 | /quaternion.h//// 22 | /rectangle.h//// 23 | /sphere.h//// 24 | /transform33.h//// 25 | /triangle.h//// 26 | /vector.h//// 27 | /vector3envelopecurve.h//// 28 | /transform44.h//// 29 | /nmath.h//// 30 | -------------------------------------------------------------------------------- /ext/mathlib/CVS/Entries.Old: -------------------------------------------------------------------------------- 1 | /_matrix33.h/1.5/Mon Sep 20 17:14:36 2004// 2 | /_matrix33_sse.h/1.4/Mon Sep 20 17:14:36 2004// 3 | /_matrix44.h/1.10/Mon Sep 20 17:14:36 2004// 4 | /_matrix44_sse.h/1.5/Mon Sep 20 17:14:36 2004// 5 | /_vector2.h/1.8/Wed Oct 13 08:25:53 2004// 6 | /_vector3.h/1.13/Mon Sep 20 17:14:36 2004// 7 | /_vector3_sse.h/1.6/Mon Sep 20 17:14:36 2004// 8 | /_vector4.h/1.11/Mon Sep 20 17:14:36 2004// 9 | /_vector4_sse.h/1.6/Mon Sep 20 17:14:36 2004// 10 | /bbox.h/1.16/Fri Oct 8 20:06:42 2004// 11 | /envelopecurve.h/1.13/Mon Sep 20 17:14:36 2004// 12 | /euler.h/1.1/Thu Jan 23 14:48:29 2003// 13 | /eulerangles.h/1.4/Mon Sep 20 17:14:36 2004// 14 | /line.h/1.6/Thu Dec 2 12:32:32 2004// 15 | /matrix.h/1.4/Fri Jun 4 13:17:32 2004// 16 | /matrixdefs.h/1.1/Thu Jan 23 14:48:29 2003// 17 | /noise.h/1.2/Mon Sep 20 17:14:36 2004// 18 | /pknorm.h/1.4/Mon Sep 20 17:14:36 2004// 19 | /plane.h/1.5/Mon Sep 20 17:14:36 2004// 20 | /polar.h/1.4/Mon Sep 20 17:14:36 2004// 21 | /quaternion.h/1.4/Mon Sep 20 17:14:36 2004// 22 | /rectangle.h/1.5/Mon Sep 20 17:14:36 2004// 23 | /sphere.h/1.5/Mon Sep 20 17:14:36 2004// 24 | /transform33.h/1.2/Mon Sep 20 17:14:36 2004// 25 | /triangle.h/1.4/Mon Sep 20 17:14:36 2004// 26 | /vector.h/1.3/Fri Jun 4 13:17:32 2004// 27 | /vector3envelopecurve.h/1.3/Mon Sep 20 17:14:36 2004// 28 | /transform44.h/1.5/Mon Jan 24 11:25:48 2005// 29 | /nmath.h/1.13/Mon Feb 7 10:12:23 2005// 30 | D 31 | -------------------------------------------------------------------------------- /ext/mathlib/CVS/Repository: -------------------------------------------------------------------------------- 1 | radonlabs/code/nebula2/inc/mathlib 2 | -------------------------------------------------------------------------------- /ext/mathlib/CVS/Root: -------------------------------------------------------------------------------- 1 | :pserver:sven3@192.168.0.90:/mnt/hdc/cvsroot 2 | -------------------------------------------------------------------------------- /ext/mathlib/_vector2.h: -------------------------------------------------------------------------------- 1 | #ifndef _VECTOR2_H 2 | #define _VECTOR2_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class _vector2 6 | @ingroup Math 7 | 8 | Generic vector2 class. 9 | 10 | (C) 2002 RadonLabs GmbH 11 | */ 12 | #include "mathlib/nmath.h" 13 | #include 14 | 15 | //------------------------------------------------------------------------------ 16 | class _vector2 { 17 | public: 18 | /// constructor 1 19 | _vector2(); 20 | /// constructor 2 21 | _vector2(const float _x, const float _y); 22 | /// constructor 3 23 | _vector2(const _vector2& vec); 24 | /// constructor 4 25 | _vector2(const float* p); 26 | /// set elements 1 27 | void set(const float _x, const float _y); 28 | /// set elements 2 29 | void set(const _vector2& vec); 30 | /// set elements 3 31 | void set(const float* p); 32 | /// return length 33 | float len() const; 34 | /// normalize 35 | void norm(); 36 | /// in place add 37 | void operator+=(const _vector2& v0); 38 | /// in place sub 39 | void operator-=(const _vector2& v0); 40 | /// in place scalar mul 41 | void operator*=(const float s); 42 | /// in place scalar div 43 | void operator/=(const float s); 44 | /// fuzzy compare operator 45 | bool isequal(const _vector2& v, const float tol) const; 46 | /// fuzzy compare, returns -1, 0, +1 47 | int compare(const _vector2& v, float tol) const; 48 | /// rotate around P(0,0) 49 | void rotate(float angle); 50 | 51 | float x, y; 52 | }; 53 | 54 | //------------------------------------------------------------------------------ 55 | /** 56 | */ 57 | inline 58 | _vector2::_vector2() : 59 | x(0.0f), 60 | y(0.0f) 61 | { 62 | // empty 63 | } 64 | 65 | //------------------------------------------------------------------------------ 66 | /** 67 | */ 68 | inline 69 | _vector2::_vector2(const float _x, const float _y) : 70 | x(_x), 71 | y(_y) 72 | { 73 | // empty 74 | } 75 | 76 | //------------------------------------------------------------------------------ 77 | /** 78 | */ 79 | inline 80 | _vector2::_vector2(const _vector2& vec) : 81 | x(vec.x), 82 | y(vec.y) 83 | { 84 | // empty 85 | } 86 | 87 | //------------------------------------------------------------------------------ 88 | /** 89 | */ 90 | inline 91 | _vector2::_vector2(const float* p) : 92 | x(p[0]), 93 | y(p[1]) 94 | { 95 | // empty 96 | } 97 | 98 | //------------------------------------------------------------------------------ 99 | /** 100 | */ 101 | inline 102 | void 103 | _vector2::set(const float _x, const float _y) 104 | { 105 | x = _x; 106 | y = _y; 107 | } 108 | 109 | //------------------------------------------------------------------------------ 110 | /** 111 | */ 112 | inline 113 | void 114 | _vector2::set(const _vector2& v) 115 | { 116 | x = v.x; 117 | y = v.y; 118 | } 119 | 120 | //------------------------------------------------------------------------------ 121 | /** 122 | */ 123 | inline 124 | void 125 | _vector2::set(const float* p) 126 | { 127 | x = p[0]; 128 | y = p[1]; 129 | } 130 | 131 | //------------------------------------------------------------------------------ 132 | /** 133 | */ 134 | inline 135 | float 136 | _vector2::len() const 137 | { 138 | return (float) sqrt(x * x + y * y); 139 | } 140 | 141 | //------------------------------------------------------------------------------ 142 | /** 143 | */ 144 | inline 145 | void 146 | _vector2::norm() 147 | { 148 | float l = len(); 149 | if (l > TINY) 150 | { 151 | x /= l; 152 | y /= l; 153 | } 154 | } 155 | 156 | //------------------------------------------------------------------------------ 157 | /** 158 | */ 159 | inline 160 | void 161 | _vector2::operator +=(const _vector2& v0) 162 | { 163 | x += v0.x; 164 | y += v0.y; 165 | } 166 | 167 | //------------------------------------------------------------------------------ 168 | /** 169 | */ 170 | inline 171 | void 172 | _vector2::operator -=(const _vector2& v0) 173 | { 174 | x -= v0.x; 175 | y -= v0.y; 176 | } 177 | 178 | //------------------------------------------------------------------------------ 179 | /** 180 | */ 181 | inline 182 | void 183 | _vector2::operator *=(const float s) 184 | { 185 | x *= s; 186 | y *= s; 187 | } 188 | 189 | //------------------------------------------------------------------------------ 190 | /** 191 | */ 192 | inline 193 | void 194 | _vector2::operator /=(const float s) 195 | { 196 | x /= s; 197 | y /= s; 198 | } 199 | 200 | //------------------------------------------------------------------------------ 201 | /** 202 | */ 203 | inline 204 | bool 205 | _vector2::isequal(const _vector2& v, const float tol) const 206 | { 207 | if (fabs(v.x - x) > tol) return false; 208 | else if (fabs(v.y - y) > tol) return false; 209 | return true; 210 | } 211 | 212 | //------------------------------------------------------------------------------ 213 | /** 214 | */ 215 | inline 216 | int 217 | _vector2::compare(const _vector2& v, float tol) const 218 | { 219 | if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1; 220 | else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1; 221 | else return 0; 222 | } 223 | 224 | //------------------------------------------------------------------------------ 225 | /** 226 | */ 227 | inline 228 | void 229 | _vector2::rotate(float angle) 230 | { 231 | // rotates this one around P(0,0). 232 | float sa, ca; 233 | 234 | sa = (float) sin(angle); 235 | ca = (float) cos(angle); 236 | 237 | // "handmade" multiplication 238 | _vector2 help(ca * this->x - sa * this->y, 239 | sa * this->x + ca * this->y); 240 | 241 | *this = help; 242 | } 243 | 244 | //------------------------------------------------------------------------------ 245 | /** 246 | */ 247 | static 248 | inline 249 | _vector2 operator +(const _vector2& v0, const _vector2& v1) 250 | { 251 | return _vector2(v0.x + v1.x, v0.y + v1.y); 252 | } 253 | 254 | //------------------------------------------------------------------------------ 255 | /** 256 | */ 257 | static 258 | inline 259 | _vector2 operator -(const _vector2& v0, const _vector2& v1) 260 | { 261 | return _vector2(v0.x - v1.x, v0.y - v1.y); 262 | } 263 | 264 | //------------------------------------------------------------------------------ 265 | /** 266 | */ 267 | static 268 | inline 269 | _vector2 operator *(const _vector2& v0, const float s) 270 | { 271 | return _vector2(v0.x * s, v0.y * s); 272 | } 273 | 274 | //------------------------------------------------------------------------------ 275 | /** 276 | */ 277 | static 278 | inline 279 | _vector2 operator -(const _vector2& v) 280 | { 281 | return _vector2(-v.x, -v.y); 282 | } 283 | 284 | //------------------------------------------------------------------------------ 285 | #endif 286 | 287 | -------------------------------------------------------------------------------- /ext/mathlib/_vector3.h: -------------------------------------------------------------------------------- 1 | #ifndef _VECTOR3_H 2 | #define _VECTOR3_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class _vector3 6 | @ingroup Math 7 | 8 | Generic vector3 class. Uses 16 Byte of mem instead of 12 (!) 9 | 10 | (C) 2002 RadonLabs GmbH 11 | */ 12 | #include "mathlib/nmath.h" 13 | #include 14 | 15 | //------------------------------------------------------------------------------ 16 | 17 | class _vector3 18 | { 19 | public: 20 | /// constructor 1 21 | _vector3(); 22 | /// constructor 2 23 | _vector3(const float _x, const float _y, const float _z); 24 | /// constructor 3 25 | _vector3(const _vector3& vec); 26 | /// set elements 1 27 | void set(const float _x, const float _y, const float _z); 28 | /// set elements 2 29 | void set(const _vector3& vec); 30 | /// return length 31 | float len() const; 32 | /// return cross 33 | _vector3 cross( const _vector3& a , const _vector3& b ) ; 34 | /// return length squared 35 | float lensquared() const; 36 | /// normalize 37 | void norm(float length=1.0); 38 | /// inplace add 39 | void operator +=(const _vector3& v0); 40 | /// inplace sub 41 | void operator -=(const _vector3& v0); 42 | /// inplace scalar multiplication 43 | void operator *=(float s); 44 | /// true if any of the elements are greater 45 | bool operator >(const _vector3& rhs); 46 | /// true if any of the elements are smaller 47 | bool operator <(const _vector3& rhs); 48 | /// fuzzy compare, return true/false 49 | bool isequal(const _vector3& v, float tol) const; 50 | /// fuzzy compare, returns -1, 0, +1 51 | int compare(const _vector3& v, float tol) const; 52 | /// rotate around axis 53 | void rotate(const _vector3& axis, float angle); 54 | /// inplace linear interpolation 55 | void lerp(const _vector3& v0, float lerpVal); 56 | /// returns a vector orthogonal to self, not normalized 57 | _vector3 findortho() const; 58 | /// saturate components between 0 and 1 59 | void saturate(); 60 | /// dot product 61 | float dot(const _vector3& v0) const; 62 | /// distance between 2 vector3's 63 | static float distance(const _vector3& v0, const _vector3& v1); 64 | /// min 65 | void minimum(const _vector3& v) 66 | { 67 | x=min(x,v.x);y=min(y,v.y);z=min(z,v.z); 68 | } 69 | /// max 70 | void maximum(const _vector3& v) 71 | { 72 | x=max(x,v.x);y=max(y,v.y);z=max(z,v.z); 73 | } 74 | float angle(const _vector3& v) 75 | { 76 | return acos(dot(v)/(v.len()*len())); 77 | } 78 | 79 | float& operator[] (int i){ return (&this->x)[i];} 80 | 81 | float x, y, z; 82 | }; 83 | 84 | //------------------------------------------------------------------------------ 85 | /** 86 | */ 87 | inline 88 | _vector3::_vector3() : 89 | x(0.0f), 90 | y(0.0f), 91 | z(0.0f) 92 | { 93 | // empty 94 | } 95 | 96 | //------------------------------------------------------------------------------ 97 | /** 98 | */ 99 | inline 100 | _vector3::_vector3(const float _x, const float _y, const float _z) : 101 | x(_x), 102 | y(_y), 103 | z(_z) 104 | { 105 | // empty 106 | } 107 | 108 | //------------------------------------------------------------------------------ 109 | /** 110 | */ 111 | inline 112 | _vector3::_vector3(const _vector3& vec) : 113 | x(vec.x), 114 | y(vec.y), 115 | z(vec.z) 116 | { 117 | // empty 118 | } 119 | 120 | //------------------------------------------------------------------------------ 121 | /** 122 | */ 123 | inline 124 | void 125 | _vector3::set(const float _x, const float _y, const float _z) 126 | { 127 | x = _x; 128 | y = _y; 129 | z = _z; 130 | } 131 | 132 | //------------------------------------------------------------------------------ 133 | /** 134 | */ 135 | inline 136 | void 137 | _vector3::set(const _vector3& vec) 138 | { 139 | x = vec.x; 140 | y = vec.y; 141 | z = vec.z; 142 | } 143 | 144 | //------------------------------------------------------------------------------ 145 | /** 146 | */ 147 | inline 148 | _vector3 149 | _vector3::cross( const _vector3& a , const _vector3& b ) 150 | { 151 | x = a.y * b.z - a.z * b.y; 152 | y = a.z * b.x - a.x * b.z; 153 | z = a.x * b.y - a.y * b.x; 154 | return *this; 155 | } 156 | 157 | //------------------------------------------------------------------------------ 158 | /** 159 | */ 160 | inline 161 | float 162 | _vector3::len() const 163 | { 164 | return (float) n_sqrt(x * x + y * y + z * z); 165 | } 166 | 167 | //------------------------------------------------------------------------------ 168 | /** 169 | */ 170 | inline 171 | float 172 | _vector3::lensquared() const 173 | { 174 | return x * x + y * y + z * z; 175 | } 176 | 177 | //------------------------------------------------------------------------------ 178 | /** 179 | */ 180 | inline 181 | void 182 | _vector3::norm(float length) 183 | { 184 | float l = len()/length; 185 | if (l > TINY) 186 | { 187 | x /= l; 188 | y /= l; 189 | z /= l; 190 | } 191 | } 192 | 193 | //------------------------------------------------------------------------------ 194 | /** 195 | */ 196 | inline 197 | void 198 | _vector3::operator +=(const _vector3& v0) 199 | { 200 | x += v0.x; 201 | y += v0.y; 202 | z += v0.z; 203 | } 204 | 205 | //------------------------------------------------------------------------------ 206 | /** 207 | */ 208 | inline 209 | void 210 | _vector3::operator -=(const _vector3& v0) 211 | { 212 | x -= v0.x; 213 | y -= v0.y; 214 | z -= v0.z; 215 | } 216 | 217 | //------------------------------------------------------------------------------ 218 | /** 219 | */ 220 | inline 221 | void 222 | _vector3::operator *=(float s) 223 | { 224 | x *= s; 225 | y *= s; 226 | z *= s; 227 | } 228 | 229 | //------------------------------------------------------------------------------ 230 | /** 231 | */ 232 | inline 233 | bool 234 | _vector3::isequal(const _vector3& v, float tol) const 235 | { 236 | if (fabs(v.x - x) > tol) return false; 237 | else if (fabs(v.y - y) > tol) return false; 238 | else if (fabs(v.z - z) > tol) return false; 239 | return true; 240 | } 241 | 242 | //------------------------------------------------------------------------------ 243 | /** 244 | */ 245 | inline 246 | int 247 | _vector3::compare(const _vector3& v, float tol) const 248 | { 249 | if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1; 250 | else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1; 251 | else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1; 252 | else return 0; 253 | } 254 | 255 | //------------------------------------------------------------------------------ 256 | /** 257 | */ 258 | inline 259 | void 260 | _vector3::rotate(const _vector3& axis, float angle) 261 | { 262 | // rotates this one around given vector. We do 263 | // rotation with matrices, but these aren't defined yet! 264 | float rotM[9]; 265 | float sa, ca; 266 | 267 | sa = (float) sin(angle); 268 | ca = (float) cos(angle); 269 | 270 | // build a rotation matrix 271 | rotM[0] = ca + (1 - ca) * axis.x * axis.x; 272 | rotM[1] = (1 - ca) * axis.x * axis.y - sa * axis.z; 273 | rotM[2] = (1 - ca) * axis.z * axis.x + sa * axis.y; 274 | rotM[3] = (1 - ca) * axis.x * axis.y + sa * axis.z; 275 | rotM[4] = ca + (1 - ca) * axis.y * axis.y; 276 | rotM[5] = (1 - ca) * axis.y * axis.z - sa * axis.x; 277 | rotM[6] = (1 - ca) * axis.z * axis.x - sa * axis.y; 278 | rotM[7] = (1 - ca) * axis.y * axis.z + sa * axis.x; 279 | rotM[8] = ca + (1 - ca) * axis.z * axis.z; 280 | 281 | // "handmade" multiplication 282 | _vector3 help(rotM[0] * this->x + rotM[1] * this->y + rotM[2] * this->z, 283 | rotM[3] * this->x + rotM[4] * this->y + rotM[5] * this->z, 284 | rotM[6] * this->x + rotM[7] * this->y + rotM[8] * this->z); 285 | *this = help; 286 | } 287 | 288 | //------------------------------------------------------------------------------ 289 | /** 290 | */ 291 | static 292 | inline 293 | _vector3 operator +(const _vector3& v0, const _vector3& v1) 294 | { 295 | return _vector3(v0.x + v1.x, v0.y + v1.y, v0.z + v1.z); 296 | } 297 | 298 | //------------------------------------------------------------------------------ 299 | /** 300 | */ 301 | static 302 | inline 303 | _vector3 operator -(const _vector3& v0, const _vector3& v1) 304 | { 305 | return _vector3(v0.x - v1.x, v0.y - v1.y, v0.z - v1.z); 306 | } 307 | 308 | //------------------------------------------------------------------------------ 309 | /** 310 | */ 311 | static 312 | inline 313 | _vector3 operator *(const _vector3& v0, const float s) 314 | { 315 | return _vector3(v0.x * s, v0.y * s, v0.z * s); 316 | } 317 | 318 | //------------------------------------------------------------------------------ 319 | /** 320 | */ 321 | static 322 | inline 323 | _vector3 operator -(const _vector3& v) 324 | { 325 | return _vector3(-v.x, -v.y, -v.z); 326 | } 327 | 328 | //------------------------------------------------------------------------------ 329 | /** 330 | */ 331 | static 332 | inline 333 | _vector3 operator /(const _vector3& v0, const float s) 334 | { 335 | float one_over_s = 1.0f/s; 336 | return _vector3(v0.x*one_over_s, v0.y*one_over_s, v0.z*one_over_s); 337 | } 338 | 339 | //------------------------------------------------------------------------------ 340 | /** 341 | Dot product. 342 | */ 343 | static 344 | inline 345 | float operator %(const _vector3& v0, const _vector3& v1) 346 | { 347 | return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z; 348 | } 349 | 350 | //------------------------------------------------------------------------------ 351 | /** 352 | multiply 353 | */ 354 | static 355 | inline 356 | _vector3 operator *(const _vector3& v0, const _vector3& v1) 357 | { 358 | // return _vector3(v0.y * v1.z - v0.z * v1.y, 359 | // v0.z * v1.x - v0.x * v1.z, 360 | // v0.x * v1.y - v0.y * v1.x); 361 | return _vector3(v0.x*v1.x,v0.y*v1.y,v0.z*v1.z); 362 | } 363 | 364 | //------------------------------------------------------------------------------ 365 | /** 366 | */ 367 | inline 368 | void 369 | _vector3::lerp(const _vector3& v0, float lerpVal) 370 | { 371 | x = v0.x + ((x - v0.x) * lerpVal); 372 | y = v0.y + ((y - v0.y) * lerpVal); 373 | z = v0.z + ((z - v0.z) * lerpVal); 374 | } 375 | 376 | //------------------------------------------------------------------------------ 377 | /** 378 | */ 379 | inline 380 | void 381 | _vector3::saturate() 382 | { 383 | x = n_saturate(x); 384 | y = n_saturate(y); 385 | z = n_saturate(z); 386 | } 387 | 388 | //------------------------------------------------------------------------------ 389 | /** 390 | Find a vector that is orthogonal to self. Self should not be (0,0,0). 391 | Return value is not normalized. 392 | */ 393 | inline 394 | _vector3 395 | _vector3::findortho() const 396 | { 397 | if (0.0 != x) 398 | { 399 | return _vector3((-y - z) / x, 1.0, 1.0); 400 | } else 401 | if (0.0 != y) 402 | { 403 | return _vector3(1.0, (-x - z) / y, 1.0); 404 | } else 405 | if (0.0 != z) 406 | { 407 | return _vector3(1.0, 1.0, (-x - y) / z); 408 | } else 409 | { 410 | return _vector3(0.0, 0.0, 0.0); 411 | } 412 | } 413 | 414 | //------------------------------------------------------------------------------ 415 | /** 416 | Dot product for vector3 417 | */ 418 | inline 419 | float 420 | _vector3::dot(const _vector3& v0) const 421 | { 422 | return ( x * v0.x + y * v0.y + z * v0.z ); 423 | } 424 | 425 | //------------------------------------------------------------------------------ 426 | /** 427 | */ 428 | inline 429 | bool 430 | _vector3::operator >(const _vector3& rhs) 431 | { 432 | if ((this->x > rhs.x) || (this->y > rhs.y) || (this->z > rhs.z)) 433 | { 434 | return true; 435 | } 436 | else 437 | { 438 | return false; 439 | } 440 | } 441 | 442 | //------------------------------------------------------------------------------ 443 | /** 444 | */ 445 | inline 446 | bool 447 | _vector3::operator <(const _vector3& rhs) 448 | { 449 | if ((this->x < rhs.x) || (this->y < rhs.y) || (this->z < rhs.z)) 450 | { 451 | return true; 452 | } 453 | else 454 | { 455 | return false; 456 | } 457 | } 458 | 459 | //------------------------------------------------------------------------------ 460 | /** 461 | */ 462 | inline 463 | float 464 | _vector3::distance(const _vector3& v0, const _vector3& v1) 465 | { 466 | _vector3 v(v1 - v0); 467 | return (float) n_sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 468 | } 469 | 470 | //------------------------------------------------------------------------------ 471 | #endif 472 | -------------------------------------------------------------------------------- /ext/mathlib/_vector3_sse.h: -------------------------------------------------------------------------------- 1 | #ifndef _VECTOR3_SSE_H 2 | #define _VECTOR3_SSE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class _vector3_sse 6 | @ingroup Math 7 | 8 | SSE based vector3 class. 9 | 10 | (C) 2002 RadonLabs GmbH 11 | */ 12 | #include 13 | #include 14 | 15 | //------------------------------------------------------------------------------ 16 | class _vector3_sse 17 | { 18 | public: 19 | /// constructor 1 20 | _vector3_sse(); 21 | /// constructor 2 22 | _vector3_sse(const float _x, const float _y, const float _z); 23 | /// constructor 3 24 | _vector3_sse(const _vector3_sse& vec); 25 | /// constructor 4 26 | _vector3_sse(const float* p); 27 | /// private constructor 28 | _vector3_sse(const __m128& m); 29 | /// set elements 1 30 | void set(const float _x, const float _y, const float _z); 31 | /// set elements 2 32 | void set(const _vector3_sse& vec); 33 | /// set elements 3 34 | void set(const float* p); 35 | /// return length 36 | float len() const; 37 | /// return length squared 38 | float lensquared() const; 39 | /// normalize 40 | void norm(); 41 | /// inplace add 42 | void operator +=(const _vector3_sse& v0); 43 | /// inplace sub 44 | void operator -=(const _vector3_sse& v0); 45 | /// inplace scalar multiplication 46 | void operator *=(float s); 47 | /// fuzzy compare 48 | bool isequal(const _vector3_sse& v, float tol) const; 49 | /// fuzzy compare, returns -1, 0, +1 50 | int compare(const _vector3_sse& v, float tol) const; 51 | /// rotate around axis 52 | void rotate(const _vector3_sse& axis, float angle); 53 | /// inplace linear interpolation 54 | void lerp(const _vector3_sse& v0, float lerpVal); 55 | /// returns a vector orthogonal to self, not normalized 56 | _vector3_sse findortho() const; 57 | 58 | union 59 | { 60 | __m128 m128; 61 | struct 62 | { 63 | float x, y, z, pad; 64 | }; 65 | }; 66 | }; 67 | 68 | //------------------------------------------------------------------------------ 69 | /** 70 | */ 71 | inline 72 | _vector3_sse::_vector3_sse() 73 | { 74 | m128 = _mm_setzero_ps(); 75 | } 76 | 77 | //------------------------------------------------------------------------------ 78 | /** 79 | */ 80 | inline 81 | _vector3_sse::_vector3_sse(const float _x, const float _y, const float _z) 82 | { 83 | m128 = _mm_set_ps(0.0f, _z, _y, _x); 84 | } 85 | 86 | //------------------------------------------------------------------------------ 87 | /** 88 | */ 89 | inline 90 | _vector3_sse::_vector3_sse(const _vector3_sse& vec) 91 | { 92 | m128 = vec.m128; 93 | } 94 | 95 | //------------------------------------------------------------------------------ 96 | /** 97 | */ 98 | inline 99 | _vector3_sse::_vector3_sse(const __m128& m) 100 | { 101 | m128 = m; 102 | } 103 | 104 | //------------------------------------------------------------------------------ 105 | /** 106 | */ 107 | inline 108 | void 109 | _vector3_sse::set(const float _x, const float _y, const float _z) 110 | { 111 | m128 = _mm_set_ps(0.0f, _z, _y, _x); 112 | } 113 | 114 | //------------------------------------------------------------------------------ 115 | /** 116 | */ 117 | inline 118 | void 119 | _vector3_sse::set(const _vector3_sse& vec) 120 | { 121 | m128 = vec.m128; 122 | } 123 | 124 | //------------------------------------------------------------------------------ 125 | /** 126 | */ 127 | inline 128 | float 129 | _vector3_sse::len() const 130 | { 131 | static const int X = 0; 132 | static const int Y = 1; 133 | static const int Z = 2; 134 | static const int W = 3; 135 | 136 | __m128 a = _mm_mul_ps(m128, m128); 137 | 138 | // horizontal add 139 | __m128 b = _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(X,X,X,X)), _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(Y,Y,Y,Y)), _mm_shuffle_ps(a, a, _MM_SHUFFLE(Z,Z,Z,Z)))); 140 | __m128 l = _mm_sqrt_ss(b); 141 | return l.m128_f32[X]; 142 | } 143 | 144 | //------------------------------------------------------------------------------ 145 | /** 146 | */ 147 | inline 148 | float 149 | _vector3_sse::lensquared() const 150 | { 151 | static const int X = 0; 152 | static const int Y = 1; 153 | static const int Z = 2; 154 | static const int W = 3; 155 | 156 | __m128 a = _mm_mul_ps(m128, m128); 157 | __m128 b = _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(X,X,X,X)), _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(Y,Y,Y,Y)), _mm_shuffle_ps(a, a, _MM_SHUFFLE(Z,Z,Z,Z)))); 158 | return b.m128_f32[X]; 159 | } 160 | 161 | //------------------------------------------------------------------------------ 162 | /** 163 | */ 164 | inline 165 | void 166 | _vector3_sse::norm() 167 | { 168 | static const int X = 0; 169 | static const int Y = 1; 170 | static const int Z = 2; 171 | static const int W = 3; 172 | 173 | __m128 a = _mm_mul_ps(m128, m128); 174 | 175 | // horizontal add 176 | __m128 b = _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(X,X,X,X)), _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(Y,Y,Y,Y)), _mm_shuffle_ps(a, a, _MM_SHUFFLE(Z,Z,Z,Z)))); 177 | 178 | // get reciprocal of square root of squared length 179 | __m128 f = _mm_rsqrt_ss(b); 180 | __m128 oneDivLen = _mm_shuffle_ps(f, f, _MM_SHUFFLE(X,X,X,X)); 181 | 182 | m128 = _mm_mul_ps(m128, oneDivLen); 183 | } 184 | 185 | //------------------------------------------------------------------------------ 186 | /** 187 | */ 188 | inline 189 | void 190 | _vector3_sse::operator +=(const _vector3_sse& v) 191 | { 192 | m128 = _mm_add_ps(m128, v.m128); 193 | } 194 | 195 | //------------------------------------------------------------------------------ 196 | /** 197 | */ 198 | inline 199 | void 200 | _vector3_sse::operator -=(const _vector3_sse& v) 201 | { 202 | m128 = _mm_sub_ps(m128, v.m128); 203 | } 204 | 205 | //------------------------------------------------------------------------------ 206 | /** 207 | */ 208 | inline 209 | void 210 | _vector3_sse::operator *=(float s) 211 | { 212 | __m128 packed = _mm_set1_ps(s); 213 | m128 = _mm_mul_ps(m128, packed); 214 | } 215 | 216 | //------------------------------------------------------------------------------ 217 | /** 218 | */ 219 | inline 220 | bool 221 | _vector3_sse::isequal(const _vector3_sse& v, float tol) const 222 | { 223 | if (fabs(v.x - x) > tol) return false; 224 | else if (fabs(v.y - y) > tol) return false; 225 | else if (fabs(v.z - z) > tol) return false; 226 | return true; 227 | } 228 | 229 | //------------------------------------------------------------------------------ 230 | /** 231 | */ 232 | inline 233 | int 234 | _vector3_sse::compare(const _vector3_sse& v, float tol) const 235 | { 236 | if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1; 237 | else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1; 238 | else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1; 239 | else return 0; 240 | } 241 | 242 | //------------------------------------------------------------------------------ 243 | /** 244 | */ 245 | inline 246 | void 247 | _vector3_sse::rotate(const _vector3_sse& axis, float angle) 248 | { 249 | // rotates this one around given vector. We do 250 | // rotation with matrices, but these aren't defined yet! 251 | float rotM[9]; 252 | float sa, ca; 253 | 254 | sa = (float) sin(angle); 255 | ca = (float) cos(angle); 256 | 257 | // build a rotation matrix 258 | rotM[0] = ca + (1 - ca) * axis.x * axis.x; 259 | rotM[1] = (1 - ca) * axis.x * axis.y - sa * axis.z; 260 | rotM[2] = (1 - ca) * axis.z * axis.x + sa * axis.y; 261 | rotM[3] = (1 - ca) * axis.x * axis.y + sa * axis.z; 262 | rotM[4] = ca + (1 - ca) * axis.y * axis.y; 263 | rotM[5] = (1 - ca) * axis.y * axis.z - sa * axis.x; 264 | rotM[6] = (1 - ca) * axis.z * axis.x - sa * axis.y; 265 | rotM[7] = (1 - ca) * axis.y * axis.z + sa * axis.x; 266 | rotM[8] = ca + (1 - ca) * axis.z * axis.z; 267 | 268 | // "handmade" multiplication 269 | _vector3_sse help(rotM[0] * this->x + rotM[1] * this->y + rotM[2] * this->z, 270 | rotM[3] * this->x + rotM[4] * this->y + rotM[5] * this->z, 271 | rotM[6] * this->x + rotM[7] * this->y + rotM[8] * this->z); 272 | *this = help; 273 | } 274 | 275 | //------------------------------------------------------------------------------ 276 | /** 277 | */ 278 | static 279 | inline 280 | _vector3_sse operator +(const _vector3_sse& v0, const _vector3_sse& v1) 281 | { 282 | return _vector3_sse(_mm_add_ps(v0.m128, v1.m128)); 283 | } 284 | 285 | //------------------------------------------------------------------------------ 286 | /** 287 | */ 288 | static 289 | inline 290 | _vector3_sse operator -(const _vector3_sse& v0, const _vector3_sse& v1) 291 | { 292 | return _vector3_sse(_mm_sub_ps(v0.m128, v1.m128)); 293 | } 294 | 295 | //------------------------------------------------------------------------------ 296 | /** 297 | */ 298 | static 299 | inline 300 | _vector3_sse operator *(const _vector3_sse& v0, const float s) 301 | { 302 | __m128 packed = _mm_set1_ps(s); 303 | return _vector3_sse(_mm_mul_ps(v0.m128, packed)); 304 | } 305 | 306 | //------------------------------------------------------------------------------ 307 | /** 308 | */ 309 | static 310 | inline 311 | _vector3_sse operator -(const _vector3_sse& v) 312 | { 313 | __m128 zero = _mm_setzero_ps(); 314 | return _vector3_sse(_mm_sub_ps(zero, v.m128)); 315 | } 316 | 317 | //------------------------------------------------------------------------------ 318 | /** 319 | Dot product. 320 | */ 321 | static 322 | inline 323 | float operator %(const _vector3_sse& v0, const _vector3_sse& v1) 324 | { 325 | __m128 a = _mm_mul_ps(v0.m128, v1.m128); 326 | __m128 b = _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(0,0,0,0)), _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(1,1,1,1)), _mm_shuffle_ps(a, a, _MM_SHUFFLE(2,2,2,2)))); 327 | return b.m128_f32[0]; 328 | } 329 | 330 | //------------------------------------------------------------------------------ 331 | /** 332 | Cross product. 333 | */ 334 | static 335 | inline 336 | _vector3_sse operator *(const _vector3_sse& v0, const _vector3_sse& v1) 337 | { 338 | // x = v0.y * v1.z - v0.z * v1.y 339 | // y = v0.z * v1.x - v0.x * v1.z 340 | // z = v0.x * v1.y - v0.y * v1.x 341 | // 342 | // a = v0.y | v0.z | v0.x | xxx 343 | // b = v1.z | v1.x | v1.y | xxx 344 | // c = v0.z | v0.x | v0.y | xxx 345 | // d = v1.y | v1.z | v1.x | xxx 346 | // 347 | 348 | static const int X = 0; 349 | static const int Y = 1; 350 | static const int Z = 2; 351 | static const int W = 3; 352 | 353 | __m128 a = _mm_shuffle_ps(v0.m128, v0.m128, _MM_SHUFFLE(W, X, Z, Y)); 354 | __m128 b = _mm_shuffle_ps(v1.m128, v1.m128, _MM_SHUFFLE(W, Y, X, Z)); 355 | __m128 c = _mm_shuffle_ps(v0.m128, v0.m128, _MM_SHUFFLE(W, Y, X, Z)); 356 | __m128 d = _mm_shuffle_ps(v1.m128, v1.m128, _MM_SHUFFLE(W, X, Z, Y)); 357 | 358 | __m128 e = _mm_mul_ps(a, b); 359 | __m128 f = _mm_mul_ps(c, d); 360 | 361 | return _vector3_sse(_mm_sub_ps(e, f)); 362 | } 363 | 364 | //------------------------------------------------------------------------------ 365 | /** 366 | */ 367 | inline 368 | void 369 | _vector3_sse::lerp(const _vector3_sse& v0, float lerpVal) 370 | { 371 | x = v0.x + ((x - v0.x) * lerpVal); 372 | y = v0.y + ((y - v0.y) * lerpVal); 373 | z = v0.z + ((z - v0.z) * lerpVal); 374 | } 375 | 376 | //------------------------------------------------------------------------------ 377 | /** 378 | Find a vector that is orthogonal to self. Self should not be (0,0,0). 379 | Return value is not normalized. 380 | */ 381 | inline 382 | _vector3_sse 383 | _vector3_sse::findortho() const 384 | { 385 | if (0.0 != x) 386 | { 387 | return _vector3_sse((-y - z) / x, 1.0, 1.0); 388 | } else 389 | if (0.0 != y) 390 | { 391 | return _vector3_sse(1.0, (-x - z) / y, 1.0); 392 | } else 393 | if (0.0 != z) 394 | { 395 | return _vector3_sse(1.0, 1.0, (-x - y) / z); 396 | } else 397 | { 398 | return _vector3_sse(0.0, 0.0, 0.0); 399 | } 400 | } 401 | 402 | //------------------------------------------------------------------------------ 403 | #endif 404 | -------------------------------------------------------------------------------- /ext/mathlib/_vector4.h: -------------------------------------------------------------------------------- 1 | #ifndef _VECTOR4_H 2 | #define _VECTOR4_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class _vector4 6 | @ingroup Math 7 | 8 | A generic vector4 class. 9 | 10 | (C) 2002 RadonLabs GmbH 11 | */ 12 | #include "mathlib/nmath.h" 13 | #include 14 | 15 | //------------------------------------------------------------------------------ 16 | class _vector4 17 | { 18 | public: 19 | enum component 20 | { 21 | X = (1<<0), 22 | Y = (1<<1), 23 | Z = (1<<2), 24 | W = (1<<3), 25 | }; 26 | 27 | /// constructor 1 28 | _vector4(); 29 | /// constructor 2 30 | _vector4(const float _x, const float _y, const float _z, const float _w); 31 | /// constructor 3 32 | _vector4(const _vector4& vec); 33 | /// constructor from vector3 (w will be set to 1.0) 34 | _vector4(const _vector3& vec3); 35 | /// set elements 1 36 | void set(const float _x, const float _y, const float _z, const float _w); 37 | /// set elements 2 38 | void set(const _vector4& v); 39 | /// set to vector3 (w will be set to 1.0) 40 | void set(const _vector3& v); 41 | /// return length 42 | float len() const; 43 | /// normalize 44 | void norm(); 45 | /// inplace add 46 | void operator +=(const _vector4& v); 47 | /// inplace sub 48 | void operator -=(const _vector4& v); 49 | /// inplace scalar mul 50 | void operator *=(const float s); 51 | /// vector3 assignment operator (w set to 1.0f) 52 | _vector4& operator=(const _vector3& v); 53 | /// fuzzy compare 54 | bool isequal(const _vector4& v, float tol) const; 55 | /// fuzzy compare, return -1, 0, +1 56 | int compare(const _vector4& v, float tol) const; 57 | /// set own components to minimum 58 | void minimum(const _vector4& v); 59 | /// set own components to maximum 60 | void maximum(const _vector4& v); 61 | /// set component float value by mask 62 | void setcomp(float val, int mask); 63 | /// get component float value by mask 64 | float getcomp(int mask); 65 | /// get write mask for smallest component 66 | int mincompmask() const; 67 | /// inplace linear interpolation 68 | void lerp(const _vector4& v0, float lerpVal); 69 | /// saturate components between 0 and 1 70 | void saturate(); 71 | /// dot product 72 | float dot(const _vector4& v0) const; 73 | 74 | float x, y, z, w; 75 | }; 76 | 77 | //------------------------------------------------------------------------------ 78 | /** 79 | */ 80 | inline 81 | _vector4::_vector4() : 82 | x(0.0f), 83 | y(0.0f), 84 | z(0.0f), 85 | w(0.0f) 86 | { 87 | // empty 88 | } 89 | 90 | //------------------------------------------------------------------------------ 91 | /** 92 | */ 93 | inline 94 | _vector4::_vector4(const float _x, const float _y, const float _z, const float _w) : 95 | x(_x), 96 | y(_y), 97 | z(_z), 98 | w(_w) 99 | { 100 | // empty 101 | } 102 | 103 | //------------------------------------------------------------------------------ 104 | /** 105 | */ 106 | inline 107 | _vector4::_vector4(const _vector4& v) : 108 | x(v.x), 109 | y(v.y), 110 | z(v.z), 111 | w(v.w) 112 | { 113 | // empty 114 | } 115 | 116 | //------------------------------------------------------------------------------ 117 | /** 118 | */ 119 | inline 120 | _vector4::_vector4(const _vector3& v) : 121 | x(v.x), 122 | y(v.y), 123 | z(v.z), 124 | w(1.0f) 125 | { 126 | // empty 127 | } 128 | 129 | //------------------------------------------------------------------------------ 130 | /** 131 | */ 132 | inline 133 | void 134 | _vector4::set(const float _x, const float _y, const float _z, const float _w) 135 | { 136 | x = _x; 137 | y = _y; 138 | z = _z; 139 | w = _w; 140 | } 141 | 142 | //------------------------------------------------------------------------------ 143 | /** 144 | */ 145 | inline 146 | void 147 | _vector4::set(const _vector4& v) 148 | { 149 | x = v.x; 150 | y = v.y; 151 | z = v.z; 152 | w = v.w; 153 | } 154 | 155 | //------------------------------------------------------------------------------ 156 | /** 157 | */ 158 | inline 159 | void 160 | _vector4::set(const _vector3& v) 161 | { 162 | x = v.x; 163 | y = v.y; 164 | z = v.z; 165 | w = 1.0f; 166 | } 167 | 168 | //------------------------------------------------------------------------------ 169 | /** 170 | */ 171 | inline 172 | float 173 | _vector4::len() const 174 | { 175 | return (float) sqrt(x * x + y * y + z * z + w * w); 176 | } 177 | 178 | //------------------------------------------------------------------------------ 179 | /** 180 | */ 181 | inline 182 | void 183 | _vector4::norm() 184 | { 185 | float l = len(); 186 | if (l > TINY) 187 | { 188 | float oneDivL = 1.0f / l; 189 | x *= oneDivL; 190 | y *= oneDivL; 191 | z *= oneDivL; 192 | w *= oneDivL; 193 | } 194 | } 195 | 196 | //------------------------------------------------------------------------------ 197 | /** 198 | */ 199 | inline 200 | void 201 | _vector4::operator +=(const _vector4& v) 202 | { 203 | x += v.x; 204 | y += v.y; 205 | z += v.z; 206 | w += v.w; 207 | } 208 | 209 | //------------------------------------------------------------------------------ 210 | /** 211 | */ 212 | inline 213 | void 214 | _vector4::operator -=(const _vector4& v) 215 | { 216 | x -= v.x; 217 | y -= v.y; 218 | z -= v.z; 219 | w -= v.w; 220 | } 221 | 222 | //------------------------------------------------------------------------------ 223 | /** 224 | */ 225 | inline 226 | void 227 | _vector4::operator *=(const float s) 228 | { 229 | x *= s; 230 | y *= s; 231 | z *= s; 232 | w *= s; 233 | } 234 | 235 | //------------------------------------------------------------------------------ 236 | /** 237 | */ 238 | inline 239 | _vector4& 240 | _vector4::operator=(const _vector3& v) 241 | { 242 | this->set(v); 243 | return *this; 244 | } 245 | 246 | //------------------------------------------------------------------------------ 247 | /** 248 | */ 249 | inline 250 | bool 251 | _vector4::isequal(const _vector4& v, float tol) const 252 | { 253 | if (fabs(v.x - x) > tol) return false; 254 | else if (fabs(v.y - y) > tol) return false; 255 | else if (fabs(v.z - z) > tol) return false; 256 | else if (fabs(v.w - w) > tol) return false; 257 | return true; 258 | } 259 | 260 | //------------------------------------------------------------------------------ 261 | /** 262 | */ 263 | inline 264 | int 265 | _vector4::compare(const _vector4& v, float tol) const 266 | { 267 | if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1; 268 | else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1; 269 | else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1; 270 | else if (fabs(v.w - w) > tol) return (v.w > w) ? +1 : -1; 271 | else return 0; 272 | } 273 | 274 | //------------------------------------------------------------------------------ 275 | /** 276 | */ 277 | inline 278 | void 279 | _vector4::minimum(const _vector4& v) 280 | { 281 | if (v.x < x) x = v.x; 282 | if (v.y < y) y = v.y; 283 | if (v.z < z) z = v.z; 284 | if (v.w < w) w = v.w; 285 | } 286 | 287 | //------------------------------------------------------------------------------ 288 | /** 289 | */ 290 | inline 291 | void 292 | _vector4::maximum(const _vector4& v) 293 | { 294 | if (v.x > x) x = v.x; 295 | if (v.y > y) y = v.y; 296 | if (v.z > z) z = v.z; 297 | if (v.w > w) w = v.w; 298 | } 299 | 300 | //------------------------------------------------------------------------------ 301 | /** 302 | */ 303 | static 304 | inline 305 | _vector4 operator +(const _vector4& v0, const _vector4& v1) 306 | { 307 | return _vector4(v0.x + v1.x, v0.y + v1.y, v0.z + v1.z, v0.w + v1.w); 308 | } 309 | 310 | //------------------------------------------------------------------------------ 311 | /** 312 | */ 313 | static 314 | inline 315 | _vector4 operator -(const _vector4& v0, const _vector4& v1) 316 | { 317 | return _vector4(v0.x - v1.x, v0.y - v1.y, v0.z - v1.z, v0.w - v1.w); 318 | } 319 | 320 | //------------------------------------------------------------------------------ 321 | /** 322 | */ 323 | static 324 | inline 325 | _vector4 operator *(const _vector4& v0, const float& s) 326 | { 327 | return _vector4(v0.x * s, v0.y * s, v0.z * s, v0.w * s); 328 | } 329 | 330 | //------------------------------------------------------------------------------ 331 | /** 332 | */ 333 | static 334 | inline 335 | _vector4 operator -(const _vector4& v) 336 | { 337 | return _vector4(-v.x, -v.y, -v.z, -v.w); 338 | } 339 | 340 | //------------------------------------------------------------------------------ 341 | /** 342 | */ 343 | inline 344 | void 345 | _vector4::setcomp(float val, int mask) 346 | { 347 | if (mask & X) x = val; 348 | if (mask & Y) y = val; 349 | if (mask & Z) z = val; 350 | if (mask & W) w = val; 351 | } 352 | 353 | //------------------------------------------------------------------------------ 354 | /** 355 | */ 356 | inline 357 | float 358 | _vector4::getcomp(int mask) 359 | { 360 | switch (mask) 361 | { 362 | case X: return x; 363 | case Y: return y; 364 | case Z: return z; 365 | default: return w; 366 | } 367 | } 368 | 369 | //------------------------------------------------------------------------------ 370 | /** 371 | */ 372 | inline 373 | int 374 | _vector4::mincompmask() const 375 | { 376 | float minVal = x; 377 | int minComp = X; 378 | if (y < minVal) 379 | { 380 | minComp = Y; 381 | minVal = y; 382 | } 383 | if (z < minVal) 384 | { 385 | minComp = Z; 386 | minVal = z; 387 | } 388 | if (w < minVal) 389 | { 390 | minComp = W; 391 | minVal = w; 392 | } 393 | return minComp; 394 | } 395 | 396 | //------------------------------------------------------------------------------ 397 | /** 398 | */ 399 | inline 400 | void 401 | _vector4::lerp(const _vector4& v0, float lerpVal) 402 | { 403 | x = v0.x + ((x - v0.x) * lerpVal); 404 | y = v0.y + ((y - v0.y) * lerpVal); 405 | z = v0.z + ((z - v0.z) * lerpVal); 406 | w = v0.w + ((w - v0.w) * lerpVal); 407 | } 408 | 409 | //------------------------------------------------------------------------------ 410 | /** 411 | */ 412 | inline 413 | void 414 | _vector4::saturate() 415 | { 416 | x = n_saturate(x); 417 | y = n_saturate(y); 418 | z = n_saturate(z); 419 | w = n_saturate(w); 420 | } 421 | 422 | 423 | //------------------------------------------------------------------------------ 424 | /** 425 | Dot product for vector4 426 | */ 427 | inline 428 | float _vector4::dot(const _vector4& v0) const 429 | { 430 | return ( x * v0.x + y * v0.y + z * v0.z + w * v0.w ); 431 | } 432 | 433 | //------------------------------------------------------------------------------ 434 | #endif 435 | 436 | -------------------------------------------------------------------------------- /ext/mathlib/_vector4_sse.h: -------------------------------------------------------------------------------- 1 | #ifndef _VECTOR4_SSE_H 2 | #define _VECTOR4_SSE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class _vector4_sse 6 | @ingroup Math 7 | 8 | A vector4 class using SSE. 9 | 10 | (C) 2002 RadonLabs GmbH 11 | */ 12 | #include 13 | 14 | //------------------------------------------------------------------------------ 15 | class _vector4_sse 16 | { 17 | public: 18 | /// constructor 1 19 | _vector4_sse(); 20 | /// constructor 2 21 | _vector4_sse(const float _x, const float _y, const float _z, const float _w); 22 | /// constructor 3 23 | _vector4_sse(const _vector4_sse& vec); 24 | /// set elements 1 25 | void set(const float _x, const float _y, const float _z, const float _w); 26 | /// set elements 2 27 | void set(const _vector4_sse& v); 28 | /// return length 29 | float len() const; 30 | /// normalize 31 | void norm(); 32 | /// inplace add 33 | void operator +=(const _vector4_sse& v); 34 | /// inplace sub 35 | void operator -=(const _vector4_sse& v); 36 | /// inplace scalar mul 37 | void operator *=(const float s); 38 | /// fuzzy compare 39 | bool isequal(const _vector4_sse& v, float tol) const; 40 | /// fuzzy compare, return -1, 0, +1 41 | int compare(const _vector4_sse& v, float tol) const; 42 | /// set own components to minimum 43 | void minimum(const _vector4_sse& v); 44 | /// set own components to maximum 45 | void maximum(const _vector4_sse& v); 46 | /// inplace linear interpolation 47 | void lerp(const _vector4_sse& v0, float lerpVal); 48 | 49 | union 50 | { 51 | __m128 m128; 52 | struct 53 | { 54 | float x, y, z, w; 55 | }; 56 | }; 57 | 58 | /// private constructor, takes _m128 59 | _vector4_sse(const __m128& m); 60 | /// add components 61 | float addhorizontal(const __m128 &a); 62 | }; 63 | 64 | //------------------------------------------------------------------------------ 65 | /** 66 | */ 67 | inline 68 | _vector4_sse::_vector4_sse() 69 | { 70 | m128 = _mm_setzero_ps(); 71 | } 72 | 73 | //------------------------------------------------------------------------------ 74 | /** 75 | */ 76 | inline 77 | _vector4_sse::_vector4_sse(const float _x, const float _y, const float _z, const float _w) 78 | { 79 | m128 = _mm_set_ps(_w, _z, _y, _x); 80 | } 81 | 82 | //------------------------------------------------------------------------------ 83 | /** 84 | */ 85 | inline 86 | _vector4_sse::_vector4_sse(const _vector4_sse& v) 87 | { 88 | m128 = v.m128; 89 | } 90 | 91 | //------------------------------------------------------------------------------ 92 | /** 93 | */ 94 | inline 95 | _vector4_sse::_vector4_sse(const __m128& m) : 96 | m128(m) 97 | { 98 | // empty 99 | } 100 | 101 | //------------------------------------------------------------------------------ 102 | /** 103 | */ 104 | inline 105 | void 106 | _vector4_sse::set(const float _x, const float _y, const float _z, const float _w) 107 | { 108 | m128 = _mm_set_ps(_w, _z, _y, _x); 109 | } 110 | 111 | //------------------------------------------------------------------------------ 112 | /** 113 | */ 114 | inline 115 | void 116 | _vector4_sse::set(const _vector4_sse& v) 117 | { 118 | m128 = v.m128; 119 | } 120 | 121 | //------------------------------------------------------------------------------ 122 | /** 123 | */ 124 | inline 125 | float 126 | _vector4_sse::len() const 127 | { 128 | const int X = 0; 129 | const int Y = 1; 130 | const int Z = 2; 131 | const int W = 3; 132 | 133 | __m128 a = _mm_mul_ps(m128, m128); 134 | 135 | // horizontal add 136 | __m128 b = _mm_add_ss(a, _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(X,X,X,X)), _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(Y,Y,Y,Y)), _mm_shuffle_ps(a, a, _MM_SHUFFLE(Z,Z,Z,Z))))); 137 | __m128 l = _mm_sqrt_ss(b); 138 | 139 | return l.m128_f32[X]; 140 | } 141 | 142 | //------------------------------------------------------------------------------ 143 | /** 144 | */ 145 | inline 146 | void 147 | _vector4_sse::norm() 148 | { 149 | const int X = 0; 150 | const int Y = 1; 151 | const int Z = 2; 152 | const int W = 3; 153 | 154 | // get len 155 | __m128 a = _mm_mul_ps(m128, m128); 156 | __m128 b = _mm_add_ss(a, _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(X,X,X,X)), _mm_add_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(Y,Y,Y,Y)), _mm_shuffle_ps(a, a, _MM_SHUFFLE(Z,Z,Z,Z))))); 157 | 158 | // get reciprocal of square root of squared length 159 | __m128 f = _mm_rsqrt_ss(b); 160 | __m128 oneDivLen = _mm_shuffle_ps(f, f, _MM_SHUFFLE(X, X, X, X)); 161 | 162 | m128 = _mm_mul_ps(m128, oneDivLen); 163 | } 164 | 165 | //------------------------------------------------------------------------------ 166 | /** 167 | */ 168 | inline 169 | void 170 | _vector4_sse::operator +=(const _vector4_sse& v) 171 | { 172 | m128 = _mm_add_ps(m128, v.m128); 173 | } 174 | 175 | //------------------------------------------------------------------------------ 176 | /** 177 | */ 178 | inline 179 | void 180 | _vector4_sse::operator -=(const _vector4_sse& v) 181 | { 182 | m128 = _mm_sub_ps(m128, v.m128); 183 | } 184 | 185 | //------------------------------------------------------------------------------ 186 | /** 187 | */ 188 | inline 189 | void 190 | _vector4_sse::operator *=(const float s) 191 | { 192 | __m128 packed = _mm_set1_ps(s); 193 | m128 = _mm_mul_ps(m128, packed); 194 | } 195 | 196 | //------------------------------------------------------------------------------ 197 | /** 198 | */ 199 | inline 200 | bool 201 | _vector4_sse::isequal(const _vector4_sse& v, float tol) const 202 | { 203 | if (fabs(v.x - x) > tol) return false; 204 | else if (fabs(v.y - y) > tol) return false; 205 | else if (fabs(v.z - z) > tol) return false; 206 | else if (fabs(v.w - w) > tol) return false; 207 | return true; 208 | } 209 | 210 | //------------------------------------------------------------------------------ 211 | /** 212 | */ 213 | inline 214 | int 215 | _vector4_sse::compare(const _vector4_sse& v, float tol) const 216 | { 217 | if (fabs(v.x - x) > tol) return (v.x > x) ? +1 : -1; 218 | else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1; 219 | else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1; 220 | else if (fabs(v.w - w) > tol) return (v.w > w) ? +1 : -1; 221 | else return 0; 222 | } 223 | 224 | //------------------------------------------------------------------------------ 225 | /** 226 | */ 227 | inline 228 | void 229 | _vector4_sse::minimum(const _vector4_sse& v) 230 | { 231 | if (v.x < x) x = v.x; 232 | if (v.y < y) y = v.y; 233 | if (v.z < z) z = v.z; 234 | if (v.w < w) w = v.w; 235 | } 236 | 237 | //------------------------------------------------------------------------------ 238 | /** 239 | */ 240 | inline 241 | void 242 | _vector4_sse::maximum(const _vector4_sse& v) 243 | { 244 | if (v.x > x) x = v.x; 245 | if (v.y > y) y = v.y; 246 | if (v.z > z) z = v.z; 247 | if (v.w > w) w = v.w; 248 | } 249 | 250 | //------------------------------------------------------------------------------ 251 | /** 252 | */ 253 | static 254 | inline 255 | _vector4_sse operator +(const _vector4_sse& v0, const _vector4_sse& v1) 256 | { 257 | return _vector4_sse(_mm_add_ps(v0.m128, v1.m128)); 258 | } 259 | 260 | //------------------------------------------------------------------------------ 261 | /** 262 | */ 263 | static 264 | inline 265 | _vector4_sse operator -(const _vector4_sse& v0, const _vector4_sse& v1) 266 | { 267 | return _vector4_sse(_mm_sub_ps(v0.m128, v1.m128)); 268 | } 269 | 270 | //------------------------------------------------------------------------------ 271 | /** 272 | */ 273 | static 274 | inline 275 | _vector4_sse operator *(const _vector4_sse& v0, const float& s) 276 | { 277 | __m128 packed = _mm_set1_ps(s); 278 | return _vector4_sse(_mm_mul_ps(v0.m128, packed)); 279 | } 280 | 281 | //------------------------------------------------------------------------------ 282 | /** 283 | */ 284 | static 285 | inline 286 | _vector4_sse operator -(const _vector4_sse& v) 287 | { 288 | __m128 zero = _mm_setzero_ps(); 289 | return _vector4_sse(_mm_sub_ps(zero, v.m128)); 290 | } 291 | 292 | //------------------------------------------------------------------------------ 293 | /** 294 | */ 295 | inline 296 | void 297 | _vector4_sse::lerp(const _vector4_sse& v0, float lerpVal) 298 | { 299 | x = v0.x + ((x - v0.x) * lerpVal); 300 | y = v0.y + ((y - v0.y) * lerpVal); 301 | z = v0.z + ((z - v0.z) * lerpVal); 302 | w = v0.w + ((w - v0.w) * lerpVal); 303 | } 304 | 305 | //------------------------------------------------------------------------------ 306 | #endif 307 | -------------------------------------------------------------------------------- /ext/mathlib/envelopecurve.h: -------------------------------------------------------------------------------- 1 | #ifndef N_ENVELOPE_CURVE_H 2 | #define N_ENVELOPE_CURVE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class nEnvelopeCurve 6 | @ingroup Math 7 | 8 | An envelope curve class. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/nmath.h" 13 | 14 | //------------------------------------------------------------------------------ 15 | // nEnvelopeVurve 16 | //------------------------------------------------------------------------------ 17 | class nEnvelopeCurve 18 | { 19 | public: 20 | /// possible modulation functions 21 | enum 22 | { 23 | Sine = 0, 24 | Cosine, 25 | }; 26 | 27 | /// constructor 1 28 | nEnvelopeCurve(); 29 | /// constructor 2 30 | nEnvelopeCurve(const float keyFrameValue0, const float keyFrameValue1, 31 | const float keyFrameValue2, const float keyFrameValue3, 32 | const float keyFramePos1, const float keyFramePos2, 33 | const float frequency, const float amplitude, 34 | const int modulationFunc); 35 | // set all parameters 36 | void SetParameters(const float keyFrameValue0, const float keyFrameValue1, 37 | const float keyFrameValue2, const float keyFrameValue3, 38 | const float keyFramePos1, const float keyFramePos2, 39 | const float frequency, const float amplitude, 40 | const int modulationFunc); 41 | // assign to other envelope curve 42 | void SetParameters(const nEnvelopeCurve& src); 43 | /// get the function value; pos must be between 0 and 1 44 | float GetValue(float pos) const; 45 | /// get the highest possible value 46 | float GetMaxPossibleValue() const; 47 | 48 | enum 49 | { 50 | NumValues = 4, 51 | }; 52 | float keyFrameValues[NumValues]; 53 | float keyFramePos1, keyFramePos2; // 0 through 1 54 | float frequency, amplitude; // parameters of the sinus function 55 | int modulationFunc; // use sine or cosine for modulation? 56 | }; 57 | 58 | //------------------------------------------------------------------------------ 59 | /** 60 | */ 61 | inline 62 | nEnvelopeCurve::nEnvelopeCurve() : 63 | keyFramePos1(.2f), 64 | keyFramePos2(.8f), 65 | frequency(.0f), 66 | amplitude(.0f), 67 | modulationFunc(Sine) 68 | { 69 | keyFrameValues[0] = .0f; 70 | keyFrameValues[1] = .0f; 71 | keyFrameValues[2] = .0f; 72 | keyFrameValues[3] = .0f; 73 | } 74 | 75 | //------------------------------------------------------------------------------ 76 | /** 77 | */ 78 | inline 79 | nEnvelopeCurve::nEnvelopeCurve(const float keyFrameValue0, 80 | const float keyFrameValue1, const float keyFrameValue2, 81 | const float keyFrameValue3, const float keyFramePos1, 82 | const float keyFramePos2, const float frequency, 83 | const float amplitude, const int modulationFunc/* = Sine*/) : 84 | keyFramePos1(keyFramePos1), 85 | keyFramePos2(keyFramePos2), 86 | frequency(frequency), 87 | amplitude(amplitude), 88 | modulationFunc(modulationFunc) 89 | { 90 | this->keyFrameValues[0] = keyFrameValue0; 91 | this->keyFrameValues[1] = keyFrameValue1; 92 | this->keyFrameValues[2] = keyFrameValue2; 93 | this->keyFrameValues[3] = keyFrameValue3; 94 | } 95 | 96 | //------------------------------------------------------------------------------ 97 | /** 98 | */ 99 | inline 100 | void nEnvelopeCurve::SetParameters(const float keyFrameValue0, const float keyFrameValue1, 101 | const float keyFrameValue2, const float keyFrameValue3, 102 | const float keyFramePos1, const float keyFramePos2, 103 | const float frequency, const float amplitude, 104 | const int modulationFunc/* = Sine*/) 105 | { 106 | this->keyFrameValues[0] = keyFrameValue0; 107 | this->keyFrameValues[1] = keyFrameValue1; 108 | this->keyFrameValues[2] = keyFrameValue2; 109 | this->keyFrameValues[3] = keyFrameValue3; 110 | this->keyFramePos1 = keyFramePos1; 111 | this->keyFramePos2 = keyFramePos2; 112 | this->frequency = frequency; 113 | this->amplitude = amplitude; 114 | this->modulationFunc = modulationFunc; 115 | } 116 | //------------------------------------------------------------------------------ 117 | /** 118 | */ 119 | inline 120 | void nEnvelopeCurve::SetParameters(const nEnvelopeCurve& src) 121 | { 122 | this->keyFrameValues[0] = src.keyFrameValues[0]; 123 | this->keyFrameValues[1] = src.keyFrameValues[1]; 124 | this->keyFrameValues[2] = src.keyFrameValues[2]; 125 | this->keyFrameValues[3] = src.keyFrameValues[3]; 126 | this->keyFramePos1 = src.keyFramePos1; 127 | this->keyFramePos2 = src.keyFramePos2; 128 | this->frequency = src.frequency; 129 | this->amplitude = src.amplitude; 130 | this->modulationFunc = src.modulationFunc; 131 | } 132 | 133 | //------------------------------------------------------------------------------ 134 | /** 135 | */ 136 | inline 137 | float nEnvelopeCurve::GetValue(float pos) const 138 | { 139 | pos = n_saturate(pos); 140 | 141 | float value; 142 | if (pos < this->keyFramePos1) 143 | { 144 | value = this->keyFrameValues[0] + 145 | (this->keyFrameValues[1] - this->keyFrameValues[0]) * 146 | (pos / this->keyFramePos1); 147 | } 148 | else if (pos < this->keyFramePos2) 149 | { 150 | value = this->keyFrameValues[1] + 151 | (this->keyFrameValues[2] - this->keyFrameValues[1]) * 152 | ((pos-this->keyFramePos1) / (this->keyFramePos2-this->keyFramePos1)); 153 | } 154 | else 155 | { 156 | value = this->keyFrameValues[2] + 157 | (this->keyFrameValues[3] - this->keyFrameValues[2]) * 158 | ((pos-this->keyFramePos2) / (1.0f-this->keyFramePos2)); 159 | } 160 | 161 | if (this->amplitude > 0.0f) 162 | { 163 | if (Sine == this->modulationFunc) 164 | { 165 | value += n_sin(pos * N_PI * 2 * this->frequency) * this->amplitude; 166 | } 167 | else 168 | { 169 | value += n_cos(pos * N_PI * 2 * this->frequency) * this->amplitude; 170 | } 171 | } 172 | 173 | return value; 174 | } 175 | 176 | //------------------------------------------------------------------------------ 177 | /** 178 | */ 179 | inline 180 | float nEnvelopeCurve::GetMaxPossibleValue() const 181 | { 182 | float maxVal; 183 | int keyFrame; 184 | maxVal = this->keyFrameValues[0]; 185 | for (keyFrame = 1; keyFrame < NumValues; keyFrame++) 186 | { 187 | maxVal = n_max(maxVal, this->keyFrameValues[keyFrame]); 188 | } 189 | 190 | return maxVal + this->amplitude; 191 | } 192 | //------------------------------------------------------------------------------ 193 | #endif 194 | -------------------------------------------------------------------------------- /ext/mathlib/euler.h: -------------------------------------------------------------------------------- 1 | /**** EulerAngles.h - Support for 24 angle schemes ****/ 2 | /* Ken Shoemake, 1993 */ 3 | #ifndef _H_EulerAngles 4 | #define _H_EulerAngles 5 | 6 | /*** Definitions ***/ 7 | typedef struct {float x, y, z, w;} QuatX; /* Quaternion */ 8 | //enum QuatPart {X, Y, Z, W}; 9 | typedef float HMatrix[4][4]; /* Right-handed, for column vectors */ 10 | typedef QuatX EulerAngles; /* (x,y,z)=ang 1,2,3, w=order code */ 11 | 12 | /*** Order type constants, constructors, extractors ***/ 13 | /* There are 24 possible conventions, designated by: */ 14 | /* o EulAxI = axis used initially */ 15 | /* o EulPar = parity of axis permutation */ 16 | /* o EulRep = repetition of initial axis as last */ 17 | /* o EulFrm = frame from which axes are taken */ 18 | /* Axes I,J,K will be a permutation of X,Y,Z. */ 19 | /* Axis H will be either I or K, depending on EulRep. */ 20 | /* Frame S takes axes from initial static frame. */ 21 | /* If ord = (AxI=X, Par=Even, Rep=No, Frm=S), then */ 22 | /* {a,b,c,ord} means Rz(c)Ry(b)Rx(a), where Rz(c)v */ 23 | /* rotates v around Z by c radians. */ 24 | #define EulFrmS 0 25 | #define EulFrmR 1 26 | #define EulFrm(ord) ((unsigned)(ord)&1) 27 | #define EulRepNo 0 28 | #define EulRepYes 1 29 | #define EulRep(ord) (((unsigned)(ord)>>1)&1) 30 | #define EulParEven 0 31 | #define EulParOdd 1 32 | #define EulPar(ord) (((unsigned)(ord)>>2)&1) 33 | #define EulSafe "\000\001\002\000" 34 | #define EulNext "\001\002\000\001" 35 | #define EulAxI(ord) ((int)(EulSafe[(((unsigned)(ord)>>3)&3)])) 36 | #define EulAxJ(ord) ((int)(EulNext[EulAxI(ord)+(EulPar(ord)==EulParOdd)])) 37 | #define EulAxK(ord) ((int)(EulNext[EulAxI(ord)+(EulPar(ord)!=EulParOdd)])) 38 | #define EulAxH(ord) ((EulRep(ord)==EulRepNo)?EulAxK(ord):EulAxI(ord)) 39 | /* EulGetOrd unpacks all useful information about order simultaneously. */ 40 | #define EulGetOrd(ord,i,j,k,h,n,s,f) {unsigned o=ord;f=o&1;o>>=1;s=o&1;o>>=1;\ 41 | n=o&1;o>>=1;i=EulSafe[o&3];j=EulNext[i+n];k=EulNext[i+1-n];h=s?k:i;} 42 | /* EulOrd creates an order value between 0 and 23 from 4-tuple choices. */ 43 | #define EulOrd(i,p,r,f) (((((((i)<<1)+(p))<<1)+(r))<<1)+(f)) 44 | /* Static axes */ 45 | enum xx_yy_zz_ww 46 | { 47 | XX = (1<<0), 48 | YY = (1<<1), 49 | ZZ = (1<<2), 50 | WW = (1<<3), 51 | }; 52 | 53 | #define EulOrdXYZs EulOrd(XX,EulParEven,EulRepNo,EulFrmS) 54 | #define EulOrdXYXs EulOrd(XX,EulParEven,EulRepYes,EulFrmS) 55 | #define EulOrdXZYs EulOrd(XX,EulParOdd,EulRepNo,EulFrmS) 56 | #define EulOrdXZXs EulOrd(XX,EulParOdd,EulRepYes,EulFrmS) 57 | #define EulOrdYZXs EulOrd(YY,EulParEven,EulRepNo,EulFrmS) 58 | #define EulOrdYZYs EulOrd(YY,EulParEven,EulRepYes,EulFrmS) 59 | #define EulOrdYXZs EulOrd(YY,EulParOdd,EulRepNo,EulFrmS) 60 | #define EulOrdYXYs EulOrd(YY,EulParOdd,EulRepYes,EulFrmS) 61 | #define EulOrdZXYs EulOrd(ZZ,EulParEven,EulRepNo,EulFrmS) 62 | #define EulOrdZXZs EulOrd(ZZ,EulParEven,EulRepYes,EulFrmS) 63 | #define EulOrdZYXs EulOrd(ZZ,EulParOdd,EulRepNo,EulFrmS) 64 | #define EulOrdZYZs EulOrd(ZZ,EulParOdd,EulRepYes,EulFrmS) 65 | /* Rotating axes */ 66 | #define EulOrdZYXr EulOrd(XX,EulParEven,EulRepNo,EulFrmR) 67 | #define EulOrdXYXr EulOrd(XX,EulParEven,EulRepYes,EulFrmR) 68 | #define EulOrdYZXr EulOrd(XX,EulParOdd,EulRepNo,EulFrmR) 69 | #define EulOrdXZXr EulOrd(XX,EulParOdd,EulRepYes,EulFrmR) 70 | #define EulOrdXZYr EulOrd(YY,EulParEven,EulRepNo,EulFrmR) 71 | #define EulOrdYZYr EulOrd(YY,EulParEven,EulRepYes,EulFrmR) 72 | #define EulOrdZXYr EulOrd(YY,EulParOdd,EulRepNo,EulFrmR) 73 | #define EulOrdYXYr EulOrd(YY,EulParOdd,EulRepYes,EulFrmR) 74 | #define EulOrdYXZr EulOrd(ZZ,EulParEven,EulRepNo,EulFrmR) 75 | #define EulOrdZXZr EulOrd(ZZ,EulParEven,EulRepYes,EulFrmR) 76 | #define EulOrdXYZr EulOrd(ZZ,EulParOdd,EulRepNo,EulFrmR) 77 | #define EulOrdZYZr EulOrd(ZZ,EulParOdd,EulRepYes,EulFrmR) 78 | 79 | EulerAngles Eul_(float ai, float aj, float ah, int order); 80 | QuatX Eul_ToQuat(EulerAngles ea); 81 | void Eul_ToHMatrix(EulerAngles ea, HMatrix M); 82 | EulerAngles Eul_FromHMatrix(HMatrix M, int order); 83 | EulerAngles Eul_FromQuat(QuatX q, int order); 84 | #endif 85 | -------------------------------------------------------------------------------- /ext/mathlib/eulerangles.h: -------------------------------------------------------------------------------- 1 | #ifndef N_EULERANGLES_H 2 | #define N_EULERANGLES_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class nEulerAngles 6 | @ingroup Math 7 | 8 | A class representing a rotation using 3 euler angles. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include 13 | #include 14 | #include "mathlib/matrix.h" 15 | #include "mathlib/euler.h" 16 | 17 | //------------------------------------------------------------------- 18 | /** 19 | @class nEulerAngles 20 | @ingroup NebulaMathDataTypes 21 | */ 22 | //------------------------------------------------------------------- 23 | class nEulerAngles { 24 | public: 25 | float x,y,z; 26 | 27 | //-- constructors ----------------------------------------------- 28 | nEulerAngles() 29 | : x(0.0f), 30 | y(0.0f), 31 | z(0.0f) 32 | {}; 33 | nEulerAngles(float _x, float _y, float _z) 34 | : x(_x), 35 | y(_y), 36 | z(_z) 37 | {}; 38 | nEulerAngles(const nEulerAngles& e) 39 | : x(e.x), 40 | y(e.y), 41 | z(e.z) 42 | {}; 43 | nEulerAngles(const matrix33& m) 44 | { 45 | Set(m); 46 | } 47 | 48 | //-- setting elements ------------------------------------------- 49 | void Set(float _x, float _y, float _z) 50 | { 51 | x = _x; 52 | y = _y; 53 | z = _z; 54 | }; 55 | void Set(const nEulerAngles& e) 56 | { 57 | x = e.x; 58 | y = e.y; 59 | z = e.z; 60 | }; 61 | void Set(const matrix33& m) 62 | { 63 | int i,j,k,h,n,s,f; 64 | EulGetOrd(EulOrdXYZs,i,j,k,h,n,s,f); 65 | if (s == EulRepYes) 66 | { 67 | double sy = (float) sqrt(m.m[0][1]*m.m[0][1] + m.m[0][2]*m.m[0][2]); 68 | if (sy > 16*FLT_EPSILON) 69 | { 70 | this->x = (float) atan2(m.m[0][1], m.m[0][2]); 71 | this->y = (float) atan2(sy, m.m[0][0]); 72 | this->z = (float) atan2(m.m[1][0], -m.m[2][0]); 73 | } 74 | else 75 | { 76 | this->x = (float) atan2(-m.m[1][2], m.m[1][1]); 77 | this->y = (float) atan2(sy, m.m[0][0]); 78 | this->z = 0; 79 | } 80 | } 81 | else 82 | { 83 | double cy = sqrt(m.m[0][0]*m.m[0][0] + m.m[1][0]*m.m[1][0]); 84 | if (cy > 16*FLT_EPSILON) 85 | { 86 | this->x = (float) atan2(m.m[2][1], m.m[2][2]); 87 | this->y = (float) atan2(-m.m[2][0], cy); 88 | this->z = (float) atan2(m.m[1][0], m.m[0][0]); 89 | } 90 | else 91 | { 92 | this->x = (float) atan2(-m.m[1][2], m.m[1][1]); 93 | this->y = (float) atan2(-m.m[2][0], cy); 94 | this->z = 0; 95 | } 96 | } 97 | if (n==EulParOdd) 98 | { 99 | this->x = -this->x; 100 | this->y = -this->y; 101 | this->z = -this->z; 102 | } 103 | if (f==EulFrmR) 104 | { 105 | float t = this->x; 106 | this->x = this->z; 107 | this->z = t; 108 | } 109 | } 110 | 111 | matrix33 GetMatrix() 112 | { 113 | matrix33 mat; 114 | 115 | double ti, tj, th, ci, cj, ch, si, sj, sh, cc, cs, sc, ss; 116 | int i,j,k,h,n,s,f; 117 | EulGetOrd(EulOrdXYZs,i,j,k,h,n,s,f); 118 | if (f==EulFrmR) {float t = x; x = z; z = t;} 119 | if (n==EulParOdd) {x = -x; y = -y; z = -z;} 120 | ti = x; tj = y; th = z; 121 | ci = cos(ti); cj = cos(tj); ch = cos(th); 122 | si = sin(ti); sj = sin(tj); sh = sin(th); 123 | cc = ci*ch; cs = ci*sh; sc = si*ch; ss = si*sh; 124 | if (s==EulRepYes) { 125 | mat.M11 = (float)(cj); mat.M12 = (float)( sj*si); mat.M13 = (float)( sj*ci); 126 | mat.M21 = (float)(sj*sh); mat.M22 = (float)(-cj*ss+cc); mat.M23 = (float)(-cj*cs-sc); 127 | mat.M31 = (float)(-sj*ch); mat.M23 = (float)( cj*sc+cs); mat.M33 = (float)( cj*cc-ss); 128 | } else { 129 | mat.M11 = (float)(cj*ch); mat.M12 = (float)(sj*sc-cs); mat.M13 = (float)(sj*cc+ss); 130 | mat.M21 = (float)(cj*sh); mat.M22 = (float)(sj*ss+cc); mat.M23 = (float)(sj*cs-sc); 131 | mat.M31 = (float)(-sj); mat.M32 = (float)(cj*si); mat.M33 = (float)(cj*ci); 132 | } 133 | 134 | return mat; 135 | } 136 | 137 | //-- operators -------------------------------------------------- 138 | bool operator== (const nEulerAngles& e) 139 | { 140 | return ((x == e.x) && (y == e.y) && (z == e.z)) ? true : false; 141 | } 142 | 143 | bool operator!= (const nEulerAngles& e) 144 | { 145 | return ((x != e.x) || (y != e.y) || (z != e.z)) ? true : false; 146 | } 147 | }; 148 | 149 | 150 | #endif 151 | -------------------------------------------------------------------------------- /ext/mathlib/line.h: -------------------------------------------------------------------------------- 1 | #ifndef N_LINE_H 2 | #define N_LINE_H 3 | //------------------------------------------------------------------- 4 | /** 5 | @class line2 6 | @ingroup Math 7 | 8 | A 2- and 3-dimensional line objects. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/vector.h" 13 | 14 | //------------------------------------------------------------------- 15 | class line2 16 | { 17 | public: 18 | /// constructor 19 | line2(); 20 | /// constructor #2 21 | line2(const vector2& v0, const vector2& v1); 22 | /// copy constructor 23 | line2(const line2& rhs); 24 | /// return start point 25 | const vector2& start() const; 26 | /// return end point 27 | vector2 end() const; 28 | /// return vector 29 | const vector2& vec() const; 30 | /// return length 31 | float len() const; 32 | /// get point on line given t 33 | vector2 ipol(const float t) const; 34 | 35 | vector2 b; 36 | vector2 m; 37 | }; 38 | 39 | //------------------------------------------------------------------- 40 | /** 41 | */ 42 | inline 43 | line2::line2() 44 | { 45 | // empty 46 | } 47 | 48 | //------------------------------------------------------------------- 49 | /** 50 | */ 51 | inline 52 | line2::line2(const vector2& v0, const vector2& v1) : 53 | b(v0), 54 | m(v1 - v0) 55 | { 56 | // empty 57 | } 58 | 59 | //------------------------------------------------------------------- 60 | /** 61 | */ 62 | inline 63 | line2::line2(const line2& rhs) : 64 | b(rhs.b), 65 | m(rhs.m) 66 | { 67 | // empty 68 | } 69 | 70 | //------------------------------------------------------------------- 71 | /** 72 | */ 73 | inline 74 | const vector2& 75 | line2::start() const 76 | { 77 | return this->b; 78 | } 79 | 80 | //------------------------------------------------------------------- 81 | /** 82 | */ 83 | inline 84 | vector2 85 | line2::end() const 86 | { 87 | return this->b + this->m; 88 | } 89 | 90 | //------------------------------------------------------------------- 91 | /** 92 | */ 93 | inline 94 | const vector2& 95 | line2::vec() const 96 | { 97 | return this->m; 98 | } 99 | 100 | //------------------------------------------------------------------- 101 | /** 102 | */ 103 | inline 104 | float 105 | line2::len() const 106 | { 107 | return m.len(); 108 | } 109 | 110 | //------------------------------------------------------------------- 111 | /** 112 | */ 113 | inline 114 | vector2 115 | line2::ipol(const float t) const 116 | { 117 | return vector2(b + m * t); 118 | } 119 | 120 | //------------------------------------------------------------------------------ 121 | /** 122 | @class line3 123 | @ingroup Math 124 | */ 125 | class line3 126 | { 127 | public: 128 | /// constructor 129 | line3(); 130 | /// constructor #2 131 | line3(const vector3& v0, const vector3& v1); 132 | /// copy constructor 133 | line3(const line3& l); 134 | /// set start and end point 135 | void set(const vector3& v0, const vector3& v1); 136 | /// get start point 137 | const vector3& start() const; 138 | /// get end point 139 | vector3 end() const; 140 | /// get vector 141 | const vector3& vec() const; 142 | /// get length 143 | float len() const; 144 | /// get squared length 145 | float lensquared() const; 146 | /// minimal distance of point to line 147 | float distance(const vector3& p) const; 148 | /// get point on line at t 149 | vector3 ipol(float t) const; 150 | 151 | vector3 b; 152 | vector3 m; 153 | }; 154 | 155 | //------------------------------------------------------------------------------ 156 | /** 157 | */ 158 | inline 159 | line3::line3() 160 | { 161 | // empty 162 | } 163 | 164 | //------------------------------------------------------------------------------ 165 | /** 166 | */ 167 | inline 168 | line3::line3(const vector3& v0, const vector3& v1) : 169 | b(v0), 170 | m(v1 - v0) 171 | { 172 | // empty 173 | } 174 | 175 | //------------------------------------------------------------------------------ 176 | /** 177 | */ 178 | inline 179 | line3::line3(const line3& rhs) : 180 | b(rhs.b), 181 | m(rhs.m) 182 | { 183 | // empty 184 | } 185 | 186 | //------------------------------------------------------------------------------ 187 | /** 188 | */ 189 | inline 190 | void 191 | line3::set(const vector3& v0, const vector3& v1) 192 | { 193 | this->b = v0; 194 | this->m = v1 - v0; 195 | } 196 | 197 | //------------------------------------------------------------------------------ 198 | /** 199 | */ 200 | inline 201 | const vector3& 202 | line3::start() const 203 | { 204 | return this->b; 205 | } 206 | 207 | //------------------------------------------------------------------------------ 208 | /** 209 | */ 210 | inline 211 | vector3 212 | line3::end() const 213 | { 214 | return this->b + this->m; 215 | } 216 | 217 | //------------------------------------------------------------------------------ 218 | /** 219 | */ 220 | inline 221 | const vector3& 222 | line3::vec() const 223 | { 224 | return this->m; 225 | } 226 | 227 | //------------------------------------------------------------------------------ 228 | /** 229 | */ 230 | inline 231 | float 232 | line3::len() const 233 | { 234 | return this->m.len(); 235 | } 236 | 237 | //------------------------------------------------------------------------------ 238 | /** 239 | */ 240 | inline 241 | float 242 | line3::lensquared() const 243 | { 244 | return this->m.lensquared(); 245 | } 246 | 247 | //------------------------------------------------------------------------------ 248 | /** 249 | */ 250 | inline 251 | float 252 | line3::distance(const vector3& p) const 253 | { 254 | vector3 diff(p - this->b); 255 | float l = (this->m % this->m); 256 | if (l > 0.0f) 257 | { 258 | float t = (this->m % diff) / l; 259 | diff = diff - this->m * t; 260 | return diff.len(); 261 | } else { 262 | // line is really a point... 263 | vector3 v(p - this->b); 264 | return v.len(); 265 | } 266 | } 267 | 268 | //------------------------------------------------------------------------------ 269 | /** 270 | */ 271 | inline 272 | vector3 273 | line3::ipol(const float t) const 274 | { 275 | return vector3(b + m*t); 276 | } 277 | 278 | //------------------------------------------------------------------------------ 279 | #endif 280 | -------------------------------------------------------------------------------- /ext/mathlib/matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef N_MATRIX_H 2 | #define N_MATRIX_H 3 | //------------------------------------------------------------------- 4 | // CLASSES 5 | // matrix33 -- 3x3 matrix 6 | // matrix34 -- 3x4 matrix 7 | // matrix44 -- 4x4 matrix 8 | //------------------------------------------------------------------- 9 | #include "mathlib/ntypes.h" 10 | 11 | #ifndef __USE_SSE__ 12 | #include "mathlib/_matrix33.h" 13 | #include "mathlib/_matrix44.h" 14 | typedef _matrix33 matrix33; 15 | typedef _matrix44 matrix44; 16 | #else 17 | #include "mathlib/_matrix33_sse.h" 18 | #include "mathlib/_matrix44_sse.h" 19 | typedef _matrix33_sse matrix33; 20 | typedef _matrix44_sse matrix44; 21 | #endif 22 | 23 | //------------------------------------------------------------------- 24 | #endif 25 | -------------------------------------------------------------------------------- /ext/mathlib/matrixdefs.h: -------------------------------------------------------------------------------- 1 | #ifndef N_MATRIXDEFS_H 2 | #define N_MATRIXDEFS_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | Common defines for matrix classes. 6 | 7 | (C) 2002 RadonLabs GmbH 8 | */ 9 | #define M11 m[0][0] 10 | #define M12 m[0][1] 11 | #define M13 m[0][2] 12 | #define M14 m[0][3] 13 | #define M21 m[1][0] 14 | #define M22 m[1][1] 15 | #define M23 m[1][2] 16 | #define M24 m[1][3] 17 | #define M31 m[2][0] 18 | #define M32 m[2][1] 19 | #define M33 m[2][2] 20 | #define M34 m[2][3] 21 | #define M41 m[3][0] 22 | #define M42 m[3][1] 23 | #define M43 m[3][2] 24 | #define M44 m[3][3] 25 | 26 | //------------------------------------------------------------------------------ 27 | #endif 28 | -------------------------------------------------------------------------------- /ext/mathlib/nmath.h: -------------------------------------------------------------------------------- 1 | #ifndef N_MATH_H 2 | #define N_MATH_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | General math functions and macros. 6 | 7 | (C) 2003 RadonLabs GmbH 8 | */ 9 | #include 10 | #include 11 | 12 | #ifdef _MSC_VER 13 | #define isnan _isnan 14 | #define isinf _isinf 15 | #endif 16 | 17 | #ifndef PI 18 | #define PI (3.1415926535897932384626433832795028841971693993751f) 19 | #endif 20 | 21 | #define N_PI PI 22 | 23 | #ifndef TINY 24 | #define TINY (0.0000001f) 25 | #endif 26 | 27 | #define n_max(a,b) (((a) > (b)) ? (a) : (b)) 28 | #define n_min(a,b) (((a) < (b)) ? (a) : (b)) 29 | #define n_abs(a) (((a)<0.0f) ? (-(a)) : (a)) 30 | #define n_sgn(a) (((a)<0.0f) ? (-1) : (1)) 31 | #define n_deg2rad(d) (((d)*PI)/180.0f) 32 | #define n_rad2deg(r) (((r)*180.0f)/PI) 33 | #define n_sin(x) (float(sin(x))) 34 | #define n_cos(x) (float(cos(x))) 35 | #define n_tan(x) (float(tan(x))) 36 | #define n_atan(x) (float(atan(x))) 37 | 38 | //------------------------------------------------------------------------------ 39 | /** 40 | log2() function. 41 | */ 42 | const float LN_2 = 0.693147180559945f; 43 | inline float n_log2(float f) 44 | { 45 | return logf(f) / LN_2; 46 | } 47 | 48 | //------------------------------------------------------------------------------ 49 | /** 50 | Integer clamping. 51 | */ 52 | inline int n_iclamp(int val, int minVal, int maxVal) 53 | { 54 | if (val < minVal) return minVal; 55 | else if (val > maxVal) return maxVal; 56 | else return val; 57 | } 58 | 59 | //------------------------------------------------------------------------------ 60 | /** 61 | acos with value clamping. 62 | */ 63 | inline float n_acos(float x) 64 | { 65 | if(x > 1.0f) x = 1.0f; 66 | if(x < -1.0f) x = -1.0f; 67 | return (float)acos(x); 68 | } 69 | 70 | //------------------------------------------------------------------------------ 71 | /** 72 | asin with value clamping. 73 | */ 74 | inline float n_asin(float x) 75 | { 76 | if(x > 1.0f) x = 1.0f; 77 | if(x < -1.0f) x = -1.0f; 78 | return (float)asin(x); 79 | } 80 | 81 | //------------------------------------------------------------------------------ 82 | /** 83 | Safe sqrt. 84 | */ 85 | inline float n_sqrt(float x) 86 | { 87 | if (x < 0.0f) x = (float) 0.0f; 88 | return (float) sqrt(x); 89 | } 90 | 91 | //------------------------------------------------------------------------------ 92 | /** 93 | A fuzzy floating point equality check 94 | */ 95 | inline bool n_fequal(float f0, float f1, float tol) { 96 | float f = f0-f1; 97 | if ((f>(-tol)) && (ftol) return true; 116 | else return false; 117 | } 118 | 119 | //------------------------------------------------------------------------------ 120 | /** 121 | fast float to int conversion (always truncates) 122 | see http://www.stereopsis.com/FPU.html for a discussion. 123 | NOTE: this works only on x86 endian machines. 124 | */ 125 | inline long n_ftol(float val) 126 | { 127 | double v = double(val) + (68719476736.0*1.5); 128 | return ((long*)&v)[0] >> 16; 129 | } 130 | 131 | //------------------------------------------------------------------------------ 132 | /** 133 | Smooth a new value towards an old value using a change value. 134 | */ 135 | inline float n_smooth(float newVal, float curVal, float maxChange) 136 | { 137 | float diff = newVal - curVal; 138 | if (fabs(diff) > maxChange) 139 | { 140 | if (diff > 0.0f) 141 | { 142 | curVal += maxChange; 143 | if (curVal > newVal) 144 | { 145 | curVal = newVal; 146 | } 147 | } 148 | else if (diff < 0.0f) 149 | { 150 | curVal -= maxChange; 151 | if (curVal < newVal) 152 | { 153 | curVal = newVal; 154 | } 155 | } 156 | } 157 | else 158 | { 159 | curVal = newVal; 160 | } 161 | return curVal; 162 | } 163 | 164 | //------------------------------------------------------------------------------ 165 | /** 166 | Clamp a value against lower und upper boundary. 167 | */ 168 | inline float n_clamp(float val, float lower, float upper) 169 | { 170 | if (val < lower) return lower; 171 | else if (val > upper) return upper; 172 | else return val; 173 | } 174 | 175 | //------------------------------------------------------------------------------ 176 | /** 177 | Saturate a value (clamps between 0.0f and 1.0f) 178 | */ 179 | inline float n_saturate(float val) 180 | { 181 | if (val < 0.0f) return 0.0f; 182 | else if (val > 1.0f) return 1.0f; 183 | else return val; 184 | } 185 | 186 | //------------------------------------------------------------------------------ 187 | /** 188 | Return a pseudo random number between 0 and 1. 189 | */ 190 | inline float n_rand() 191 | { 192 | return float(rand()) / float(RAND_MAX); 193 | } 194 | 195 | //------------------------------------------------------------------------------ 196 | /** 197 | Chop float to int. 198 | */ 199 | inline int n_fchop(float f) 200 | { 201 | // FIXME! 202 | return int(f); 203 | } 204 | 205 | //------------------------------------------------------------------------------ 206 | /** 207 | Round float to integer. 208 | */ 209 | inline int n_frnd(float f) 210 | { 211 | return n_fchop(floorf(f + 0.5f)); 212 | } 213 | 214 | //------------------------------------------------------------------------------ 215 | /** 216 | Linearly interpolate between 2 values: ret = x + l * (y - x) 217 | */ 218 | inline float n_lerp(float x, float y, float l) 219 | { 220 | return x + l * (y - x); 221 | } 222 | 223 | //------------------------------------------------------------------------------ 224 | #endif 225 | 226 | -------------------------------------------------------------------------------- /ext/mathlib/noise.h: -------------------------------------------------------------------------------- 1 | #ifndef N_PERLIN_H 2 | #define N_PERLIN_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class noise 6 | @ingroup Math 7 | 8 | Perlin noise class. 9 | 10 | See http://mrl.nyu.edu/~perlin/noise/ for details. 11 | 12 | (C) 2004 RadonLabs GmbH 13 | */ 14 | #include "kernel/ntypes.h" 15 | #include "math.h" 16 | 17 | //------------------------------------------------------------------------------ 18 | class noise 19 | { 20 | public: 21 | /// generate noise value 22 | static float gen(float x, float y, float z); 23 | 24 | private: 25 | /// compute fade curve 26 | static float fade(float t); 27 | /// lerp between a and b 28 | static float lerp(float t, float a, float b); 29 | /// convert into gradient direction 30 | static float grad(int hash, float x, float y, float z); 31 | 32 | static int perm[512]; 33 | }; 34 | 35 | //------------------------------------------------------------------------------ 36 | /** 37 | */ 38 | inline 39 | float 40 | noise::fade(float t) 41 | { 42 | return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); 43 | } 44 | 45 | //------------------------------------------------------------------------------ 46 | /** 47 | */ 48 | inline 49 | float 50 | noise::lerp(float t, float a, float b) 51 | { 52 | return a + t * (b - a); 53 | } 54 | 55 | //------------------------------------------------------------------------------ 56 | /** 57 | */ 58 | inline 59 | float 60 | noise::grad(int hash, float x, float y, float z) 61 | { 62 | int h = hash & 15; 63 | float u = h < 8 ? x : y; 64 | float v = h < 4 ? y : ((h == 12) || (h==14)) ? x : z; 65 | return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); 66 | } 67 | 68 | //------------------------------------------------------------------------------ 69 | /** 70 | */ 71 | inline 72 | float 73 | noise::gen(float x, float y, float z) 74 | { 75 | float floorX = floorf(x); 76 | float floorY = floorf(y); 77 | float floorZ = floorf(z); 78 | 79 | // find unit cube that contains point 80 | int X = int(floorX) & 255; 81 | int Y = int(floorY) & 255; 82 | int Z = int(floorZ) & 255; 83 | 84 | // find relative x,y,z of point in cube 85 | x -= floorX; 86 | y -= floorY; 87 | z -= floorZ; 88 | 89 | // compute fade curves for x, y, z 90 | float u = fade(x); 91 | float v = fade(y); 92 | float w = fade(z); 93 | 94 | // hash coords of 8 cube corners 95 | int A = perm[X] + Y; 96 | int AA = perm[A] + Z; 97 | int AB = perm[A+1] + Z; 98 | int B = perm[X+1] + Y; 99 | int BA = perm[B] + Z; 100 | int BB = perm[B+1] + Z; 101 | 102 | // add blended results from 8 corners of cube 103 | return lerp(w, lerp(v, lerp(u, grad(perm[AA ], x , y , z ), 104 | grad(perm[BA ], x-1, y , z )), 105 | lerp(u, grad(perm[AB ], x , y-1, z ), 106 | grad(perm[BB ], x-1, y-1, z ))), 107 | lerp(v, lerp(u, grad(perm[AA+1], x , y , z-1 ), 108 | grad(perm[BA+1], x-1, y , z-1 )), 109 | lerp(u, grad(perm[AB+1], x , y-1, z-1 ), 110 | grad(perm[BB+1], x-1, y-1, z-1 )))); 111 | } 112 | 113 | //------------------------------------------------------------------------------ 114 | #endif -------------------------------------------------------------------------------- /ext/mathlib/ntypes.h: -------------------------------------------------------------------------------- 1 | #ifndef N_TYPES_H 2 | #define N_TYPES_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | Lowlevel Nebula defs. 6 | 7 | (C) 2002 RadonLabs GmbH 8 | */ 9 | #ifndef __XBxX__ 10 | #include 11 | #include 12 | #endif 13 | 14 | //#include "kernel/nsystem.h" 15 | //#include "kernel/ndebug.h" 16 | //#include "kernel/ndefclass.h" 17 | 18 | // Shortcut Typedefs 19 | typedef unsigned long ulong; 20 | //typedef unsigned int uint; 21 | ////typedef unsigned short ushort; 22 | //typedef unsigned char uchar; 23 | //typedef float float2[2]; 24 | //typedef float float3[3]; 25 | //typedef float float4[4]; 26 | struct nFloat4 27 | { 28 | float x; 29 | float y; 30 | float z; 31 | float w; 32 | }; 33 | struct nFloat3 34 | { 35 | float x; 36 | float y; 37 | float z; 38 | }; 39 | struct nFloat2 40 | { 41 | float x; 42 | float y; 43 | }; 44 | typedef unsigned int nFourCC; 45 | typedef double nTime; 46 | 47 | #ifndef NULL 48 | #define NULL (0L) 49 | #endif 50 | 51 | //------------------------------------------------------------------------------ 52 | #define N_MAXPATH (512) // maximum length for complete path 53 | #define N_MAXNAMELEN (32) // maximum length for single path component 54 | 55 | //------------------------------------------------------------------------------ 56 | #define nID(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d)) 57 | #define MAKE_FOURCC(ch0,ch1,ch2,ch3) (ch0 | ch1<<8 | ch2<<16 | ch3<<24) 58 | #define FOURCC(i) (((i&0xff000000)>>24) | ((i&0x00ff0000)>>8) | ((i&0x0000ff00)<<8) | ((i&0x000000ff)<<24)) 59 | #define N_WHITESPACE " \r\n\t" 60 | 61 | #ifdef __LINUX__ 62 | #define n_stricmp strcasecmp 63 | #else 64 | #define n_stricmp stricmp 65 | #endif 66 | 67 | #ifdef __WIN32__ 68 | #define snprintf _snprintf 69 | #define vsnprintf _vsnprintf 70 | #endif 71 | 72 | // maps unsigned 8 bits/channel to D3DCOLOR 73 | #define N_ARGB(a,r,g,b) ((uint)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff))) 74 | #define N_RGBA(r,g,b,a) N_ARGB(a,r,g,b) 75 | #define N_XRGB(r,g,b) N_ARGB(0xff,r,g,b) 76 | #define N_COLORVALUE(r,g,b,a) N_RGBA((uint)((r)*255.f),(uint)((g)*255.f),(uint)((b)*255.f),(uint)((a)*255.f)) 77 | 78 | //------------------------------------------------------------------------------ 79 | // public kernel C functions 80 | //------------------------------------------------------------------------------ 81 | /* 82 | void __cdecl n_printf(const char *, ...); 83 | void __cdecl n_error(const char*, ...); 84 | void __cdecl n_message(const char*, ...); 85 | void n_sleep(double); 86 | char *n_strdup(const char *); 87 | char *n_strncpy2(char *, const char *, size_t); 88 | bool n_strmatch(const char *, const char *); 89 | void n_strcat(char *, const char *, size_t); 90 | 91 | void n_barf(const char *, const char *, int); 92 | void n_barf2(const char*, const char*, const char*, int); 93 | 94 | void *n_dllopen(const char *); 95 | void n_dllclose(void *); 96 | void *n_dllsymbol(void *, const char *); 97 | 98 | nFourCC n_strtofourcc(const char*); 99 | const char* n_fourcctostr(nFourCC); 100 | */ 101 | //------------------------------------------------------------------------------ 102 | // Nebula memory management and debugging stuff. 103 | //------------------------------------------------------------------------------ 104 | extern bool nMemoryLoggingEnabled; 105 | struct nMemoryStats 106 | { 107 | int highWaterSize; // max allocated size so far 108 | int totalCount; // total number of allocations 109 | int totalSize; // current allocated size 110 | }; 111 | 112 | void n_dbgmeminit(); // initialize memory debugging system 113 | nMemoryStats n_dbgmemgetstats(); // defined in ndbgalloc.cc 114 | 115 | #ifdef new 116 | #undef new 117 | #endif 118 | 119 | #ifdef delete 120 | #undef delete 121 | #endif 122 | 123 | // implemented in ndbgalloc.cc 124 | void* operator new(size_t size); 125 | void* operator new(size_t size, const char* file, int line); 126 | void* operator new[](size_t size); 127 | void* operator new[](size_t size, const char* file, int line); 128 | void operator delete(void* p); 129 | void operator delete[](void* p); 130 | void* n_malloc_dbg(size_t size, const char* file, int line); 131 | void* n_calloc_dbg(size_t num, size_t size, const char* file, int line); 132 | void* n_realloc_dbg(void* memblock, size_t size, const char* file, int line); 133 | void n_free_dbg(void* memblock, const char* file, int line); 134 | 135 | #ifdef _DEBUG 136 | #define n_new(type) new(__FILE__,__LINE__) type 137 | #define n_new_array(type,size) new(__FILE__,__LINE__) type[size] 138 | #define n_delete(ptr) delete ptr 139 | #define n_delete_array(ptr) delete[] ptr 140 | #define n_malloc(size) n_malloc_dbg(size, __FILE__, __LINE__); 141 | #define n_calloc(num, size) n_calloc_dbg(num, size, __FILE__, __LINE__); 142 | #define n_realloc(memblock, size) n_realloc_dbg(memblock, size, __FILE__, __LINE__); 143 | #define n_free(memblock) n_free_dbg(memblock, __FILE__, __LINE__); 144 | #else 145 | #define n_new(type) new type 146 | #define n_new_array(type,size) new type[size] 147 | #define n_delete(ptr) delete ptr 148 | #define n_delete_array(ptr) delete[] ptr 149 | #define n_malloc(size) malloc(size) 150 | #define n_calloc(num, size) calloc(num, size) 151 | #define n_realloc(memblock, size) realloc(memblock, size) 152 | #define n_free(memblock) free(memblock) 153 | #endif 154 | 155 | // define an nAttribute C++ class extension, declares 156 | // a function member, setter and getter method for the attribute 157 | // #define __ref_attr(TYPE,NAME) private: TYPE NAME; public: void Set##NAME(const TYPE& t) {this->NAME = t; }; const TYPE& Get##NAME() const { return this->NAME; }; 158 | // #define __attr(TYPE,NAME) private: TYPE NAME; public: void Set##NAME(TYPE t) {this->NAME = t}; TYPE Get##NAME() const { return this->NAME; }; 159 | 160 | #define nSetter(METHOD, TYPE, MEMBER) inline void METHOD(TYPE t) { this->MEMBER = t; } 161 | #define nGetter(TYPE, METHOD, MEMBER) inline TYPE METHOD() const { return this->MEMBER; } 162 | 163 | //-------------------------------------------------------------------- 164 | #endif 165 | -------------------------------------------------------------------------------- /ext/mathlib/ntypes.h~: -------------------------------------------------------------------------------- 1 | #ifndef N_TYPES_H 2 | #define N_TYPES_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | Lowlevel Nebula defs. 6 | 7 | (C) 2002 RadonLabs GmbH 8 | */ 9 | #ifndef __XBxX__ 10 | #include 11 | #include 12 | #endif 13 | 14 | //#include "kernel/nsystem.h" 15 | //#include "kernel/ndebug.h" 16 | //#include "kernel/ndefclass.h" 17 | 18 | // Shortcut Typedefs 19 | typedef unsigned long ulong; 20 | //typedef unsigned int uint; 21 | ////typedef unsigned short ushort; 22 | //typedef unsigned char uchar; 23 | typedef float float2[2]; 24 | typedef float float3[3]; 25 | typedef float float4[4]; 26 | struct nFloat4 27 | { 28 | float x; 29 | float y; 30 | float z; 31 | float w; 32 | }; 33 | struct nFloat3 34 | { 35 | float x; 36 | float y; 37 | float z; 38 | }; 39 | struct nFloat2 40 | { 41 | float x; 42 | float y; 43 | }; 44 | typedef unsigned int nFourCC; 45 | typedef double nTime; 46 | 47 | #ifndef NULL 48 | #define NULL (0L) 49 | #endif 50 | 51 | //------------------------------------------------------------------------------ 52 | #define N_MAXPATH (512) // maximum length for complete path 53 | #define N_MAXNAMELEN (32) // maximum length for single path component 54 | 55 | //------------------------------------------------------------------------------ 56 | #define nID(a,b,c,d) ((a<<24)|(b<<16)|(c<<8)|(d)) 57 | #define MAKE_FOURCC(ch0,ch1,ch2,ch3) (ch0 | ch1<<8 | ch2<<16 | ch3<<24) 58 | #define FOURCC(i) (((i&0xff000000)>>24) | ((i&0x00ff0000)>>8) | ((i&0x0000ff00)<<8) | ((i&0x000000ff)<<24)) 59 | #define N_WHITESPACE " \r\n\t" 60 | 61 | #ifdef __LINUX__ 62 | #define n_stricmp strcasecmp 63 | #else 64 | #define n_stricmp stricmp 65 | #endif 66 | 67 | #ifdef __WIN32__ 68 | #define snprintf _snprintf 69 | #define vsnprintf _vsnprintf 70 | #endif 71 | 72 | // maps unsigned 8 bits/channel to D3DCOLOR 73 | #define N_ARGB(a,r,g,b) ((uint)((((a)&0xff)<<24)|(((r)&0xff)<<16)|(((g)&0xff)<<8)|((b)&0xff))) 74 | #define N_RGBA(r,g,b,a) N_ARGB(a,r,g,b) 75 | #define N_XRGB(r,g,b) N_ARGB(0xff,r,g,b) 76 | #define N_COLORVALUE(r,g,b,a) N_RGBA((uint)((r)*255.f),(uint)((g)*255.f),(uint)((b)*255.f),(uint)((a)*255.f)) 77 | 78 | //------------------------------------------------------------------------------ 79 | // public kernel C functions 80 | //------------------------------------------------------------------------------ 81 | void __cdecl n_printf(const char *, ...); 82 | void __cdecl n_error(const char*, ...); 83 | void __cdecl n_message(const char*, ...); 84 | void n_sleep(double); 85 | char *n_strdup(const char *); 86 | char *n_strncpy2(char *, const char *, size_t); 87 | bool n_strmatch(const char *, const char *); 88 | void n_strcat(char *, const char *, size_t); 89 | 90 | void n_barf(const char *, const char *, int); 91 | void n_barf2(const char*, const char*, const char*, int); 92 | 93 | void *n_dllopen(const char *); 94 | void n_dllclose(void *); 95 | void *n_dllsymbol(void *, const char *); 96 | 97 | nFourCC n_strtofourcc(const char*); 98 | const char* n_fourcctostr(nFourCC); 99 | 100 | //------------------------------------------------------------------------------ 101 | // Nebula memory management and debugging stuff. 102 | //------------------------------------------------------------------------------ 103 | extern bool nMemoryLoggingEnabled; 104 | struct nMemoryStats 105 | { 106 | int highWaterSize; // max allocated size so far 107 | int totalCount; // total number of allocations 108 | int totalSize; // current allocated size 109 | }; 110 | 111 | void n_dbgmeminit(); // initialize memory debugging system 112 | nMemoryStats n_dbgmemgetstats(); // defined in ndbgalloc.cc 113 | 114 | #ifdef new 115 | #undef new 116 | #endif 117 | 118 | #ifdef delete 119 | #undef delete 120 | #endif 121 | 122 | // implemented in ndbgalloc.cc 123 | void* operator new(size_t size); 124 | void* operator new(size_t size, const char* file, int line); 125 | void* operator new[](size_t size); 126 | void* operator new[](size_t size, const char* file, int line); 127 | void operator delete(void* p); 128 | void operator delete[](void* p); 129 | void* n_malloc_dbg(size_t size, const char* file, int line); 130 | void* n_calloc_dbg(size_t num, size_t size, const char* file, int line); 131 | void* n_realloc_dbg(void* memblock, size_t size, const char* file, int line); 132 | void n_free_dbg(void* memblock, const char* file, int line); 133 | 134 | #ifdef _DEBUG 135 | #define n_new(type) new(__FILE__,__LINE__) type 136 | #define n_new_array(type,size) new(__FILE__,__LINE__) type[size] 137 | #define n_delete(ptr) delete ptr 138 | #define n_delete_array(ptr) delete[] ptr 139 | #define n_malloc(size) n_malloc_dbg(size, __FILE__, __LINE__); 140 | #define n_calloc(num, size) n_calloc_dbg(num, size, __FILE__, __LINE__); 141 | #define n_realloc(memblock, size) n_realloc_dbg(memblock, size, __FILE__, __LINE__); 142 | #define n_free(memblock) n_free_dbg(memblock, __FILE__, __LINE__); 143 | #else 144 | #define n_new(type) new type 145 | #define n_new_array(type,size) new type[size] 146 | #define n_delete(ptr) delete ptr 147 | #define n_delete_array(ptr) delete[] ptr 148 | #define n_malloc(size) malloc(size) 149 | #define n_calloc(num, size) calloc(num, size) 150 | #define n_realloc(memblock, size) realloc(memblock, size) 151 | #define n_free(memblock) free(memblock) 152 | #endif 153 | 154 | // define an nAttribute C++ class extension, declares 155 | // a function member, setter and getter method for the attribute 156 | // #define __ref_attr(TYPE,NAME) private: TYPE NAME; public: void Set##NAME(const TYPE& t) {this->NAME = t; }; const TYPE& Get##NAME() const { return this->NAME; }; 157 | // #define __attr(TYPE,NAME) private: TYPE NAME; public: void Set##NAME(TYPE t) {this->NAME = t}; TYPE Get##NAME() const { return this->NAME; }; 158 | 159 | #define nSetter(METHOD, TYPE, MEMBER) inline void METHOD(TYPE t) { this->MEMBER = t; } 160 | #define nGetter(TYPE, METHOD, MEMBER) inline TYPE METHOD() const { return this->MEMBER; } 161 | 162 | //-------------------------------------------------------------------- 163 | #endif 164 | -------------------------------------------------------------------------------- /ext/mathlib/pknorm.h: -------------------------------------------------------------------------------- 1 | #ifndef N_PKNORM_H 2 | #define N_PKNORM_H 3 | //------------------------------------------------------------------- 4 | /** 5 | @class pknorm3 6 | @ingroup Math 7 | 8 | A normal packed into 16 bits 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/vector.h" 13 | 14 | //------------------------------------------------------------------- 15 | class pknorm3 { 16 | ushort n; 17 | 18 | public: 19 | //--- pack each component into 5 bits --------------------------- 20 | void pack(vector3& v) { 21 | const float r = 31.999f; 22 | unsigned int ix = (unsigned int) (r * (v.x+1.0f)*0.5f); 23 | unsigned int iy = (unsigned int)(r * (v.y+1.0f)*0.5f); 24 | unsigned int iz = (unsigned int) (r * (v.z+1.0f)*0.5f); 25 | n = ((ix&31)<<10) | ((iy&31)<<5) | (iz&31); 26 | }; 27 | 28 | //--- unpack into vector3 --------------------------------------- 29 | vector3 unpack(void) { 30 | const float r = 31.999f; 31 | float ix = float((n>>10) & 31); 32 | float iy = float((n>>5) & 31); 33 | float iz = float(n & 31); 34 | vector3 v((((ix/r)*2.0f)-1.0f), 35 | (((iy/r)*2.0f)-1.0f), 36 | (((iz/r)*2.0f)-1.0f)); 37 | return v; 38 | }; 39 | 40 | //-- constructors ----------------------------------------------- 41 | pknorm3() : n(0) {}; 42 | pknorm3(vector3& v) { 43 | pack(v); 44 | }; 45 | 46 | //-- get/set packed representation ------------------------------ 47 | ushort getpacked(void) { 48 | return n; 49 | }; 50 | void setpacked(ushort _n) { 51 | n = _n; 52 | }; 53 | }; 54 | //------------------------------------------------------------------- 55 | #endif 56 | -------------------------------------------------------------------------------- /ext/mathlib/plane.h: -------------------------------------------------------------------------------- 1 | #ifndef N_PLANE_H 2 | #define N_PLANE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class plane 6 | @ingroup Math 7 | 8 | A plane in 3d space. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/vector.h" 13 | #include "mathlib/line.h" 14 | 15 | //------------------------------------------------------------------------------ 16 | class plane 17 | { 18 | public: 19 | /// default constructor 20 | plane(); 21 | /// constructor 1 22 | plane(float A, float B, float C, float D); 23 | /// constructor 2 24 | plane(const plane& p); 25 | /// constructor 3 26 | plane(const vector3& v0, const vector3& v1, const vector3& v2); 27 | /// set contents 28 | void set(float A, float B, float C, float D); 29 | /// construct plane from 3 vectors 30 | void set(const vector3& v0, const vector3& v1, const vector3& v2); 31 | /// compute distance of point to plane 32 | float distance(const vector3& v) const; 33 | /// get plane normal 34 | vector3 normal() const; 35 | /// get intersecting t of line with one sided plane 36 | bool intersect(const line3& l, float& t) const; 37 | /// get plane/plane intersection 38 | bool intersect(const plane& p, line3& l) const; 39 | 40 | float a , b, c, d; 41 | }; 42 | 43 | //------------------------------------------------------------------------------ 44 | /** 45 | */ 46 | inline 47 | plane::plane() : 48 | a(0.0f), 49 | b(0.0f), 50 | c(0.0f), 51 | d(1.0f) 52 | { 53 | // empty 54 | } 55 | 56 | //------------------------------------------------------------------------------ 57 | /** 58 | */ 59 | inline 60 | plane::plane(float A, float B, float C, float D) : 61 | a(A), 62 | b(B), 63 | c(C), 64 | d(D) 65 | { 66 | // empty 67 | } 68 | 69 | //------------------------------------------------------------------------------ 70 | /** 71 | */ 72 | inline 73 | plane::plane(const plane& rhs) : 74 | a(rhs.a), 75 | b(rhs.b), 76 | c(rhs.c), 77 | d(rhs.d) 78 | { 79 | // empty 80 | } 81 | 82 | //------------------------------------------------------------------------------ 83 | /** 84 | */ 85 | inline 86 | void 87 | plane::set(float A, float B, float C, float D) 88 | { 89 | this->a = A; 90 | this->b = B; 91 | this->c = C; 92 | this->d = D; 93 | } 94 | 95 | //------------------------------------------------------------------------------ 96 | /** 97 | Constructs a plane from 3 position vectors. 98 | */ 99 | inline 100 | void 101 | plane::set(const vector3& v0, const vector3& v1, const vector3& v2) 102 | { 103 | vector3 cross((v2 - v0) * (v1 - v0)); 104 | cross.norm(); 105 | this->a = cross.x; 106 | this->b = cross.y; 107 | this->c = cross.z; 108 | this->d = -(a * v0.x + b * v0.y + c * v0.z); 109 | } 110 | 111 | //------------------------------------------------------------------------------ 112 | /** 113 | */ 114 | inline 115 | plane::plane(const vector3& v0, const vector3& v1, const vector3& v2) 116 | { 117 | this->set(v0, v1, v2); 118 | } 119 | 120 | //------------------------------------------------------------------------------ 121 | /** 122 | Computes the distance of a point to the plane. Return 0.0 if the 123 | point is on the plane. 124 | */ 125 | inline 126 | float 127 | plane::distance(const vector3& v) const 128 | { 129 | return this->a * v.x + this->b * v.y + this->c * v.z + this->d; 130 | } 131 | 132 | //------------------------------------------------------------------------------ 133 | /** 134 | Returns the plane normal. 135 | */ 136 | inline 137 | vector3 138 | plane::normal() const 139 | { 140 | return vector3(this->a, this->b, this->c); 141 | } 142 | 143 | //------------------------------------------------------------------------------ 144 | /** 145 | Get intersecting t of line with one-sided plane. Returns false 146 | if the line is parallel to the plane. 147 | */ 148 | inline 149 | bool 150 | plane::intersect(const line3& l, float& t) const 151 | { 152 | float f0 = this->a * l.b.x + this->b * l.b.y + this->c * l.b.z + this->d; 153 | float f1 = this->a * -l.m.x + this->b * -l.m.y + this->c * -l.m.z; 154 | if ((f1 < -0.0001f) || (f1 > 0.0001f)) 155 | { 156 | t = f0 / f1; 157 | return true; 158 | } 159 | else 160 | { 161 | return false; 162 | } 163 | } 164 | 165 | //------------------------------------------------------------------------------ 166 | /** 167 | Get plane/plane intersection. Return false if planes are parallel. 168 | */ 169 | inline 170 | bool 171 | plane::intersect(const plane& p, line3& l) const 172 | { 173 | vector3 n0 = this->normal(); 174 | vector3 n1 = p.normal(); 175 | float n00 = n0 % n0; 176 | float n01 = n0 % n1; 177 | float n11 = n1 % n1; 178 | float det = n00 * n11 - n01 * n01; 179 | const float tol = 1e-06f; 180 | if (fabs(det) < tol) 181 | { 182 | return false; 183 | } 184 | else 185 | { 186 | float inv_det = 1.0f/det; 187 | float c0 = (n11 * this->d - n01 * p.d) * inv_det; 188 | float c1 = (n00 * p.d - n01 * this->d)* inv_det; 189 | l.m = n0 * n1; 190 | l.b = n0 * c0 + n1 * c1; 191 | return true; 192 | } 193 | } 194 | 195 | //------------------------------------------------------------------------------ 196 | #endif 197 | -------------------------------------------------------------------------------- /ext/mathlib/polar.h: -------------------------------------------------------------------------------- 1 | #ifndef N_POLAR_H 2 | #define N_POLAR_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class polar2 6 | @ingroup Math 7 | 8 | A polar coordinate inline class, consisting of 2 angles theta (latitude) 9 | and rho (longitude). Also offers conversion between cartesian and 10 | polar space. 11 | 12 | Allowed range for theta is 0..180 degree (in rad!) and for rho 0..360 degree 13 | (in rad). 14 | 15 | (C) 2004 RadonLabs GmbH 16 | */ 17 | #include 18 | #include 19 | #include 20 | 21 | #include "mathlib/vector.h" 22 | 23 | //------------------------------------------------------------------------------ 24 | class polar2 25 | { 26 | public: 27 | /// the default constructor 28 | polar2(); 29 | /// constructor, theta and rho args 30 | polar2(float t, float r); 31 | /// constructor, normalized cartesian vector as arg 32 | polar2(const vector3& v); 33 | /// the copy constructor 34 | polar2(const polar2& src); 35 | /// the assignment operator 36 | polar2& operator=(const polar2& rhs); 37 | /// convert to normalized cartesian coords 38 | vector3 get_cartesian() const; 39 | /// get theta and rho in a 2d vec 40 | vector2 get() const; 41 | /// set to polar object 42 | void set(const polar2& p); 43 | /// set to theta and rho 44 | void set(const float t, const float r); 45 | /// set to cartesian 46 | void set(const vector3&); 47 | /// fuzzy equality check 48 | bool isequal(const polar2& rhs, float tol); 49 | 50 | float theta; 51 | float rho; 52 | 53 | private: 54 | /// the equal operator is not allowed, use isequal() with tolerance! 55 | bool operator==(const polar2& /*rhs*/) { return false; } 56 | }; 57 | 58 | //------------------------------------------------------------------------------ 59 | /** 60 | */ 61 | inline 62 | polar2::polar2() : 63 | theta(0.0f), 64 | rho(0.0f) 65 | { 66 | // empty 67 | } 68 | 69 | //------------------------------------------------------------------------------ 70 | /** 71 | */ 72 | inline 73 | polar2::polar2(float t, float r) : 74 | theta(t), 75 | rho(r) 76 | { 77 | // empty 78 | } 79 | 80 | //------------------------------------------------------------------------------ 81 | /** 82 | */ 83 | inline 84 | polar2::polar2(const vector3& v) 85 | { 86 | this->set(v); 87 | } 88 | 89 | //------------------------------------------------------------------------------ 90 | /** 91 | */ 92 | inline 93 | polar2::polar2(const polar2& src) : 94 | theta(src.theta), 95 | rho(src.rho) 96 | { 97 | // empty 98 | } 99 | 100 | //------------------------------------------------------------------------------ 101 | /** 102 | */ 103 | inline 104 | polar2& 105 | polar2::operator=(const polar2& rhs) 106 | { 107 | this->theta = rhs.theta; 108 | this->rho = rhs.rho; 109 | return *this; 110 | } 111 | 112 | //------------------------------------------------------------------------------ 113 | /** 114 | */ 115 | inline 116 | void 117 | polar2::set(const polar2& p) 118 | { 119 | this->theta = p.theta; 120 | this->rho = p.rho; 121 | } 122 | 123 | //------------------------------------------------------------------------------ 124 | /** 125 | */ 126 | inline 127 | void 128 | polar2::set(const float t, const float r) 129 | { 130 | this->theta = t; 131 | this->rho = r; 132 | } 133 | 134 | //------------------------------------------------------------------------------ 135 | /** 136 | Convert cartesian to polar. 137 | */ 138 | inline 139 | void 140 | polar2::set(const vector3& vec) 141 | { 142 | double dTheta = acos(vec.y); 143 | 144 | // build a normalized 2d vector of the xz component 145 | vector2 v2(vec.x, vec.z); 146 | v2.norm(); 147 | 148 | // adjust dRho based on the quadrant we are in 149 | double dRho; 150 | if ((v2.x >= 0.0f) && (v2.y >= 0.0f)) 151 | { 152 | // quadrant 1 153 | dRho = asin(v2.x); 154 | } 155 | else if ((v2.x < 0.0f) && (v2.y >= 0.0f)) 156 | { 157 | // quadrant 2 158 | dRho = asin(v2.y) + n_deg2rad(270.0f); 159 | } 160 | else if ((v2.x < 0.0f) && (v2.y < 0.0f)) 161 | { 162 | // quadrant 3 163 | dRho = asin(-v2.x) + n_deg2rad(180.0f); 164 | } 165 | else 166 | { 167 | // quadrant 4 168 | dRho = asin(-v2.y) + n_deg2rad(90.0f); 169 | } 170 | 171 | this->theta = (float) dTheta; 172 | this->rho = (float) dRho; 173 | } 174 | 175 | //------------------------------------------------------------------------------ 176 | /** 177 | Convert polar to cartesian. 178 | */ 179 | inline 180 | vector3 181 | polar2::get_cartesian() const 182 | { 183 | vector3 v; 184 | double sin_theta = sin(this->theta); 185 | double cos_theta = cos(this->theta); 186 | double sin_rho = sin(this->rho); 187 | double cos_rho = cos(this->rho); 188 | float x = (float) (sin_theta * sin_rho); 189 | float y = (float) cos_theta; 190 | float z = (float) (sin_theta * cos_rho); 191 | v.set(x,y,z); 192 | return v; 193 | } 194 | 195 | //------------------------------------------------------------------------------ 196 | /** 197 | */ 198 | inline 199 | bool 200 | polar2::isequal(const polar2& rhs, float tol) 201 | { 202 | float dt = n_abs(rhs.theta - this->theta); 203 | float dr = n_abs(rhs.rho - this->rho); 204 | if (dt > tol) return false; 205 | else if (dr > tol) return false; 206 | return true; 207 | } 208 | 209 | //------------------------------------------------------------------------------ 210 | #endif 211 | -------------------------------------------------------------------------------- /ext/mathlib/quaternion.h: -------------------------------------------------------------------------------- 1 | #ifndef N_QUATERNION_H 2 | #define N_QUATERNION_H 3 | //------------------------------------------------------------------- 4 | /** 5 | @class quaternion 6 | @ingroup Math 7 | 8 | quaternion class with some basic operators. 9 | 10 | - 08-Dec-00 floh extended, ATTENTION: argument ordering in 11 | constructor has changed! 12 | 13 | (C) 2004 RadonLabs GmbH 14 | */ 15 | #include 16 | #include 17 | #include 18 | #include "mathlib/vector.h" 19 | 20 | //------------------------------------------------------------------- 21 | // quaternion 22 | //------------------------------------------------------------------- 23 | class quaternion { 24 | public: 25 | float x,y,z,w; 26 | 27 | //-- constructors ----------------------------------------------- 28 | quaternion() 29 | : x(0.0f), y(0.0f), z(0.0f), w(1.0f) 30 | {}; 31 | quaternion(float _x, float _y, float _z, float _w) 32 | : x(_x), y(_y), z(_z), w(_w) 33 | {}; 34 | quaternion(const quaternion& q) 35 | : x(q.x), y(q.y), z(q.z), w(q.w) 36 | {}; 37 | 38 | //-- setting elements ------------------------------------------- 39 | void set(float _x, float _y, float _z, float _w) { 40 | x = _x; 41 | y = _y; 42 | z = _z; 43 | w = _w; 44 | }; 45 | void set(const quaternion& q) { 46 | x = q.x; 47 | y = q.y; 48 | z = q.z; 49 | w = q.w; 50 | }; 51 | 52 | //-- misc operations -------------------------------------------- 53 | void ident(void) { 54 | x = 0.0f; 55 | y = 0.0f; 56 | z = 0.0f; 57 | w = 1.0f; 58 | }; 59 | 60 | void conjugate(void) { 61 | x = -x; 62 | y = -y; 63 | z = -z; 64 | }; 65 | 66 | void scale(float s) { 67 | x *= s; 68 | y *= s; 69 | z *= s; 70 | w *= s; 71 | }; 72 | 73 | float norm(void) { 74 | return x*x + y*y + z*z + w*w; 75 | }; 76 | 77 | float magnitude(void) { 78 | float n = norm(); 79 | if (n > 0.0f) return n_sqrt(n); 80 | else return 0.0f; 81 | }; 82 | 83 | void invert(void) { 84 | float n = norm(); 85 | if (n > 0.0f) scale(1.0f / norm()); 86 | conjugate(); 87 | }; 88 | 89 | void normalize(void) { 90 | float l = magnitude(); 91 | if (l > 0.0f) scale(1.0f / l); 92 | else set(0.0f,0.0f,0.0f,1.0f); 93 | }; 94 | 95 | //-- operators -------------------------------------------------- 96 | bool operator==(const quaternion& q) { 97 | return ((x==q.x) && (y==q.y) && (z==q.z) && (w==q.w)) ? true : false; 98 | }; 99 | 100 | bool operator!=(const quaternion& q) { 101 | return ((x!=q.x) || (y!=q.y) || (z!=q.z) || (w!=q.w)) ? true : false; 102 | }; 103 | 104 | const quaternion& operator+=(const quaternion& q) { 105 | x += q.x; 106 | y += q.y; 107 | z += q.z; 108 | w += q.w; 109 | return *this; 110 | }; 111 | 112 | const quaternion& operator-=(const quaternion& q) { 113 | x -= q.x; 114 | y -= q.y; 115 | z -= q.z; 116 | w -= q.w; 117 | return *this; 118 | }; 119 | 120 | const quaternion& operator*=(const quaternion& q) { 121 | float qx = w*q.x + x*q.w + y*q.z - z*q.y; 122 | float qy = w*q.y + y*q.w + z*q.x - x*q.z; 123 | float qz = w*q.z + z*q.w + x*q.y - y*q.x; 124 | float qw = w*q.w - x*q.x - y*q.y - z*q.z; 125 | x = qx; 126 | y = qy; 127 | z = qz; 128 | w = qw; 129 | return *this; 130 | }; 131 | 132 | /// rotate vector by quaternion 133 | vector3 rotate(const vector3& v) { 134 | quaternion q(v.x * w + v.z * y - v.y * z, 135 | v.y * w + v.x * z - v.z * x, 136 | v.z * w + v.y * x - v.x * y, 137 | v.x * x + v.y * y + v.z * z); 138 | 139 | return vector3(w * q.x + x * q.w + y * q.z - z * q.y, 140 | w * q.y + y * q.w + z * q.x - x * q.z, 141 | w * q.z + z * q.w + x * q.y - y * q.x); 142 | }; 143 | 144 | /** 145 | Create a rotation from one vector to an other. Works only with unit vectors. 146 | 147 | See http://www.martinb.com/maths/algebra/vectors/angleBetween/index.htm for 148 | more information. 149 | 150 | @param from source unit vector 151 | @param to destination unit vector 152 | */ 153 | void set_from_axes( const vector3& from, const vector3& to ) 154 | { 155 | vector3 c(from * to); 156 | set(c.x, c.y, c.z, from % to); 157 | w += 1.0f; // reducing angle to halfangle 158 | if( w <= TINY ) // angle close to PI 159 | { 160 | if ((from.z * from.z) > (from.x * from.x)) 161 | set(0, from.z, -from.y, w); 162 | //from*vector3(1,0,0) 163 | else 164 | set(from.y, -from.x, 0, w); 165 | //from*vector3(0,0,1) 166 | } 167 | normalize(); 168 | } 169 | 170 | /** 171 | Create a rotation from one vector to an other. Works with non unit vectors. 172 | 173 | See http://www.martinb.com/maths/algebra/vectors/angleBetween/index.htm for 174 | more information. 175 | 176 | @param from source vector 177 | @param to destination vector 178 | */ 179 | void set_from_axes2( const vector3& from, const vector3& to ) 180 | { 181 | vector3 c(from * to); 182 | set(c.x, c.y, c.z, from % to); 183 | normalize(); // if "from" or "to" not unit, normalize quat 184 | w += 1.0f; // reducing angle to halfangle 185 | if( w <= TINY ) // angle close to PI 186 | { 187 | if ((from.z * from.z) > (from.x * from.x)) 188 | set(0, from.z, -from.y, w); 189 | //from*vector3(1,0,0) 190 | else 191 | set(from.y, -from.x, 0, w); 192 | //from*vector3(0,0,1) 193 | } 194 | normalize(); 195 | } 196 | 197 | //-- convert from euler angles ---------------------------------- 198 | void set_rotate_axis_angle(const vector3& v, float a) { 199 | float sin_a = n_sin(a * 0.5f); 200 | float cos_a = n_cos(a * 0.5f); 201 | x = v.x * sin_a; 202 | y = v.y * sin_a; 203 | z = v.z * sin_a; 204 | w = cos_a; 205 | }; 206 | 207 | void set_rotate_x(float a) { 208 | float sin_a = n_sin(a * 0.5f); 209 | float cos_a = n_cos(a * 0.5f); 210 | x = sin_a; 211 | y = 0.0f; 212 | z = 0.0f; 213 | w = cos_a; 214 | }; 215 | 216 | void set_rotate_y(float a) { 217 | float sin_a = n_sin(a * 0.5f); 218 | float cos_a = n_cos(a * 0.5f); 219 | x = 0.0f; 220 | y = sin_a; 221 | z = 0.0f; 222 | w = cos_a; 223 | }; 224 | 225 | void set_rotate_z(float a) { 226 | float sin_a = n_sin(a * 0.5f); 227 | float cos_a = n_cos(a * 0.5f); 228 | x = 0.0f; 229 | y = 0.0f; 230 | z = sin_a; 231 | w = cos_a; 232 | }; 233 | 234 | void set_rotate_xyz(float ax, float ay, float az) { 235 | quaternion qx, qy, qz; 236 | qx.set_rotate_x(ax); 237 | qy.set_rotate_y(ay); 238 | qz.set_rotate_z(az); 239 | *this = qx; 240 | *this *= qy; 241 | *this *= qz; 242 | }; 243 | 244 | //--- fuzzy compare operators ----------------------------------- 245 | bool isequal(const quaternion& v, float tol) const 246 | { 247 | if (fabs(v.x-x) > tol) return false; 248 | else if (fabs(v.y-y) > tol) return false; 249 | else if (fabs(v.z-z) > tol) return false; 250 | else if (fabs(v.w-w) > tol) return false; 251 | return true; 252 | }; 253 | 254 | //-- rotation interpolation, set this matrix to the ------------- 255 | //-- interpolated result of q0->q1 with l as interpolator ------- 256 | void slerp(const quaternion& q0, const quaternion& q1, float l) 257 | { 258 | float fScale1; 259 | float fScale2; 260 | quaternion A = q0; 261 | quaternion B = q1; 262 | 263 | // compute dot product, aka cos(theta): 264 | float fCosTheta = A.x*B.x + A.y*B.y + A.z*B.z + A.w*B.w; 265 | 266 | if (fCosTheta < 0.0f) 267 | { 268 | // flip start quaternion 269 | A.x = -A.x; A.y = -A.y; A.z = -A.z; A.w = -A.w; 270 | fCosTheta = -fCosTheta; 271 | } 272 | 273 | if ((fCosTheta + 1.0f) > 0.05f) 274 | { 275 | // If the quaternions are close, use linear interploation 276 | if ((1.0f - fCosTheta) < 0.05f) 277 | { 278 | fScale1 = 1.0f - l; 279 | fScale2 = l; 280 | } 281 | else 282 | { 283 | // Otherwise, do spherical interpolation 284 | float fTheta = n_acos(fCosTheta); 285 | float fSinTheta = n_sin(fTheta); 286 | fScale1 = n_sin( fTheta * (1.0f-l) ) / fSinTheta; 287 | fScale2 = n_sin( fTheta * l ) / fSinTheta; 288 | } 289 | } 290 | else 291 | { 292 | B.x = -A.y; 293 | B.y = A.x; 294 | B.z = -A.w; 295 | B.w = A.z; 296 | fScale1 = n_sin( PI * (0.5f - l) ); 297 | fScale2 = n_sin( PI * l ); 298 | } 299 | 300 | x = fScale1 * A.x + fScale2 * B.x; 301 | y = fScale1 * A.y + fScale2 * B.y; 302 | z = fScale1 * A.z + fScale2 * B.z; 303 | w = fScale1 * A.w + fScale2 * B.w; 304 | }; 305 | }; 306 | 307 | //--- global operators ---------------------------------------------- 308 | static inline quaternion operator+(const quaternion& q0, const quaternion& q1) { 309 | return quaternion(q0.x+q1.x, q0.y+q1.y, q0.z+q1.z, q0.w+q1.w); 310 | }; 311 | 312 | static inline quaternion operator-(const quaternion& q0, const quaternion& q1) { 313 | return quaternion(q0.x-q1.x, q0.y-q1.y, q0.z-q1.z, q0.w-q1.w); 314 | }; 315 | 316 | static inline quaternion operator*(const quaternion& q0, const quaternion& q1) { 317 | return quaternion(q0.w*q1.x + q0.x*q1.w + q0.y*q1.z - q0.z*q1.y, 318 | q0.w*q1.y + q0.y*q1.w + q0.z*q1.x - q0.x*q1.z, 319 | q0.w*q1.z + q0.z*q1.w + q0.x*q1.y - q0.y*q1.x, 320 | q0.w*q1.w - q0.x*q1.x - q0.y*q1.y - q0.z*q1.z); 321 | }; 322 | //------------------------------------------------------------------- 323 | #endif 324 | -------------------------------------------------------------------------------- /ext/mathlib/rectangle.h: -------------------------------------------------------------------------------- 1 | #ifndef N_RECTANGLE_H 2 | #define N_RECTANGLE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class rectangle 6 | @ingroup Math 7 | 8 | A 2d rectangle class. 9 | 10 | (C) 2003 RadonLabs GmbH 11 | */ 12 | #include "mathlib/vector.h" 13 | 14 | //------------------------------------------------------------------------------ 15 | class rectangle 16 | { 17 | public: 18 | /// default constructor 19 | rectangle(); 20 | /// constructor 1 21 | rectangle(const vector2& topLeft, const vector2& bottomRight); 22 | /// set content 23 | void set(const vector2& topLeft, const vector2& bottomRight); 24 | /// return true if point is inside 25 | bool inside(const vector2& p) const; 26 | /// return midpoint 27 | vector2 midpoint() const; 28 | /// return width 29 | float width() const; 30 | /// return height 31 | float height() const; 32 | /// return size 33 | vector2 size() const; 34 | 35 | vector2 v0; 36 | vector2 v1; 37 | }; 38 | 39 | //------------------------------------------------------------------------------ 40 | /** 41 | */ 42 | inline 43 | rectangle::rectangle() 44 | { 45 | // empty 46 | } 47 | 48 | //------------------------------------------------------------------------------ 49 | /** 50 | */ 51 | inline 52 | rectangle::rectangle(const vector2& topLeft, const vector2& bottomRight) : 53 | v0(topLeft), 54 | v1(bottomRight) 55 | { 56 | // empty 57 | } 58 | 59 | //------------------------------------------------------------------------------ 60 | /** 61 | */ 62 | inline 63 | void 64 | rectangle::set(const vector2& topLeft, const vector2& bottomRight) 65 | { 66 | this->v0 = topLeft; 67 | this->v1 = bottomRight; 68 | } 69 | 70 | //------------------------------------------------------------------------------ 71 | /** 72 | */ 73 | inline 74 | bool 75 | rectangle::inside(const vector2& p) const 76 | { 77 | return ((this->v0.x <= p.x) && (p.x <= this->v1.x) && 78 | (this->v0.y <= p.y) && (p.y <= this->v1.y)); 79 | } 80 | 81 | //------------------------------------------------------------------------------ 82 | /** 83 | */ 84 | inline 85 | vector2 86 | rectangle::midpoint() const 87 | { 88 | return (this->v0 + this->v1) * 0.5f; 89 | } 90 | 91 | //------------------------------------------------------------------------------ 92 | /** 93 | */ 94 | inline 95 | float 96 | rectangle::width() const 97 | { 98 | return this->v1.x - this->v0.x; 99 | } 100 | 101 | //------------------------------------------------------------------------------ 102 | /** 103 | */ 104 | inline 105 | float 106 | rectangle::height() const 107 | { 108 | return this->v1.y - this->v0.y; 109 | } 110 | 111 | //------------------------------------------------------------------------------ 112 | /** 113 | */ 114 | inline 115 | vector2 116 | rectangle::size() const 117 | { 118 | return this->v1 - this->v0; 119 | } 120 | 121 | //------------------------------------------------------------------------------ 122 | #endif 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /ext/mathlib/sphere.h: -------------------------------------------------------------------------------- 1 | #ifndef N_SPHERE_H 2 | #define N_SPHERE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class sphere 6 | @ingroup Math 7 | 8 | A 3-dimensional sphere. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/vector.h" 13 | #include "mathlib/bbox.h" 14 | #include "mathlib/matrix.h" 15 | #include "mathlib/rectangle.h" 16 | 17 | //------------------------------------------------------------------------------ 18 | class sphere 19 | { 20 | public: 21 | /// default constructor 22 | sphere(); 23 | /// pos/radius constructor 24 | sphere(const vector3& _p, float _r); 25 | /// x,y,z,r constructor 26 | sphere(float _x, float _y, float _z, float _r); 27 | /// copy constructor 28 | sphere(const sphere& rhs); 29 | /// set position and radius 30 | void set(const vector3& _p, float _r); 31 | /// set x,y,z, radius 32 | void set(float _x, float _y, float _z, float _r); 33 | /// check if 2 spheres overlap 34 | bool intersects(const sphere& s) const; 35 | /// check if sphere intersects box 36 | bool intersects(const bbox3& box) const; 37 | /// check if 2 moving sphere have contact 38 | bool intersect_sweep(const vector3& va, const sphere& sb, const vector3& vb, float& u0, float& u1) const; 39 | /// project sphere to screen rectangle (right handed coordinate system) 40 | rectangle project_screen_rh(const matrix44& modelView, const matrix44& projection) const; 41 | 42 | vector3 p; // position 43 | float r; // radius 44 | }; 45 | 46 | //------------------------------------------------------------------------------ 47 | /** 48 | */ 49 | inline 50 | sphere::sphere() : 51 | r(1.0f) 52 | { 53 | // empty 54 | } 55 | 56 | //------------------------------------------------------------------------------ 57 | /** 58 | */ 59 | inline 60 | sphere::sphere(const vector3& _p, float _r) : 61 | p(_p), 62 | r(_r) 63 | { 64 | // empty 65 | } 66 | 67 | //------------------------------------------------------------------------------ 68 | /** 69 | */ 70 | inline 71 | sphere::sphere(float _x, float _y, float _z, float _r) : 72 | p(_x, _y, _z), 73 | r(_r) 74 | { 75 | // empty 76 | } 77 | 78 | //------------------------------------------------------------------------------ 79 | /** 80 | */ 81 | inline 82 | sphere::sphere(const sphere& rhs) : 83 | p(rhs.p), 84 | r(rhs.r) 85 | { 86 | // empty 87 | } 88 | 89 | //------------------------------------------------------------------------------ 90 | /** 91 | */ 92 | inline 93 | void 94 | sphere::set(const vector3& _p, float _r) 95 | { 96 | this->p = _p; 97 | this->r = _r; 98 | } 99 | 100 | //------------------------------------------------------------------------------ 101 | /** 102 | */ 103 | inline 104 | void 105 | sphere::set(float _x, float _y, float _z, float _r) 106 | { 107 | this->p.set(_x, _y, _z); 108 | this->r = _r; 109 | } 110 | 111 | //------------------------------------------------------------------------------ 112 | /** 113 | */ 114 | inline 115 | bool 116 | sphere::intersects(const sphere& s) const 117 | { 118 | vector3 d(s.p - p); 119 | float rsum = s.r + r; 120 | if (d.lensquared() <= (rsum * rsum)) return true; 121 | else return false; 122 | } 123 | 124 | //------------------------------------------------------------------------------ 125 | /** 126 | Check if sphere intersects with box. 127 | Taken from "Simple Intersection Tests For Games", 128 | Gamasutra, Oct 18 1999 129 | */ 130 | inline 131 | bool 132 | sphere::intersects(const bbox3& box) const 133 | { 134 | float s, d = 0; 135 | 136 | // find the square of the distance 137 | // from the sphere to the box, 138 | if (p.x < box.vmin.x) 139 | { 140 | s = p.x - box.vmin.x; 141 | d += s*s; 142 | } 143 | else if (p.x > box.vmax.x) 144 | { 145 | s = p.x - box.vmax.x; 146 | d += s*s; 147 | } 148 | 149 | if (p.y < box.vmin.y) 150 | { 151 | s = p.y - box.vmin.y; 152 | d += s*s; 153 | } 154 | else if (p.y > box.vmax.y) 155 | { 156 | s = p.y - box.vmax.y; 157 | d += s*s; 158 | } 159 | 160 | if (p.z < box.vmin.z) 161 | { 162 | s = p.z - box.vmin.z; 163 | d += s*s; 164 | } 165 | else if (p.z > box.vmax.z) 166 | { 167 | s = p.z - box.vmax.z; 168 | d += s*s; 169 | } 170 | 171 | return d <= r*r; 172 | } 173 | 174 | //------------------------------------------------------------------------------ 175 | /** 176 | Check if 2 moving spheres have contact. 177 | Taken from "Simple Intersection Tests For Games" 178 | article in Gamasutra, Oct 18 1999 179 | 180 | @param va [in] distance travelled by 'this' 181 | @param sb [in] the other sphere 182 | @param vb [in] distance travelled by sb 183 | @param u0 [out] normalized intro contact 184 | @param u1 [out] normalized outro contact 185 | */ 186 | inline 187 | bool 188 | sphere::intersect_sweep(const vector3& va, const sphere& sb, const vector3& vb, float& u0, float& u1) const 189 | { 190 | vector3 vab(vb - va); 191 | vector3 ab(sb.p - p); 192 | float rab = r + sb.r; 193 | 194 | // check if spheres are currently overlapping... 195 | if ((ab % ab) <= (rab * rab)) 196 | { 197 | u0 = 0.0f; 198 | u1 = 0.0f; 199 | return true; 200 | } 201 | else 202 | { 203 | // check if they hit each other 204 | float a = vab % vab; 205 | if ((a < -TINY) || (a > +TINY)) 206 | { 207 | // if a is '0' then the objects don't move relative to each other 208 | float b = (vab % ab) * 2.0f; 209 | float c = (ab % ab) - (rab * rab); 210 | float q = b*b - 4*a*c; 211 | if (q >= 0.0f) 212 | { 213 | // 1 or 2 contacts 214 | float sq = (float) sqrt(q); 215 | float d = 1.0f / (2.0f*a); 216 | float r1 = (-b + sq) * d; 217 | float r2 = (-b - sq) * d; 218 | if (r1 < r2) 219 | { 220 | u0 = r1; 221 | u1 = r2; 222 | } 223 | else 224 | { 225 | u0 = r2; 226 | u1 = r1; 227 | } 228 | return true; 229 | } 230 | else 231 | { 232 | return false; 233 | } 234 | } 235 | else 236 | { 237 | return false; 238 | } 239 | } 240 | } 241 | 242 | //------------------------------------------------------------------------------ 243 | /** 244 | Project the sphere (defined in global space) to a screen space rectangle, 245 | given the current View and Projection matrices. The method assumes that 246 | the sphere is at least partially visible. 247 | */ 248 | inline 249 | rectangle 250 | sphere::project_screen_rh(const matrix44& view, const matrix44& projection) const 251 | { 252 | // compute front center point of the sphere in view space 253 | vector3 viewPos = view * this->p; 254 | if (viewPos.z < 0.0f) 255 | { 256 | vector3 screenPos = projection.mult_divw(viewPos); 257 | screenPos.y = -screenPos.y; 258 | vector3 screenSize = projection.mult_divw(vector3(this->r, this->r, viewPos.z)); 259 | screenSize.y = -screenSize.y; 260 | float left = n_saturate(0.5f * (1.0f + (screenPos.x - screenSize.x))); 261 | float right = n_saturate(0.5f * (1.0f + (screenPos.x + screenSize.x))); 262 | float top = n_saturate(0.5f * (1.0f + (screenPos.y + screenSize.y))); 263 | float bottom = n_saturate(0.5f * (1.0f + (screenPos.y - screenSize.y))); 264 | 265 | return rectangle(vector2(left, top), vector2(right, bottom)); 266 | } 267 | else 268 | { 269 | // hmm, we're behind the view volume 270 | return rectangle(vector2(0.0f, 0.0f), vector2(1.0f, 1.0f)); 271 | } 272 | } 273 | 274 | 275 | //------------------------------------------------------------------------------ 276 | #endif 277 | -------------------------------------------------------------------------------- /ext/mathlib/transform33.h: -------------------------------------------------------------------------------- 1 | #ifndef N_TRANSFORM33_H 2 | #define N_TRANSFORM33_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class transform33 6 | @ingroup Math 7 | 8 | A 3x3 matrix which is described by 2D translation, rotation and scale. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/matrix.h" 13 | #include "mathlib/vector.h" 14 | 15 | //------------------------------------------------------------------------------ 16 | class transform33 17 | { 18 | public: 19 | /// constructor 20 | transform33(); 21 | /// set translation 22 | void settranslation(const vector2& v); 23 | /// get translation 24 | const vector2& gettranslation() const; 25 | /// set euler rotation 26 | void seteulerrotation(const vector2& v); 27 | /// get euler rotation 28 | const vector2& geteulerrotation() const; 29 | /// return true if the transformation matrix is dirty 30 | bool isdirty() const; 31 | /// set scale 32 | void setscale(const vector2& v); 33 | /// get scale 34 | const vector2& getscale() const; 35 | /// get resulting 3x3 matrix 36 | const matrix33& getmatrix33(); 37 | /// get resulting 4x4 matrix, with 3x3 matrix cast to the upper-left corner 38 | void getmatrix44(matrix44& out); 39 | 40 | private: 41 | /// update internal matrix 42 | void update(); 43 | 44 | enum 45 | { 46 | Dirty = (1<<0), 47 | }; 48 | vector2 translation; 49 | vector2 euler; 50 | vector2 scale; 51 | matrix33 matrix; 52 | uchar flags; 53 | }; 54 | 55 | //------------------------------------------------------------------------------ 56 | /** 57 | */ 58 | inline 59 | transform33::transform33() : 60 | scale(1.0f, 1.0f), 61 | flags(0) 62 | { 63 | // empty 64 | } 65 | 66 | //------------------------------------------------------------------------------ 67 | /** 68 | */ 69 | inline 70 | void 71 | transform33::settranslation(const vector2& v) 72 | { 73 | this->translation = v; 74 | this->flags |= Dirty; 75 | } 76 | 77 | //------------------------------------------------------------------------------ 78 | /** 79 | */ 80 | inline 81 | const vector2& 82 | transform33::gettranslation() const 83 | { 84 | return this->translation; 85 | } 86 | 87 | //------------------------------------------------------------------------------ 88 | /** 89 | */ 90 | inline 91 | void 92 | transform33::seteulerrotation(const vector2& v) 93 | { 94 | this->euler = v; 95 | this->flags |= Dirty; 96 | } 97 | 98 | //------------------------------------------------------------------------------ 99 | /** 100 | */ 101 | inline 102 | const vector2& 103 | transform33::geteulerrotation() const 104 | { 105 | return this->euler; 106 | } 107 | 108 | //------------------------------------------------------------------------------ 109 | /** 110 | */ 111 | inline 112 | void 113 | transform33::setscale(const vector2& v) 114 | { 115 | this->scale = v; 116 | this->flags |= Dirty; 117 | } 118 | 119 | //------------------------------------------------------------------------------ 120 | /** 121 | */ 122 | inline 123 | const vector2& 124 | transform33::getscale() const 125 | { 126 | return this->scale; 127 | } 128 | 129 | //------------------------------------------------------------------------------ 130 | /** 131 | */ 132 | inline 133 | void 134 | transform33::update() 135 | { 136 | if (this->flags & Dirty) 137 | { 138 | this->matrix.ident(); 139 | this->matrix.scale(vector3(this->scale.x, this->scale.y, 1.0f)); 140 | this->matrix.rotate_x(this->euler.x); 141 | this->matrix.rotate_y(this->euler.y); 142 | this->matrix.translate(this->translation); 143 | this->flags &= ~Dirty; 144 | } 145 | } 146 | 147 | //------------------------------------------------------------------------------ 148 | /** 149 | */ 150 | inline 151 | const matrix33& 152 | transform33::getmatrix33() 153 | { 154 | this->update(); 155 | return this->matrix; 156 | } 157 | 158 | //------------------------------------------------------------------------------ 159 | /** 160 | */ 161 | inline 162 | void 163 | transform33::getmatrix44(matrix44& out) 164 | { 165 | this->update(); 166 | out.set(this->matrix.M11, this->matrix.M12, this->matrix.M13, 0.0f, 167 | this->matrix.M21, this->matrix.M22, this->matrix.M23, 0.0f, 168 | this->matrix.M31, this->matrix.M32, this->matrix.M33, 0.0f, 169 | 0.0f, 0.0f, 0.0f, 1.0f); 170 | } 171 | 172 | //------------------------------------------------------------------------------ 173 | /** 174 | */ 175 | inline 176 | bool 177 | transform33::isdirty() const 178 | { 179 | return (0 != (this->flags & Dirty)); 180 | } 181 | 182 | //------------------------------------------------------------------------------ 183 | #endif 184 | 185 | 186 | -------------------------------------------------------------------------------- /ext/mathlib/transform44.h: -------------------------------------------------------------------------------- 1 | #ifndef N_TRANSFORM44_H 2 | #define N_TRANSFORM44_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class transform44 6 | @ingroup Math 7 | 8 | A 4x4 matrix which is described by translation, rotation and scale. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/matrix.h" 13 | #include "mathlib/vector.h" 14 | 15 | //------------------------------------------------------------------------------ 16 | class transform44 17 | { 18 | public: 19 | /// constructor 20 | transform44(); 21 | /// set translation 22 | void settranslation(const vector3& v); 23 | /// get translation 24 | const vector3& gettranslation() const; 25 | /// set euler rotation 26 | void seteulerrotation(const vector3& v); 27 | /// get euler rotation 28 | const vector3& geteulerrotation() const; 29 | /// set quaternion rotation 30 | void setquatrotation(const quaternion& q); 31 | /// get quaternion rotation 32 | const quaternion& getquatrotation() const; 33 | /// set scale 34 | void setscale(const vector3& v); 35 | /// get scale 36 | const vector3& getscale() const; 37 | /// set optional rotate pivot 38 | void setrotatepivot(const vector3& p, bool balance); 39 | /// get optional rotate pivot 40 | const vector3& getrotatepivot() const; 41 | /// set optional scale pivot 42 | void setscalepivot(const vector3& p); 43 | /// get optional scale pivot 44 | const vector3& getscalepivot() const; 45 | /// set matrix 4x4 46 | void setmatrix(matrix44 matrix); //not per reference - so make shure the data is copyed 47 | /// get resulting 4x4 matrix 48 | const matrix44& getmatrix(); 49 | /// return true if euler rotation is used (otherwise quaternion rotation is used) 50 | bool iseulerrotation() const; 51 | /// return true if the transformation matrix is dirty 52 | bool isdirty() const; 53 | /// return true if rotate pivot has been set 54 | bool hasrotatepivot() const; 55 | /// return true if scale pivot has been set 56 | bool hasscalepivot() const; 57 | 58 | private: 59 | enum 60 | { 61 | Dirty = (1<<0), 62 | UseEuler = (1<<1), 63 | HasRotatePivot = (1<<2), 64 | HasRotatePivotTranslation = (1<<3), 65 | HasScalePivot = (1<<4), 66 | }; 67 | vector3 translation; 68 | vector3 euler; 69 | quaternion quat; 70 | vector3 scale; 71 | vector3 rotatePivot; 72 | vector3 rotatePivotTranslation; 73 | vector3 scalePivot; 74 | matrix44 matrix; 75 | uchar flags; 76 | }; 77 | 78 | //------------------------------------------------------------------------------ 79 | /** 80 | */ 81 | inline 82 | transform44::transform44() : 83 | scale(1.0f, 1.0f, 1.0f), 84 | flags(0) 85 | { 86 | // empty 87 | } 88 | 89 | //------------------------------------------------------------------------------ 90 | /** 91 | */ 92 | inline 93 | void 94 | transform44::settranslation(const vector3& v) 95 | { 96 | this->translation = v; 97 | this->flags |= Dirty; 98 | } 99 | 100 | //------------------------------------------------------------------------------ 101 | /** 102 | */ 103 | inline 104 | const vector3& 105 | transform44::gettranslation() const 106 | { 107 | return this->translation; 108 | } 109 | 110 | //------------------------------------------------------------------------------ 111 | /** 112 | */ 113 | inline 114 | void 115 | transform44::seteulerrotation(const vector3& v) 116 | { 117 | this->euler = v; 118 | this->flags |= (Dirty | UseEuler); 119 | } 120 | 121 | //------------------------------------------------------------------------------ 122 | /** 123 | */ 124 | inline 125 | const vector3& 126 | transform44::geteulerrotation() const 127 | { 128 | return this->euler; 129 | } 130 | 131 | //------------------------------------------------------------------------------ 132 | /** 133 | */ 134 | inline 135 | void 136 | transform44::setquatrotation(const quaternion& q) 137 | { 138 | this->quat = q; 139 | this->flags |= Dirty; 140 | this->flags &= ~UseEuler; 141 | } 142 | 143 | //------------------------------------------------------------------------------ 144 | /** 145 | */ 146 | inline 147 | const quaternion& 148 | transform44::getquatrotation() const 149 | { 150 | return this->quat; 151 | } 152 | 153 | //------------------------------------------------------------------------------ 154 | /** 155 | */ 156 | inline 157 | void 158 | transform44::setscale(const vector3& v) 159 | { 160 | this->scale = v; 161 | this->flags |= Dirty; 162 | } 163 | 164 | //------------------------------------------------------------------------------ 165 | /** 166 | */ 167 | inline 168 | const vector3& 169 | transform44::getscale() const 170 | { 171 | return this->scale; 172 | } 173 | 174 | //------------------------------------------------------------------------------ 175 | /** 176 | Same as Maya MFnTransfrom: 177 | If balance if true, then the overall transformation matrix will not change 178 | and a compensating transformation will be added to the rotate translate 179 | pivot to compensate for the pivot modification. 180 | */ 181 | inline 182 | void 183 | transform44::setrotatepivot(const vector3& p, bool balance) 184 | { 185 | if (balance) 186 | { 187 | this->rotatePivotTranslation = p * -1.f; 188 | this->flags |= Dirty | HasRotatePivotTranslation; 189 | } 190 | this->rotatePivot = p; 191 | this->flags |= Dirty | HasRotatePivot; 192 | } 193 | 194 | //------------------------------------------------------------------------------ 195 | /** 196 | */ 197 | inline 198 | const vector3& 199 | transform44::getrotatepivot() const 200 | { 201 | return this->rotatePivot; 202 | } 203 | 204 | //------------------------------------------------------------------------------ 205 | /** 206 | */ 207 | inline 208 | void 209 | transform44::setscalepivot(const vector3& p) 210 | { 211 | this->scalePivot = p; 212 | this->flags |= Dirty | HasScalePivot; 213 | } 214 | 215 | //------------------------------------------------------------------------------ 216 | /** 217 | */ 218 | inline 219 | const vector3& 220 | transform44::getscalepivot() const 221 | { 222 | return this->scalePivot; 223 | } 224 | 225 | //------------------------------------------------------------------------------ 226 | /** 227 | */ 228 | inline 229 | const matrix44& 230 | transform44::getmatrix() 231 | { 232 | if (this->flags & Dirty) 233 | { 234 | if (this->flags & UseEuler) 235 | { 236 | this->matrix.ident(); 237 | if (this->flags & HasScalePivot) 238 | { 239 | this->matrix.translate(-this->scalePivot); 240 | } 241 | this->matrix.scale(this->scale); 242 | if (this->flags & HasScalePivot) 243 | { 244 | this->matrix.translate(this->scalePivot); 245 | } 246 | if (this->flags & HasRotatePivot) 247 | { 248 | this->matrix.translate(-this->rotatePivot); 249 | } 250 | this->matrix.rotate_x(this->euler.x); 251 | this->matrix.rotate_y(this->euler.y); 252 | this->matrix.rotate_z(this->euler.z); 253 | if (this->flags & HasRotatePivot) 254 | { 255 | this->matrix.translate(this->rotatePivot); 256 | } 257 | if (this->flags & HasRotatePivotTranslation) 258 | { 259 | this->matrix.translate(this->rotatePivotTranslation); 260 | } 261 | this->matrix.translate(this->translation); 262 | } 263 | else 264 | { 265 | this->matrix.ident(); 266 | if (this->flags & HasScalePivot) 267 | { 268 | this->matrix.translate(-this->scalePivot); 269 | } 270 | this->matrix.scale(this->scale); 271 | if (this->flags & HasScalePivot) 272 | { 273 | this->matrix.translate(this->scalePivot); 274 | } 275 | if (this->flags & HasRotatePivot) 276 | { 277 | this->matrix.translate(-this->rotatePivot); 278 | } 279 | this->matrix.mult_simple(matrix44(this->quat)); 280 | if (this->flags & HasRotatePivot) 281 | { 282 | this->matrix.translate(this->rotatePivot); 283 | } 284 | if (this->flags & HasRotatePivotTranslation) 285 | { 286 | this->matrix.translate(this->rotatePivotTranslation); 287 | } 288 | this->matrix.translate(this->translation); 289 | } 290 | this->flags &= ~Dirty; 291 | } 292 | return this->matrix; 293 | } 294 | 295 | //------------------------------------------------------------------------------ 296 | /** 297 | */ 298 | inline 299 | bool 300 | transform44::iseulerrotation() const 301 | { 302 | return (0 != (this->flags & UseEuler)); 303 | } 304 | 305 | //------------------------------------------------------------------------------ 306 | /** 307 | */ 308 | inline 309 | bool 310 | transform44::isdirty() const 311 | { 312 | return (0 != (this->flags & Dirty)); 313 | } 314 | 315 | //------------------------------------------------------------------------------ 316 | /** 317 | */ 318 | inline 319 | bool 320 | transform44::hasrotatepivot() const 321 | { 322 | return (0 != (this->flags & HasRotatePivot)); 323 | } 324 | 325 | //------------------------------------------------------------------------------ 326 | /** 327 | */ 328 | inline 329 | bool 330 | transform44::hasscalepivot() const 331 | { 332 | return (0 != (this->flags & HasScalePivot)); 333 | } 334 | 335 | //------------------------------------------------------------------------------ 336 | /** 337 | */ 338 | inline 339 | void 340 | transform44::setmatrix(matrix44 m) 341 | { 342 | this->matrix = m; 343 | this->flags &= ~Dirty; 344 | } 345 | //------------------------------------------------------------------------------ 346 | #endif 347 | 348 | 349 | -------------------------------------------------------------------------------- /ext/mathlib/triangle.h: -------------------------------------------------------------------------------- 1 | #ifndef N_TRIANGLE_H 2 | #define N_TRIANGLE_H 3 | //------------------------------------------------------------------- 4 | /** 5 | @class triangle 6 | @ingroup Math 7 | 8 | A 3D-triangle. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/vector.h" 13 | #include "mathlib/line.h" 14 | #include "mathlib/plane.h" 15 | #include 16 | #include 17 | 18 | //------------------------------------------------------------------- 19 | // Triangle points are tri(s,t)=b + s*e0 + t*e1 where 20 | // 0<=s<=1, 0<=t<=1 and 0<=s+t<=1 21 | //------------------------------------------------------------------- 22 | class triangle { 23 | public: 24 | vector3 b,e0,e1; 25 | 26 | triangle() {}; 27 | triangle(const vector3& v0, const vector3& v1, const vector3& v2) 28 | : b(v0), e0(v1-v0), e1(v2-v0) {}; 29 | triangle(const triangle& t) 30 | : b(t.b), e0(t.e0), e1(t.e1) {}; 31 | 32 | void set(const vector3& v0, const vector3& v1, const vector3& v2) { 33 | b = v0; 34 | e0 = v1-v0; 35 | e1 = v2-v0; 36 | }; 37 | 38 | //--- get the face normal of the triangle --------------------------------- 39 | vector3 normal(void) const { 40 | vector3 cross(e0*e1); 41 | cross.norm(); 42 | return cross; 43 | }; 44 | 45 | //--- get the midpoint (center of gravity) of the triangle ---------------- 46 | vector3 midpoint(void) const { 47 | const float oneThird = 1.0f / 3.0f; 48 | return b + ((e0+e1) * oneThird); 49 | }; 50 | 51 | //--- get the plane of the triangle --------------------------------------- 52 | plane getplane(void) const { 53 | return plane(b,b+e0,b+e1); 54 | }; 55 | 56 | //--- get one the edge points --------------------------------------------- 57 | vector3 point(int i) const 58 | { 59 | switch (i) 60 | { 61 | case 0: return b; 62 | case 1: return b + e0; 63 | case 2: return b + e1; 64 | default: return vector3(0.0f, 0.0f, 0.0f); 65 | } 66 | }; 67 | 68 | //--- check if and where line intersects triangle ------------------------- 69 | // Taken from Magic Software (http://www.cs.unc.edu/~eberly) 70 | // Return false if line is parallel to triangle or hits its backside. 71 | // 72 | bool intersect(const line3& line, float& ipos) { 73 | 74 | // Compute plane of triangle, Dot(normal,X-tri.b) = 0 where 'normal' is 75 | // the plane normal. If the angle between the line direction and normal 76 | // is small, then the line is effectively parallel to the triangle. 77 | const float fTolerance = 1e-04f; 78 | vector3 norm(e0*e1); 79 | float fDenominator = norm % line.m; 80 | //float fLLenSqr = line.m % line.m; 81 | //float fNLenSqr = norm % norm; 82 | 83 | // check if intersecting backface or parallel... 84 | if (fDenominator >= -fTolerance) return false; 85 | 86 | //if ((fDenominator*fDenominator) <= (fTolerance*fLLenSqr*fNLenSqr)) { 87 | // // line and triangle are parallel 88 | // return false; 89 | //} 90 | 91 | // The line is X(t) = line.b + t*line.m. Compute line parameter t for 92 | // intersection of line and plane of triangle. Substitute in the plane 93 | // equation to get Dot(normal,line.b-tri.b) + t*Dot(normal,line.m) 94 | vector3 kDiff0(line.b - b); 95 | float fTime = -(norm % kDiff0) / fDenominator; 96 | if ((fTime<-fTolerance) || (fTime>(1.0f+fTolerance))) return false; 97 | 98 | // Find difference of intersection point of line with plane and vertex 99 | // of triangle. 100 | vector3 kDiff1(kDiff0 + line.m*fTime); 101 | 102 | // Compute if intersection point is inside triangle. Write 103 | // kDiff1 = s0*E0 + s1*E1 and solve for s0 and s1. 104 | float fE00 = e0 % e0; 105 | float fE01 = e0 % e1; 106 | float fE11 = e1 % e1; 107 | float fDet = (float) fabs(fE00*fE11-fE01*fE01); // = |normal|^2 > 0 108 | float fR0 = e0 % kDiff1; 109 | float fR1 = e1 % kDiff1; 110 | 111 | float fS0 = fE11*fR0 - fE01*fR1; 112 | float fS1 = fE00*fR1 - fE01*fR0; 113 | 114 | if ((fS0>=-fTolerance) && (fS1>=-fTolerance) && (fS0+fS1<=fDet+fTolerance)) { 115 | // intersection is inside triangle 116 | ipos = fTime; 117 | return true; 118 | } else { 119 | // intersection is outside triangle 120 | return false; 121 | } 122 | }; 123 | 124 | //--- check if and where line intersects triangle ------------------------- 125 | // Taken from Magic Software (http://www.cs.unc.edu/~eberly) 126 | // Return false if line is parallel to triangle 127 | // 128 | bool intersect_both_sides(const line3& line, float& ipos) { 129 | 130 | // Compute plane of triangle, Dot(normal,X-tri.b) = 0 where 'normal' is 131 | // the plane normal. If the angle between the line direction and normal 132 | // is small, then the line is effectively parallel to the triangle. 133 | const float fTolerance = 1e-04f; 134 | vector3 norm(e0*e1); 135 | float fDenominator = norm % line.m; 136 | float fLLenSqr = line.m % line.m; 137 | float fNLenSqr = norm % norm; 138 | 139 | // check if intersecting backface or parallel... 140 | if (fDenominator*fDenominator <= fTolerance*fLLenSqr*fNLenSqr) return false; 141 | 142 | //if ((fDenominator*fDenominator) <= (fTolerance*fLLenSqr*fNLenSqr)) { 143 | // // line and triangle are parallel 144 | // return false; 145 | //} 146 | 147 | // The line is X(t) = line.b + t*line.m. Compute line parameter t for 148 | // intersection of line and plane of triangle. Substitute in the plane 149 | // equation to get Dot(normal,line.b-tri.b) + t*Dot(normal,line.m) 150 | vector3 kDiff0(line.b - b); 151 | float fTime = -(norm % kDiff0) / fDenominator; 152 | if ((fTime<-fTolerance) || (fTime>(1.0f+fTolerance))) return false; 153 | 154 | // Find difference of intersection point of line with plane and vertex 155 | // of triangle. 156 | vector3 kDiff1(kDiff0 + line.m*fTime); 157 | 158 | // Compute if intersection point is inside triangle. Write 159 | // kDiff1 = s0*E0 + s1*E1 and solve for s0 and s1. 160 | float fE00 = e0 % e0; 161 | float fE01 = e0 % e1; 162 | float fE11 = e1 % e1; 163 | float fDet = (float) fabs(fE00*fE11-fE01*fE01); // = |normal|^2 > 0 164 | float fR0 = e0 % kDiff1; 165 | float fR1 = e1 % kDiff1; 166 | 167 | float fS0 = fE11*fR0 - fE01*fR1; 168 | float fS1 = fE00*fR1 - fE01*fR0; 169 | 170 | if ((fS0>=-fTolerance) && (fS1>=-fTolerance) && (fS0+fS1<=fDet+fTolerance)) { 171 | // intersection is inside triangle 172 | ipos = fTime; 173 | return true; 174 | } else { 175 | // intersection is outside triangle 176 | return false; 177 | } 178 | }; 179 | }; 180 | 181 | //------------------------------------------------------------------- 182 | #endif 183 | 184 | -------------------------------------------------------------------------------- /ext/mathlib/vector.h: -------------------------------------------------------------------------------- 1 | #ifndef N_VECTOR_H 2 | #define N_VECTOR_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | Implement 2, 3 and 4-dimensional vector classes. 6 | 7 | (C) 2002 RadonLabs GmbH 8 | */ 9 | #include "ntypes.h" 10 | #define vec3f vector3 11 | #define vec4f vector4 12 | #ifndef __USE_SSE__ 13 | // generic vector classes 14 | #include "mathlib/_vector2.h" 15 | #include "mathlib/_vector3.h" 16 | #include "mathlib/_vector4.h" 17 | typedef _vector2 vector2; 18 | typedef _vector3 vector3; 19 | typedef _vector4 vector4; 20 | #else 21 | // sse vector classes 22 | #include "mathlib/_vector2.h" 23 | #include "mathlib/_vector3_sse.h" 24 | #include "mathlib/_vector4_sse.h" 25 | typedef _vector2 vector2; 26 | typedef _vector3_sse vector3; 27 | typedef _vector4_sse vector4; 28 | #endif 29 | 30 | //------------------------------------------------------------------------------ 31 | #endif 32 | -------------------------------------------------------------------------------- /ext/mathlib/vector3envelopecurve.h: -------------------------------------------------------------------------------- 1 | #ifndef N_VECTOR3_ENVELOPE_CURVE_H 2 | #define N_VECTOR3_ENVELOPE_CURVE_H 3 | //------------------------------------------------------------------------------ 4 | /** 5 | @class nVector3EnvelopeCurve 6 | @ingroup Math 7 | 8 | A 3-dimensional envelope curve. 9 | 10 | (C) 2004 RadonLabs GmbH 11 | */ 12 | #include "mathlib/nmath.h" 13 | #include "mathlib/vector.h" 14 | 15 | //------------------------------------------------------------------------------ 16 | // nColorEnvelopeVurve 17 | //------------------------------------------------------------------------------ 18 | class nVector3EnvelopeCurve 19 | { 20 | public: 21 | /// constructor 1 22 | nVector3EnvelopeCurve(); 23 | /// constructor 2 24 | nVector3EnvelopeCurve(const vector3& keyFrameValue0, const vector3& keyFrameValue1, 25 | const vector3& keyFrameValue2, const vector3& keyFrameValue3, 26 | const float keyFramePos1, const float keyFramePos2); 27 | // set all parameters 28 | void SetParameters(const vector3& keyFrameValue0, const vector3& keyFrameValue1, 29 | const vector3& keyFrameValue2, const vector3& keyFrameValue3, 30 | const float keyFramePos1, const float keyFramePos2); 31 | // assign to other color envelope curve 32 | void SetParameters(const nVector3EnvelopeCurve& src); 33 | /// get the function value; pos must be between 0 and 1 34 | const vector3& GetValue(float pos) const; 35 | 36 | enum 37 | { 38 | NumValues = 4, 39 | }; 40 | 41 | vector3 keyFrameValues[NumValues]; 42 | float keyFramePos1, keyFramePos2; // 0 through 1 43 | float frequency, amplitude; // parameters of the sinus function 44 | }; 45 | 46 | //------------------------------------------------------------------------------ 47 | /** 48 | */ 49 | inline 50 | nVector3EnvelopeCurve::nVector3EnvelopeCurve() : 51 | keyFramePos1(.2f), 52 | keyFramePos2(.8f) 53 | { 54 | keyFrameValues[0] = vector3(1.0f, 1.0f, 1.0f); 55 | keyFrameValues[1] = vector3(1.0f, 1.0f, 1.0f); 56 | keyFrameValues[2] = vector3(1.0f, 1.0f, 1.0f); 57 | keyFrameValues[3] = vector3(1.0f, 1.0f, 1.0f); 58 | } 59 | 60 | //------------------------------------------------------------------------------ 61 | /** 62 | */ 63 | inline 64 | nVector3EnvelopeCurve::nVector3EnvelopeCurve(const vector3& keyFrameValue0, 65 | const vector3& keyFrameValue1, const vector3& keyFrameValue2, 66 | const vector3& keyFrameValue3, const float keyFramePos1, 67 | const float keyFramePos2) : 68 | keyFramePos1(keyFramePos1), 69 | keyFramePos2(keyFramePos2) 70 | { 71 | this->keyFrameValues[0] = keyFrameValue0; 72 | this->keyFrameValues[1] = keyFrameValue1; 73 | this->keyFrameValues[2] = keyFrameValue2; 74 | this->keyFrameValues[3] = keyFrameValue3; 75 | } 76 | 77 | //------------------------------------------------------------------------------ 78 | /** 79 | */ 80 | inline 81 | void 82 | nVector3EnvelopeCurve::SetParameters(const vector3& keyFrameValue0, const vector3& keyFrameValue1, 83 | const vector3& keyFrameValue2, const vector3& keyFrameValue3, 84 | const float keyFramePos1, const float keyFramePos2) 85 | { 86 | this->keyFrameValues[0] = keyFrameValue0; 87 | this->keyFrameValues[1] = keyFrameValue1; 88 | this->keyFrameValues[2] = keyFrameValue2; 89 | this->keyFrameValues[3] = keyFrameValue3; 90 | this->keyFramePos1 = keyFramePos1; 91 | this->keyFramePos2 = keyFramePos2; 92 | } 93 | //------------------------------------------------------------------------------ 94 | /** 95 | */ 96 | inline 97 | void 98 | nVector3EnvelopeCurve::SetParameters(const nVector3EnvelopeCurve& src) 99 | { 100 | this->keyFrameValues[0] = src.keyFrameValues[0]; 101 | this->keyFrameValues[1] = src.keyFrameValues[1]; 102 | this->keyFrameValues[2] = src.keyFrameValues[2]; 103 | this->keyFrameValues[3] = src.keyFrameValues[3]; 104 | this->keyFramePos1 = src.keyFramePos1; 105 | this->keyFramePos2 = src.keyFramePos2; 106 | } 107 | 108 | //------------------------------------------------------------------------------ 109 | /** 110 | */ 111 | inline 112 | const vector3& 113 | nVector3EnvelopeCurve::GetValue(float pos) const 114 | { 115 | n_assert(pos >= 0.0); 116 | n_assert(pos <= 1.0); 117 | 118 | static vector3 linearValue; 119 | 120 | if (pos < this->keyFramePos1) 121 | { 122 | linearValue = this->keyFrameValues[1]; 123 | linearValue.lerp(this->keyFrameValues[0], 124 | (pos / this->keyFramePos1)); 125 | } 126 | else if (pos < this->keyFramePos2) 127 | { 128 | linearValue = this->keyFrameValues[2]; 129 | linearValue.lerp(this->keyFrameValues[1], 130 | (pos-this->keyFramePos1) / (this->keyFramePos2-this->keyFramePos1)); 131 | } 132 | else 133 | { 134 | linearValue = this->keyFrameValues[3]; 135 | linearValue.lerp(this->keyFrameValues[2], 136 | (pos-this->keyFramePos2) / (1.0f-this->keyFramePos2)); 137 | } 138 | 139 | return linearValue; 140 | } 141 | 142 | //------------------------------------------------------------------------------ 143 | #endif 144 | -------------------------------------------------------------------------------- /lib32/glut32.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Geometry-Clipmap-Tutorial-in-OpenGL/154218b1e405d0e98aba3de41d488b1822a4725d/lib32/glut32.lib -------------------------------------------------------------------------------- /lib64/glut32.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sp4cerat/Geometry-Clipmap-Tutorial-in-OpenGL/154218b1e405d0e98aba3de41d488b1822a4725d/lib64/glut32.lib -------------------------------------------------------------------------------- /shader/frag.txt: -------------------------------------------------------------------------------- 1 | uniform sampler2D tex_terrain; 2 | 3 | varying vec2 vs_tex_coord; 4 | 5 | void main(void) 6 | { 7 | gl_FragColor = texture2D(tex_terrain,vs_tex_coord); 8 | } 9 | -------------------------------------------------------------------------------- /shader/vs.txt: -------------------------------------------------------------------------------- 1 | uniform sampler2D tex_heightmap; 2 | 3 | uniform vec4 map_position; 4 | uniform vec4 offset; 5 | uniform vec4 scale; 6 | 7 | varying vec2 vs_tex_coord; 8 | 9 | void main(void) 10 | { 11 | vec4 pos = scale*(offset+vec4(gl_Vertex.xyz,1.0)); 12 | vec4 pos_map = (pos+map_position)*2.0; 13 | pos.z= texture2D(tex_heightmap,pos_map.xy).x; 14 | pos.z*=-0.05; 15 | gl_Position = gl_ModelViewProjectionMatrix*pos; 16 | 17 | vs_tex_coord.xy = pos_map.xy ; 18 | } 19 | -------------------------------------------------------------------------------- /src/Bmp.cpp: -------------------------------------------------------------------------------- 1 | //#################################################################// 2 | #include "Bmp.h" 3 | //#################################################################// 4 | Bmp::Bmp() 5 | { 6 | width=height=0; 7 | data=NULL; 8 | } 9 | //#################################################################// 10 | Bmp::Bmp(int x,int y,int b,unsigned char*buffer) 11 | { 12 | width=height=0; 13 | data=NULL; 14 | set(x,y,b,buffer); 15 | } 16 | //#################################################################// 17 | Bmp::Bmp(const char*filename) 18 | { 19 | width=height=0; 20 | data=NULL; 21 | load(filename); 22 | } 23 | //#################################################################// 24 | Bmp::~Bmp() 25 | { 26 | if (data) free(data); 27 | } 28 | //#################################################################// 29 | void Bmp::save(const char*filename) 30 | { 31 | printf("saving image %s\n",filename); 32 | unsigned char bmp[58]= 33 | {0x42,0x4D,0x36,0x30,0,0,0,0,0,0,0x36,0,0,0,0x28,0,0,0, 34 | 0x40,0,0,0, // X-Size 35 | 0x40,0,0,0, // Y-Size 36 | 1,0,0x18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 37 | 38 | bmp[18] =width; 39 | bmp[19] =width>>8; 40 | bmp[22] =height; 41 | bmp[23] =height>>8; 42 | bmp[28] =bpp; 43 | 44 | FILE* fn; 45 | if ((fn = fopen (filename,"wb")) != NULL) 46 | { 47 | fwrite(bmp ,1,54 ,fn); 48 | fwrite(data,1,width*height*(bpp/8),fn); 49 | fclose(fn); 50 | } 51 | else error_stop("Bmp::save"); 52 | } 53 | //#################################################################// 54 | void Bmp::load(const char*filename) 55 | { 56 | FILE* handle; 57 | 58 | if(filename==NULL) 59 | {printf("File not found %s !\n",filename);while(1);;} 60 | if((char)filename[0]==0) 61 | {printf("File not found %s !\n",filename);while(1);;} 62 | 63 | if ((handle = fopen(filename, "rb")) == NULL) 64 | {printf("File not found %s !\n",filename);while(1);;} 65 | 66 | if(!fread(bmp, 11, 1, handle)) 67 | { 68 | printf("Error reading file %s!\n",filename); 69 | while(1);; 70 | } 71 | if(!fread(&bmp[11], (int)((unsigned char)bmp[10])-11, 1, handle)) 72 | { 73 | printf("Error reading file %s!\n",filename); 74 | while(1);; 75 | } 76 | 77 | width =(int)((unsigned char)bmp[18])+((int)((unsigned char)(bmp[19]))<<8); 78 | height =(int)((unsigned char)bmp[22])+((int)((unsigned char)(bmp[23]))<<8); 79 | bpp =bmp[28]; 80 | 81 | //printf("%s : %dx%dx%d Bit \n",filename,width,height,bpp); 82 | 83 | if(data)free(data); 84 | 85 | int size=width*height*(bpp/8); 86 | 87 | data=(unsigned char*)malloc(size+1); 88 | fread(data,size,1,handle); 89 | 90 | fclose(handle); 91 | 92 | printf("read successfully %s ; %dx%dx%d Bit \n",filename,width,height,bpp); 93 | } 94 | //#################################################################// 95 | void Bmp::save_float(const char*filename) 96 | { 97 | FILE* fn; 98 | if ((fn = fopen (filename,"wb")) == NULL) error_stop("Bmp::save_float"); 99 | fwrite(data,1,4*width*height,fn); 100 | fclose(fn); 101 | } 102 | //#################################################################// 103 | void Bmp::load_float(const char*filename) 104 | { 105 | FILE* fn; 106 | if ((fn = fopen (filename,"rb")) == NULL) error_stop("Bmp::load_float"); 107 | fread(data,1,4*width*height,fn); 108 | fclose(fn); 109 | } 110 | //#################################################################// 111 | void Bmp::set_pixel(int x,int y,int r,int g,int b) 112 | { 113 | data[(x+y*width)*(bpp/8)+2]=r; 114 | data[(x+y*width)*(bpp/8)+1]=g; 115 | data[(x+y*width)*(bpp/8)+0]=b; 116 | } 117 | //#################################################################// 118 | int Bmp::get_pixel(int x,int y) 119 | { 120 | if(data==0) error_stop("get_pixel data=0"); 121 | if(x>=width)return 0; 122 | if(y>=height)return 0; 123 | return 124 | data[(x+y*width)*(bpp/8)+0]+ 125 | data[(x+y*width)*(bpp/8)+1]*256+ 126 | data[(x+y*width)*(bpp/8)+2]*256*256; 127 | } 128 | //#################################################################// 129 | vec3f Bmp::get_pixel3f(int x,int y) 130 | { 131 | int color=get_pixel(x,y); 132 | float r=float(color&255)/255.0f; 133 | float g=float((color>>8)&255)/255.0f; 134 | float b=float((color>>16)&255)/255.0f; 135 | return vec3f(r,g,b); 136 | } 137 | //#################################################################// 138 | void Bmp::blur(int radius) 139 | { 140 | } 141 | //#################################################################// 142 | void Bmp::set(int x,int y,int b,unsigned char*buffer) 143 | { 144 | width=x; 145 | height=y; 146 | bpp=b; 147 | if(data) free(data); 148 | 149 | data=(unsigned char*) malloc(width*height*(bpp/8)); 150 | if(!data) error_stop("Bmp::set : out of memory"); 151 | 152 | if(buffer==0) 153 | memset(data,0,width*height*(bpp/8)); 154 | else 155 | memmove(data,buffer,width*height*(bpp/8)); 156 | 157 | bmp[18] =width; 158 | bmp[19] =width>>8; 159 | bmp[22] =height; 160 | bmp[23] =height>>8; 161 | bmp[28] =bpp; 162 | } 163 | //#################################################################// 164 | -------------------------------------------------------------------------------- /src/Bmp.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////// 2 | #pragma once 3 | /////////////////////////////////////////// 4 | #include "core.h" 5 | /////////////////////////////////////////// 6 | class Bmp 7 | { 8 | public: 9 | 10 | Bmp(); 11 | Bmp(const char*filename); 12 | Bmp(int x,int y,int bpp,unsigned char*data=0); 13 | ~Bmp(); 14 | 15 | void load(const char*filename); 16 | void save(const char*filename); 17 | void save_float(const char*filename); 18 | void load_float(const char*filename); 19 | void set(int x,int y,int bpp,unsigned char*data); 20 | void blur(int radius); 21 | void set_pixel(int x,int y,int r,int g,int b); 22 | int get_pixel(int x,int y); 23 | vec3f get_pixel3f(int x,int y); 24 | 25 | public: 26 | 27 | unsigned char*data; 28 | int width; 29 | int height; 30 | int bpp; 31 | 32 | private: 33 | 34 | unsigned char bmp[54]; 35 | }; 36 | 37 | -------------------------------------------------------------------------------- /src/ClipMap.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 18 | 19 | 20 | 21 | 22 | 29 | 32 | 35 | 38 | 41 | 44 | 56 | 59 | 62 | 65 | 77 | 80 | 83 | 86 | 89 | 92 | 95 | 98 | 99 | 106 | 109 | 112 | 115 | 118 | 122 | 134 | 137 | 140 | 143 | 155 | 158 | 161 | 164 | 167 | 170 | 173 | 176 | 177 | 184 | 187 | 190 | 193 | 196 | 199 | 210 | 213 | 216 | 219 | 233 | 236 | 239 | 242 | 245 | 248 | 251 | 254 | 255 | 262 | 265 | 268 | 271 | 274 | 278 | 288 | 291 | 294 | 297 | 311 | 314 | 317 | 320 | 323 | 326 | 329 | 332 | 333 | 334 | 335 | 336 | 337 | 340 | 343 | 344 | 347 | 348 | 349 | 352 | 355 | 356 | 359 | 360 | 363 | 364 | 367 | 368 | 369 | 372 | 375 | 376 | 379 | 380 | 383 | 384 | 387 | 388 | 391 | 392 | 395 | 396 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | -------------------------------------------------------------------------------- /src/ClipMap.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {98864040-E723-43CF-9479-6FBCDF2F6C51} 23 | Cutscene 24 | Win32Proj 25 | ClipMaps 26 | 27 | 28 | 29 | Application 30 | NotSet 31 | 32 | 33 | Application 34 | NotSet 35 | 36 | 37 | Application 38 | NotSet 39 | Windows7.1SDK 40 | 41 | 42 | Application 43 | Unicode 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | <_ProjectFileVersion>10.0.30319.1 63 | Debug\ 64 | Debug\ 65 | true 66 | $(Platform)\$(Configuration)\ 67 | $(Platform)\$(Configuration)\ 68 | true 69 | Release\ 70 | Release\ 71 | false 72 | $(Platform)\$(Configuration)\ 73 | $(Platform)\$(Configuration)\ 74 | false 75 | 76 | 77 | 78 | Disabled 79 | ..\ext;%(AdditionalIncludeDirectories) 80 | WIN32;_DEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 81 | true 82 | EnableFastChecks 83 | MultiThreadedDebugDLL 84 | 85 | 86 | TurnOffAllWarnings 87 | EditAndContinue 88 | 89 | 90 | strmiids.lib;winmm.lib;glut32.lib;%(AdditionalDependencies) 91 | ..\bin32\$(ProjectName).exe 92 | ..\lib32;%(AdditionalLibraryDirectories) 93 | true 94 | Console 95 | false 96 | 97 | 98 | MachineX86 99 | 100 | 101 | 102 | 103 | X64 104 | 105 | 106 | Disabled 107 | ..\ext;%(AdditionalIncludeDirectories) 108 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) 109 | true 110 | EnableFastChecks 111 | MultiThreadedDebugDLL 112 | 113 | 114 | TurnOffAllWarnings 115 | ProgramDatabase 116 | 117 | 118 | strmiids.lib;winmm.lib;glut32.lib;%(AdditionalDependencies) 119 | ..\bin64\$(ProjectName).exe 120 | ..\lib64;%(AdditionalLibraryDirectories) 121 | true 122 | Console 123 | false 124 | 125 | 126 | MachineX64 127 | 128 | 129 | 130 | 131 | MaxSpeed 132 | ..\ext;%(AdditionalIncludeDirectories) 133 | WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 134 | MultiThreadedDLL 135 | true 136 | 137 | 138 | TurnOffAllWarnings 139 | ProgramDatabase 140 | 141 | 142 | strmiids.lib;winmm.lib;glut32.lib;%(AdditionalDependencies) 143 | ..\bin32\$(ProjectName).exe 144 | ..\lib32;%(AdditionalLibraryDirectories) 145 | true 146 | Console 147 | true 148 | true 149 | false 150 | 151 | 152 | MachineX86 153 | 154 | 155 | 156 | 157 | X64 158 | 159 | 160 | MaxSpeed 161 | ..\ext;%(AdditionalIncludeDirectories) 162 | WIN32;NDEBUG;_WINDOWS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) 163 | MultiThreadedDLL 164 | 165 | 166 | TurnOffAllWarnings 167 | ProgramDatabase 168 | 169 | 170 | glut32.lib 171 | ..\bin64\$(ProjectName).exe 172 | ..\lib64;%(AdditionalLibraryDirectories) 173 | true 174 | Console 175 | true 176 | true 177 | false 178 | 179 | 180 | MachineX64 181 | false 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | -------------------------------------------------------------------------------- /src/ClipMap.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {0afe1f4c-a089-43f2-9880-9653e3e32383} 6 | 7 | 8 | {68ac1930-7cbf-408a-97bd-650bca509cb8} 9 | 10 | 11 | {9598472a-f3af-468e-b477-a16a6fc8fb2d} 12 | 13 | 14 | 15 | 16 | src 17 | 18 | 19 | ext 20 | 21 | 22 | ext 23 | 24 | 25 | ext 26 | 27 | 28 | src 29 | 30 | 31 | src 32 | 33 | 34 | 35 | 36 | src 37 | 38 | 39 | ext 40 | 41 | 42 | src 43 | 44 | 45 | 46 | 47 | shader 48 | 49 | 50 | shader 51 | 52 | 53 | -------------------------------------------------------------------------------- /src/ClipMap.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | ..\bin64\$(ProjectName).exe 5 | WindowsLocalDebugger 6 | 7 | 8 | ..\bin32\$(ProjectName).exe 9 | WindowsLocalDebugger 10 | 11 | 12 | ..\bin32\$(ProjectName).exe 13 | WindowsLocalDebugger 14 | 15 | -------------------------------------------------------------------------------- /src/Main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////// 2 | // 3 | // Geometry Clip-Maps Tutorial 4 | // 5 | // (C) by Sven Forstmann in 2014 6 | // 7 | // License : MIT 8 | // http://opensource.org/licenses/MIT 9 | ///////////////////////////////////////////// 10 | // Mathlib included from 11 | // http://sourceforge.net/projects/nebuladevice/ 12 | ///////////////////////////////////////////// 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | using namespace std; 23 | #include "glsl.h" 24 | #pragma comment(lib,"winmm.lib") 25 | /////////////////////////////////////////// 26 | #include "Bmp.h" 27 | #include "ogl.h" 28 | /////////////////////////////////////////// 29 | int grid= 64; // patch resolution 30 | int levels=5; // LOD levels 31 | int width=2048,height=2048; // heightmap dimensions 32 | /////////////////////////////////////////// 33 | 34 | void DrawScene() 35 | { 36 | if ( GetAsyncKeyState(VK_ESCAPE) ) exit(0); 37 | 38 | POINT cursor; 39 | GetCursorPos(&cursor); // mouse pointer position 40 | 41 | bool wireframe= GetAsyncKeyState(VK_SPACE); // render wireframe 42 | bool topdown = GetAsyncKeyState(VK_RETURN); // view top-down 43 | float viewangle= float(cursor.x)/5.0; 44 | vec3f viewpos ( (timeGetTime()>>2)&((1<<17)-1) , -(float(cursor.y)/1000.0)* 0.1-0.01 , 0 ); 45 | 46 | glClearDepth(1.0f); 47 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 48 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 49 | glEnable(GL_CULL_FACE); 50 | glCullFace(GL_FRONT); 51 | glEnable(GL_DEPTH_TEST); 52 | glDepthFunc(GL_LESS); 53 | 54 | static int tex_heightmap=0; 55 | static int tex_terrain=0; 56 | 57 | static bool init=true; 58 | static Shader shader("Shader"); 59 | 60 | static int vbo=0; 61 | static std::vector vert; 62 | 63 | if(init) 64 | { 65 | /*+++++++++++++++++++++++++++++++++++++*/ 66 | // terrain heightmap 67 | Bmp bmp(width,height,32); 68 | loopj(0,height) loopi(0,width) 69 | { 70 | float x= float(i)/float(width); 71 | float y= float(j)/float(height); 72 | float h = (sin(4*M_PI*x)+sin(4*M_PI*y)+sin(16*M_PI*x)*sin(16*M_PI*y))*0.125+0.5; 73 | ((float*)bmp.data)[i+j*width]=h; 74 | } 75 | //bmp.load_float("../result.f32"); // <-- use this for loading raw float map from file 76 | tex_heightmap = ogl_tex_new(width,height,GL_LINEAR_MIPMAP_LINEAR,GL_REPEAT,GL_LUMINANCE16F_ARB,GL_LUMINANCE,bmp.data, GL_FLOAT); 77 | /*+++++++++++++++++++++++++++++++++++++*/ 78 | // terrain texture 79 | loopj(0,height) loopi(0,width) loopk(0,3) 80 | { 81 | bmp.data[(i+j*width)*3+k]=i^j^(k*192); 82 | } 83 | tex_terrain = ogl_tex_new(width,height,GL_LINEAR_MIPMAP_LINEAR,GL_REPEAT,GL_RGB,GL_RGB,bmp.data, GL_UNSIGNED_BYTE); 84 | /*+++++++++++++++++++++++++++++++++++++*/ 85 | // driver info 86 | std::cout << "GL_VERSION: " << glGetString(GL_VERSION) << std::endl; //std::cout << "GL_EXTENSIONS: " << glGetString(GL_EXTENSIONS) << std::endl; 87 | std::cout << "GL_RENDERER: " << glGetString(GL_RENDERER) << std::endl; 88 | std::cout << "GL_VENDOR: " << glGetString(GL_VENDOR) << std::endl; 89 | std::cout << "GLU_VERSION: " << gluGetString(GLU_VERSION) << std::endl; //std::cout << "GLU_EXTENSIONS: " << gluGetString(GLU_EXTENSIONS) << std::endl; 90 | std::cout << "GLUT_API_VERSION: " << GLUT_API_VERSION << std::endl; 91 | /*+++++++++++++++++++++++++++++++++++++*/ 92 | // load shaders 93 | shader.attach(GL_VERTEX_SHADER,"../shader/vs.txt"); 94 | shader.attach(GL_FRAGMENT_SHADER,"../shader/frag.txt"); 95 | shader.link(); 96 | /*+++++++++++++++++++++++++++++++++++++*/ 97 | // make vbo quad patch 98 | loopj(0,grid+1) 99 | loopi(0,grid+2) 100 | { 101 | loopk(0, ((i==0) ? 2 : 1) ) 102 | { 103 | vert.push_back(float(i)/grid); 104 | vert.push_back(float(j)/grid); 105 | vert.push_back(0); 106 | } 107 | ++j; 108 | loopk(0, ((i==grid+1) ? 2 : 1) ) 109 | { 110 | vert.push_back(float(i)/grid); 111 | vert.push_back(float(j)/grid); 112 | vert.push_back(0); 113 | } 114 | --j; 115 | } 116 | /*+++++++++++++++++++++++++++++++++++++*/ 117 | glGenBuffers(1, (GLuint *)(&vbo)); 118 | glBindBuffer(GL_ARRAY_BUFFER, vbo); 119 | glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vert.size(),&vert[0], GL_DYNAMIC_DRAW_ARB ); 120 | /*+++++++++++++++++++++++++++++++++++++*/ 121 | init=false; 122 | /*+++++++++++++++++++++++++++++++++++++*/ 123 | } 124 | glMatrixMode( GL_PROJECTION); 125 | glLoadIdentity(); 126 | 127 | if (topdown) 128 | { 129 | glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 130 | glRotatef(180,1,0,0); 131 | wireframe^=1; 132 | } 133 | else 134 | { 135 | int vp[4]; 136 | glGetIntegerv(GL_VIEWPORT, vp); 137 | gluPerspective(90.0,float(vp[2])/float(vp[3]) , 0.0001, 1.0); 138 | glTranslatef(0,viewpos.y,0); // set height 139 | glRotatef(130,1,0,0); 140 | glRotatef(viewangle,0,0,1); // set rotation 141 | } 142 | 143 | matrix44 mat; 144 | glGetFloatv(GL_PROJECTION_MATRIX, &mat.m[0][0]); CHECK_GL_ERROR(); 145 | 146 | // Enable VBO 147 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); CHECK_GL_ERROR(); 148 | glEnableClientState(GL_VERTEX_ARRAY); CHECK_GL_ERROR(); 149 | glVertexPointer ( 3, GL_FLOAT,0, (char *) 0); CHECK_GL_ERROR(); 150 | 151 | glEnable(GL_TEXTURE_2D); 152 | glActiveTextureARB( GL_TEXTURE0 ); 153 | glBindTexture(GL_TEXTURE_2D, tex_heightmap); 154 | glEnable(GL_TEXTURE_2D); 155 | glActiveTextureARB( GL_TEXTURE1 ); 156 | glBindTexture(GL_TEXTURE_2D, tex_terrain); 157 | 158 | // Triangle Mesh 159 | shader.begin(); 160 | shader.setUniform1i("tex_heightmap",0); 161 | shader.setUniform1i("tex_terrain",1); 162 | 163 | float sxy=2; // scale x/y 164 | shader.setUniform4f("map_position", 165 | -viewpos.x/float(2*512*grid), 166 | -viewpos.z/float(2*512*grid),0,0); 167 | 168 | loopi(0,levels) 169 | { 170 | float ox=(int(viewpos.x*(1<=0) offset.y-=1.0/float(grid); // adjust offset for proper overlapping 182 | if(j>=0) offset.x-=1.0/float(grid); // adjust offset for proper overlapping 183 | 184 | //cull 185 | int xp=0,xm=0,yp=0,ym=0,zp=0; 186 | looplmn(0,0,0,2,2,2) 187 | { 188 | vec3f v = scale*(offset+vec3f(l,m,float(-n)*0.05)); // bbox vector 189 | vec4f cs = mat * vec4f(v.x,v.y,v.z,1); // clipspace 190 | if(cs.z< cs.w) zp++; 191 | if(cs.x<-cs.w) xm++; if(cs.x>cs.w) xp++; 192 | if(cs.y<-cs.w) ym++; if(cs.y>cs.w) yp++; 193 | } 194 | if(zp==0 || xm==8 || xp==8 || ym==8 || yp==8)continue; // skip if invisible 195 | 196 | //render 197 | shader.setUniform4f("offset", offset.x,offset.y,0,0); 198 | if(wireframe) glDrawArrays( GL_LINES, 0, vert.size()/3); 199 | else glDrawArrays( GL_TRIANGLE_STRIP, 0, vert.size()/3); 200 | } 201 | sxy*=0.5; 202 | } 203 | shader.end(); 204 | 205 | // Disable VBO 206 | glDisableClientState(GL_VERTEX_ARRAY); CHECK_GL_ERROR(); 207 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); CHECK_GL_ERROR(); 208 | glutSwapBuffers(); 209 | } 210 | /////////////////////////////////////////// 211 | int main(int argc, char **argv) 212 | { 213 | glutInit(&argc, argv); 214 | glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH); 215 | glutInitWindowSize(1024, 512); 216 | glutInitWindowPosition(0, 0); 217 | glutCreateWindow("Geometry Clipmaps Example (c) Sven Forstmann 2014"); 218 | glutDisplayFunc(DrawScene); 219 | glutIdleFunc(DrawScene); 220 | glewInit(); 221 | wglSwapIntervalEXT(0); 222 | glutMainLoop(); 223 | return 0; 224 | } 225 | /////////////////////////////////////////// 226 | -------------------------------------------------------------------------------- /src/core.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /*------------------------------------------------------*/ 3 | #define _USE_MATH_DEFINES 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "mathlib/vector.h" 15 | #include "mathlib/matrix.h" 16 | /*------------------------------------------------------*/ 17 | #define ushort unsigned short 18 | #define uint unsigned int 19 | #define uchar unsigned char 20 | /*------------------------------------------------------*/ 21 | #define loop0i(end_l) for ( int i=0;i(b))?(a):(b)) 41 | #define clamp(a_,b_,c_) min(max(a_,b_),c_) 42 | //#define lerp(t, a, b) ( a + t * (b - a) ) 43 | //#define lerp(a_,b_,t_) ( a*(1-t_) + b*t_ ) 44 | /*------------------------------------------------------*/ 45 | #define error_stop(e){MessageBoxA(0,e,"some error",0);exit(0);} 46 | /*------------------------------------------------------*/ 47 | -------------------------------------------------------------------------------- /src/glsl.h: -------------------------------------------------------------------------------- 1 | // GL ERROR CHECK 2 | #define CHECK_GL_ERROR() CheckGLError(__FILE__, __LINE__) 3 | // GL ERROR CHECK 4 | int CheckGLError(char *file, int line) 5 | { 6 | //return 0; 7 | GLenum glErr,glErr2; 8 | int retCode = 0; 9 | 10 | glErr = glErr2 = glGetError(); 11 | while (glErr != GL_NO_ERROR) 12 | { 13 | char* str1 = (char*)gluErrorString(glErr); 14 | if (str1) 15 | cout << "GL Error #" << glErr << "(" << str1 << ") " << " in File " << file << " at line: " << line << endl; 16 | else 17 | cout << "GL Error #" << glErr << " in File " << file << " at line: " << line << endl; 18 | retCode = 1; 19 | glErr = glGetError(); 20 | } 21 | if (glErr2 != GL_NO_ERROR) while(1)Sleep(100);; 22 | 23 | return 0; 24 | } 25 | /////////////////////////////////////////// 26 | class Shader 27 | { 28 | public: 29 | Shader(std::string shadername){name=shadername;}; 30 | void attach(int type,char* filename) 31 | { 32 | char* mem=read_file(filename); 33 | if(mem==0) 34 | { 35 | printf("Shader file %s not found\n",filename); 36 | while(1);; 37 | } 38 | GLuint handle = glCreateShader(type);CHECK_GL_ERROR(); 39 | glShaderSource(handle, 1, (const GLchar**)(&mem), 0);CHECK_GL_ERROR(); 40 | glCompileShader(handle);CHECK_GL_ERROR(); 41 | 42 | GLint compileSuccess=0; 43 | GLchar compilerSpew[256]; 44 | 45 | glGetShaderiv(handle, GL_COMPILE_STATUS, &compileSuccess); 46 | CHECK_GL_ERROR(); 47 | if(!compileSuccess) 48 | { 49 | glGetShaderInfoLog(handle, sizeof(compilerSpew), 0, compilerSpew); 50 | printf("Shader %s\n%s\ncompileSuccess=%d\n",filename,compilerSpew,compileSuccess); 51 | CHECK_GL_ERROR(); 52 | while(1);; 53 | } 54 | free(mem); 55 | handles.push_back(handle); 56 | } 57 | void link() 58 | { 59 | program_handle = glCreateProgram(); 60 | for (int i=0;i handles; 127 | GLuint program_handle; 128 | std::string name; 129 | 130 | char* read_file(char* name) 131 | { 132 | FILE * fp = fopen (name, "rb"); 133 | 134 | if (fp==0) 135 | { 136 | printf ("File %s NOT FOUND\n"); 137 | while(1);; 138 | } 139 | fseek(fp, 0L, SEEK_END); 140 | int fsize = ftell(fp); 141 | fseek(fp, 0L, SEEK_SET); 142 | char* mem=(char*)malloc(fsize+1); 143 | for(int i=0;i=2))/ry; 150 | vec3f c01=c0+(c1-c0)*a; 151 | vec3f c32=c3+(c2-c3)*a; 152 | vec3f c=c01+(c32-c01)*b; 153 | glColor3f(c.x,c.y,c.z); 154 | float x=a*(x1-x0)+x0; 155 | float y=b*(y1-y0)+y0; 156 | glVertex2f(x,y); 157 | if (k==3)glEnd(); 158 | } 159 | } 160 | 161 | void ogl_drawlinequad(float x0,float y0,float x1,float y1) 162 | { 163 | glBegin(GL_LINE_LOOP); 164 | glVertex2f(x0,y0); 165 | glVertex2f(x1,y0); 166 | glVertex2f(x1,y1); 167 | glVertex2f(x0,y1); 168 | glEnd(); 169 | } 170 | 171 | void ogl_subdiv_tri(vec3f p0,vec3f p1,vec3f p2) 172 | { 173 | static int iteration=0; 174 | 175 | if(iteration==4) 176 | { 177 | vec3f mid=(p0+p1+p2)*(1.0/3.0); 178 | mid.norm(); 179 | float size=0.01; 180 | ogl_drawlinequad( 181 | mid.x-size, 182 | mid.y-size, 183 | mid.x+size, 184 | mid.y+size); 185 | //p0.norm(); 186 | //p1.norm(); 187 | //p2.norm(); 188 | 189 | ogl_drawline(p0,p1); 190 | ogl_drawline(p1,p2); 191 | ogl_drawline(p2,p0); 192 | return; 193 | } 194 | 195 | iteration++; 196 | 197 | vec3f p01=(p0+p1)*0.5;p01.norm(); 198 | vec3f p12=(p1+p2)*0.5;p12.norm(); 199 | vec3f p20=(p2+p0)*0.5;p20.norm(); 200 | 201 | ogl_subdiv_tri(p0,p20,p01); 202 | ogl_subdiv_tri(p1,p01,p12); 203 | ogl_subdiv_tri(p2,p12,p20); 204 | ogl_subdiv_tri(p01,p20,p12); 205 | 206 | iteration--; 207 | } 208 | 209 | void ogl_bind(int tmu,int tex) 210 | { 211 | glActiveTextureARB(GL_TEXTURE0+tmu ); 212 | glEnable(GL_TEXTURE_2D); 213 | glBindTexture(GL_TEXTURE_2D, tex); 214 | CHECK_GL_ERROR(); 215 | } 216 | --------------------------------------------------------------------------------