├── README.txt ├── bin ├── win32example.exe └── win32exampleDebug.exe ├── inc └── IGizmo.h ├── libgizmo.png └── src ├── Debug └── win32example.pdb ├── LibGizmo.sln ├── Release └── win32example.pdb ├── libGizmo.suo ├── libgizmo ├── GizmoTransform.h ├── GizmoTransformMove.cpp ├── GizmoTransformMove.h ├── GizmoTransformRender.cpp ├── GizmoTransformRender.h ├── GizmoTransformRenderDX9.cpp ├── GizmoTransformRotate.cpp ├── GizmoTransformRotate.h ├── GizmoTransformScale.cpp ├── GizmoTransformScale.h ├── LibBase.h ├── ZBaseDefs.h ├── ZBaseMaths.cpp ├── ZBaseMaths.h ├── ZBaseMaths.inl ├── ZCollisionsUtils.h ├── ZMathsFunc.cpp ├── ZMathsFunc.h ├── libgizmo.vcxproj ├── libgizmo.vcxproj.filters ├── libgizmo.vcxproj.user ├── stdafx.cpp ├── stdafx.h └── targetver.h └── win32example ├── Lesson6.cpp ├── NeHe's Readme.txt ├── Release ├── CL.read.1.tlog ├── CL.write.1.tlog ├── Lesson6.obj ├── cl.command.1.tlog ├── link.command.1.tlog ├── link.read.1.tlog ├── link.write.1.tlog ├── mt.command.1.tlog ├── mt.read.1.tlog ├── mt.write.1.tlog ├── vc100.pdb ├── win32example.exe.intermediate.manifest ├── win32example.lastbuildstate └── win32example.log ├── win32example.vcxproj ├── win32example.vcxproj.filters └── win32example.vcxproj.user /README.txt: -------------------------------------------------------------------------------- 1 | ******************************************************************************************************** 2 | * INTRODUCTION 3 | 4 | - LibGizmo is a small, standalone library that adds a 3D matrix (4x4 floats) manipulation control 5 | called 'Gizmo'. It consists of 3 different controls: a Move, a Rotate and a Scale. It works the 6 | same way as in 3DStudio Max or Maya. It's written using C++ and the current implementation use 7 | OpenGL fixed pipeline. Integration should be easy. 8 | 9 | ******************************************************************************************************** 10 | * LICENSE 11 | 12 | Copyright (C) 2012 Cedric Guillemet 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy of 15 | this software and associated documentation files (the "Software"), to deal in 16 | the Software without restriction, including without limitation the rights to 17 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 18 | of the Software, and to permit persons to whom the Software is furnished to do 19 | so, subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in all 22 | copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 | SOFTWARE. 31 | 32 | ******************************************************************************************************** 33 | * Contact 34 | 35 | - email/GTALK : cedric.guillemet@gmail.com 36 | - Twitter : @skaven_ 37 | - web : http://www.skaven.fr 38 | 39 | ******************************************************************************************************** 40 | * USE 41 | 42 | - Set include path to inc, library path to lib 43 | 44 | - Link with LibGizmoDebug.lib for debug build, LibGizmo.lib for release 45 | 46 | - In your code : 47 | #include "iGizmo.h" 48 | 49 | - create one or more Gizmo with the functions : CreateMoveGizmo(), CreateRotateGizmo(), CreateScaleGizmo() 50 | 51 | - set a pointer to the 16 floats composing the matrix you want to edit 52 | gizmo->SetEditMatrix( objectMatrix ); 53 | 54 | - Any time the display viewport changes, call SetScreenDimensions 55 | gizmo->SetScreenDimension( screenWidth, screenHeight ); 56 | 57 | - Once a frame, call SetCameraMatrix. The matrices you send 58 | 59 | float viewMat[16]; 60 | float projMat[16]; 61 | 62 | glGetFloatv (GL_MODELVIEW_MATRIX, viewMat ); 63 | glGetFloatv (GL_PROJECTION_MATRIX, projMat ); 64 | 65 | gizmo->SetCameraMatrix( viewMat, projMat ); 66 | 67 | 68 | - Draw the gizmo 69 | gizmo->Draw(); 70 | 71 | - Change the kind of matrix manipulation (view, local, world). Example for 'WORLD' mode: 72 | gizmo->SetLocation( IGizmo::LOCATE_WORLD ); 73 | 74 | - When you receive a mouse button down, call 75 | if (gizmo->OnMouseDown( mousex, mousey )) 76 | SetCapture( hWnd ); 77 | This methods returns true if you have to capture the mouse. mousex and mousey are display viewport 78 | local coordinates. 0,0 is upper left. 79 | - for mouse move and mouse button up, call: 80 | gizmo->OnMouseMove( mousex, mousey ); 81 | gizmo->OnMouseUp( mousex, mousey ); 82 | 83 | ******************************************************************************************************** 84 | * RENDERING 85 | 86 | - The rendering code is done by OpenGL Fixed pipeline. The implementation is done in GizmoTransformRender.cpp. 87 | 6 methodes are called by all the 3 gizmo: 88 | 89 | void DrawCircle(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx,const tvector3 &vty); 90 | void DrawCircleHalf(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx,const tvector3 &vty,tplane &camPlan); 91 | void DrawAxis(const tvector3 &orig, const tvector3 &axis, const tvector3 &vtx,const tvector3 &vty, float fct,float fct2,const tvector4 &col); 92 | void DrawCamem(const tvector3& orig,const tvector3& vtx,const tvector3& vty,float ng); 93 | void DrawQuad(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3 &axisV); 94 | void DrawTri(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3& axisV); 95 | 96 | You can implement those methods using any API. There aren't any other call to a rendering library anywhere 97 | else. There is an old (untested) version using DX9 api. 98 | 99 | ******************************************************************************************************** 100 | * TODO 101 | 102 | - Linux/MacOSX port 103 | - Code cleanup 104 | - improve example with local/world/screen space change 105 | - DX10/11 rendering 106 | 107 | 108 | -------------------------------------------------------------------------------- /bin/win32example.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/bin/win32example.exe -------------------------------------------------------------------------------- /bin/win32exampleDebug.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/bin/win32exampleDebug.exe -------------------------------------------------------------------------------- /inc/IGizmo.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : IGizmo.h 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | #ifndef IGIZMO_H__ 30 | #define IGIZMO_H__ 31 | 32 | 33 | class IGizmo 34 | { 35 | public: 36 | enum LOCATION 37 | { 38 | LOCATE_VIEW, 39 | LOCATE_WORLD, 40 | LOCATE_LOCAL, 41 | }; 42 | 43 | enum ROTATE_AXIS 44 | { 45 | AXIS_X = 1, 46 | AXIS_Y = 2, 47 | AXIS_Z = 4, 48 | AXIS_TRACKBALL = 8, 49 | AXIS_SCREEN = 16, 50 | AXIS_ALL = 31 51 | 52 | }; 53 | 54 | 55 | virtual void SetEditMatrix(float *pMatrix) = 0; 56 | 57 | virtual void SetCameraMatrix(const float *Model, const float *Proj) = 0; 58 | virtual void SetScreenDimension( int screenWidth, int screenHeight) = 0; 59 | virtual void SetDisplayScale( float aScale ) = 0; 60 | 61 | // return true if gizmo transform capture mouse 62 | virtual bool OnMouseDown(unsigned int x, unsigned int y) = 0; 63 | virtual void OnMouseMove(unsigned int x, unsigned int y) = 0; 64 | virtual void OnMouseUp(unsigned int x, unsigned int y) = 0; 65 | 66 | // snaping 67 | virtual void UseSnap(bool bUseSnap) = 0; 68 | virtual bool IsUsingSnap() = 0; 69 | virtual void SetSnap(float snapx, float snapy, float snapz) = 0; 70 | virtual void SetSnap(const float snap) = 0; 71 | 72 | 73 | virtual void SetLocation(LOCATION aLocation) = 0; 74 | virtual LOCATION GetLocation() = 0; 75 | virtual void SetAxisMask(unsigned int mask) = 0; 76 | 77 | // rendering 78 | virtual void Draw() = 0; 79 | }; 80 | 81 | // Create new gizmo 82 | IGizmo *CreateMoveGizmo(); 83 | IGizmo *CreateRotateGizmo(); 84 | IGizmo *CreateScaleGizmo(); 85 | 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /libgizmo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/libgizmo.png -------------------------------------------------------------------------------- /src/Debug/win32example.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/Debug/win32example.pdb -------------------------------------------------------------------------------- /src/LibGizmo.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libgizmo", "libgizmo\libgizmo.vcxproj", "{5AEF535C-D65A-40B7-AA87-8130CF1B4650}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "win32example", "win32example\win32example.vcxproj", "{FF6718FF-BFDA-4832-B526-F8223ABB74F2}" 7 | ProjectSection(ProjectDependencies) = postProject 8 | {5AEF535C-D65A-40B7-AA87-8130CF1B4650} = {5AEF535C-D65A-40B7-AA87-8130CF1B4650} 9 | EndProjectSection 10 | EndProject 11 | Global 12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 13 | Debug|Win32 = Debug|Win32 14 | Release|Win32 = Release|Win32 15 | EndGlobalSection 16 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 17 | {5AEF535C-D65A-40B7-AA87-8130CF1B4650}.Debug|Win32.ActiveCfg = Debug|Win32 18 | {5AEF535C-D65A-40B7-AA87-8130CF1B4650}.Debug|Win32.Build.0 = Debug|Win32 19 | {5AEF535C-D65A-40B7-AA87-8130CF1B4650}.Release|Win32.ActiveCfg = Release|Win32 20 | {5AEF535C-D65A-40B7-AA87-8130CF1B4650}.Release|Win32.Build.0 = Release|Win32 21 | {FF6718FF-BFDA-4832-B526-F8223ABB74F2}.Debug|Win32.ActiveCfg = Debug|Win32 22 | {FF6718FF-BFDA-4832-B526-F8223ABB74F2}.Debug|Win32.Build.0 = Debug|Win32 23 | {FF6718FF-BFDA-4832-B526-F8223ABB74F2}.Release|Win32.ActiveCfg = Release|Win32 24 | {FF6718FF-BFDA-4832-B526-F8223ABB74F2}.Release|Win32.Build.0 = Release|Win32 25 | EndGlobalSection 26 | GlobalSection(SolutionProperties) = preSolution 27 | HideSolutionNode = FALSE 28 | EndGlobalSection 29 | EndGlobal 30 | -------------------------------------------------------------------------------- /src/Release/win32example.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/Release/win32example.pdb -------------------------------------------------------------------------------- /src/libGizmo.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/libGizmo.suo -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransform.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | #ifndef GIZMOTRANSFORM_H__ 30 | #define GIZMOTRANSFORM_H__ 31 | 32 | #include "GizmoTransformRender.h" 33 | #include "IGizmo.h" 34 | 35 | class CGizmoTransform : public IGizmo , protected CGizmoTransformRender 36 | { 37 | public: 38 | 39 | 40 | 41 | CGizmoTransform() 42 | { 43 | m_pMatrix = NULL; 44 | m_bUseSnap = false; 45 | //mCamera = NULL; 46 | //mTransform = NULL; 47 | mEditPos = mEditScale = NULL; 48 | mEditQT = NULL; 49 | mMask = AXIS_ALL; 50 | m_Lng = 1.f; 51 | mScreenHeight = mScreenWidth = 1; 52 | m_ScreenFactor = 1; 53 | mDisplayScale = 1.f; 54 | } 55 | 56 | virtual ~CGizmoTransform() 57 | { 58 | } 59 | 60 | 61 | 62 | virtual void SetEditMatrix(float *pMatrix) 63 | { 64 | m_pMatrix = (tmatrix*)pMatrix; 65 | //mTransform = NULL; 66 | 67 | mEditPos = mEditScale = NULL; 68 | mEditQT = NULL; 69 | } 70 | virtual void SetDisplayScale( float aScale ) { mDisplayScale = aScale; } 71 | /* 72 | virtual void SetEditTransform(ZTransform *pTransform) 73 | { 74 | mTransform = pTransform; 75 | if(!pTransform) 76 | m_pMatrix = NULL; 77 | else 78 | m_pMatrix = (tmatrix*)&mTransform->GetLocalMatrix(); 79 | mEditPos = mEditScale = NULL; 80 | mEditQT = NULL; 81 | } 82 | */ 83 | /* 84 | virtual void SetEditVQS(tvector3* aPosition, tquaternion *aQT, tvector3 *aScale) 85 | { 86 | mEditPos = aPosition; 87 | mEditQT = aQT; 88 | mEditScale = aScale; 89 | 90 | //mTransform = NULL; 91 | m_pMatrix = NULL; 92 | 93 | tmatrix p1,p2,p3; 94 | p1.Identity(); 95 | p2.Identity(); 96 | p3.Identity(); 97 | if (aPosition) 98 | p1.Translation(*aPosition); 99 | if (aQT) 100 | p2.RotationQuaternion(*aQT); 101 | if (aScale) 102 | p3.Scaling(*aScale); 103 | mWorkingMatrix = p3 * p2 * p1; 104 | m_pMatrix = &mWorkingMatrix; 105 | } 106 | */ 107 | virtual void SetScreenDimension( int screenWidth, int screenHeight) 108 | { 109 | mScreenWidth = screenWidth; 110 | mScreenHeight = screenHeight; 111 | } 112 | virtual void SetCameraMatrix(const float *Model, const float *Proj) 113 | { 114 | m_Model = *(tmatrix*)Model; 115 | m_Proj = *(tmatrix*)Proj; 116 | 117 | m_invmodel=m_Model; 118 | m_invmodel.Inverse(); 119 | 120 | m_invproj=m_Proj; 121 | m_invproj.Inverse(); 122 | 123 | m_CamSrc = m_invmodel.V4.position; 124 | m_CamDir = m_invmodel.V4.dir; 125 | m_CamUp = m_invmodel.V4.up; 126 | } 127 | 128 | 129 | 130 | // tools 131 | 132 | void BuildRay(int x, int y, tvector3 &rayOrigin, tvector3 &rayDir) 133 | { 134 | float frameX = (float)mScreenWidth; 135 | float frameY = (float)mScreenHeight; 136 | tvector3 screen_space; 137 | 138 | // device space to normalized screen space 139 | screen_space.x = ( ( (2.f * (float)x) / frameX ) - 1 ) / m_Proj.m[0][0];//.right.x; 140 | screen_space.y = -( ( (2.f * (float)y) / frameY ) - 1 ) / m_Proj.m[1][1]; 141 | screen_space.z = -1.f; 142 | 143 | // screen space to world space 144 | 145 | rayOrigin = m_invmodel.V4.position; 146 | rayDir.TransformVector(screen_space, m_invmodel); 147 | rayDir.Normalize(); 148 | } 149 | 150 | 151 | tvector3 GetVector(int vtID) 152 | { 153 | switch (vtID) 154 | { 155 | case 0: return tvector3(1,0,0); 156 | case 1: return tvector3(0,1,0); 157 | case 2: return tvector3(0,0,1); 158 | } 159 | return tvector3(0,0,0); 160 | } 161 | 162 | tvector3 GetTransformedVector(int vtID) 163 | { 164 | tvector3 vt; 165 | switch (vtID) 166 | { 167 | case 0: vt = tvector3(1,0,0); break; 168 | case 1: vt = tvector3(0,1,0); break; 169 | case 2: vt = tvector3(0,0,1); break; 170 | } 171 | if (mLocation == LOCATE_LOCAL) 172 | { 173 | vt.TransformVector(*m_pMatrix); 174 | vt.Normalize(); 175 | } 176 | return vt; 177 | } 178 | virtual void SetAxisMask(unsigned int mask) 179 | { 180 | mMask = mask; 181 | } 182 | /* 183 | tvector3 GetTransformedVector(tvector3& vt) 184 | { 185 | tmatrix mt = *m_pMatrix; 186 | mt.NoTrans(); 187 | vt.TransformPoint(mt); 188 | return vt; 189 | } 190 | */ 191 | void ComputeScreenFactor() 192 | { 193 | tmatrix viewproj = m_Model * m_Proj; 194 | 195 | tvector4 trf = vector4( m_pMatrix->V4.position.x, m_pMatrix->V4.position.y, m_pMatrix->V4.position.z, 1.f); 196 | trf.Transform( viewproj ); 197 | m_ScreenFactor = mDisplayScale * 0.15f * trf.w; 198 | } 199 | 200 | tplane m_plan; 201 | tvector3 m_LockVertex; 202 | float m_Lng; 203 | 204 | tvector3 RayTrace2(const tvector3& rayOrigin, const tvector3& rayDir, const tvector3& norm, const tmatrix& mt, tvector3 trss, bool lockVTNorm = true) 205 | { 206 | extern tvector3 ptd; 207 | 208 | tvector3 df,inters; 209 | 210 | m_plan=vector4(m_pMatrix->GetTranslation(), norm); 211 | m_plan.RayInter(inters,rayOrigin,rayDir); 212 | df.TransformPoint( inters, mt ); 213 | 214 | df /=GetScreenFactor(); 215 | /* 216 | ptd = inters; 217 | df = inters - m_pMatrix->GetTranslation(); 218 | df /=GetScreenFactor(); 219 | df2 = df; 220 | 221 | df2.TransformPoint(mt); 222 | df2 *= trss; 223 | */ 224 | m_LockVertex = df; 225 | if (lockVTNorm) 226 | { 227 | m_LockVertex.Normalize(); 228 | } 229 | else 230 | { 231 | m_LockVertex = inters; 232 | } 233 | m_Lng = df.Length(); 234 | 235 | return df; 236 | } 237 | 238 | float GetScreenFactor() 239 | { 240 | return m_ScreenFactor; 241 | } 242 | 243 | 244 | // snap 245 | virtual void UseSnap(bool bUseSnap) 246 | { 247 | m_bUseSnap = bUseSnap; 248 | } 249 | 250 | virtual bool IsUsingSnap() 251 | { 252 | return m_bUseSnap; 253 | } 254 | //transform 255 | virtual void ApplyTransform(tvector3& trans, bool bAbsolute) = 0; 256 | 257 | void SetLocation(LOCATION aLocation) { mLocation = aLocation; } 258 | LOCATION GetLocation() { return mLocation; } 259 | 260 | 261 | protected: 262 | tmatrix *m_pMatrix; 263 | tmatrix m_Model,m_Proj; 264 | tmatrix m_invmodel,m_invproj; 265 | tvector3 m_CamSrc,m_CamDir,m_CamUp; 266 | 267 | tmatrix m_svgMatrix; 268 | float m_ScreenFactor; 269 | bool m_bUseSnap; 270 | float mDisplayScale; 271 | 272 | LOCATION mLocation; 273 | 274 | tmatrix mWorkingMatrix; // for dissociated components 275 | tvector3 *mEditPos, *mEditScale ; 276 | tquaternion *mEditQT; 277 | //draw helpers 278 | 279 | unsigned int mMask; 280 | void SnapIt(float &pos, float &snap) 281 | { 282 | float sn = (float)fmod(pos,snap); 283 | if (fabs(sn)< (snap*0.25f) ) pos-=sn; 284 | if (fabs(sn)> (snap*0.75f) ) pos=( (pos-sn) + ((sn>0)?snap:-snap) ); 285 | } 286 | 287 | int mScreenWidth, mScreenHeight; 288 | }; 289 | 290 | #endif // !defined(AFX_GIZMOTRANSFORM_H__913D353E_E420_4B1C_95F3_5A0258161651__INCLUDED_) 291 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformMove.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #include "stdafx.h" 31 | #include "GizmoTransformMove.h" 32 | #ifdef MAC_OS 33 | #import 34 | #else 35 | #include 36 | #endif 37 | 38 | 39 | IGizmo *CreateMoveGizmo() 40 | { 41 | return new CGizmoTransformMove; 42 | } 43 | 44 | tvector3 ptd; 45 | ////////////////////////////////////////////////////////////////////// 46 | // Construction/Destruction 47 | ////////////////////////////////////////////////////////////////////// 48 | 49 | CGizmoTransformMove::CGizmoTransformMove() : CGizmoTransform() 50 | { 51 | m_MoveType = MOVE_NONE; 52 | } 53 | 54 | CGizmoTransformMove::~CGizmoTransformMove() 55 | { 56 | 57 | } 58 | 59 | tvector3 CGizmoTransformMove::RayTrace(tvector3& rayOrigin, tvector3& rayDir, tvector3& norm) 60 | { 61 | tvector3 df,inters; 62 | m_plan=vector4(m_pMatrix->GetTranslation(), norm); 63 | m_plan.RayInter(inters,rayOrigin,rayDir); 64 | ptd = inters; 65 | df = inters - m_pMatrix->GetTranslation(); 66 | df /=GetScreenFactor(); 67 | m_LockVertex = inters; 68 | return df; 69 | } 70 | 71 | bool CGizmoTransformMove::GetOpType(MOVETYPE &type, unsigned int x, unsigned int y) 72 | { 73 | tvector3 rayOrigin, rayDir, df; 74 | BuildRay(x, y, rayOrigin, rayDir); 75 | m_svgMatrix = *m_pMatrix; 76 | 77 | 78 | tvector3 trss(GetTransformedVector(0).Length(), 79 | GetTransformedVector(1).Length(), 80 | GetTransformedVector(2).Length()); 81 | 82 | tmatrix mt; 83 | if (mLocation == LOCATE_LOCAL) 84 | { 85 | mt = *m_pMatrix; 86 | mt.Inverse(); 87 | } 88 | else 89 | { 90 | // world 91 | mt.Translation( -m_pMatrix->V4.position); 92 | } 93 | 94 | // plan 1 : X/Z 95 | df = RayTrace2(rayOrigin, rayDir, GetTransformedVector(1), mt, trss, false); 96 | 97 | if ( ( df.x >= 0 ) && (df.x <= 1) && ( fabs(df.z) < 0.1f ) ) { type = MOVE_X; return true; } 98 | else if ( ( df.z >= 0 ) && (df.z <= 1) && ( fabs(df.x) < 0.1f ) ){ type = MOVE_Z; return true; } 99 | else if ( (df.x<0.5f) && (df.z<0.5f) && (df.x>0) && (df.z>0)) { type = MOVE_XZ; return true; } 100 | else { 101 | 102 | //plan 2 : X/Y 103 | df = RayTrace2(rayOrigin, rayDir, GetTransformedVector(2), mt, trss, false); 104 | 105 | if ( ( df.x >= 0 ) && (df.x <= 1) && ( fabs(df.y) < 0.1f ) ) { type = MOVE_X; return true; } 106 | if ( ( df.y >= 0 ) && (df.y <= 1) && ( fabs(df.x) < 0.1f ) ) { type = MOVE_Y; return true; } 107 | else if ( (df.x<0.5f) && (df.y<0.5f) && (df.x>0) && (df.y>0)) { type = MOVE_XY; return true; } 108 | else 109 | { 110 | //plan 3: Y/Z 111 | df = RayTrace2(rayOrigin, rayDir, GetTransformedVector(0), mt, trss, false); 112 | 113 | if ( ( df.y >= 0 ) && (df.y <= 1) && ( fabs(df.z) < 0.1f ) ) { type = MOVE_Y; return true; } 114 | else if ( ( df.z >= 0 ) && (df.z <= 1) && ( fabs(df.y) < 0.1f ) ) { type = MOVE_Z; return true; } 115 | else if ( (df.y<0.5f) && (df.z<0.5f) && (df.y>0) && (df.z>0)) { type = MOVE_YZ; return true; } 116 | 117 | } 118 | } 119 | 120 | type = MOVE_NONE; 121 | return false; 122 | } 123 | 124 | 125 | bool CGizmoTransformMove::OnMouseDown(unsigned int x, unsigned int y) 126 | { 127 | if (m_pMatrix) 128 | { 129 | return GetOpType(m_MoveType, x, y); 130 | } 131 | 132 | m_MoveType = MOVE_NONE; 133 | return false; 134 | } 135 | 136 | 137 | void CGizmoTransformMove::OnMouseMove(unsigned int x, unsigned int y) 138 | { 139 | if (m_MoveType != MOVE_NONE) 140 | { 141 | tvector3 rayOrigin,rayDir,df, inters; 142 | 143 | BuildRay(x, y, rayOrigin, rayDir); 144 | m_plan.RayInter(inters,rayOrigin,rayDir); 145 | 146 | tvector3 axeX(1,0,0),axeY(0,1,0),axeZ(0,0,1); 147 | 148 | 149 | if (mLocation == LOCATE_LOCAL) 150 | { 151 | axeX.TransformVector(*m_pMatrix); 152 | axeY.TransformVector(*m_pMatrix); 153 | axeZ.TransformVector(*m_pMatrix); 154 | axeX.Normalize(); 155 | axeY.Normalize(); 156 | axeZ.Normalize(); 157 | } 158 | 159 | df = inters - m_LockVertex; 160 | 161 | switch (m_MoveType) 162 | { 163 | case MOVE_XZ: df = tvector3(df.Dot(axeX) , 0,df.Dot(axeZ)); break; 164 | case MOVE_X: df = tvector3(df.Dot(axeX) , 0,0); break; 165 | case MOVE_Z: df = tvector3(0, 0,df.Dot(axeZ)); break; 166 | case MOVE_XY: df = tvector3(df.Dot(axeX) ,df.Dot(axeY), 0); break; 167 | case MOVE_YZ: df = tvector3(0,df.Dot(axeY) ,df.Dot(axeZ)); break; 168 | case MOVE_Y: df = tvector3(0,df.Dot(axeY), 0); break; 169 | } 170 | 171 | tvector3 adf; 172 | 173 | tmatrix mt; 174 | if (m_bUseSnap) 175 | { 176 | SnapIt(df.x,m_MoveSnap.x); 177 | SnapIt(df.y,m_MoveSnap.y); 178 | SnapIt(df.z,m_MoveSnap.z); 179 | } 180 | 181 | adf = df.x*axeX + df.y*axeY + df.z*axeZ; 182 | 183 | mt.Translation(adf); 184 | *m_pMatrix = m_svgMatrix; 185 | m_pMatrix->Multiply(mt); 186 | //if (mTransform) mTransform->Update(); 187 | 188 | if (mEditPos) 189 | *mEditPos = m_pMatrix->V4.position; 190 | } 191 | else 192 | { 193 | // predict move 194 | if (m_pMatrix) 195 | { 196 | GetOpType(m_MoveTypePredict, x, y); 197 | } 198 | } 199 | 200 | } 201 | 202 | void CGizmoTransformMove::OnMouseUp(unsigned int x, unsigned int y) 203 | { 204 | m_MoveType = MOVE_NONE; 205 | } 206 | 207 | void CGizmoTransformMove::Draw() 208 | { 209 | ComputeScreenFactor(); 210 | 211 | if (m_pMatrix) 212 | { 213 | 214 | //glDisable(GL_DEPTH_TEST); 215 | tvector3 orig = m_pMatrix->GetTranslation(); 216 | 217 | tvector3 axeX(1,0,0),axeY(0,1,0),axeZ(0,0,1); 218 | 219 | 220 | if (mLocation == LOCATE_LOCAL) 221 | { 222 | axeX.TransformVector(*m_pMatrix); 223 | axeY.TransformVector(*m_pMatrix); 224 | axeZ.TransformVector(*m_pMatrix); 225 | axeX.Normalize(); 226 | axeY.Normalize(); 227 | axeZ.Normalize(); 228 | } 229 | 230 | 231 | 232 | 233 | DrawQuad(orig, 0.5f*GetScreenFactor(), (m_MoveTypePredict == MOVE_XZ), axeX, axeZ); 234 | DrawQuad(orig, 0.5f*GetScreenFactor(), (m_MoveTypePredict == MOVE_XY), axeX, axeY); 235 | DrawQuad(orig, 0.5f*GetScreenFactor(), (m_MoveTypePredict == MOVE_YZ), axeY, axeZ); 236 | 237 | axeX*=GetScreenFactor(); 238 | axeY*=GetScreenFactor(); 239 | axeZ*=GetScreenFactor(); 240 | 241 | // plan1 242 | if (m_MoveTypePredict != MOVE_X) DrawAxis(orig,axeX,axeY,axeZ,0.05f,0.83f,vector4(1,0,0,1)); 243 | else DrawAxis(orig,axeX,axeY,axeZ, 0.05f,0.83f,vector4(1,1,1,1)); 244 | 245 | //plan2 246 | if (m_MoveTypePredict != MOVE_Y) DrawAxis(orig,axeY,axeX,axeZ, 0.05f,0.83f,vector4(0,1,0,1)); 247 | else DrawAxis(orig,axeY,axeX,axeZ, 0.05f,0.83f,vector4(1,1,1,1)); 248 | 249 | //plan3 250 | if (m_MoveTypePredict != MOVE_Z) DrawAxis(orig,axeZ,axeX,axeY, 0.05f,0.83f,vector4(0,0,1,1)); 251 | else DrawAxis(orig,axeZ,axeX,axeY, 0.05f,0.83f,vector4(1,1,1,1)); 252 | #if 0 253 | #ifdef WIN32 254 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 255 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE ); 256 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 257 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHATESTENABLE , FALSE); 258 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZWRITEENABLE , TRUE); 259 | #endif 260 | extern RenderingState_t GRenderingState; 261 | GRenderingState.mAlphaTestEnable = 0; 262 | GRenderingState.mZWriteEnable = 1; 263 | GRenderingState.mBlending = 0; 264 | GRenderingState.mCulling = 0; 265 | GRenderingState.mZTestType = 1; 266 | #endif 267 | /* 268 | 269 | PSM_LVERTEX svVts[2]; 270 | svVts[0].x = ptd.x; 271 | svVts[0].y = ptd.y; 272 | svVts[0].z = ptd.z; 273 | svVts[0].diffuse = 0xFFFFFFFF; 274 | 275 | svVts[1].x = ptd.x+10; 276 | svVts[1].y = ptd.y+10; 277 | svVts[1].z = ptd.z+10; 278 | svVts[1].diffuse = 0xFFFFFFFF; 279 | 280 | 281 | IDirect3DDevice9 *pDev = ((PSM_D3D9RenderDevice*)PSM_D3D9RenderDevice::GetInterfacePtr())->d3dDevice; 282 | pDev->DrawPrimitiveUP(D3DPT_LINESTRIP , 1, svVts, sizeof(PSM_LVERTEX)); 283 | */ 284 | } 285 | /* 286 | // debug 287 | glPointSize(20); 288 | glBegin(GL_POINTS); 289 | glVertex3fv(&ptd.x); 290 | glEnd(); 291 | 292 | glEnable(GL_DEPTH_TEST); 293 | */ 294 | 295 | } 296 | 297 | void CGizmoTransformMove::ApplyTransform(tvector3& trans, bool bAbsolute) 298 | { 299 | if (bAbsolute) 300 | { 301 | m_pMatrix->m16[12] = trans.x; 302 | m_pMatrix->m16[13] = trans.y; 303 | m_pMatrix->m16[14] = trans.z; 304 | } 305 | else 306 | { 307 | m_pMatrix->m16[12] += trans.x; 308 | m_pMatrix->m16[13] += trans.y; 309 | m_pMatrix->m16[14] += trans.z; 310 | } 311 | } 312 | 313 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformMove.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | 31 | #ifndef GIZMOTRANSFORMMOVE_H__ 32 | #define GIZMOTRANSFORMMOVE_H__ 33 | 34 | 35 | #include "GizmoTransform.h" 36 | 37 | class CGizmoTransformMove : public CGizmoTransform 38 | { 39 | 40 | public: 41 | CGizmoTransformMove(); 42 | virtual ~CGizmoTransformMove(); 43 | 44 | // return true if gizmo transform capture mouse 45 | virtual bool OnMouseDown(unsigned int x, unsigned int y); 46 | virtual void OnMouseMove(unsigned int x, unsigned int y); 47 | virtual void OnMouseUp(unsigned int x, unsigned int y); 48 | 49 | virtual void Draw(); 50 | // snap 51 | 52 | virtual void SetSnap(float snapx, float snapy, float snapz) 53 | { 54 | m_MoveSnap = tvector3(snapx, snapy, snapz); 55 | } 56 | virtual void SetSnap(const float snap) {} 57 | 58 | tvector3 GetMoveSnap() 59 | { 60 | return m_MoveSnap; 61 | } 62 | 63 | virtual void ApplyTransform(tvector3& trans, bool bAbsolute); 64 | 65 | protected: 66 | enum MOVETYPE 67 | { 68 | MOVE_NONE, 69 | MOVE_X, 70 | MOVE_Y, 71 | MOVE_Z, 72 | MOVE_XY, 73 | MOVE_XZ, 74 | MOVE_YZ, 75 | MOVE_XYZ 76 | }; 77 | MOVETYPE m_MoveType,m_MoveTypePredict; 78 | //tplane m_plan; 79 | //tvector3 m_LockVertex; 80 | tvector3 m_MoveSnap; 81 | 82 | bool GetOpType(MOVETYPE &type, unsigned int x, unsigned int y); 83 | tvector3 RayTrace(tvector3& rayOrigin, tvector3& rayDir, tvector3& norm); 84 | }; 85 | 86 | #endif // !defined(AFX_GIZMOTRANSFORMMOVE_H__8276C568_C663_463C_AE7F_B913E2A712A4__INCLUDED_) 87 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformRender.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #include "stdafx.h" 31 | #include "GizmoTransformRender.h" 32 | #ifdef MAC_OS 33 | #import 34 | #else 35 | #include 36 | #endif 37 | 38 | void CGizmoTransformRender::DrawCircle(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx,const tvector3 &vty) 39 | { 40 | glDisable(GL_DEPTH_TEST); 41 | glDisable(GL_LIGHTING); 42 | glColor4f(r,g,b,1); 43 | 44 | glBegin(GL_LINE_LOOP); 45 | for (int i = 0; i < 50 ; i++) 46 | { 47 | tvector3 vt; 48 | vt = vtx * cos((2*ZPI/50)*i); 49 | vt += vty * sin((2*ZPI/50)*i); 50 | vt += orig; 51 | glVertex3f(vt.x,vt.y,vt.z); 52 | } 53 | glEnd(); 54 | } 55 | 56 | 57 | void CGizmoTransformRender::DrawCircleHalf(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx,const tvector3 &vty,tplane &camPlan) 58 | { 59 | glDisable(GL_DEPTH_TEST); 60 | glDisable(GL_LIGHTING); 61 | glColor4f(r,g,b,1); 62 | 63 | glBegin(GL_LINE_STRIP); 64 | for (int i = 0; i < 30 ; i++) 65 | { 66 | tvector3 vt; 67 | vt = vtx * cos((ZPI/30)*i); 68 | vt += vty * sin((ZPI/30)*i); 69 | vt +=orig; 70 | if (camPlan.DotNormal(vt)) 71 | glVertex3f(vt.x,vt.y,vt.z); 72 | } 73 | glEnd(); 74 | } 75 | 76 | void CGizmoTransformRender::DrawAxis(const tvector3 &orig, const tvector3 &axis, const tvector3 &vtx,const tvector3 &vty, float fct,float fct2,const tvector4 &col) 77 | { 78 | glDisable(GL_DEPTH_TEST); 79 | glDisable(GL_LIGHTING); 80 | glColor4fv(&col.x); 81 | glBegin(GL_LINES); 82 | glVertex3fv(&orig.x); 83 | glVertex3f(orig.x+axis.x,orig.y+axis.y,orig.z+axis.z); 84 | glEnd(); 85 | 86 | glBegin(GL_TRIANGLE_FAN); 87 | for (int i=0;i<=30;i++) 88 | { 89 | tvector3 pt; 90 | pt = vtx * cos(((2*ZPI)/30.0f)*i)*fct; 91 | pt+= vty * sin(((2*ZPI)/30.0f)*i)*fct; 92 | pt+=axis*fct2; 93 | pt+=orig; 94 | glVertex3fv(&pt.x); 95 | pt = vtx * cos(((2*ZPI)/30.0f)*(i+1))*fct; 96 | pt+= vty * sin(((2*ZPI)/30.0f)*(i+1))*fct; 97 | pt+=axis*fct2; 98 | pt+=orig; 99 | glVertex3fv(&pt.x); 100 | glVertex3f(orig.x+axis.x,orig.y+axis.y,orig.z+axis.z); 101 | 102 | } 103 | glEnd(); 104 | 105 | } 106 | 107 | void CGizmoTransformRender::DrawCamem(const tvector3& orig,const tvector3& vtx,const tvector3& vty,float ng) 108 | { 109 | glDisable(GL_DEPTH_TEST); 110 | glDisable(GL_LIGHTING); 111 | int i = 0 ; 112 | glEnable(GL_BLEND); 113 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 114 | 115 | glDisable(GL_CULL_FACE); 116 | 117 | 118 | glColor4f(1,1,0,0.5f); 119 | glBegin(GL_TRIANGLE_FAN); 120 | glVertex3fv(&orig.x); 121 | for (i = 0 ; i <= 50 ; i++) 122 | { 123 | tvector3 vt; 124 | vt = vtx * cos(((ng)/50)*i); 125 | vt += vty * sin(((ng)/50)*i); 126 | vt+=orig; 127 | glVertex3f(vt.x,vt.y,vt.z); 128 | } 129 | glEnd(); 130 | 131 | glDisable(GL_BLEND); 132 | 133 | 134 | glColor4f(1,1,0.2f,1); 135 | tvector3 vt; 136 | glBegin(GL_LINE_LOOP); 137 | 138 | glVertex3fv(&orig.x); 139 | for ( i = 0 ; i <= 50 ; i++) 140 | { 141 | tvector3 vt; 142 | vt = vtx * cos(((ng)/50)*i); 143 | vt += vty * sin(((ng)/50)*i); 144 | vt+=orig; 145 | glVertex3f(vt.x,vt.y,vt.z); 146 | } 147 | 148 | glEnd(); 149 | } 150 | 151 | void CGizmoTransformRender::DrawQuad(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3 &axisV) 152 | { 153 | glDisable(GL_DEPTH_TEST); 154 | glDisable(GL_LIGHTING); 155 | glEnable(GL_BLEND); 156 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 157 | 158 | glDisable(GL_CULL_FACE); 159 | 160 | tvector3 pts[4]; 161 | pts[0] = orig; 162 | pts[1] = orig + (axisU * size); 163 | pts[2] = orig + (axisU + axisV)*size; 164 | pts[3] = orig + (axisV * size); 165 | 166 | if (!bSelected) 167 | glColor4f(1,1,0,0.5f); 168 | else 169 | glColor4f(1,1,1,0.6f); 170 | 171 | glBegin(GL_QUADS); 172 | glVertex3fv(&pts[0].x); 173 | glVertex3fv(&pts[1].x); 174 | glVertex3fv(&pts[2].x); 175 | glVertex3fv(&pts[3].x); 176 | glEnd(); 177 | 178 | if (!bSelected) 179 | glColor4f(1,1,0.2f,1); 180 | else 181 | glColor4f(1,1,1,0.6f); 182 | 183 | glBegin(GL_LINE_STRIP); 184 | glVertex3fv(&pts[0].x); 185 | glVertex3fv(&pts[1].x); 186 | glVertex3fv(&pts[2].x); 187 | glVertex3fv(&pts[3].x); 188 | glEnd(); 189 | 190 | glDisable(GL_BLEND); 191 | } 192 | 193 | 194 | void CGizmoTransformRender::DrawTri(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3& axisV) 195 | { 196 | glDisable(GL_DEPTH_TEST); 197 | glDisable(GL_LIGHTING); 198 | glEnable(GL_BLEND); 199 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 200 | 201 | glDisable(GL_CULL_FACE); 202 | 203 | tvector3 pts[3]; 204 | pts[0] = orig; 205 | 206 | pts[1] = (axisU ); 207 | pts[2] = (axisV ); 208 | 209 | pts[1]*=size; 210 | pts[2]*=size; 211 | pts[1]+=orig; 212 | pts[2]+=orig; 213 | 214 | if (!bSelected) 215 | glColor4f(1,1,0,0.5f); 216 | else 217 | glColor4f(1,1,1,0.6f); 218 | 219 | glBegin(GL_TRIANGLES); 220 | glVertex3fv(&pts[0].x); 221 | glVertex3fv(&pts[1].x); 222 | glVertex3fv(&pts[2].x); 223 | glVertex3fv(&pts[3].x); 224 | glEnd(); 225 | 226 | if (!bSelected) 227 | glColor4f(1,1,0.2f,1); 228 | else 229 | glColor4f(1,1,1,0.6f); 230 | 231 | glBegin(GL_LINE_STRIP); 232 | glVertex3fv(&pts[0].x); 233 | glVertex3fv(&pts[1].x); 234 | glVertex3fv(&pts[2].x); 235 | glEnd(); 236 | 237 | glDisable(GL_BLEND); 238 | } 239 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformRender.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | 31 | #ifndef GIZMOTRANSFORMRENDER_H__ 32 | #define GIZMOTRANSFORMRENDER_H__ 33 | 34 | typedef tvector4 tplane; 35 | 36 | class CGizmoTransformRender 37 | { 38 | public: 39 | CGizmoTransformRender() {} 40 | virtual ~CGizmoTransformRender() {} 41 | 42 | static void DrawCircle(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx,const tvector3 &vty); 43 | static void DrawCircleHalf(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx,const tvector3 &vty,tplane &camPlan); 44 | static void DrawAxis(const tvector3 &orig, const tvector3 &axis, const tvector3 &vtx,const tvector3 &vty, float fct,float fct2,const tvector4 &col); 45 | static void DrawCamem(const tvector3& orig,const tvector3& vtx,const tvector3& vty,float ng); 46 | static void DrawQuad(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3 &axisV); 47 | static void DrawTri(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3& axisV); 48 | }; 49 | 50 | #endif // !defined(AFX_GIZMOTRANSFORMRENDER_H__549F6E7A_D46D_4B18_9E74_76B7E43A3841__INCLUDED_) 51 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformRenderDX9.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // Zenith Engine 3 | // File Name : GizmoTransform.h 4 | // Creation : 12/07/2007 5 | // Author : Cedric Guillemet 6 | // Description : 7 | // 8 | /////////////////////////////////////////////////////////////////////////////////////////////////// 9 | // 10 | // This program is free software; you can redistribute it and/or modify 11 | // it under the terms of the GNU General Public License as published by 12 | // the Free Software Foundation; version 2 of the License. 13 | // 14 | // This program is distributed in the hope that it will be useful, 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | // GNU General Public License for more details. 18 | // 19 | /////////////////////////////////////////////////////////////////////////////////////////////////// 20 | 21 | #include "stdafx.h" 22 | #include "GizmoTransformRender.h" 23 | #include 24 | 25 | #ifdef WIN32 26 | typedef struct { 27 | float x; 28 | float y; 29 | float z; 30 | 31 | tulong diffuse; 32 | } PSM_LVERTEX; 33 | IDirect3DVertexDeclaration9* GGizmoVertDecl = NULL; 34 | #endif 35 | void InitDecl() 36 | { 37 | #ifdef WIN32 38 | if (!GGizmoVertDecl) 39 | { 40 | D3DVERTEXELEMENT9 decl1[] = 41 | { 42 | { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, 43 | { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, 44 | D3DDECL_END() 45 | }; 46 | 47 | GDD->GetD3D9Device()->CreateVertexDeclaration( decl1, &GGizmoVertDecl ); 48 | } 49 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_FALSE); 50 | GDD->GetD3D9Device()->SetTexture(0,NULL); 51 | GDD->GetD3D9Device()->SetTexture(1,NULL); 52 | GDD->GetD3D9Device()->SetTexture(2,NULL); 53 | GDD->GetD3D9Device()->SetTexture(3,NULL); 54 | GDD->GetD3D9Device()->SetTexture(4,NULL); 55 | GDD->GetD3D9Device()->SetTexture(5,NULL); 56 | GDD->GetD3D9Device()->SetTexture(6,NULL); 57 | GDD->GetD3D9Device()->SetTexture(7,NULL); 58 | GDD->GetD3D9Device()->SetRenderState(D3DRS_COLORVERTEX , true); 59 | //GDD->GetD3D9Device()->SetRenderState(D3DRS_LIGHTING , true); 60 | GDD->GetD3D9Device()->SetRenderState(D3DRS_AMBIENT , 0xFFFFFFFF); 61 | GDD->GetD3D9Device()->SetVertexShader(NULL); 62 | GDD->GetD3D9Device()->SetPixelShader(NULL); 63 | 64 | 65 | #endif 66 | } 67 | void CGizmoTransformRender::DrawCircle(const tvector3 &orig,float r,float g,float b, const tvector3 &vtx, const tvector3 &vty) 68 | { 69 | #ifdef WIN32 70 | InitDecl(); 71 | PSM_LVERTEX svVts[51]; 72 | for (int i = 0; i <= 50 ; i++) 73 | { 74 | tvector3 vt; 75 | vt = vtx * cos((2*ZPI/50)*i); 76 | vt += vty * sin((2*ZPI/50)*i); 77 | vt += orig; 78 | svVts[i].diffuse = 0xFF000000 + (int(r*255.0f) << 16) + (int(g*255.0f) << 8) + (int(b*255.0f)); 79 | svVts[i].x = vt.x; 80 | svVts[i].y = vt.y; 81 | svVts[i].z = vt.z; 82 | } 83 | IDirect3DDevice9 *pDev = GDD->GetD3D9Device(); 84 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 85 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_LINESTRIP , 50, svVts, sizeof(PSM_LVERTEX)); 86 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 87 | #endif 88 | } 89 | 90 | 91 | void CGizmoTransformRender::DrawCircleHalf(const tvector3 &orig,float r,float g,float b,const tvector3 &vtx, const tvector3 &vty, tplane &camPlan) 92 | { 93 | #ifdef WIN32 94 | InitDecl(); 95 | int inc = 0; 96 | PSM_LVERTEX svVts[51]; 97 | for (int i = 0; i < 30 ; i++) 98 | { 99 | tvector3 vt; 100 | vt = vtx * cos((ZPI/30)*i); 101 | vt += vty * sin((ZPI/30)*i); 102 | vt +=orig; 103 | if (camPlan.DotNormal(vt)) 104 | { 105 | svVts[inc].diffuse = 0xFF000000 + (int(r*255.0f) << 16) + (int(g*255.0f) << 8) + (int(b*255.0f)); 106 | svVts[inc].x = vt.x; 107 | svVts[inc].y = vt.y; 108 | svVts[inc].z = vt.z; 109 | inc ++; 110 | } 111 | } 112 | IDirect3DDevice9 *pDev = GDD->GetD3D9Device(); 113 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 114 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_LINESTRIP , inc-1, svVts, sizeof(PSM_LVERTEX)); 115 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 116 | #endif 117 | } 118 | 119 | void CGizmoTransformRender::DrawAxis(const tvector3 &orig, const tvector3 &axis, const tvector3 &vtx, const tvector3 &vty, float fct,float fct2, const tvector4 &col) 120 | { 121 | #ifdef WIN32 122 | InitDecl(); 123 | PSM_LVERTEX svVts[100]; 124 | int inc = 0; 125 | IDirect3DDevice9 *pDev = GDD->GetD3D9Device(); 126 | 127 | svVts[0].x = orig.x; 128 | svVts[0].y = orig.y; 129 | svVts[0].z = orig.z; 130 | svVts[0].diffuse = 0xFF000000 + (int(col.x*255.0f) << 16) + (int(col.y*255.0f) << 8) + (int(col.z*255.0f)); 131 | 132 | svVts[1].x = orig.x + axis.x; 133 | svVts[1].y = orig.y + axis.y; 134 | svVts[1].z = orig.z + axis.z; 135 | svVts[1].diffuse = svVts[0].diffuse; 136 | 137 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 138 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_LINELIST , 1, svVts, sizeof(PSM_LVERTEX)); 139 | 140 | //glBegin(GL_TRIANGLE_FAN); 141 | for (int i=0;i<=30;i++) 142 | { 143 | tvector3 pt; 144 | pt = vtx * cos(((2*ZPI)/30.0f)*i)*fct; 145 | pt+= vty * sin(((2*ZPI)/30.0f)*i)*fct; 146 | pt+=axis*fct2; 147 | pt+=orig; 148 | //glVertex3fv(&pt.x); 149 | 150 | svVts[inc].x = pt.x; 151 | svVts[inc].y = pt.y; 152 | svVts[inc].z = pt.z; 153 | svVts[inc++].diffuse = svVts[0].diffuse; 154 | 155 | pt = vtx * cos(((2*ZPI)/30.0f)*(i+1))*fct; 156 | pt+= vty * sin(((2*ZPI)/30.0f)*(i+1))*fct; 157 | pt+=axis*fct2; 158 | pt+=orig; 159 | //glVertex3fv(&pt.x); 160 | 161 | svVts[inc].x = pt.x; 162 | svVts[inc].y = pt.y; 163 | svVts[inc].z = pt.z; 164 | svVts[inc++].diffuse = svVts[0].diffuse; 165 | 166 | //glVertex3f(orig.x+axis.x,orig.y+axis.y,orig.z+axis.z); 167 | 168 | svVts[inc].x = orig.x+axis.x; 169 | svVts[inc].y = orig.y+axis.y; 170 | svVts[inc].z = orig.z+axis.z; 171 | svVts[inc++].diffuse = svVts[0].diffuse; 172 | 173 | } 174 | //glEnd(); 175 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 176 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN , 91, svVts, sizeof(PSM_LVERTEX)); 177 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 178 | #endif 179 | } 180 | 181 | void CGizmoTransformRender::DrawCamem(const tvector3& orig,const tvector3& vtx,const tvector3& vty,float ng) 182 | { 183 | #ifdef WIN32 184 | InitDecl(); 185 | IDirect3DDevice9 *pDev = GDD->GetD3D9Device(); 186 | GDD->GetD3D9Device()->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCALPHA); 187 | GDD->GetD3D9Device()->SetRenderState(D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA); 188 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 189 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE ); 190 | 191 | PSM_LVERTEX svVts[52]; 192 | 193 | svVts[0].x = orig.x; 194 | svVts[0].y = orig.y; 195 | svVts[0].z = orig.z; 196 | svVts[0].diffuse = 0x80FFFF00; 197 | 198 | for (int i = 0 ; i <= 50 ; i++) 199 | { 200 | tvector3 vt; 201 | vt = vtx * cos(((ng)/50)*i); 202 | vt += vty * sin(((ng)/50)*i); 203 | vt+=orig; 204 | //glVertex3f(vt.x,vt.y,vt.z); 205 | 206 | svVts[i+1].x = vt.x; 207 | svVts[i+1].y = vt.y; 208 | svVts[i+1].z = vt.z; 209 | svVts[i+1].diffuse = svVts[0].diffuse; 210 | } 211 | 212 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 213 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN , 50, svVts, sizeof(PSM_LVERTEX)); 214 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 215 | 216 | for (int i = 0 ; i < 52 ; i++) 217 | svVts[i].diffuse = 0xFFFFFF33; 218 | 219 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 220 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_LINESTRIP , 50, svVts, sizeof(PSM_LVERTEX)); 221 | 222 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_CCW ); 223 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 224 | #endif 225 | } 226 | 227 | void CGizmoTransformRender::DrawQuad(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3 &axisV) 228 | { 229 | #ifdef WIN32 230 | InitDecl(); 231 | IDirect3DDevice9 *pDev = GDD->GetD3D9Device(); 232 | GDD->GetD3D9Device()->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCALPHA); 233 | GDD->GetD3D9Device()->SetRenderState(D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA); 234 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 235 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE ); 236 | 237 | PSM_LVERTEX svVts[5]; 238 | svVts[0].x = orig.x; 239 | svVts[0].y = orig.y; 240 | svVts[0].z = orig.z; 241 | svVts[1].x = orig.x + (axisU.x * size); 242 | svVts[1].y = orig.y + (axisU.y * size); 243 | svVts[1].z = orig.z + (axisU.z * size); 244 | 245 | svVts[2].x = orig.x + (axisV.x * size); 246 | svVts[2].y = orig.y + (axisV.y * size); 247 | svVts[2].z = orig.z + (axisV.z * size); 248 | svVts[3].x = orig.x + (axisU.x + axisV.x)*size; 249 | svVts[3].y = orig.y + (axisU.y + axisV.y)*size; 250 | svVts[3].z = orig.z + (axisU.z + axisV.z)*size; 251 | svVts[4].x = orig.x; 252 | svVts[4].y = orig.y; 253 | svVts[4].z = orig.z; 254 | 255 | 256 | if (!bSelected) 257 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0x80FFFF00; 258 | else 259 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0xA0FFFFFF; 260 | 261 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 262 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP , 2, svVts, sizeof(PSM_LVERTEX)); 263 | 264 | if (!bSelected) 265 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0xFFFFFF30; 266 | else 267 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0xA0FFFF90; 268 | 269 | 270 | svVts[3].x = orig.x + (axisV.x * size); 271 | svVts[3].y = orig.y + (axisV.y * size); 272 | svVts[3].z = orig.z + (axisV.z * size); 273 | svVts[2].x = orig.x + (axisU.x + axisV.x)*size; 274 | svVts[2].y = orig.y + (axisU.y + axisV.y)*size; 275 | svVts[2].z = orig.z + (axisU.z + axisV.z)*size; 276 | 277 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 278 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_LINESTRIP , 4, svVts, sizeof(PSM_LVERTEX)); 279 | 280 | 281 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 282 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_CCW ); 283 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 284 | #endif 285 | } 286 | 287 | 288 | void CGizmoTransformRender::DrawTri(const tvector3& orig, float size, bool bSelected, const tvector3& axisU, const tvector3& axisV) 289 | { 290 | #ifdef WIN32 291 | InitDecl(); 292 | IDirect3DDevice9 *pDev = GDD->GetD3D9Device(); 293 | GDD->GetD3D9Device()->SetRenderState(D3DRS_SRCBLEND , D3DBLEND_SRCALPHA); 294 | GDD->GetD3D9Device()->SetRenderState(D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA); 295 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); 296 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE ); 297 | 298 | 299 | tvector3 pts[3]; 300 | pts[0] = orig; 301 | 302 | pts[1] = (axisU ); 303 | pts[2] = (axisV ); 304 | 305 | pts[1]*=size; 306 | pts[2]*=size; 307 | pts[1]+=orig; 308 | pts[2]+=orig; 309 | 310 | 311 | PSM_LVERTEX svVts[4]; 312 | svVts[0].x = pts[0].x; 313 | svVts[0].y = pts[0].y; 314 | svVts[0].z = pts[0].z; 315 | svVts[1].x = pts[1].x; 316 | svVts[1].y = pts[1].y; 317 | svVts[1].z = pts[1].z; 318 | svVts[2].x = pts[2].x; 319 | svVts[2].y = pts[2].y; 320 | svVts[2].z = pts[2].z; 321 | svVts[3].x = pts[0].x; 322 | svVts[3].y = pts[0].y; 323 | svVts[3].z = pts[0].z; 324 | 325 | 326 | 327 | if (!bSelected) 328 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0x80FFFF00; 329 | else 330 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0xA0FFFFFF; 331 | 332 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 333 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_TRIANGLELIST , 1, svVts, sizeof(PSM_LVERTEX)); 334 | 335 | if (!bSelected) 336 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0xFFFFFF30; 337 | else 338 | svVts[0].diffuse = svVts[1].diffuse = svVts[2].diffuse = svVts[3].diffuse = svVts[4].diffuse = 0xA0FFFF90; 339 | 340 | GDD->GetD3D9Device()->SetVertexDeclaration(GGizmoVertDecl); 341 | GDD->GetD3D9Device()->DrawPrimitiveUP(D3DPT_LINESTRIP , 3, svVts, sizeof(PSM_LVERTEX)); 342 | 343 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 344 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_CCW ); 345 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 346 | #endif 347 | } -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformRotate.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #include "stdafx.h" 31 | #include "GizmoTransformRotate.h" 32 | #ifdef MAC_OS 33 | #import 34 | #else 35 | #include 36 | #endif 37 | 38 | ////////////////////////////////////////////////////////////////////// 39 | // Construction/Destruction 40 | ////////////////////////////////////////////////////////////////////// 41 | extern tvector3 ptd; 42 | 43 | 44 | IGizmo *CreateRotateGizmo() 45 | { 46 | return new CGizmoTransformRotate; 47 | } 48 | 49 | 50 | CGizmoTransformRotate::CGizmoTransformRotate() : CGizmoTransform() 51 | { 52 | m_RotateType = ROTATE_NONE; 53 | m_RotateTypePredict = ROTATE_NONE; 54 | m_Ng2 = 0; 55 | m_AngleSnap = 0.f; 56 | 57 | } 58 | 59 | CGizmoTransformRotate::~CGizmoTransformRotate() 60 | { 61 | 62 | } 63 | 64 | bool CGizmoTransformRotate::CheckRotatePlan(tvector3 &vNorm, float factor, 65 | const tvector3 &rayOrig,const tvector3 &rayDir,int id) 66 | { 67 | tvector3 df, inters; 68 | m_Axis2 = vNorm; 69 | m_plan=vector4(m_pMatrix->GetTranslation(), vNorm); 70 | m_plan.RayInter(inters,rayOrig,rayDir); 71 | ptd = inters; 72 | df = inters - m_pMatrix->GetTranslation(); 73 | df/=GetScreenFactor(); 74 | 75 | if ( ((df.Length()/factor) >0.9f) && ( (df.Length()/factor) < 1.1f) ) 76 | { 77 | m_svgMatrix = *m_pMatrix; 78 | 79 | m_LockVertex = df; 80 | m_LockVertex.Normalize(); 81 | 82 | m_Vtx = m_LockVertex; 83 | m_Vty.Cross(m_LockVertex,vNorm); 84 | m_Vty.Normalize(); 85 | m_Vtx *= factor; 86 | m_Vty *= factor; 87 | m_Vtz.Cross(m_Vtx,m_Vty); 88 | m_Ng2 = 0; 89 | if (id!=-1) 90 | m_Axis = GetVector(id); 91 | 92 | m_OrigScale.Scaling(GetTransformedVector(0).Length(), 93 | GetTransformedVector(1).Length(), 94 | GetTransformedVector(2).Length()); 95 | 96 | m_InvOrigScale.Inverse(m_OrigScale); 97 | 98 | return true; 99 | } 100 | return false; 101 | } 102 | 103 | bool CGizmoTransformRotate::GetOpType(ROTATETYPE &type, unsigned int x, unsigned int y) 104 | { 105 | tvector3 rayOrigin,rayDir, axis; 106 | tvector3 dir = m_pMatrix->GetTranslation()-m_CamSrc; 107 | dir.Normalize(); 108 | 109 | BuildRay(x, y, rayOrigin, rayDir); 110 | 111 | if (mMask&AXIS_TRACKBALL) 112 | if (CheckRotatePlan(dir,1.0f,rayOrigin,rayDir,-1)) 113 | { 114 | tmatrix mt = *m_pMatrix; 115 | mt.NoTrans(); 116 | //mt.Inverse(); 117 | //m_Axis = m_Axis2 = dir; 118 | m_Axis.TransformPoint(mt); 119 | /* 120 | m_Axis *=tvector3(GetTransformedVector(0).Length(), 121 | GetTransformedVector(1).Length(), 122 | GetTransformedVector(2).Length()); 123 | */ 124 | type = ROTATE_TWIN; 125 | return true; 126 | } 127 | 128 | // plan 1 : X/Z 129 | m_Axis = GetTransformedVector(0); 130 | if (mMask&AXIS_X) 131 | if (CheckRotatePlan(m_Axis,1.0f,rayOrigin,rayDir,0)) { type = ROTATE_X; return true; } 132 | m_Axis = GetTransformedVector(1); 133 | if (mMask&AXIS_Y) 134 | if (CheckRotatePlan(m_Axis,1.0f,rayOrigin,rayDir,1)) { type = ROTATE_Y; return true; } 135 | m_Axis = GetTransformedVector(2); 136 | if (mMask&AXIS_Z) 137 | if (CheckRotatePlan(m_Axis,1.0f,rayOrigin,rayDir,2)) { type = ROTATE_Z; return true; } 138 | 139 | //m_Axis = GetTransformedVector(dir); 140 | if (mMask&AXIS_SCREEN) 141 | if (CheckRotatePlan(dir,1.2f,rayOrigin,rayDir,-1)) 142 | { 143 | tmatrix mt = *m_pMatrix; 144 | mt.NoTrans(); 145 | mt.Inverse(); 146 | m_Axis = m_Axis2 = dir; 147 | m_Axis.TransformPoint(mt); 148 | 149 | m_Axis *=tvector3(GetTransformedVector(0).Length(), 150 | GetTransformedVector(1).Length(), 151 | GetTransformedVector(2).Length()); 152 | 153 | type = ROTATE_SCREEN; 154 | return true; 155 | } 156 | 157 | type = ROTATE_NONE; 158 | 159 | return false; 160 | } 161 | 162 | bool CGizmoTransformRotate::OnMouseDown(unsigned int x, unsigned int y) 163 | { 164 | if (m_pMatrix) 165 | { 166 | return GetOpType(m_RotateType, x, y); 167 | } 168 | 169 | m_RotateType = ROTATE_NONE; 170 | return false; 171 | } 172 | 173 | 174 | void CGizmoTransformRotate::Rotate1Axe(const tvector3& rayOrigin,const tvector3& rayDir) 175 | { 176 | tvector3 inters; 177 | m_plan=vector4(m_pMatrix->GetTranslation(), m_Axis2); 178 | m_plan.RayInter(inters,rayOrigin,rayDir); 179 | ptd = inters; 180 | 181 | tvector3 df = inters - m_pMatrix->GetTranslation(); 182 | 183 | df.Normalize(); 184 | m_LockVertex2 = df; 185 | 186 | float acosng = df.Dot(m_LockVertex); 187 | if ( (acosng<-0.99999f) || (acosng> 0.99999f) ) 188 | m_Ng2 = 0.f; 189 | else 190 | m_Ng2 = (float)acos(acosng); 191 | 192 | if (df.Dot(m_Vty)>0) 193 | m_Ng2 = -m_Ng2; 194 | 195 | tmatrix mt,mt2; 196 | 197 | if (m_bUseSnap) 198 | { 199 | m_Ng2*=(360.0f/ZPI); 200 | SnapIt(m_Ng2,m_AngleSnap); 201 | m_Ng2/=(360.0f/ZPI); 202 | } 203 | 204 | mt.RotationAxis(m_Axis,m_Ng2); 205 | mt.Multiply(m_InvOrigScale); 206 | mt.Multiply(m_svgMatrix); 207 | mt2 = m_OrigScale; 208 | mt2.Multiply(mt); 209 | *m_pMatrix=mt2; 210 | 211 | if (m_Axis == tvector3::ZAxis) 212 | { 213 | if (mEditQT) 214 | { 215 | /* 216 | Dans le cadre du jeu, et vu les pb avec les quaternions, 217 | le 1er float du quaternion en stockage est l'angle en radian. 218 | le stockage reste un quaternion. 219 | il y a des pbs de conversion quaternion/matrix 220 | */ 221 | #if USE_QUATERNION 222 | tquaternion gth(*m_pMatrix); 223 | 224 | gth.Normalize(); 225 | gth.UnitInverse(); 226 | 227 | tquaternion qtg; 228 | qtg.RotationAxis(m_Axis,m_Ng2); 229 | *mEditQT = gth;//gth+qtg;//tquaternion(mt2); 230 | mEditQT->Normalize(); 231 | #else 232 | mEditQT->z = m_Ng2; 233 | #endif 234 | } 235 | } 236 | } 237 | 238 | void CGizmoTransformRotate::OnMouseMove(unsigned int x, unsigned int y) 239 | { 240 | tvector3 rayOrigin, rayDir, axis; 241 | 242 | BuildRay(x, y, rayOrigin, rayDir); 243 | 244 | if (m_RotateType != ROTATE_NONE) 245 | { 246 | if (m_RotateType == ROTATE_TWIN) 247 | { 248 | tvector3 inters; 249 | tvector3 dir = m_pMatrix->GetTranslation()-m_CamSrc; 250 | dir.Normalize(); 251 | 252 | m_plan=vector4(m_pMatrix->GetTranslation(), dir); 253 | m_plan.RayInter(inters,rayOrigin,rayDir); 254 | ptd = inters; 255 | tvector3 df = inters - m_pMatrix->GetTranslation(); 256 | df/=GetScreenFactor(); 257 | float lng1 = df.Length(); 258 | if (lng1 >= 1.0f) lng1 = 0.9f; 259 | 260 | float height = (float)sin(acos(lng1)); 261 | tvector3 m_CamRealUp,camRight; 262 | camRight.Cross(m_Axis,m_CamUp); 263 | m_CamRealUp.Cross(camRight,dir); 264 | 265 | tvector3 idt = height*dir; 266 | idt+=df; 267 | 268 | //ptd = idt; 269 | idt= m_LockVertex-idt; 270 | 271 | tmatrix mt,mt2; 272 | 273 | mt.LookAtLH(tvector3(0,0,0),idt,m_CamRealUp); 274 | mt.Multiply(m_InvOrigScale); 275 | mt.Multiply(m_svgMatrix); 276 | mt2 = m_OrigScale; 277 | mt2.Multiply(mt); 278 | *m_pMatrix=mt2; 279 | /* 280 | if (mEditQT) 281 | { 282 | *mEditQT = tquaternion(mt2); 283 | } 284 | */ 285 | } 286 | else 287 | { 288 | Rotate1Axe(rayOrigin, rayDir); 289 | 290 | } 291 | 292 | //if (mTransform) mTransform->Update(); 293 | } 294 | else 295 | { 296 | // predict move 297 | if (m_pMatrix) 298 | { 299 | GetOpType(m_RotateTypePredict, x, y); 300 | } 301 | } 302 | } 303 | 304 | void CGizmoTransformRotate::OnMouseUp(unsigned int x, unsigned int y) 305 | { 306 | m_RotateType = ROTATE_NONE; 307 | } 308 | /* 309 | char tmps[512]; 310 | sprintf(tmps, "%5.2f %5.2f %5.2f %5.2f", plCam.x, plCam.y, plCam.z, plCam.w ); 311 | MessageBoxA(NULL, tmps, tmps, MB_OK); 312 | */ 313 | void CGizmoTransformRotate::Draw() 314 | { 315 | if (m_pMatrix) 316 | { 317 | 318 | ComputeScreenFactor(); 319 | 320 | tvector3 right,up,frnt,dir; 321 | 322 | //glDisable(GL_DEPTH_TEST); 323 | tvector3 orig(m_pMatrix->GetTranslation()); 324 | 325 | tvector3 plnorm(m_CamSrc-orig); 326 | 327 | 328 | plnorm.Normalize(); 329 | 330 | 331 | 332 | 333 | 334 | tplane plCam = vector4(plnorm,0); 335 | 336 | 337 | dir = orig-m_CamSrc; 338 | dir.Normalize(); 339 | 340 | right.Cross(dir,GetTransformedVector(1)); 341 | right.Normalize(); 342 | 343 | up.Cross(dir,right); 344 | up.Normalize(); 345 | 346 | right.Cross(dir,up); 347 | right.Normalize(); 348 | 349 | tvector3 axeX(1,0,0),axeY(0,1,0),axeZ(0,0,1); 350 | 351 | 352 | if (mLocation == LOCATE_LOCAL) 353 | { 354 | axeX.TransformVector(*m_pMatrix); 355 | axeY.TransformVector(*m_pMatrix); 356 | axeZ.TransformVector(*m_pMatrix); 357 | axeX.Normalize(); 358 | axeY.Normalize(); 359 | axeZ.Normalize(); 360 | } 361 | 362 | // Twin 363 | if (mMask&AXIS_TRACKBALL) 364 | { 365 | 366 | if (m_RotateTypePredict != ROTATE_TWIN) 367 | DrawCircle(orig, 0.2f,0.2f,0.2f,right*GetScreenFactor(),up*GetScreenFactor()); 368 | else 369 | DrawCircle(orig, 1,1,1,right*GetScreenFactor(),up*GetScreenFactor()); 370 | } 371 | 372 | // Screen 373 | if (mMask&AXIS_SCREEN) 374 | { 375 | if (m_RotateTypePredict != ROTATE_SCREEN) 376 | DrawCircle(orig, 1.0f,0.3f,1.0f,up*1.2f*GetScreenFactor(),right*1.2f*GetScreenFactor()); 377 | else 378 | DrawCircle(orig, 1,1,1,up*1.2f*GetScreenFactor(),right*1.2f*GetScreenFactor()); 379 | } 380 | 381 | // X 382 | right.Cross(dir, axeX); 383 | right.Normalize(); 384 | frnt.Cross(right, axeX); 385 | 386 | frnt.Normalize(); 387 | 388 | if (mMask&AXIS_X) 389 | { 390 | if (m_RotateTypePredict != ROTATE_X) 391 | DrawCircleHalf(orig, 1,0,0,right*GetScreenFactor(),frnt*GetScreenFactor(),plCam); 392 | else 393 | DrawCircleHalf(orig, 1,1,1,right*GetScreenFactor(),frnt*GetScreenFactor(),plCam); 394 | } 395 | 396 | // Y 397 | 398 | right.Cross(dir, axeY); 399 | right.Normalize(); 400 | frnt.Cross(right, axeY); 401 | 402 | frnt.Normalize(); 403 | 404 | if (mMask&AXIS_Y) 405 | { 406 | 407 | if (m_RotateTypePredict != ROTATE_Y) 408 | DrawCircleHalf(orig, 0,1,0,right*GetScreenFactor(),frnt*GetScreenFactor(),plCam); 409 | else 410 | DrawCircleHalf(orig, 1,1,1,right*GetScreenFactor(),frnt*GetScreenFactor(),plCam); 411 | } 412 | 413 | // Z 414 | right.Cross(dir, axeZ); 415 | right.Normalize(); 416 | frnt.Cross(right, axeZ); 417 | 418 | frnt.Normalize(); 419 | 420 | if (mMask&AXIS_Z) 421 | { 422 | if (m_RotateTypePredict != ROTATE_Z) 423 | DrawCircleHalf(orig, 0,0,1,right*GetScreenFactor(),frnt*GetScreenFactor(),plCam); 424 | else 425 | DrawCircleHalf(orig, 1,1,1,right*GetScreenFactor(),frnt*GetScreenFactor(),plCam); 426 | } 427 | // camembert 428 | if ( (m_RotateType != ROTATE_NONE) && (m_RotateType != ROTATE_TWIN ) ) 429 | DrawCamem(orig,m_Vtx*GetScreenFactor(),m_Vty*GetScreenFactor(),-m_Ng2); 430 | /* 431 | // debug 432 | glPointSize(20); 433 | glBegin(GL_POINTS); 434 | glVertex3fv(&ptd.x); 435 | glEnd(); 436 | 437 | glEnable(GL_DEPTH_TEST); 438 | */ 439 | #if 0 440 | #ifdef WIN32 441 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 442 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE ); 443 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 444 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHATESTENABLE , FALSE); 445 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZWRITEENABLE , TRUE); 446 | #endif 447 | extern RenderingState_t GRenderingState; 448 | GRenderingState.mAlphaTestEnable = 0; 449 | GRenderingState.mZWriteEnable = 1; 450 | GRenderingState.mBlending = 0; 451 | GRenderingState.mCulling = 0; 452 | GRenderingState.mZTestType = 1; 453 | #endif 454 | } 455 | 456 | 457 | } 458 | 459 | void CGizmoTransformRotate::ApplyTransform(tvector3& trans, bool bAbsolute) 460 | { 461 | tmatrix mt; 462 | m_OrigScale.Scaling(GetTransformedVector(0).Length(), 463 | GetTransformedVector(1).Length(), 464 | GetTransformedVector(2).Length()); 465 | 466 | if (bAbsolute) 467 | { 468 | tvector3 translation = m_pMatrix->GetTranslation(); 469 | 470 | //X 471 | mt.RotationAxis(GetVector(0),((trans.x/360)*ZPI)); 472 | mt.Multiply(m_OrigScale); 473 | *m_pMatrix=mt; 474 | //Y 475 | mt.RotationAxis(GetVector(1),((trans.y/360)*ZPI)); 476 | mt.Multiply(m_OrigScale); 477 | *m_pMatrix=mt; 478 | //Z 479 | mt.RotationAxis(GetVector(2),((trans.z/360)*ZPI)); 480 | mt.Multiply(m_OrigScale); 481 | *m_pMatrix=mt; 482 | 483 | //translate 484 | m_pMatrix->m16[12] = translation.x; 485 | m_pMatrix->m16[13] = translation.y; 486 | m_pMatrix->m16[14] = translation.z; 487 | } 488 | else 489 | { 490 | tmatrix mt2; 491 | m_InvOrigScale.Inverse(m_OrigScale); 492 | 493 | if (trans.x!=0) 494 | { 495 | m_svgMatrix = *m_pMatrix; 496 | mt.RotationAxis(GetVector(0),((trans.x/360)*ZPI)); 497 | mt.Multiply(m_InvOrigScale); 498 | mt.Multiply(m_svgMatrix); 499 | mt2 = m_OrigScale; 500 | mt2.Multiply(mt); 501 | *m_pMatrix=mt2; 502 | } 503 | if (trans.y!=0) 504 | { 505 | m_svgMatrix = *m_pMatrix; 506 | mt.RotationAxis(GetVector(1),((trans.y/360)*ZPI)); 507 | mt.Multiply(m_InvOrigScale); 508 | mt.Multiply(m_svgMatrix); 509 | mt2 = m_OrigScale; 510 | mt2.Multiply(mt); 511 | *m_pMatrix=mt2; 512 | } 513 | if (trans.z!=0) 514 | { 515 | m_svgMatrix = *m_pMatrix; 516 | mt.RotationAxis(GetVector(2),((trans.z/360)*ZPI)); 517 | mt.Multiply(m_InvOrigScale); 518 | mt.Multiply(m_svgMatrix); 519 | mt2 = m_OrigScale; 520 | mt2.Multiply(mt); 521 | *m_pMatrix=mt2; 522 | } 523 | } 524 | } 525 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformRotate.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #ifndef GIZMOTRANSFORMROTATE_H__ 31 | #define GIZMOTRANSFORMROTATE_H__ 32 | 33 | #include "GizmoTransform.h" 34 | 35 | class CGizmoTransformRotate : public CGizmoTransform 36 | { 37 | 38 | public: 39 | CGizmoTransformRotate(); 40 | virtual ~CGizmoTransformRotate(); 41 | 42 | // return true if gizmo transform capture mouse 43 | virtual bool OnMouseDown(unsigned int x, unsigned int y); 44 | virtual void OnMouseMove(unsigned int x, unsigned int y); 45 | virtual void OnMouseUp(unsigned int x, unsigned int y); 46 | 47 | virtual void Draw(); 48 | 49 | virtual void SetSnap(const float snap) {m_AngleSnap = snap; } 50 | virtual void SetSnap(float snapx, float snapy, float snapz) {} 51 | /* 52 | void SetAngleSnap(float snap) 53 | { 54 | m_AngleSnap = snap; 55 | } 56 | */ 57 | 58 | float GetAngleSnap() 59 | { 60 | return m_AngleSnap; 61 | } 62 | 63 | virtual void ApplyTransform(tvector3& trans, bool bAbsolute); 64 | 65 | protected: 66 | enum ROTATETYPE 67 | { 68 | ROTATE_NONE, 69 | ROTATE_X, 70 | ROTATE_Y, 71 | ROTATE_Z, 72 | ROTATE_SCREEN, 73 | ROTATE_TWIN 74 | }; 75 | ROTATETYPE m_RotateType,m_RotateTypePredict; 76 | tplane m_plan; 77 | tvector3 m_LockVertex,m_LockVertex2; 78 | float m_Ng2; 79 | tvector3 m_Vtx,m_Vty,m_Vtz; 80 | tvector3 m_Axis,m_Axis2; 81 | tmatrix m_OrigScale,m_InvOrigScale; 82 | float m_AngleSnap; 83 | 84 | 85 | bool GetOpType(ROTATETYPE &type, unsigned int x, unsigned int y); 86 | bool CheckRotatePlan(tvector3 &vNorm, float factor, const tvector3 &rayOrig,const tvector3 &rayDir,int id); 87 | void Rotate1Axe(const tvector3& rayOrigin,const tvector3& rayDir); 88 | 89 | }; 90 | 91 | #endif // !defined(AFX_GIZMOTRANSFORMROTATE_H__F92EF632_4BAE_49CE_B7B8_213704C82589__INCLUDED_) 92 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformScale.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #include "stdafx.h" 31 | #include "GizmoTransformScale.h" 32 | #ifdef MAC_OS 33 | #import 34 | #else 35 | #include 36 | #endif 37 | 38 | extern tvector3 ptd; 39 | 40 | 41 | IGizmo *CreateScaleGizmo() 42 | { 43 | return new CGizmoTransformScale; 44 | } 45 | 46 | 47 | ////////////////////////////////////////////////////////////////////// 48 | // Construction/Destruction 49 | ////////////////////////////////////////////////////////////////////// 50 | 51 | CGizmoTransformScale::CGizmoTransformScale() : CGizmoTransform() 52 | { 53 | m_ScaleType = SCALE_NONE; 54 | } 55 | 56 | CGizmoTransformScale::~CGizmoTransformScale() 57 | { 58 | 59 | } 60 | 61 | 62 | 63 | bool CGizmoTransformScale::GetOpType(SCALETYPE &type, unsigned int x, unsigned int y) 64 | { 65 | // init 66 | tvector3 trss(GetTransformedVector(0).Length(), 67 | GetTransformedVector(1).Length(), 68 | GetTransformedVector(2).Length()); 69 | 70 | m_LockX = x; 71 | m_LockY = y; 72 | m_svgMatrix = *m_pMatrix; 73 | 74 | tmatrix mt; 75 | mt = *m_pMatrix; 76 | mt.NoTrans(); 77 | mt.Inverse(); 78 | 79 | 80 | //tmatrix mt; 81 | if (mLocation == LOCATE_LOCAL) 82 | { 83 | mt = *m_pMatrix; 84 | mt.Inverse(); 85 | } 86 | else 87 | { 88 | // world 89 | mt.Translation( -m_pMatrix->V4.position); 90 | } 91 | 92 | // ray casting 93 | tvector3 rayOrigin,rayDir,df2; 94 | BuildRay(x, y, rayOrigin, rayDir); 95 | 96 | // plan 1 : X/Z 97 | df2 = RayTrace2(rayOrigin, rayDir, GetTransformedVector(1), mt, trss); 98 | 99 | 100 | if ( (df2.x<0.2f) && (df2.z<0.2f) && (df2.x>0) && (df2.z>0)) { type = SCALE_XYZ; return true; } 101 | else if ( ( df2.x >= 0 ) && (df2.x <= 1) && ( fabs(df2.z) < 0.1f ) ) { type = SCALE_X; return true; } 102 | else if ( ( df2.z >= 0 ) && (df2.z <= 1) && ( fabs(df2.x) < 0.1f ) ) { type = SCALE_Z; return true; } 103 | else if ( (df2.x<0.5f) && (df2.z<0.5f) && (df2.x>0) && (df2.z>0)) { type = SCALE_XZ; return true; } 104 | else 105 | { 106 | //plan 2 : X/Y 107 | df2 = RayTrace2(rayOrigin, rayDir, GetTransformedVector(2), mt, trss); 108 | 109 | if ( (df2.x<0.2f) && (df2.y<0.2f) && (df2.x>0) && (df2.y>0)) { type = SCALE_XYZ; return true; } 110 | else if ( ( df2.x >= 0 ) && (df2.x <= 1) && ( fabs(df2.y) < 0.1f ) ) { type = SCALE_X; return true; } 111 | else if ( ( df2.y >= 0 ) && (df2.y <= 1) && ( fabs(df2.x) < 0.1f ) ) { type = SCALE_Y; return true; } 112 | else if ( (df2.x<0.5f) && (df2.y<0.5f) && (df2.x>0) && (df2.y>0)) { type = SCALE_XY; return true; } 113 | else 114 | { 115 | //plan 3: Y/Z 116 | df2 = RayTrace2(rayOrigin, rayDir, GetTransformedVector(0), mt, trss); 117 | 118 | if ( (df2.y<0.2f) && (df2.z<0.2f) && (df2.y>0) && (df2.z>0)) { type = SCALE_XYZ; return true; } 119 | else if ( ( df2.y >= 0 ) && (df2.y <= 1) && ( fabs(df2.z) < 0.1f ) ) { type = SCALE_Y; return true; } 120 | else if ( ( df2.z >= 0 ) && (df2.z <= 1) && ( fabs(df2.y) < 0.1f ) ) { type = SCALE_Z; return true; } 121 | else if ( (df2.y<0.5f) && (df2.z<0.5f) && (df2.y>0) && (df2.z>0)) { type = SCALE_YZ; return true; } 122 | } 123 | } 124 | 125 | type = SCALE_NONE; 126 | return false; 127 | } 128 | 129 | 130 | bool CGizmoTransformScale::OnMouseDown(unsigned int x, unsigned int y) 131 | { 132 | if (m_pMatrix) 133 | { 134 | return GetOpType(m_ScaleType, x, y); 135 | } 136 | 137 | m_ScaleType = SCALE_NONE; 138 | return false; 139 | } 140 | 141 | void CGizmoTransformScale::SnapScale(float &val) 142 | { 143 | if (m_bUseSnap) 144 | { 145 | val*=(100.0f); 146 | SnapIt(val,m_ScaleSnap); 147 | val/=(100.0f); 148 | } 149 | } 150 | 151 | void CGizmoTransformScale::OnMouseMove(unsigned int x, unsigned int y) 152 | { 153 | if (m_ScaleType != SCALE_NONE) 154 | { 155 | tvector3 rayOrigin,rayDir,df, inters, machin; 156 | tvector3 scVect,scVect2; 157 | 158 | BuildRay(x, y, rayOrigin, rayDir); 159 | m_plan.RayInter(inters,rayOrigin,rayDir); 160 | 161 | switch (m_ScaleType) 162 | { 163 | case SCALE_XZ: scVect = tvector3(1,0,1); break; 164 | case SCALE_X: scVect = tvector3(1,0,0); break; 165 | case SCALE_Z: scVect = tvector3(0,0,1); break; 166 | case SCALE_XY: scVect = tvector3(1,1,0); break; 167 | case SCALE_YZ: scVect = tvector3(0,1,1); break; 168 | case SCALE_Y: scVect = tvector3(0,1,0); break; 169 | case SCALE_XYZ:scVect = tvector3(1,1,1); break; 170 | } 171 | 172 | df = inters-m_pMatrix->GetTranslation(); 173 | df/=GetScreenFactor(); 174 | scVect2 = tvector3(1,1,1) - scVect; 175 | 176 | if (m_ScaleType == SCALE_XYZ) 177 | { 178 | int difx = x - m_LockX; 179 | float lng2 = 1.0f + ( float(difx) / 200.0f); 180 | SnapScale(lng2); 181 | scVect *=lng2; 182 | } 183 | else 184 | { 185 | int difx = x - m_LockX; 186 | int dify = y - m_LockY; 187 | 188 | float len = sqrtf( (float)(difx*difx) + (float)(dify*dify) ); 189 | 190 | float lng2 = len /100.f; 191 | /* 192 | float lng2 = ( df.Dot(m_LockVertex)); 193 | char tmps[512]; 194 | sprintf(tmps, "%5.4f\n", lng2 ); 195 | OutputDebugStringA( tmps ); 196 | 197 | 198 | if (lng2 < 1.f) 199 | { 200 | if ( lng2<= 0.001f ) 201 | lng2 = 0.001f; 202 | else 203 | { 204 | //lng2+=4.f; 205 | lng2/=5.f; 206 | } 207 | } 208 | else 209 | { 210 | int a = 1; 211 | } 212 | */ 213 | SnapScale(lng2); 214 | scVect *= lng2; 215 | scVect += scVect2; 216 | } 217 | 218 | 219 | tmatrix mt,mt2; 220 | 221 | 222 | 223 | mt.Scaling(scVect); 224 | 225 | mt2.Identity(); 226 | mt2.SetLine(0,GetTransformedVector(0)); 227 | mt2.SetLine(1,GetTransformedVector(1)); 228 | mt2.SetLine(2,GetTransformedVector(2)); 229 | 230 | //mt2.Translation(0,0,0); 231 | //mt.Multiply(mt2); 232 | 233 | if (mLocation == LOCATE_WORLD) 234 | { 235 | mt2 = mt * m_svgMatrix; 236 | } 237 | else 238 | { 239 | mt2 = mt * m_svgMatrix;//.Multiply(m_svgMatrix); 240 | } 241 | *m_pMatrix = mt2; 242 | //if (mTransform) mTransform->Update(); 243 | } 244 | else 245 | { 246 | // predict move 247 | if (m_pMatrix) 248 | { 249 | GetOpType(m_ScaleTypePredict, x, y); 250 | } 251 | } 252 | 253 | } 254 | 255 | void CGizmoTransformScale::OnMouseUp(unsigned int x, unsigned int y) 256 | { 257 | m_ScaleType = SCALE_NONE; 258 | } 259 | 260 | void CGizmoTransformScale::Draw() 261 | { 262 | if (m_pMatrix) 263 | { 264 | ComputeScreenFactor(); 265 | 266 | //glDisable(GL_DEPTH_TEST); 267 | tvector3 orig(m_pMatrix->m16[12],m_pMatrix->m16[13],m_pMatrix->m16[14]); 268 | 269 | 270 | // axis 271 | tvector3 axeX(1,0,0),axeY(0,1,0),axeZ(0,0,1); 272 | if (mLocation == LOCATE_LOCAL) 273 | { 274 | axeX.TransformVector(*m_pMatrix); 275 | axeY.TransformVector(*m_pMatrix); 276 | axeZ.TransformVector(*m_pMatrix); 277 | axeX.Normalize(); 278 | axeY.Normalize(); 279 | axeZ.Normalize(); 280 | } 281 | 282 | DrawTri(orig, 0.5f*GetScreenFactor(),((m_ScaleTypePredict==SCALE_XZ)||(m_ScaleTypePredict==SCALE_XYZ)), axeX, axeZ); 283 | DrawTri(orig, 0.5f*GetScreenFactor(),((m_ScaleTypePredict==SCALE_XY)||(m_ScaleTypePredict==SCALE_XYZ)), axeX, axeY); 284 | DrawTri(orig, 0.5f*GetScreenFactor(),((m_ScaleTypePredict==SCALE_YZ)||(m_ScaleTypePredict==SCALE_XYZ)), axeY, axeZ); 285 | 286 | axeX*=GetScreenFactor(); 287 | axeY*=GetScreenFactor(); 288 | axeZ*=GetScreenFactor(); 289 | 290 | 291 | // plan1 292 | if (m_ScaleTypePredict != SCALE_X) 293 | DrawAxis(orig,axeX,axeY,axeZ,0.05f,0.83f,vector4(1,0,0,1)); 294 | else 295 | DrawAxis(orig,axeX,axeY,axeZ,0.05f,0.83f,vector4(1,1,1,1)); 296 | 297 | //plan2 298 | if (m_ScaleTypePredict != SCALE_Y) 299 | DrawAxis(orig,axeY,axeX,axeZ,0.05f,0.83f,vector4(0,1,0,1)); 300 | else 301 | DrawAxis(orig,axeY,axeX,axeZ,0.05f,0.83f,vector4(1,1,1,1)); 302 | 303 | //plan3 304 | if (m_ScaleTypePredict != SCALE_Z) 305 | DrawAxis(orig,axeZ,axeX,axeY,0.05f,0.83f,vector4(0,0,1,1)); 306 | else 307 | DrawAxis(orig,axeZ,axeX,axeY,0.05f,0.83f,vector4(1,1,1,1)); 308 | 309 | /* 310 | // debug 311 | glPointSize(20); 312 | glBegin(GL_POINTS); 313 | glVertex3fv(&ptd.x); 314 | glEnd(); 315 | 316 | glEnable(GL_DEPTH_TEST); 317 | */ 318 | #if 0 319 | #ifdef WIN32 320 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); 321 | GDD->GetD3D9Device()->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE ); 322 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE); 323 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ALPHATESTENABLE , FALSE); 324 | GDD->GetD3D9Device()->SetRenderState(D3DRS_ZWRITEENABLE , TRUE); 325 | #endif 326 | extern RenderingState_t GRenderingState; 327 | GRenderingState.mAlphaTestEnable = 0; 328 | GRenderingState.mZWriteEnable = 1; 329 | GRenderingState.mBlending = 0; 330 | GRenderingState.mCulling = 0; 331 | GRenderingState.mZTestType = 1; 332 | #endif 333 | } 334 | 335 | 336 | } 337 | 338 | void CGizmoTransformScale::ApplyTransform(tvector3& trans, bool bAbsolute) 339 | { 340 | if (bAbsolute) 341 | { 342 | tmatrix m_InvOrigScale,m_OrigScale; 343 | 344 | m_OrigScale.Scaling(GetTransformedVector(0).Length(), 345 | GetTransformedVector(1).Length(), 346 | GetTransformedVector(2).Length()); 347 | 348 | m_InvOrigScale.Inverse(m_OrigScale); 349 | m_svgMatrix = *m_pMatrix; 350 | 351 | tmatrix mt; 352 | mt.Scaling(trans.x/100.0f,trans.y/100.0f,trans.z/100.0f); 353 | mt.Multiply(m_InvOrigScale); 354 | mt.Multiply(m_svgMatrix); 355 | *m_pMatrix=mt; 356 | } 357 | else 358 | { 359 | tmatrix mt,mt2; 360 | m_svgMatrix = *m_pMatrix; 361 | mt.Scaling(trans.x/100.0f,trans.y/100.0f,trans.z/100.0f); 362 | 363 | mt2.SetLine(0,GetTransformedVector(0)); 364 | mt2.SetLine(1,GetTransformedVector(1)); 365 | mt2.SetLine(2,GetTransformedVector(2)); 366 | mt2.Translation(0,0,0); 367 | mt.Multiply(mt2); 368 | mt.Multiply(m_svgMatrix); 369 | *m_pMatrix = mt; 370 | } 371 | 372 | } 373 | -------------------------------------------------------------------------------- /src/libgizmo/GizmoTransformScale.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #ifndef GIZMOTRANSFORMSCALE_H__ 31 | #define GIZMOTRANSFORMSCALE_H__ 32 | 33 | #include "GizmoTransform.h" 34 | 35 | class CGizmoTransformScale : public CGizmoTransform 36 | { 37 | public: 38 | CGizmoTransformScale(); 39 | virtual ~CGizmoTransformScale(); 40 | 41 | // return true if gizmo transform capture mouse 42 | virtual bool OnMouseDown(unsigned int x, unsigned int y); 43 | virtual void OnMouseMove(unsigned int x, unsigned int y); 44 | virtual void OnMouseUp(unsigned int x, unsigned int y); 45 | 46 | virtual void Draw(); 47 | 48 | /* 49 | void SetScaleSnap(float snap) 50 | { 51 | m_ScaleSnap = snap; 52 | } 53 | */ 54 | virtual void SetSnap(const float snap) {m_ScaleSnap = snap; } 55 | virtual void SetSnap(float snapx, float snapy, float snapz) {} 56 | 57 | float GetScaleSnap() 58 | { 59 | return m_ScaleSnap; 60 | } 61 | 62 | virtual void ApplyTransform(tvector3& trans, bool bAbsolute); 63 | 64 | protected: 65 | enum SCALETYPE 66 | { 67 | SCALE_NONE, 68 | SCALE_X, 69 | SCALE_Y, 70 | SCALE_Z, 71 | SCALE_XY, 72 | SCALE_XZ, 73 | SCALE_YZ, 74 | SCALE_XYZ 75 | }; 76 | SCALETYPE m_ScaleType,m_ScaleTypePredict; 77 | 78 | 79 | unsigned int m_LockX, m_LockY; 80 | float m_ScaleSnap; 81 | 82 | bool GetOpType(SCALETYPE &type, unsigned int x, unsigned int y); 83 | //tvector3 RayTrace(tvector3& rayOrigin, tvector3& rayDir, tvector3& norm, tmatrix& mt, tvector3 trss); 84 | void SnapScale(float &val); 85 | 86 | }; 87 | 88 | #endif // !defined(AFX_GIZMOTRANSFORMSCALE_H__85E46839_15B0_4CE4_A85A_547015AF853A__INCLUDED_) 89 | -------------------------------------------------------------------------------- /src/libgizmo/LibBase.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #ifndef LIBBASE_H__ 31 | #define LIBBASE_H__ 32 | 33 | #include "ZBaseDefs.h" 34 | #include "ZBaseMaths.h" 35 | #include "ZCollisionsUtils.h" 36 | #include "ZMathsFunc.h" 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /src/libgizmo/ZBaseDefs.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #ifndef ZBASEDEFS_H__ 31 | #define ZBASEDEFS_H__ 32 | 33 | #ifdef WIN32 34 | #include 35 | #include 36 | #endif 37 | 38 | 39 | typedef unsigned long tulong; 40 | typedef long tlong; 41 | typedef double tdouble; 42 | 43 | typedef char tchar; 44 | typedef unsigned char tuchar; 45 | 46 | typedef short tshort; 47 | typedef unsigned short tushort; 48 | 49 | typedef unsigned char uint8; 50 | typedef char int8; 51 | typedef unsigned short uint16; 52 | typedef short int16; 53 | typedef unsigned int uint32; 54 | typedef int int32; 55 | 56 | typedef unsigned short TClassID; 57 | 58 | typedef unsigned int uint; 59 | #define SAFE_DELETE(x) if (x) { delete x; x = NULL; } 60 | #define FLOAT_EPSILON float(1.192092896e-07) // Smallest positive number x, such that x+1.0 is not equal to 1.0 61 | const float RealEpsilon=FLOAT_EPSILON*16.f; 62 | 63 | 64 | #define ZMAX(a,b) (((a) > (b)) ? (a) : (b)) 65 | #define ZMIN(a,b) (((a) < (b)) ? (a) : (b)) 66 | 67 | inline bool isPowerOf2(unsigned int n) 68 | { 69 | return n == 1 || (n & (n-1)) == 0; 70 | } 71 | 72 | #include 73 | #include 74 | #include 75 | #include 76 | 77 | #define tarray std::vector 78 | #define tlist std::list 79 | 80 | // targa header 81 | 82 | typedef struct TargaHeader_t { 83 | tuchar IDLength; 84 | tuchar ColormapType; 85 | tuchar ImageType; 86 | tuchar ColormapSpecification[5]; 87 | tushort XOrigin; 88 | tushort YOrigin; 89 | tushort ImageWidth; 90 | tushort ImageHeight; 91 | tuchar PixelDepth; 92 | tuchar ImageDescriptor; 93 | } TargaHeader_t; 94 | 95 | // macros 96 | 97 | #define foreach( i, c, type )\ 98 | if (!c.empty()) for( std::vector::iterator i = c.begin(); i != c.end(); ++i ) 99 | 100 | #define foreach_const( i, c, type )\ 101 | if (!c.empty()) for( std::vector::const_iterator i = c.begin(); i != c.end(); ++i ) 102 | 103 | #define foreachmap( i, c, type )\ 104 | if (!c.empty()) for( std::map::iterator i = c.begin(); i != c.end(); ++i ) 105 | 106 | #define foreachmap_const( i, c, type )\ 107 | if (!c.empty()) for( std::map::const_iterator i = c.begin(); i != c.end(); ++i ) 108 | 109 | void Zexit(int aRet); 110 | 111 | 112 | #ifdef WIN32 113 | typedef CRITICAL_SECTION ZCriticalSection_t; 114 | typedef HWND WindowHandle_t; 115 | typedef HANDLE ThreadHandle_t; 116 | 117 | inline char* GetCurrentDirectory(int bufLength, char *pszDest) 118 | { 119 | return (char*)GetCurrentDirectoryA(bufLength, pszDest); 120 | } 121 | 122 | #endif 123 | 124 | #ifdef LINUX 125 | #include 126 | 127 | typedef pthread_mutex_t ZCriticalSection_t; 128 | typedef int WindowHandle_t; 129 | typedef pthread_t ThreadHandle_t; 130 | 131 | #define TRUE 1 132 | #define FALSE 0 133 | 134 | typedef struct RECT 135 | { 136 | int left, right, bottom, top; 137 | } RECT; 138 | 139 | 140 | #define _MAX_PATH 256 141 | #define MAX_PATH _MAX_PATH 142 | 143 | #define stricmp strcasecmp 144 | #define _stricmp strcasecmp 145 | #define sscanf_s sscanf 146 | #define sprintf_s snprintf 147 | #define OutputDebugString printf 148 | #define OutputDebugStringA printf 149 | 150 | inline void strlwr(char *) 151 | { 152 | } 153 | #include 154 | 155 | #include 156 | 157 | #include 158 | #include 159 | #include 160 | #include 161 | #include 162 | 163 | inline char* GetCurrentDirectory(int bufLength, char *pszDest) 164 | { 165 | return getcwd(pszDest, bufLength); 166 | } 167 | 168 | inline void ZeroMemory(void *pDest, int aSize) 169 | { 170 | memset(pDest, 0, aSize); 171 | } 172 | 173 | inline unsigned long GetCurrentThreadId() 174 | { 175 | return (unsigned long) pthread_self();//getpid(); 176 | } 177 | 178 | #endif 179 | 180 | #ifdef MAC_OS 181 | #import 182 | 183 | typedef MPCriticalRegionID ZCriticalSection_t; 184 | typedef int WindowHandle_t; 185 | typedef pthread_t ThreadHandle_t; 186 | 187 | #define TRUE 1 188 | #define FALSE 0 189 | 190 | typedef struct RECT 191 | { 192 | int left, right, bottom, top; 193 | } RECT; 194 | 195 | 196 | #define _MAX_PATH 256 197 | #define MAX_PATH _MAX_PATH 198 | 199 | #define stricmp strcasecmp 200 | #define _stricmp strcasecmp 201 | #define sscanf_s sscanf 202 | #define sprintf_s snprintf 203 | #define OutputDebugString printf 204 | #define OutputDebugStringA printf 205 | 206 | inline void strlwr(char *pszBuf) 207 | { 208 | } 209 | #include 210 | 211 | #include 212 | 213 | #include 214 | #include 215 | #include 216 | #include 217 | #include 218 | 219 | inline char* GetCurrentDirectory(int bufLength, char *pszDest) 220 | { 221 | return getcwd(pszDest, bufLength); 222 | } 223 | 224 | inline void ZeroMemory(void *pDest, int aSize) 225 | { 226 | memset(pDest, 0, aSize); 227 | } 228 | 229 | inline unsigned long GetCurrentThreadId() 230 | { 231 | return (unsigned long) pthread_self();//getpid(); 232 | } 233 | 234 | // DX compatibility 235 | /* 236 | typedef void IDirect3DDevice9; 237 | typedef void IDirect3DTexture9; 238 | */ 239 | 240 | 241 | #endif 242 | 243 | #endif 244 | -------------------------------------------------------------------------------- /src/libgizmo/ZBaseMaths.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #include "stdafx.h" 31 | #include "ZBaseMaths.h" 32 | 33 | /////////////////////////////////////////////////////////////////////////////////////////////////// 34 | 35 | const tvector3 tvector3::zero (0, 0, 0); 36 | const tvector3 tvector3::up (0, 1, 0); 37 | const tvector3 tvector3::one (1,1,1); 38 | const tvector3 tvector3::XAxis(1,0,0); 39 | const tvector3 tvector3::YAxis(0,1,0); 40 | const tvector3 tvector3::ZAxis(0,0,1); 41 | const tvector3 tvector3::opXAxis(-1,0,0); 42 | const tvector3 tvector3::opYAxis(0,-1,0); 43 | const tvector3 tvector3::opZAxis(0,0,-1); 44 | const tvector3 tvector3::Epsilon(RealEpsilon,RealEpsilon,RealEpsilon); 45 | 46 | /////////////////////////////////////////////////////////////////////////////////////////////////// 47 | 48 | const tvector4 tvector4::zero = vector4(0,0,0,0); 49 | const tvector4 tvector4::one = vector4(1,1,1,1); 50 | const tvector4 tvector4::XAxis = vector4(1,0,0,0); 51 | const tvector4 tvector4::YAxis = vector4(0,1,0,0); 52 | const tvector4 tvector4::ZAxis = vector4(0,0,1,0); 53 | const tvector4 tvector4::WAxis = vector4(0,0,0,1); 54 | const tvector4 tvector4::opXAxis = vector4(-1,0,0,0); 55 | const tvector4 tvector4::opYAxis = vector4(0,-1,0,0); 56 | const tvector4 tvector4::opZAxis = vector4(0,0,-1,0); 57 | const tvector4 tvector4::opWAxis = vector4(0,0,0,-1); 58 | 59 | /////////////////////////////////////////////////////////////////////////////////////////////////// 60 | 61 | const tcolor tcolor::black (0, 0, 0, 0); 62 | const tcolor tcolor::white (1, 1, 1, 1); 63 | const tcolor tcolor::red (1, 0, 0, 1); 64 | const tcolor tcolor::green (0, 1, 0, 1); 65 | const tcolor tcolor::blue (0, 0, 1, 1); 66 | 67 | 68 | tmatrix tmatrix::identity = tmatrix(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1); 69 | tquaternion tquaternion::identity = tquaternion(0,0,0,1); 70 | 71 | /////////////////////////////////////////////////////////////////////////////////////////////////// 72 | 73 | tvector3 tvector3::vecLimitDeviationAngleUtility (const bool insideOrOutside, 74 | const tvector3& source, 75 | const float cosineOfConeAngle, 76 | const tvector3& basis) 77 | { 78 | // immediately return zero length input vectors 79 | float sourceLength = source.Length(); 80 | if (sourceLength == 0) return source; 81 | 82 | // measure the angular diviation of "source" from "basis" 83 | const tvector3 direction = source / sourceLength; 84 | float cosineOfSourceAngle = direction.Dot (basis); 85 | 86 | // Simply return "source" if it already meets the angle criteria. 87 | // (note: we hope this top "if" gets compiled out since the flag 88 | // is a constant when the function is inlined into its caller) 89 | if (insideOrOutside) 90 | { 91 | // source vector is already inside the cone, just return it 92 | if (cosineOfSourceAngle >= cosineOfConeAngle) return source; 93 | } 94 | else 95 | { 96 | // source vector is already outside the cone, just return it 97 | if (cosineOfSourceAngle <= cosineOfConeAngle) return source; 98 | } 99 | 100 | // find the portion of "source" that is perpendicular to "basis" 101 | const tvector3 perp = source.perpendicularComponent (basis); 102 | 103 | // normalize that perpendicular 104 | tvector3 unitPerp = perp; 105 | unitPerp.Normalize (); 106 | 107 | // construct a new vector whose length equals the source vector, 108 | // and lies on the intersection of a plane (formed the source and 109 | // basis vectors) and a cone (whose axis is "basis" and whose 110 | // angle corresponds to cosineOfConeAngle) 111 | float perpDist = (float)sqrt (1 - (cosineOfConeAngle * cosineOfConeAngle)); 112 | const tvector3 c0 = basis * cosineOfConeAngle; 113 | const tvector3 c1 = unitPerp * perpDist; 114 | return (c0 + c1) * sourceLength; 115 | } 116 | 117 | 118 | tvector3 vecLimitDeviationAngleUtility (const bool insideOrOutside, 119 | const tvector3& source, 120 | const float cosineOfConeAngle, 121 | const tvector3& basis) 122 | { 123 | // immediately return zero length input vectors 124 | float sourceLength = source.Length(); 125 | if (sourceLength == 0) return source; 126 | 127 | // measure the angular diviation of "source" from "basis" 128 | const tvector3 direction = source / sourceLength; 129 | float cosineOfSourceAngle = direction.Dot (basis); 130 | 131 | // Simply return "source" if it already meets the angle criteria. 132 | // (note: we hope this top "if" gets compiled out since the flag 133 | // is a constant when the function is inlined into its caller) 134 | if (insideOrOutside) 135 | { 136 | // source vector is already inside the cone, just return it 137 | if (cosineOfSourceAngle >= cosineOfConeAngle) return source; 138 | } 139 | else 140 | { 141 | // source vector is already outside the cone, just return it 142 | if (cosineOfSourceAngle <= cosineOfConeAngle) return source; 143 | } 144 | 145 | // find the portion of "source" that is perpendicular to "basis" 146 | const tvector3 perp = source.perpendicularComponent (basis); 147 | 148 | // normalize that perpendicular 149 | tvector3 unitPerp = perp; 150 | unitPerp.Normalize (); 151 | 152 | // construct a new vector whose length equals the source vector, 153 | // and lies on the intersection of a plane (formed the source and 154 | // basis vectors) and a cone (whose axis is "basis" and whose 155 | // angle corresponds to cosineOfConeAngle) 156 | float perpDist = (float)sqrt (1 - (cosineOfConeAngle * cosineOfConeAngle)); 157 | const tvector3 c0 = basis * cosineOfConeAngle; 158 | const tvector3 c1 = unitPerp * perpDist; 159 | return (c0 + c1) * sourceLength; 160 | } 161 | 162 | 163 | /////////////////////////////////////////////////////////////////////////////////////////////////// 164 | 165 | void tvector3::ClosestPointOnSegment(const tvector3 & point, const tvector3 & point1, const tvector3 & point2 ) 166 | { 167 | tvector3 c, segment; 168 | tvector3 v; 169 | 170 | c = point - point1; 171 | 172 | tvector3 tmp = point2 - point1; 173 | segment.Normalize(tmp); 174 | 175 | float t = segment.Dot(c); 176 | 177 | if(t < 0) 178 | { 179 | x = point1.x; 180 | y = point1.y; 181 | z = point1.z; 182 | } 183 | else 184 | { 185 | tmp = point2 - point1; 186 | float distance = tmp.Length(); 187 | 188 | if(t > distance) 189 | { 190 | x = point2.x; 191 | y = point2.y; 192 | z = point2.z; 193 | } 194 | else 195 | { 196 | x = point1.x + segment.x * t; 197 | y = point1.y + segment.y * t; 198 | z = point1.z + segment.z * t; 199 | } 200 | } 201 | } 202 | 203 | /////////////////////////////////////////////////////////////////////////////////////////////////// 204 | 205 | void tvector3::ClosestPointOnTriangle(const tvector3 & point, const tvector3 & point1, const tvector3 & point2, const tvector3 & point3 ) 206 | { 207 | tvector3 pointSegAB; 208 | tvector3 pointSegBC; 209 | tvector3 pointSegCA; 210 | 211 | pointSegAB.ClosestPointOnSegment( point, point1, point2 ); 212 | pointSegBC.ClosestPointOnSegment( point, point2, point3 ); 213 | pointSegCA.ClosestPointOnSegment( point, point3, point1 ); 214 | 215 | tvector3 tmp = point - pointSegAB; 216 | float dsegAB = tmp.Length(); 217 | 218 | tmp = point - pointSegBC; 219 | float dsegBC = tmp.Length(); 220 | 221 | tmp = point - pointSegCA; 222 | float dsegCA = tmp.Length(); 223 | 224 | if( (dsegAB < dsegBC) && ( dsegAB < dsegCA )) 225 | { 226 | x = pointSegAB.x; 227 | y = pointSegAB.y; 228 | z = pointSegAB.z; 229 | } 230 | else{ 231 | if( (dsegBC < dsegAB) && ( dsegBC < dsegCA )) 232 | { 233 | x = pointSegBC.x; 234 | y = pointSegBC.y; 235 | z = pointSegBC.z; 236 | } 237 | else 238 | { 239 | x = pointSegCA.x; 240 | y = pointSegCA.y; 241 | z = pointSegCA.z; 242 | } 243 | } 244 | } 245 | 246 | 247 | /////////////////////////////////////////////////////////////////////////////////////////////////// 248 | 249 | void tvector3::ParametricQuadratic(const tvector3& v1, const tvector3& v2, const tvector3& v3, const float s) 250 | { 251 | x = v1.x * MathParQuadSplineF3(s) + v2.x * MathParQuadSplineF2(s) + v3.x * MathParQuadSplineF1(s); 252 | y = v1.y * MathParQuadSplineF3(s) + v2.y * MathParQuadSplineF2(s) + v3.y * MathParQuadSplineF1(s); 253 | z = v1.z * MathParQuadSplineF3(s) + v2.z * MathParQuadSplineF2(s) + v3.z * MathParQuadSplineF1(s); 254 | } 255 | 256 | /////////////////////////////////////////////////////////////////////////////////////////////////// 257 | 258 | void tvector3::ParametricCubic(const tvector3& v1, const tvector3& v2, const tvector3& v3, const tvector3& v4, const float s) 259 | { 260 | x = v1.x * MathParCubicSplineF4(s) + v2.x * MathParCubicSplineF3(s) + v4.x * MathParCubicSplineF2(s) + v3.x * MathParCubicSplineF1(s); 261 | y = v1.y * MathParCubicSplineF4(s) + v2.y * MathParCubicSplineF3(s) + v4.y * MathParCubicSplineF2(s) + v3.y * MathParCubicSplineF1(s); 262 | z = v1.z * MathParCubicSplineF4(s) + v2.z * MathParCubicSplineF3(s) + v4.z * MathParCubicSplineF2(s) + v3.z * MathParCubicSplineF1(s); 263 | } 264 | 265 | /////////////////////////////////////////////////////////////////////////////////////////////////// 266 | 267 | void tvector3::BezierQuadratic(const tvector3& v1, const tvector3& v2, const tvector3& v3, const float s) 268 | { 269 | x = v1.x * MathBezierQuadSplineF3(s) + v2.x * MathBezierQuadSplineF2(s) + v3.x * MathBezierQuadSplineF1(s); 270 | y = v1.y * MathBezierQuadSplineF3(s) + v2.y * MathBezierQuadSplineF2(s) + v3.y * MathBezierQuadSplineF1(s); 271 | z = v1.z * MathBezierQuadSplineF3(s) + v2.z * MathBezierQuadSplineF2(s) + v3.z * MathBezierQuadSplineF1(s); 272 | } 273 | 274 | /////////////////////////////////////////////////////////////////////////////////////////////////// 275 | 276 | void tvector3::BezierCubic(const tvector3& v1, const tvector3& v2, const tvector3& v3, const tvector3& v4, const float s) 277 | { 278 | x = v1.x * MathBezierCubicSplineF4(s) + v2.x * MathBezierCubicSplineF3(s) + v4.x * MathBezierCubicSplineF2(s) + v3.x * MathBezierCubicSplineF1(s); 279 | y = v1.y * MathBezierCubicSplineF4(s) + v2.y * MathBezierCubicSplineF3(s) + v4.y * MathBezierCubicSplineF2(s) + v3.y * MathBezierCubicSplineF1(s); 280 | z = v1.z * MathBezierCubicSplineF4(s) + v2.z * MathBezierCubicSplineF3(s) + v4.z * MathBezierCubicSplineF2(s) + v3.z * MathBezierCubicSplineF1(s); 281 | } 282 | 283 | /////////////////////////////////////////////////////////////////////////////////////////////////// 284 | 285 | void tvector3::CoonsQuadratic(const tvector3& v1, const tvector3& t1, const tvector3& v2, const float s) 286 | { 287 | x = v1.x * MathCoonsQuadSplineF1(s) + v2.x * MathCoonsQuadSplineF2(s) + t1.x * MathCoonsQuadSplineF3(s); 288 | y = v1.y * MathCoonsQuadSplineF1(s) + v2.y * MathCoonsQuadSplineF2(s) + t1.y * MathCoonsQuadSplineF3(s); 289 | z = v1.z * MathCoonsQuadSplineF1(s) + v2.z * MathCoonsQuadSplineF2(s) + t1.z * MathCoonsQuadSplineF3(s); 290 | } 291 | 292 | /////////////////////////////////////////////////////////////////////////////////////////////////// 293 | 294 | void tvector3::CoonsCubic(const tvector3& v1, const tvector3& t1, const tvector3& v2, const tvector3& t2, const float s) 295 | { 296 | x = v1.x * MathCoonsCubicSplineF1(s) + v2.x * MathCoonsCubicSplineF2(s) + t1.x * MathCoonsCubicSplineF3(s) + t2.x * MathCoonsCubicSplineF4(s); 297 | y = v1.y * MathCoonsCubicSplineF1(s) + v2.y * MathCoonsCubicSplineF2(s) + t1.y * MathCoonsCubicSplineF3(s) + t2.y * MathCoonsCubicSplineF4(s); 298 | z = v1.z * MathCoonsCubicSplineF1(s) + v2.z * MathCoonsCubicSplineF2(s) + t1.z * MathCoonsCubicSplineF3(s) + t2.z * MathCoonsCubicSplineF4(s); 299 | } 300 | 301 | /////////////////////////////////////////////////////////////////////////////////////////////////// 302 | 303 | void tvector3::CatmullRom(const tvector3& v1, const tvector3& v2, const tvector3& v3, const tvector3& v4, const float s) 304 | { 305 | x = 0.5f*((-v2.x + 3 * v1.x - 3 * v4.x + v3.x) * s * s * s + (2 * v2.x - 5 * v1.x + 4 * v4.x - v3.x) * s * s+(-v2.x + v4.x) * s + 2 * v1.x); 306 | y = 0.5f*((-v2.y + 3 * v1.y - 3 * v4.y + v3.y) * s * s * s + (2 * v2.y - 5 * v1.y + 4 * v4.y - v3.y) * s * s+(-v2.y + v4.y) * s + 2 * v1.y); 307 | z = 0.5f*((-v2.z + 3 * v1.z - 3 * v4.z + v3.z) * s * s * s + (2 * v2.z - 5 * v1.z + 4 * v4.z - v3.z) * s * s+(-v2.z + v4.z) * s + 2 * v1.z); 308 | 309 | } 310 | 311 | /////////////////////////////////////////////////////////////////////////////////////////////////// 312 | 313 | float tmatrix::Inverse(const tmatrix &srcMatrix, bool affine ) 314 | { 315 | float det = 0; 316 | 317 | if(affine) 318 | { 319 | det = GetDeterminant(); 320 | float s = 1 / det; 321 | m[0][0] = (srcMatrix.m[1][1]*srcMatrix.m[2][2] - srcMatrix.m[1][2]*srcMatrix.m[2][1]) * s; 322 | m[0][1] = (srcMatrix.m[2][1]*srcMatrix.m[0][2] - srcMatrix.m[2][2]*srcMatrix.m[0][1]) * s; 323 | m[0][2] = (srcMatrix.m[0][1]*srcMatrix.m[1][2] - srcMatrix.m[0][2]*srcMatrix.m[1][1]) * s; 324 | m[1][0] = (srcMatrix.m[1][2]*srcMatrix.m[2][0] - srcMatrix.m[1][0]*srcMatrix.m[2][2]) * s; 325 | m[1][1] = (srcMatrix.m[2][2]*srcMatrix.m[0][0] - srcMatrix.m[2][0]*srcMatrix.m[0][2]) * s; 326 | m[1][2] = (srcMatrix.m[0][2]*srcMatrix.m[1][0] - srcMatrix.m[0][0]*srcMatrix.m[1][2]) * s; 327 | m[2][0] = (srcMatrix.m[1][0]*srcMatrix.m[2][1] - srcMatrix.m[1][1]*srcMatrix.m[2][0]) * s; 328 | m[2][1] = (srcMatrix.m[2][0]*srcMatrix.m[0][1] - srcMatrix.m[2][1]*srcMatrix.m[0][0]) * s; 329 | m[2][2] = (srcMatrix.m[0][0]*srcMatrix.m[1][1] - srcMatrix.m[0][1]*srcMatrix.m[1][0]) * s; 330 | m[3][0] = -(m[0][0]*srcMatrix.m[3][0] + m[1][0]*srcMatrix.m[3][1] + m[2][0]*srcMatrix.m[3][2]); 331 | m[3][1] = -(m[0][1]*srcMatrix.m[3][0] + m[1][1]*srcMatrix.m[3][1] + m[2][1]*srcMatrix.m[3][2]); 332 | m[3][2] = -(m[0][2]*srcMatrix.m[3][0] + m[1][2]*srcMatrix.m[3][1] + m[2][2]*srcMatrix.m[3][2]); 333 | } 334 | else 335 | { 336 | // transpose matrix 337 | float src[16]; 338 | for ( int i=0; i<4; ++i ) 339 | { 340 | src[i] = srcMatrix.m16[i*4]; 341 | src[i + 4] = srcMatrix.m16[i*4 + 1]; 342 | src[i + 8] = srcMatrix.m16[i*4 + 2]; 343 | src[i + 12] = srcMatrix.m16[i*4 + 3]; 344 | } 345 | 346 | // calculate pairs for first 8 elements (cofactors) 347 | float tmp[12]; // temp array for pairs 348 | tmp[0] = src[10] * src[15]; 349 | tmp[1] = src[11] * src[14]; 350 | tmp[2] = src[9] * src[15]; 351 | tmp[3] = src[11] * src[13]; 352 | tmp[4] = src[9] * src[14]; 353 | tmp[5] = src[10] * src[13]; 354 | tmp[6] = src[8] * src[15]; 355 | tmp[7] = src[11] * src[12]; 356 | tmp[8] = src[8] * src[14]; 357 | tmp[9] = src[10] * src[12]; 358 | tmp[10] = src[8] * src[13]; 359 | tmp[11] = src[9] * src[12]; 360 | 361 | // calculate first 8 elements (cofactors) 362 | m16[0] = (tmp[0] * src[5] + tmp[3] * src[6] + tmp[4] * src[7]) - (tmp[1] * src[5] + tmp[2] * src[6] + tmp[5] * src[7]); 363 | m16[1] = (tmp[1] * src[4] + tmp[6] * src[6] + tmp[9] * src[7]) - (tmp[0] * src[4] + tmp[7] * src[6] + tmp[8] * src[7]); 364 | m16[2] = (tmp[2] * src[4] + tmp[7] * src[5] + tmp[10] * src[7]) - (tmp[3] * src[4] + tmp[6] * src[5] + tmp[11] * src[7]); 365 | m16[3] = (tmp[5] * src[4] + tmp[8] * src[5] + tmp[11] * src[6]) - (tmp[4] * src[4] + tmp[9] * src[5] + tmp[10] * src[6]); 366 | m16[4] = (tmp[1] * src[1] + tmp[2] * src[2] + tmp[5] * src[3]) - (tmp[0] * src[1] + tmp[3] * src[2] + tmp[4] * src[3]); 367 | m16[5] = (tmp[0] * src[0] + tmp[7] * src[2] + tmp[8] * src[3]) - (tmp[1] * src[0] + tmp[6] * src[2] + tmp[9] * src[3]); 368 | m16[6] = (tmp[3] * src[0] + tmp[6] * src[1] + tmp[11] * src[3]) - (tmp[2] * src[0] + tmp[7] * src[1] + tmp[10] * src[3]); 369 | m16[7] = (tmp[4] * src[0] + tmp[9] * src[1] + tmp[10] * src[2]) - (tmp[5] * src[0] + tmp[8] * src[1] + tmp[11] * src[2]); 370 | 371 | // calculate pairs for second 8 elements (cofactors) 372 | tmp[0] = src[2] * src[7]; 373 | tmp[1] = src[3] * src[6]; 374 | tmp[2] = src[1] * src[7]; 375 | tmp[3] = src[3] * src[5]; 376 | tmp[4] = src[1] * src[6]; 377 | tmp[5] = src[2] * src[5]; 378 | tmp[6] = src[0] * src[7]; 379 | tmp[7] = src[3] * src[4]; 380 | tmp[8] = src[0] * src[6]; 381 | tmp[9] = src[2] * src[4]; 382 | tmp[10] = src[0] * src[5]; 383 | tmp[11] = src[1] * src[4]; 384 | 385 | // calculate second 8 elements (cofactors) 386 | m16[8] = (tmp[0] * src[13] + tmp[3] * src[14] + tmp[4] * src[15]) - (tmp[1] * src[13] + tmp[2] * src[14] + tmp[5] * src[15]); 387 | m16[9] = (tmp[1] * src[12] + tmp[6] * src[14] + tmp[9] * src[15]) - (tmp[0] * src[12] + tmp[7] * src[14] + tmp[8] * src[15]); 388 | m16[10] = (tmp[2] * src[12] + tmp[7] * src[13] + tmp[10] * src[15]) - (tmp[3] * src[12] + tmp[6] * src[13] + tmp[11] * src[15]); 389 | m16[11] = (tmp[5] * src[12] + tmp[8] * src[13] + tmp[11] * src[14]) - (tmp[4] * src[12] + tmp[9] * src[13] + tmp[10] * src[14]); 390 | m16[12] = (tmp[2] * src[10] + tmp[5] * src[11] + tmp[1] * src[9]) - (tmp[4] * src[11] + tmp[0] * src[9] + tmp[3] * src[10]); 391 | m16[13] = (tmp[8] * src[11] + tmp[0] * src[8] + tmp[7] * src[10]) - (tmp[6] * src[10] + tmp[9] * src[11] + tmp[1] * src[8]); 392 | m16[14] = (tmp[6] * src[9] + tmp[11] * src[11] + tmp[3] * src[8]) - (tmp[10] * src[11] + tmp[2] * src[8] + tmp[7] * src[9]); 393 | m16[15] = (tmp[10] * src[10] + tmp[4] * src[8] + tmp[9] * src[9]) - (tmp[8] * src[9] + tmp[11] * src[10] + tmp[5] * src[8]); 394 | 395 | // calculate determinant 396 | float det = src[0]*m16[0]+src[1]*m16[1]+src[2]*m16[2]+src[3]*m16[3]; 397 | 398 | // calculate matrix inverse 399 | float invdet = 1 / det; 400 | for ( int j=0; j<16; ++j ) 401 | { 402 | m16[j] *= invdet; 403 | } 404 | } 405 | 406 | return det; 407 | 408 | } 409 | 410 | /////////////////////////////////////////////////////////////////////////////////////////////////// 411 | 412 | float tmatrix::Inverse(bool affine) 413 | { 414 | float det = 0; 415 | 416 | if(affine) 417 | { 418 | det = GetDeterminant(); 419 | float s = 1 / det; 420 | float v00 = (m[1][1]*m[2][2] - m[1][2]*m[2][1]) * s; 421 | float v01 = (m[2][1]*m[0][2] - m[2][2]*m[0][1]) * s; 422 | float v02 = (m[0][1]*m[1][2] - m[0][2]*m[1][1]) * s; 423 | float v10 = (m[1][2]*m[2][0] - m[1][0]*m[2][2]) * s; 424 | float v11 = (m[2][2]*m[0][0] - m[2][0]*m[0][2]) * s; 425 | float v12 = (m[0][2]*m[1][0] - m[0][0]*m[1][2]) * s; 426 | float v20 = (m[1][0]*m[2][1] - m[1][1]*m[2][0]) * s; 427 | float v21 = (m[2][0]*m[0][1] - m[2][1]*m[0][0]) * s; 428 | float v22 = (m[0][0]*m[1][1] - m[0][1]*m[1][0]) * s; 429 | float v30 = -(v00*m[3][0] + v10*m[3][1] + v20*m[3][2]); 430 | float v31 = -(v01*m[3][0] + v11*m[3][1] + v21*m[3][2]); 431 | float v32 = -(v02*m[3][0] + v12*m[3][1] + v22*m[3][2]); 432 | m[0][0] = v00; 433 | m[0][1] = v01; 434 | m[0][2] = v02; 435 | m[1][0] = v10; 436 | m[1][1] = v11; 437 | m[1][2] = v12; 438 | m[2][0] = v20; 439 | m[2][1] = v21; 440 | m[2][2] = v22; 441 | m[3][0] = v30; 442 | m[3][1] = v31; 443 | m[3][2] = v32; 444 | } 445 | else 446 | { 447 | // transpose matrix 448 | float src[16]; 449 | for ( int i=0; i<4; ++i ) 450 | { 451 | src[i] = m16[i*4]; 452 | src[i + 4] = m16[i*4 + 1]; 453 | src[i + 8] = m16[i*4 + 2]; 454 | src[i + 12] = m16[i*4 + 3]; 455 | } 456 | 457 | // calculate pairs for first 8 elements (cofactors) 458 | float tmp[12]; // temp array for pairs 459 | tmp[0] = src[10] * src[15]; 460 | tmp[1] = src[11] * src[14]; 461 | tmp[2] = src[9] * src[15]; 462 | tmp[3] = src[11] * src[13]; 463 | tmp[4] = src[9] * src[14]; 464 | tmp[5] = src[10] * src[13]; 465 | tmp[6] = src[8] * src[15]; 466 | tmp[7] = src[11] * src[12]; 467 | tmp[8] = src[8] * src[14]; 468 | tmp[9] = src[10] * src[12]; 469 | tmp[10] = src[8] * src[13]; 470 | tmp[11] = src[9] * src[12]; 471 | 472 | // calculate first 8 elements (cofactors) 473 | m16[0] = (tmp[0] * src[5] + tmp[3] * src[6] + tmp[4] * src[7]) - (tmp[1] * src[5] + tmp[2] * src[6] + tmp[5] * src[7]); 474 | m16[1] = (tmp[1] * src[4] + tmp[6] * src[6] + tmp[9] * src[7]) - (tmp[0] * src[4] + tmp[7] * src[6] + tmp[8] * src[7]); 475 | m16[2] = (tmp[2] * src[4] + tmp[7] * src[5] + tmp[10] * src[7]) - (tmp[3] * src[4] + tmp[6] * src[5] + tmp[11] * src[7]); 476 | m16[3] = (tmp[5] * src[4] + tmp[8] * src[5] + tmp[11] * src[6]) - (tmp[4] * src[4] + tmp[9] * src[5] + tmp[10] * src[6]); 477 | m16[4] = (tmp[1] * src[1] + tmp[2] * src[2] + tmp[5] * src[3]) - (tmp[0] * src[1] + tmp[3] * src[2] + tmp[4] * src[3]); 478 | m16[5] = (tmp[0] * src[0] + tmp[7] * src[2] + tmp[8] * src[3]) - (tmp[1] * src[0] + tmp[6] * src[2] + tmp[9] * src[3]); 479 | m16[6] = (tmp[3] * src[0] + tmp[6] * src[1] + tmp[11] * src[3]) - (tmp[2] * src[0] + tmp[7] * src[1] + tmp[10] * src[3]); 480 | m16[7] = (tmp[4] * src[0] + tmp[9] * src[1] + tmp[10] * src[2]) - (tmp[5] * src[0] + tmp[8] * src[1] + tmp[11] * src[2]); 481 | 482 | // calculate pairs for second 8 elements (cofactors) 483 | tmp[0] = src[2] * src[7]; 484 | tmp[1] = src[3] * src[6]; 485 | tmp[2] = src[1] * src[7]; 486 | tmp[3] = src[3] * src[5]; 487 | tmp[4] = src[1] * src[6]; 488 | tmp[5] = src[2] * src[5]; 489 | tmp[6] = src[0] * src[7]; 490 | tmp[7] = src[3] * src[4]; 491 | tmp[8] = src[0] * src[6]; 492 | tmp[9] = src[2] * src[4]; 493 | tmp[10] = src[0] * src[5]; 494 | tmp[11] = src[1] * src[4]; 495 | 496 | // calculate second 8 elements (cofactors) 497 | m16[8] = (tmp[0] * src[13] + tmp[3] * src[14] + tmp[4] * src[15]) - (tmp[1] * src[13] + tmp[2] * src[14] + tmp[5] * src[15]); 498 | m16[9] = (tmp[1] * src[12] + tmp[6] * src[14] + tmp[9] * src[15]) - (tmp[0] * src[12] + tmp[7] * src[14] + tmp[8] * src[15]); 499 | m16[10] = (tmp[2] * src[12] + tmp[7] * src[13] + tmp[10] * src[15]) - (tmp[3] * src[12] + tmp[6] * src[13] + tmp[11] * src[15]); 500 | m16[11] = (tmp[5] * src[12] + tmp[8] * src[13] + tmp[11] * src[14]) - (tmp[4] * src[12] + tmp[9] * src[13] + tmp[10] * src[14]); 501 | m16[12] = (tmp[2] * src[10] + tmp[5] * src[11] + tmp[1] * src[9]) - (tmp[4] * src[11] + tmp[0] * src[9] + tmp[3] * src[10]); 502 | m16[13] = (tmp[8] * src[11] + tmp[0] * src[8] + tmp[7] * src[10]) - (tmp[6] * src[10] + tmp[9] * src[11] + tmp[1] * src[8]); 503 | m16[14] = (tmp[6] * src[9] + tmp[11] * src[11] + tmp[3] * src[8]) - (tmp[10] * src[11] + tmp[2] * src[8] + tmp[7] * src[9]); 504 | m16[15] = (tmp[10] * src[10] + tmp[4] * src[8] + tmp[9] * src[9]) - (tmp[8] * src[9] + tmp[11] * src[10] + tmp[5] * src[8]); 505 | 506 | // calculate determinant 507 | float det = src[0]*m16[0]+src[1]*m16[1]+src[2]*m16[2]+src[3]*m16[3]; 508 | 509 | // calculate matrix inverse 510 | float invdet = 1 / det; 511 | for ( int j=0; j<16; ++j ) 512 | { 513 | m16[j] *= invdet; 514 | } 515 | } 516 | 517 | return det; 518 | 519 | } 520 | 521 | -------------------------------------------------------------------------------- /src/libgizmo/ZCollisionsUtils.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #ifndef ZCOLLISIONSUTILS_H__ 31 | #define ZCOLLISIONSUTILS_H__ 32 | 33 | 34 | /////////////////////////////////////////////////////////////////////////////////////////////////// 35 | 36 | 37 | inline bool CollisionClosestPointOnSegment( const tvector3 & point, const tvector3 & vertPos1, const tvector3 & vertPos2, tvector3& res ) 38 | { 39 | 40 | tvector3 c = point - vertPos1; 41 | tvector3 V; 42 | 43 | V.Normalize(vertPos2 - vertPos1); 44 | float d = (vertPos2 - vertPos1).Length(); 45 | float t = V.Dot(c); 46 | 47 | if (t < 0) 48 | { 49 | return false;//vertPos1; 50 | } 51 | 52 | if (t > d) 53 | { 54 | return false;//vertPos2; 55 | } 56 | 57 | res = vertPos1 + V * t; 58 | return true; 59 | } 60 | 61 | inline tvector3 CollisionClosestPointOnSegment( const tvector3 & point, const tvector3 & vertPos1, const tvector3 & vertPos2 ) 62 | { 63 | 64 | tvector3 c = point - vertPos1; 65 | tvector3 V; 66 | 67 | V.Normalize(vertPos2 - vertPos1); 68 | float d = (vertPos2 - vertPos1).Length(); 69 | float t = V.Dot(c); 70 | 71 | if (t < 0) 72 | { 73 | return vertPos1; 74 | } 75 | 76 | if (t > d) 77 | { 78 | return vertPos2; 79 | } 80 | 81 | return vertPos1 + V * t; 82 | } 83 | 84 | /////////////////////////////////////////////////////////////////////////////////////////////////// 85 | 86 | inline tvector3 CollisionClosestPointOnTriangle( tvector3 & point, tvector3 & vertPos1, tvector3 & vertPos2, tvector3 & vertPos3 ) 87 | { 88 | tvector3 Rab = CollisionClosestPointOnSegment( point, vertPos1, vertPos2 ); 89 | tvector3 Rbc = CollisionClosestPointOnSegment( point, vertPos2, vertPos3 ); 90 | tvector3 Rca = CollisionClosestPointOnSegment( point, vertPos3, vertPos1 ); 91 | 92 | float dRab = (point - Rab).Length(); 93 | float dRbc = (point - Rbc).Length(); 94 | float dRca = (point - Rca).Length(); 95 | 96 | if( (dRab < dRbc) && ( dRab < dRca )) 97 | { 98 | return Rab; 99 | } 100 | else{ 101 | if( (dRbc < dRab) && ( dRbc < dRca )) 102 | { 103 | return Rbc; 104 | } 105 | else{ 106 | return Rca; 107 | } 108 | } 109 | } 110 | 111 | /////////////////////////////////////////////////////////////////////////////////////////////////// 112 | 113 | //inline bool isRayAABBoxIntersect(const tvector3& aMinB, const tvector3& aMaxB, const tvector3& aOrigin, const tvector3& aDir) 114 | #define NUMDIM 3 115 | 116 | inline char HitBoundingBox(float *minB, float *maxB, float *origin, float *dir,float *coord) 117 | { 118 | char inside = TRUE; 119 | char quadrant[NUMDIM]; 120 | register int i; 121 | int whichPlane; 122 | float maxT[NUMDIM]; 123 | float candidatePlane[NUMDIM]; 124 | 125 | /* Find candidate planes; this loop can be avoided if 126 | rays cast all from the eye(assume perpsective view) */ 127 | for (i=0; i maxB[i]) { 133 | quadrant[i] = 0; 134 | candidatePlane[i] = maxB[i]; 135 | inside = FALSE; 136 | }else { 137 | quadrant[i] = 2; 138 | } 139 | 140 | /* Ray origin inside bounding box */ 141 | if(inside) { 142 | coord = origin; 143 | return (TRUE); 144 | } 145 | 146 | 147 | /* Calculate T distances to candidate planes */ 148 | for (i = 0; i < NUMDIM; i++) 149 | if (quadrant[i] != 2 && dir[i] !=0.) 150 | maxT[i] = (candidatePlane[i]-origin[i]) / dir[i]; 151 | else 152 | maxT[i] = -1.; 153 | 154 | /* Get largest of the maxT's for final choice of intersection */ 155 | whichPlane = 0; 156 | for (i = 1; i < NUMDIM; i++) 157 | if (maxT[whichPlane] < maxT[i]) 158 | whichPlane = i; 159 | 160 | /* Check final candidate actually inside box */ 161 | if (maxT[whichPlane] < 0.) return (FALSE); 162 | for (i = 0; i < NUMDIM; i++) 163 | if (whichPlane != i) { 164 | coord[i] = origin[i] + maxT[whichPlane] *dir[i]; 165 | if (coord[i] < minB[i] || coord[i] > maxB[i]) 166 | return (FALSE); 167 | } else { 168 | coord[i] = candidatePlane[i]; 169 | } 170 | return (TRUE); /* ray hits box */ 171 | } 172 | 173 | /////////////////////////////////////////////////////////////////////////////////////////////////// 174 | 175 | inline bool AABBOverlapsSphere ( float *bMin, float *bMax, const float r, float* C ) 176 | { 177 | 178 | float s, d = 0; 179 | 180 | //find the square of the distance 181 | //from the sphere to the box 182 | for( long i=0 ; i<3 ; i++ ) 183 | { 184 | 185 | if( C[i] < bMin[i] ) 186 | { 187 | 188 | s = C[i] - bMin[i]; 189 | d += s*s; 190 | 191 | } 192 | 193 | else if( C[i] > bMax[i] ) 194 | { 195 | 196 | s = C[i] - bMax[i]; 197 | d += s*s; 198 | 199 | } 200 | 201 | } 202 | return d <= r*r; 203 | 204 | } 205 | 206 | inline bool SphereOverlapsSphere (const tvector4& sph1, const tvector4& sph2) 207 | { 208 | float distSq = SquaredDistance( sph1, sph2 ); 209 | float sumSquaredRadius = (sph1.w * sph1.w) + (sph2.w * sph2.w); 210 | 211 | return (distSq <= sumSquaredRadius); 212 | } 213 | 214 | /////////////////////////////////////////////////////////////////////////////////////////////////// 215 | 216 | inline bool isRayAABBoxIntersect(const tvector3& aMinB, const tvector3& aMaxB, const tvector3& aOrigin, const tvector3& aDir) 217 | { 218 | tvector3 dmin(aMinB,aOrigin); 219 | tvector3 dmax(aMaxB,aOrigin); 220 | 221 | int inside=0; 222 | float tmax=-1.f; 223 | 224 | if (dmin.x>0.0f && aDir.x>0.0f) 225 | tmax=dmin.x/aDir.x; 226 | else 227 | if (dmax.x<0.0f && aDir.x<0.0f) 228 | tmax=dmax.x/aDir.x; 229 | else 230 | inside++; 231 | 232 | if (dmax.y<0.0f && aDir.y<0.0f && tmax*aDir.y> dmax.y) 233 | tmax=dmax.y/aDir.y; 234 | else 235 | if (dmin.y>0.0f && aDir.y>0.0f && tmax*aDir.y0.0f && aDir.z>0.0f && tmax*aDir.zdmax.z) 244 | tmax= dmax.z/aDir.z; 245 | else 246 | inside++; 247 | 248 | if (inside==3) 249 | return TRUE; 250 | 251 | if (tmax<0.0f) 252 | return FALSE; 253 | 254 | // Check final candidate actually inside box 255 | tvector3 V = aDir * tmax; 256 | //Mul(V,aDir,tmax); 257 | dmin-=V; 258 | dmax-=V; 259 | dmin-=tvector3::Epsilon; 260 | dmax+=tvector3::Epsilon; 261 | 262 | if (dmin.x>0.0f || dmin.y>0.0f || dmin.z>0.0f || 263 | dmax.x<0.0f || dmax.y <0.0f || dmax.z<0.0f) 264 | return FALSE; 265 | 266 | return TRUE; // ray hits box 267 | } 268 | 269 | /////////////////////////////////////////////////////////////////////////////////////////////////// 270 | 271 | inline float IntersectRayPlane(const tvector3 & rOrigin, const tvector3& rVector, const tvector3& pOrigin, const tvector3 & pNormal) 272 | { 273 | 274 | float d = - pNormal.Dot(pOrigin); 275 | 276 | float numer = pNormal.Dot(rOrigin) + d; 277 | float denom = pNormal.Dot(rVector); 278 | 279 | if (denom == 0) // normal is orthogonal to vector, cant intersect 280 | { 281 | return (-1.0f); 282 | } 283 | 284 | return -(numer / denom); 285 | } 286 | 287 | /////////////////////////////////////////////////////////////////////////////////////////////////// 288 | 289 | /////////////////////////////////////////////////////////////////////////////////////////////////// 290 | // ---------------------------------------------------------------------- 291 | // Name : intersectRaySphere() 292 | // Input : rO - origin of ray in world space 293 | // rV - vector describing direction of ray in world space 294 | // sO - Origin of sphere 295 | // sR - radius of sphere 296 | // Notes : Normalized directional vectors expected 297 | // Return: distance to sphere in world units, -1 if no intersection. 298 | // ----------------------------------------------------------------------- 299 | 300 | inline float IntersectRaySphere(const tvector3 & rO, const tvector3 & rV, const tvector3 & sO, float sR) 301 | { 302 | 303 | tvector3 Q = sO-rO; 304 | 305 | //float c = Q.Length(); 306 | float v = Q.Dot(rV); 307 | float d = sR*sR - (Q.LengthSq() - v*v); 308 | 309 | // If there was no intersection, return -1 310 | if (d < 0.0) 311 | { 312 | return (-1.0f); 313 | } 314 | 315 | // Return the distance to the [first] intersecting point 316 | return (v - MathSqrt(d)); 317 | } 318 | 319 | /////////////////////////////////////////////////////////////////////////////////////////////////// 320 | // ---------------------------------------------------------------------- 321 | // Name : CheckPointInTriangle() 322 | // Input : point - point we wish to check for inclusion 323 | // a - first vertex in triangle 324 | // b - second vertex in triangle 325 | // c - third vertex in triangle 326 | // Notes : Triangle should be defined in clockwise order a,b,c 327 | // Return: TRUE if point is in triangle, FALSE if not. 328 | // ----------------------------------------------------------------------- 329 | 330 | #define ZTRIin(a) ((tulong&) a) 331 | 332 | inline bool CheckPointInTriangle(const tvector3 & point, const tvector3 & pa, const tvector3 & pb, const tvector3& pc) 333 | { 334 | // old Method 335 | 336 | tvector3 e10 = pb-pa; 337 | tvector3 e20 = pc-pa; 338 | 339 | 340 | 341 | float a = e10.Dot(e10); 342 | float b = e10.Dot(e20); 343 | float c = e20.Dot(e20); 344 | float ac_bb = (a*c)-(b*b); 345 | 346 | tvector3 vp(point.x-pa.x, point.y - pa.y, point.z - pa.z); 347 | 348 | 349 | float d = vp.Dot(e10); 350 | float e = vp.Dot(e20); 351 | float x = (d*c) - (e*b); 352 | float y = (e*a) - (d*b); 353 | float z = x+y-ac_bb; 354 | 355 | return (bool)((( ZTRIin(z) & ~(ZTRIin(x)|ZTRIin(y)) ) & 0x80000000)>=1); 356 | 357 | } 358 | 359 | /////////////////////////////////////////////////////////////////////////////////////////////////// 360 | // ---------------------------------------------------------------------- 361 | // Name : CheckPointInTriangle() 362 | // Input : point - point we wish to check for inclusion 363 | // sO - Origin of sphere 364 | // sR - radius of sphere 365 | // Notes : 366 | // Return: TRUE if point is in sphere, FALSE if not. 367 | // ----------------------------------------------------------------------- 368 | 369 | inline bool CheckPointInSphere(const tvector3 & point, const tvector3 & sO, float sR) 370 | { 371 | 372 | float d = (point-sO).Length(); 373 | 374 | if(d<= sR) 375 | { 376 | return true; 377 | } 378 | 379 | return false; 380 | } 381 | 382 | /////////////////////////////////////////////////////////////////////////////////////////////////// 383 | 384 | #define EPSILON 0.000001 385 | #define CROSS(dest,v1,v2) \ 386 | dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \ 387 | dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \ 388 | dest[2]=v1[0]*v2[1]-v1[1]*v2[0]; 389 | #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]) 390 | #define SUB(dest,v1,v2) \ 391 | dest[0]=v1[0]-v2[0]; \ 392 | dest[1]=v1[1]-v2[1]; \ 393 | dest[2]=v1[2]-v2[2]; 394 | 395 | inline int intersect_triangle(float orig[3], float dir[3], 396 | float vert0[3], float vert1[3], float vert2[3], 397 | float *t, float *u, float *v) 398 | { 399 | float edge1[3], edge2[3], tvec[3], pvec[3], qvec[3]; 400 | float det,inv_det; 401 | 402 | /* find vectors for two edges sharing vert0 */ 403 | SUB(edge1, vert1, vert0); 404 | SUB(edge2, vert2, vert0); 405 | 406 | /* begin calculating determinant - also used to calculate U parameter */ 407 | CROSS(pvec, dir, edge2); 408 | 409 | /* if determinant is near zero, ray lies in plane of triangle */ 410 | det = DOT(edge1, pvec); 411 | 412 | #ifdef TEST_CULL /* define TEST_CULL if culling is desired */ 413 | if (det < EPSILON) 414 | return 0; 415 | 416 | /* calculate distance from vert0 to ray origin */ 417 | SUB(tvec, orig, vert0); 418 | 419 | /* calculate U parameter and test bounds */ 420 | *u = DOT(tvec, pvec); 421 | if (*u < 0.0 || *u > det) 422 | return 0; 423 | 424 | /* prepare to test V parameter */ 425 | CROSS(qvec, tvec, edge1); 426 | 427 | /* calculate V parameter and test bounds */ 428 | *v = DOT(dir, qvec); 429 | if (*v < 0.0 || *u + *v > det) 430 | return 0; 431 | 432 | /* calculate t, scale parameters, ray intersects triangle */ 433 | *t = DOT(edge2, qvec); 434 | inv_det = 1.0 / det; 435 | *t *= inv_det; 436 | *u *= inv_det; 437 | *v *= inv_det; 438 | #else /* the non-culling branch */ 439 | if (det > -EPSILON && det < EPSILON) 440 | return 0; 441 | inv_det = 1.0f / det; 442 | 443 | /* calculate distance from vert0 to ray origin */ 444 | SUB(tvec, orig, vert0); 445 | 446 | /* calculate U parameter and test bounds */ 447 | *u = DOT(tvec, pvec) * inv_det; 448 | if (*u < 0.0 || *u > 1.0) 449 | return 0; 450 | 451 | /* prepare to test V parameter */ 452 | CROSS(qvec, tvec, edge1); 453 | 454 | /* calculate V parameter and test bounds */ 455 | *v = DOT(dir, qvec) * inv_det; 456 | if (*v < 0.0 || *u + *v > 1.0) 457 | return 0; 458 | 459 | /* calculate t, ray intersects triangle */ 460 | *t = DOT(edge2, qvec) * inv_det; 461 | #endif 462 | return 1; 463 | } 464 | 465 | 466 | inline bool IsPointInCone(const tvector3& point, const tvector3& conepos, const tvector3& conedir, float length, float radius) 467 | { 468 | tvector3 topToPoint = point-conepos; 469 | 470 | // conedir is normalized 471 | 472 | float dt = topToPoint.Dot(conedir); 473 | 474 | if ((dt <0) || (dt>length)) 475 | return false; 476 | 477 | float dist = Distance(point, conepos + (conedir*dt) ); 478 | 479 | float ht = LERP(0, radius, dt/length); 480 | 481 | if (dist > ht) 482 | return false; 483 | 484 | return true; 485 | } 486 | 487 | #endif 488 | -------------------------------------------------------------------------------- /src/libgizmo/ZMathsFunc.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #include "stdafx.h" 31 | #include "ZMathsFunc.h" 32 | // Globals //////////////////////////////////////////////////////////////////////////////////////// 33 | 34 | 35 | /////////////////////////////////////////////////////////////////////////////////////////////////// 36 | 37 | /////////////////////////////////////////////////////////////////////////////////////////////////// 38 | 39 | float MathFloatBezierCubic(const float &v1, const float &v2, const float& v3, const float& v4, const float s) 40 | { 41 | float x = v1 * MathBezierCubicSplineF4(s) + v2 * MathBezierCubicSplineF3(s) + v4 * MathBezierCubicSplineF2(s) + v3 * MathBezierCubicSplineF1(s); 42 | return x; 43 | } 44 | /////////////////////////////////////////////////////////////////////////////////////////////////// 45 | -------------------------------------------------------------------------------- /src/libgizmo/ZMathsFunc.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #ifndef ZMATHSFUNCS_H 31 | #define ZMATHSFUNCS_H 32 | 33 | // Includes /////////////////////////////////////////////////////////////////////////////////////// 34 | 35 | #include 36 | #include 37 | #include "ZBaseDefs.h" 38 | 39 | // Constants ////////////////////////////////////////////////////////////////////////////////////// 40 | 41 | const float ZPI = 3.14159265358979323846f; 42 | 43 | const float PI_MUL_2 = 6.28318530717958647692f; 44 | const float PI_DIV_2 = 1.57079632679489655800f; 45 | const float PI_DIV_4 = 0.78539816339744827900f; 46 | const float INV_PI = 0.31830988618379069122f; 47 | const float DEGTORAD = 0.01745329251994329547f; 48 | const float RADTODEG = 57.29577951308232286465f; 49 | const float SQRT2 = 1.41421356237309504880f; 50 | const float SQRT3 = 1.73205080756887729352f; 51 | 52 | #define Rad2Deg(x) ((x)*180.f*INV_PI) 53 | #define Deg2Rad(x) ((x)*ZPI/180.f) 54 | 55 | 56 | inline bool MathFloatIsVeryClose(float flt1, float flt2) 57 | { 58 | return (fabsf(flt1-flt2)<0.001f); 59 | } 60 | 61 | #define PSwap(a, b) do {\ 62 | char c[sizeof(a)]; \ 63 | memcpy((void *)&c, (void *)&a, sizeof(c)); \ 64 | memcpy((void *)&a, (void *)&b, sizeof(a)); \ 65 | memcpy((void *)&b, (void *)&c, sizeof(b)); \ 66 | } while (0) 67 | 68 | #define PMax(a,b) ((a>b)?a:b) 69 | #define PMin(a,b) ((a= 0.0f) 174 | { 175 | return value; 176 | } 177 | 178 | return -value; 179 | 180 | } 181 | 182 | 183 | /////////////////////////////////////////////////////////////////////////////////////////////////// 184 | 185 | inline tlong MathFloatRound(const float value) 186 | { 187 | tlong newValue; 188 | double fract, ent; 189 | 190 | fract = modf(value, &ent); 191 | 192 | newValue = (tlong)ent; 193 | 194 | if(fract > 0.5f) 195 | { 196 | newValue++; 197 | } 198 | else 199 | { 200 | if(fract < -0.5f) 201 | { 202 | newValue--; 203 | } 204 | } 205 | 206 | return newValue; 207 | } 208 | 209 | /////////////////////////////////////////////////////////////////////////////////////////////////// 210 | 211 | inline bool MathFloatIsEqual(const float value,const float value2) 212 | { 213 | if(MathFloatAbs(value-value2)>RealEpsilon) 214 | { 215 | return false; 216 | } 217 | 218 | return true; 219 | } 220 | 221 | /////////////////////////////////////////////////////////////////////////////////////////////////// 222 | 223 | inline bool MathFloatIsGreater(const float value, const float value2) 224 | { 225 | 226 | if(MathFloatIsEqual(value, value2)) 227 | { 228 | return false; 229 | } 230 | 231 | if((value-value2)>0.0f) 232 | { 233 | return true; 234 | } 235 | 236 | return false; 237 | } 238 | 239 | /////////////////////////////////////////////////////////////////////////////////////////////////// 240 | 241 | inline bool MathFloatIsGreaterOrEqual(const float value, const float value2) 242 | { 243 | 244 | if(MathFloatIsEqual(value, value2)) 245 | { 246 | return true; 247 | } 248 | 249 | if((value-value2)>0.0f) 250 | { 251 | return true; 252 | } 253 | 254 | return false; 255 | } 256 | 257 | 258 | /////////////////////////////////////////////////////////////////////////////////////////////////// 259 | 260 | inline bool MathQuadraticFormula(float A, float B, float C, float * u0, float * u1) 261 | { 262 | float q = B*B - 4*A*C; 263 | if(q>=0) 264 | { 265 | float sq = (float)MathSqrt(q); 266 | float d = 1/(2*A); 267 | *u0 = ( -B + sq ) * d; 268 | *u1 = ( -B - sq ) * d; 269 | return true; 270 | } 271 | else 272 | { 273 | return false; 274 | } 275 | } 276 | 277 | 278 | /////////////////////////////////////////////////////////////////////////////////////////////////// 279 | 280 | inline tdouble MathDoubleAbs(const tdouble value) 281 | { 282 | 283 | if(value >= 0.0) 284 | { 285 | return value; 286 | } 287 | 288 | return -value; 289 | 290 | } 291 | 292 | /////////////////////////////////////////////////////////////////////////////////////////////////// 293 | 294 | float MathFloatBezierCubic(const float &v1, const float &v2, const float& v3, const float& v4, const float s); 295 | 296 | /////////////////////////////////////////////////////////////////////////////////////////////////// 297 | 298 | inline float MathParQuadSplineF1(float t) 299 | { 300 | return t*(2.0f*t-1.0f); 301 | } 302 | 303 | /////////////////////////////////////////////////////////////////////////////////////////////////// 304 | 305 | inline float MathParQuadSplineF2(float t) 306 | { 307 | return 4.0f*t*(1.0f-t); 308 | } 309 | 310 | /////////////////////////////////////////////////////////////////////////////////////////////////// 311 | 312 | inline float MathParQuadSplineF3(float t) 313 | { 314 | return (t-1.0f)*(2.0f*t-1.0f); 315 | } 316 | 317 | /////////////////////////////////////////////////////////////////////////////////////////////////// 318 | 319 | inline float MathParCubicSplineF1(float t) 320 | { 321 | return t*(4.5f*t*(t-1.0f)+1.0f); 322 | } 323 | 324 | /////////////////////////////////////////////////////////////////////////////////////////////////// 325 | 326 | inline float MathParCubicSplineF2(float t) 327 | { 328 | return 4.5f*t*((t-1.0f)*(-3.0f*t+1.0f)); 329 | } 330 | 331 | /////////////////////////////////////////////////////////////////////////////////////////////////// 332 | 333 | inline float MathParCubicSplineF3(float t) 334 | { 335 | return 4.5f*t*((t-1.0f)*(3.0f*t-2.0f)); 336 | } 337 | 338 | /////////////////////////////////////////////////////////////////////////////////////////////////// 339 | 340 | inline float MathParCubicSplineF4(float t) 341 | { 342 | return 4.5f*t*(t*(2.0f-t))-5.5f*t+1.0f; 343 | } 344 | 345 | /////////////////////////////////////////////////////////////////////////////////////////////////// 346 | 347 | inline float MathBezierQuadSplineF1(float t) 348 | { 349 | return t*t; 350 | } 351 | 352 | /////////////////////////////////////////////////////////////////////////////////////////////////// 353 | 354 | inline float MathBezierQuadSplineF2(float t) 355 | { 356 | return 2.0f*t*(1.0f-t); 357 | } 358 | 359 | /////////////////////////////////////////////////////////////////////////////////////////////////// 360 | 361 | inline float MathBezierQuadSplineF3(float t) 362 | { 363 | return (1.0f-t)*(1.0f-t); 364 | } 365 | 366 | /////////////////////////////////////////////////////////////////////////////////////////////////// 367 | 368 | inline float MathBezierCubicSplineF1(float t) 369 | { 370 | return t*t*t; 371 | } 372 | 373 | /////////////////////////////////////////////////////////////////////////////////////////////////// 374 | 375 | inline float MathBezierCubicSplineF2(float t) 376 | { 377 | return 3.0f*t*t*(1.0f-t); 378 | } 379 | 380 | /////////////////////////////////////////////////////////////////////////////////////////////////// 381 | 382 | inline float MathBezierCubicSplineF3(float t) 383 | { 384 | return 3.0f*t*(1.0f-t)*(1.0f-t); 385 | } 386 | 387 | /////////////////////////////////////////////////////////////////////////////////////////////////// 388 | 389 | inline float MathBezierCubicSplineF4(float t) 390 | { 391 | return (1.0f-t)*(1.0f-t)*(1.0f-t); 392 | } 393 | 394 | /////////////////////////////////////////////////////////////////////////////////////////////////// 395 | 396 | inline float MathCoonsQuadSplineF1(float t) 397 | { 398 | return (1.0f-t)*(1.0f+t); 399 | } 400 | 401 | /////////////////////////////////////////////////////////////////////////////////////////////////// 402 | 403 | inline float MathCoonsQuadSplineF2(float t) 404 | { 405 | return t*t; 406 | } 407 | 408 | /////////////////////////////////////////////////////////////////////////////////////////////////// 409 | 410 | inline float MathCoonsQuadSplineF3(float t) 411 | { 412 | return t*(1.0f-t); 413 | } 414 | 415 | /////////////////////////////////////////////////////////////////////////////////////////////////// 416 | 417 | inline float MathCoonsCubicSplineF1(float t) 418 | { 419 | return t*t*(2.0f*t-3.0f)+1.0f; 420 | } 421 | 422 | /////////////////////////////////////////////////////////////////////////////////////////////////// 423 | 424 | inline float MathCoonsCubicSplineF2(float t) 425 | { 426 | return t*t*(3.0f-2.0f*t); 427 | } 428 | 429 | /////////////////////////////////////////////////////////////////////////////////////////////////// 430 | 431 | inline float MathCoonsCubicSplineF3(float t) 432 | { 433 | return t*((t-1.0f)*(t-1.0f)); 434 | } 435 | 436 | /////////////////////////////////////////////////////////////////////////////////////////////////// 437 | 438 | inline float MathCoonsCubicSplineF4(float t) 439 | { 440 | return t*t*(t-1.0f); 441 | } 442 | 443 | /////////////////////////////////////////////////////////////////////////////////////////////////// 444 | 445 | __forceinline float MathCos(const float f) 446 | { 447 | #ifdef PSM_GCC 448 | return (float)cos(f); 449 | #else 450 | return cosf(f); 451 | #endif 452 | } 453 | 454 | /////////////////////////////////////////////////////////////////////////////////////////////////// 455 | 456 | __forceinline float MathSin(const float f) 457 | { 458 | #ifdef PSM_GCC 459 | return (float)sin(f); 460 | #else 461 | return sinf(f); 462 | #endif 463 | } 464 | 465 | /////////////////////////////////////////////////////////////////////////////////////////////////// 466 | 467 | __forceinline float MathTan(const float f) 468 | { 469 | #ifdef PSM_GCC 470 | return (float)tan(f); 471 | #else 472 | return tanf(f); 473 | #endif 474 | } 475 | 476 | /////////////////////////////////////////////////////////////////////////////////////////////////// 477 | 478 | __forceinline float MathACos(const float f) 479 | { 480 | #ifdef PSM_GCC 481 | return (float)acos(f); 482 | #else 483 | if(f> -1.0) 484 | { 485 | if( f < 1.0 ) 486 | { 487 | return (float)(acos(f)); 488 | } 489 | else 490 | { 491 | return 0.0f; 492 | } 493 | } 494 | else 495 | { 496 | return ZPI; 497 | } 498 | #endif 499 | } 500 | 501 | /////////////////////////////////////////////////////////////////////////////////////////////////// 502 | 503 | __forceinline float MathASin(const float f) 504 | { 505 | #ifdef PSM_GCC 506 | return (float)asin(f); 507 | #else 508 | return asinf(f); 509 | #endif 510 | } 511 | 512 | /////////////////////////////////////////////////////////////////////////////////////////////////// 513 | 514 | __forceinline float MathATan(const float f) 515 | { 516 | #ifdef PSM_GCC 517 | return atan(f); 518 | #else 519 | return atanf(f); 520 | #endif 521 | } 522 | 523 | 524 | /////////////////////////////////////////////////////////////////////////////////////////////////// 525 | 526 | inline bool GetLowestRoot(float a, float b, float c, float maxR, float* root) 527 | { 528 | // Check if a solution exists 529 | float determinant = b*b - 4.0f*a*c; 530 | 531 | // If determinant is negative it means no solutions. 532 | if (determinant < 0.0f) 533 | { 534 | return false; 535 | } 536 | 537 | // calculate the two roots: (if determinant == 0 then 538 | // x1==x2 but let's disregard that slight optimization) 539 | float sqrtD = (float)MathSqrt(determinant); 540 | float r1 = (-b - sqrtD) / (2*a); 541 | float r2 = (-b + sqrtD) / (2*a); 542 | 543 | // Sort so x1 <= x2 544 | if (r1 > r2) 545 | { 546 | float temp = r2; 547 | r2 = r1; 548 | r1 = temp; 549 | } 550 | 551 | // Get lowest root: 552 | if (r1 > 0 && r1 < maxR) 553 | { 554 | *root = r1; 555 | return true; 556 | } 557 | 558 | // It is possible that we want x2 - this can happen 559 | // if x1 < 0 560 | if (r2 > 0 && r2 < maxR) 561 | { 562 | *root = r2; 563 | return true; 564 | } 565 | 566 | // No (valid) solutions 567 | return false; 568 | } 569 | 570 | /////////////////////////////////////////////////////////////////////////////////////////////////// 571 | 572 | inline float Clamp(float val, float inf, float sup) 573 | { 574 | if (valsup) return sup; 576 | return val; 577 | } 578 | 579 | #endif 580 | 581 | 582 | -------------------------------------------------------------------------------- /src/libgizmo/libgizmo.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | {5AEF535C-D65A-40B7-AA87-8130CF1B4650} 39 | Win32Proj 40 | libgizmo 41 | 42 | 43 | 44 | StaticLibrary 45 | true 46 | Unicode 47 | 48 | 49 | StaticLibrary 50 | false 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | $(ProjectName)Debug 66 | 67 | 68 | 69 | 70 | 71 | Level3 72 | Disabled 73 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) 74 | $(SolutionDir)\..\inc;%(AdditionalIncludeDirectories) 75 | false 76 | MultiThreadedDebug 77 | 78 | 79 | Windows 80 | true 81 | 82 | 83 | $(SolutionDir)\..\lib\$(TargetName)$(TargetExt) 84 | 85 | 86 | 87 | 88 | Level3 89 | 90 | 91 | MaxSpeed 92 | true 93 | true 94 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) 95 | $(SolutionDir)\..\inc 96 | false 97 | MultiThreaded 98 | 99 | 100 | Windows 101 | true 102 | true 103 | true 104 | 105 | 106 | $(SolutionDir)\..\lib\$(TargetName)$(TargetExt) 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /src/libgizmo/libgizmo.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | Header Files 29 | 30 | 31 | Header Files 32 | 33 | 34 | Header Files 35 | 36 | 37 | Header Files 38 | 39 | 40 | Header Files 41 | 42 | 43 | Header Files 44 | 45 | 46 | Header Files 47 | 48 | 49 | Header Files 50 | 51 | 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | Source Files 64 | 65 | 66 | Source Files 67 | 68 | 69 | Source Files 70 | 71 | 72 | 73 | 74 | Header Files 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/libgizmo/libgizmo.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | -------------------------------------------------------------------------------- /src/libgizmo/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // libGizmo.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /src/libgizmo/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | #include "LibBase.h" 10 | 11 | #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 12 | 13 | 14 | 15 | // TODO: reference additional headers your program requires here 16 | -------------------------------------------------------------------------------- /src/libgizmo/targetver.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////////////////////////// 2 | // LibGizmo 3 | // File Name : 4 | // Creation : 10/01/2012 5 | // Author : Cedric Guillemet 6 | // Description : LibGizmo 7 | // 8 | ///Copyright (C) 2012 Cedric Guillemet 9 | // 10 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 11 | // this software and associated documentation files (the "Software"), to deal in 12 | // the Software without restriction, including without limitation the rights to 13 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 14 | //of the Software, and to permit persons to whom the Software is furnished to do 15 | ///so, subject to the following conditions: 16 | // 17 | // The above copyright notice and this permission notice shall be included in all 18 | // copies or substantial portions of the Software. 19 | // 20 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | ///FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | // SOFTWARE. 27 | // 28 | 29 | 30 | #pragma once 31 | 32 | // Including SDKDDKVer.h defines the highest available Windows platform. 33 | 34 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 35 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 36 | 37 | #include 38 | -------------------------------------------------------------------------------- /src/win32example/Lesson6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * This Code Was Created By Jeff Molofee 2000 3 | * A HUGE Thanks To Fredric Echols For Cleaning Up 4 | * And Optimizing The Base Code, Making It More Flexible! 5 | * If You've Found This Code Useful, Please Let Me Know. 6 | * Visit My Site At nehe.gamedev.net 7 | */ 8 | 9 | #include // Header File For Windows 10 | #include // Header File For Standard Input/Output 11 | #include // Header File For The OpenGL32 Library 12 | #include // Header File For The GLu32 Library 13 | #include "IGizmo.h" 14 | 15 | //float objectMatrix[16]; 16 | 17 | float objectMatrix[16]={-0.3210,0.0000,0.9471,0.0000,0.0000,1.0000,0.0000,0.0000,-0.9471,0.0000,-0.3210,0.0000,-137.1790,16.4949,375.4003,1.0000}; 18 | 19 | IGizmo *gizmo = NULL; 20 | 21 | IGizmo *gizmoMove, *gizmoRotate, *gizmoScale; 22 | int screenWidth, screenHeight; 23 | 24 | int mousex; 25 | int mousey; 26 | bool mbMouseDown = 0; 27 | 28 | HDC hDC=NULL; // Private GDI Device Context 29 | HGLRC hRC=NULL; // Permanent Rendering Context 30 | HWND hWnd=NULL; // Holds Our Window Handle 31 | HINSTANCE hInstance; // Holds The Instance Of The Application 32 | 33 | bool keys[256]; // Array Used For The Keyboard Routine 34 | bool active=TRUE; // Window Active Flag Set To TRUE By Default 35 | bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default 36 | 37 | GLfloat xrot; // X Rotation ( NEW ) 38 | GLfloat yrot; // Y Rotation ( NEW ) 39 | GLfloat zrot; // Z Rotation ( NEW ) 40 | 41 | GLuint texture[1]; // Storage For One Texture ( NEW ) 42 | 43 | LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc 44 | 45 | int LoadGLTextures() // Load Bitmaps And Convert To Textures 46 | { 47 | //objectMatrix.Identity(); 48 | /* 49 | objectMatrix[0] = 1.f; 50 | objectMatrix[1] = 0.f; 51 | objectMatrix[2] = 0.f; 52 | objectMatrix[3] = 0.f; 53 | objectMatrix[4] = 0.f; 54 | objectMatrix[5] = 1.f; 55 | objectMatrix[6] = 0.f; 56 | objectMatrix[7] = 0.f; 57 | objectMatrix[8] = 0.f; 58 | objectMatrix[9] = 0.f; 59 | objectMatrix[10] = 1.f; 60 | objectMatrix[11] = 0.f; 61 | objectMatrix[12] = 0.f; 62 | objectMatrix[13] = 0.f; 63 | objectMatrix[14] = 0.f; 64 | objectMatrix[15] = 1.f; 65 | */ 66 | gizmoMove = CreateMoveGizmo(); 67 | gizmoRotate = CreateRotateGizmo(); 68 | gizmoScale = CreateScaleGizmo(); 69 | 70 | gizmo = gizmoMove; 71 | 72 | gizmo->SetEditMatrix( objectMatrix ); 73 | gizmo->SetScreenDimension( screenWidth, screenHeight ); 74 | gizmoMove->SetDisplayScale( 2.f ); 75 | gizmoRotate->SetDisplayScale( 2.f ); 76 | gizmoScale->SetDisplayScale( 2.f ); 77 | return TRUE; // Return The Status 78 | } 79 | 80 | GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window 81 | { 82 | if (height==0) // Prevent A Divide By Zero By 83 | { 84 | height=1; // Making Height Equal One 85 | } 86 | 87 | glViewport(0,0,width,height); // Reset The Current Viewport 88 | 89 | glMatrixMode(GL_PROJECTION); // Select The Projection Matrix 90 | glLoadIdentity(); // Reset The Projection Matrix 91 | 92 | // Calculate The Aspect Ratio Of The Window 93 | gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); 94 | 95 | glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix 96 | glLoadIdentity(); // Reset The Modelview Matrix 97 | gluLookAt(5,5,5,0,0,0,0,1,0); 98 | 99 | screenWidth = width; screenHeight = height; 100 | 101 | if (gizmo) 102 | gizmo->SetScreenDimension( screenWidth, screenHeight ); 103 | } 104 | 105 | int InitGL(GLvoid) // All Setup For OpenGL Goes Here 106 | { 107 | if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW ) 108 | { 109 | return FALSE; // If Texture Didn't Load Return FALSE 110 | } 111 | 112 | glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW ) 113 | glShadeModel(GL_SMOOTH); // Enable Smooth Shading 114 | glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background 115 | glClearDepth(1.0f); // Depth Buffer Setup 116 | glEnable(GL_DEPTH_TEST); // Enables Depth Testing 117 | glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do 118 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations 119 | glDepthMask(1); 120 | return TRUE; // Initialization Went OK 121 | } 122 | 123 | int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing 124 | { 125 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer 126 | /* 127 | float viewMat[16]; 128 | float projMat[16]; 129 | 130 | glGetFloatv (GL_MODELVIEW_MATRIX, viewMat ); 131 | glGetFloatv (GL_PROJECTION_MATRIX, projMat ); 132 | */ 133 | 134 | float viewMat[16]={-0.4747,-0.2647,0.8394,0.0000,0.0000,0.9537,0.3007,0.0000,-0.8802,0.1427,-0.4527,0.0000,180.6443,-110.9036,-91.6591,1.0000}; 135 | float projMat[16]={0.5625,0.0000,0.0000,0.0000,0.0000,1.0000,0.0000,0.0000,0.0000,0.0000,-1.0002,-1.0000,0.0000,0.0000,-0.2000,0.0000}; 136 | 137 | glMatrixMode( GL_PROJECTION ); 138 | glLoadIdentity(); 139 | glLoadMatrixf( projMat ); 140 | 141 | glMatrixMode( GL_MODELVIEW ); 142 | glLoadIdentity(); 143 | glLoadMatrixf( viewMat ); 144 | 145 | glPushMatrix(); 146 | glMultMatrixf( objectMatrix ); 147 | glScalef(50,50,50); 148 | glColor4f(0.5f, 0.5f, 0.5f, 1.f); 149 | glCullFace(GL_CW); 150 | glEnable(GL_CULL_FACE); 151 | glBegin(GL_QUADS); 152 | // Front Face 153 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 154 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 155 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 156 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 157 | // Back Face 158 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 159 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 160 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 161 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 162 | // Top Face 163 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 164 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 165 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 166 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 167 | // Bottom Face 168 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 169 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 170 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 171 | glColor4f(0.6f,0.6f,0.6f,1.f);glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 172 | // Right face 173 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); 174 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); 175 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); 176 | glColor4f(0.5f,0.5f,0.5f,1.f);glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); 177 | // Left Face 178 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); 179 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); 180 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); 181 | glColor4f(0.4f,0.4f,0.4f,1.f);glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); 182 | glEnd(); 183 | glPopMatrix(); 184 | 185 | if (gizmo) 186 | { 187 | 188 | gizmo->SetCameraMatrix( viewMat, projMat ); 189 | gizmo->Draw(); 190 | } 191 | 192 | return TRUE; // Keep Going 193 | } 194 | 195 | GLvoid KillGLWindow(GLvoid) // Properly Kill The Window 196 | { 197 | if (fullscreen) // Are We In Fullscreen Mode? 198 | { 199 | ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop 200 | ShowCursor(TRUE); // Show Mouse Pointer 201 | } 202 | 203 | if (hRC) // Do We Have A Rendering Context? 204 | { 205 | if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts? 206 | { 207 | MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); 208 | } 209 | 210 | if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC? 211 | { 212 | MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); 213 | } 214 | hRC=NULL; // Set RC To NULL 215 | } 216 | 217 | if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC 218 | { 219 | MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); 220 | hDC=NULL; // Set DC To NULL 221 | } 222 | 223 | if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window? 224 | { 225 | MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); 226 | hWnd=NULL; // Set hWnd To NULL 227 | } 228 | 229 | if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class 230 | { 231 | MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION); 232 | hInstance=NULL; // Set hInstance To NULL 233 | } 234 | } 235 | 236 | /* This Code Creates Our OpenGL Window. Parameters Are: * 237 | * title - Title To Appear At The Top Of The Window * 238 | * width - Width Of The GL Window Or Fullscreen Mode * 239 | * height - Height Of The GL Window Or Fullscreen Mode * 240 | * bits - Number Of Bits To Use For Color (8/16/24/32) * 241 | * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */ 242 | 243 | BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag) 244 | { 245 | GLuint PixelFormat; // Holds The Results After Searching For A Match 246 | WNDCLASS wc; // Windows Class Structure 247 | DWORD dwExStyle; // Window Extended Style 248 | DWORD dwStyle; // Window Style 249 | RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values 250 | WindowRect.left=(long)0; // Set Left Value To 0 251 | WindowRect.right=(long)width; // Set Right Value To Requested Width 252 | WindowRect.top=(long)0; // Set Top Value To 0 253 | WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height 254 | 255 | fullscreen=fullscreenflag; // Set The Global Fullscreen Flag 256 | 257 | hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window 258 | wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window. 259 | wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages 260 | wc.cbClsExtra = 0; // No Extra Window Data 261 | wc.cbWndExtra = 0; // No Extra Window Data 262 | wc.hInstance = hInstance; // Set The Instance 263 | wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon 264 | wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer 265 | wc.hbrBackground = NULL; // No Background Required For GL 266 | wc.lpszMenuName = NULL; // We Don't Want A Menu 267 | wc.lpszClassName = "OpenGL"; // Set The Class Name 268 | 269 | if (!RegisterClass(&wc)) // Attempt To Register The Window Class 270 | { 271 | MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); 272 | return FALSE; // Return FALSE 273 | } 274 | 275 | if (fullscreen) // Attempt Fullscreen Mode? 276 | { 277 | DEVMODE dmScreenSettings; // Device Mode 278 | memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared 279 | dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure 280 | dmScreenSettings.dmPelsWidth = width; // Selected Screen Width 281 | dmScreenSettings.dmPelsHeight = height; // Selected Screen Height 282 | dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel 283 | dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; 284 | 285 | // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. 286 | if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) 287 | { 288 | // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. 289 | if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) 290 | { 291 | fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE 292 | } 293 | else 294 | { 295 | // Pop Up A Message Box Letting User Know The Program Is Closing. 296 | MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP); 297 | return FALSE; // Return FALSE 298 | } 299 | } 300 | } 301 | 302 | if (fullscreen) // Are We Still In Fullscreen Mode? 303 | { 304 | dwExStyle=WS_EX_APPWINDOW; // Window Extended Style 305 | dwStyle=WS_POPUP; // Windows Style 306 | ShowCursor(FALSE); // Hide Mouse Pointer 307 | } 308 | else 309 | { 310 | dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style 311 | dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style 312 | } 313 | 314 | AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size 315 | 316 | // Create The Window 317 | if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window 318 | "OpenGL", // Class Name 319 | title, // Window Title 320 | dwStyle | // Defined Window Style 321 | WS_CLIPSIBLINGS | // Required Window Style 322 | WS_CLIPCHILDREN, // Required Window Style 323 | 0, 0, // Window Position 324 | WindowRect.right-WindowRect.left, // Calculate Window Width 325 | WindowRect.bottom-WindowRect.top, // Calculate Window Height 326 | NULL, // No Parent Window 327 | NULL, // No Menu 328 | hInstance, // Instance 329 | NULL))) // Dont Pass Anything To WM_CREATE 330 | { 331 | KillGLWindow(); // Reset The Display 332 | MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION); 333 | return FALSE; // Return FALSE 334 | } 335 | 336 | static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be 337 | { 338 | sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 339 | 1, // Version Number 340 | PFD_DRAW_TO_WINDOW | // Format Must Support Window 341 | PFD_SUPPORT_OPENGL | // Format Must Support OpenGL 342 | PFD_DOUBLEBUFFER, // Must Support Double Buffering 343 | PFD_TYPE_RGBA, // Request An RGBA Format 344 | bits, // Select Our Color Depth 345 | 0, 0, 0, 0, 0, 0, // Color Bits Ignored 346 | 0, // No Alpha Buffer 347 | 0, // Shift Bit Ignored 348 | 0, // No Accumulation Buffer 349 | 0, 0, 0, 0, // Accumulation Bits Ignored 350 | 24, // 16Bit Z-Buffer (Depth Buffer) 351 | 8, // No Stencil Buffer 352 | 0, // No Auxiliary Buffer 353 | PFD_MAIN_PLANE, // Main Drawing Layer 354 | 0, // Reserved 355 | 0, 0, 0 // Layer Masks Ignored 356 | }; 357 | 358 | if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context? 359 | { 360 | KillGLWindow(); // Reset The Display 361 | MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); 362 | return FALSE; // Return FALSE 363 | } 364 | 365 | if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format? 366 | { 367 | KillGLWindow(); // Reset The Display 368 | MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); 369 | return FALSE; // Return FALSE 370 | } 371 | 372 | if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format? 373 | { 374 | KillGLWindow(); // Reset The Display 375 | MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); 376 | return FALSE; // Return FALSE 377 | } 378 | 379 | if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context? 380 | { 381 | KillGLWindow(); // Reset The Display 382 | MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); 383 | return FALSE; // Return FALSE 384 | } 385 | 386 | if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context 387 | { 388 | KillGLWindow(); // Reset The Display 389 | MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); 390 | return FALSE; // Return FALSE 391 | } 392 | 393 | ShowWindow(hWnd,SW_SHOW); // Show The Window 394 | SetForegroundWindow(hWnd); // Slightly Higher Priority 395 | SetFocus(hWnd); // Sets Keyboard Focus To The Window 396 | ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen 397 | 398 | if (!InitGL()) // Initialize Our Newly Created GL Window 399 | { 400 | KillGLWindow(); // Reset The Display 401 | MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION); 402 | return FALSE; // Return FALSE 403 | } 404 | 405 | return TRUE; // Success 406 | } 407 | 408 | LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window 409 | UINT uMsg, // Message For This Window 410 | WPARAM wParam, // Additional Message Information 411 | LPARAM lParam) // Additional Message Information 412 | { 413 | switch (uMsg) // Check For Windows Messages 414 | { 415 | case WM_ACTIVATE: // Watch For Window Activate Message 416 | { 417 | if (!HIWORD(wParam)) // Check Minimization State 418 | { 419 | active=TRUE; // Program Is Active 420 | } 421 | else 422 | { 423 | active=FALSE; // Program Is No Longer Active 424 | } 425 | 426 | return 0; // Return To The Message Loop 427 | } 428 | 429 | case WM_SYSCOMMAND: // Intercept System Commands 430 | { 431 | switch (wParam) // Check System Calls 432 | { 433 | case SC_SCREENSAVE: // Screensaver Trying To Start? 434 | case SC_MONITORPOWER: // Monitor Trying To Enter Powersave? 435 | return 0; // Prevent From Happening 436 | } 437 | break; // Exit 438 | } 439 | case WM_KEYDOWN: 440 | if ( wParam == 0x31 ) 441 | { 442 | gizmo = gizmoMove; 443 | gizmo->SetLocation( IGizmo::LOCATE_WORLD ); 444 | } 445 | else if ( wParam==0x32 ) 446 | { 447 | gizmo = gizmoRotate; 448 | gizmo->SetLocation( IGizmo::LOCATE_LOCAL ); 449 | } 450 | else if ( wParam==0x33 ) 451 | { 452 | gizmo = gizmoScale; 453 | gizmo->SetLocation( IGizmo::LOCATE_WORLD ); 454 | } 455 | 456 | gizmo->SetEditMatrix( objectMatrix ); 457 | gizmo->SetScreenDimension( screenWidth, screenHeight ); 458 | //gizmo->SetLocation( IGizmo::LOCATE_VIEW ); 459 | //gizmo->SetLocation( IGizmo::LOCATE_LOCAL ); 460 | break; 461 | case WM_CLOSE: // Did We Receive A Close Message? 462 | { 463 | PostQuitMessage(0); // Send A Quit Message 464 | return 0; // Jump Back 465 | } 466 | case WM_MOUSEMOVE: 467 | { 468 | mousex = LOWORD(lParam); 469 | mousey = HIWORD(lParam); 470 | if (gizmo) 471 | //gizmo->OnMouseMove( 513, 366 );//mousex, mousey ); 472 | gizmo->OnMouseMove( mousex, mousey ); 473 | return 0; 474 | } 475 | case WM_LBUTTONDOWN: 476 | mbMouseDown = true; 477 | if (gizmo) 478 | if (gizmo->OnMouseDown( mousex, mousey )) 479 | SetCapture( hWnd ); 480 | break; 481 | case WM_LBUTTONUP: 482 | ReleaseCapture(); 483 | mbMouseDown = false; 484 | if (gizmo) 485 | gizmo->OnMouseUp( mousex, mousey ); 486 | break; 487 | 488 | 489 | case WM_SIZE: // Resize The OpenGL Window 490 | { 491 | ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height 492 | return 0; // Jump Back 493 | } 494 | } 495 | 496 | // Pass All Unhandled Messages To DefWindowProc 497 | return DefWindowProc(hWnd,uMsg,wParam,lParam); 498 | } 499 | 500 | int WINAPI WinMain( HINSTANCE hInstance, // Instance 501 | HINSTANCE hPrevInstance, // Previous Instance 502 | LPSTR lpCmdLine, // Command Line Parameters 503 | int nCmdShow) // Window Show State 504 | { 505 | MSG msg; // Windows Message Structure 506 | BOOL done=FALSE; // Bool Variable To Exit Loop 507 | 508 | // Ask The User Which Screen Mode They Prefer 509 | MessageBox(NULL,"Press 1/2/3 to change gizmo.", "Manual",MB_OK); 510 | 511 | fullscreen = FALSE; // Windowed Mode 512 | 513 | 514 | // Create Our OpenGL Window 515 | if (!CreateGLWindow("LibGizmo Test application Base on NeHe's Texture Mapping Tutorial",1280,720,32,fullscreen)) 516 | { 517 | return 0; // Quit If Window Was Not Created 518 | } 519 | 520 | while(!done) // Loop That Runs While done=FALSE 521 | { 522 | if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? 523 | { 524 | if (msg.message==WM_QUIT) // Have We Received A Quit Message? 525 | { 526 | done=TRUE; // If So done=TRUE 527 | } 528 | else // If Not, Deal With Window Messages 529 | { 530 | TranslateMessage(&msg); // Translate The Message 531 | DispatchMessage(&msg); // Dispatch The Message 532 | } 533 | } 534 | else // If There Are No Messages 535 | { 536 | // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() 537 | if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received? 538 | { 539 | done=TRUE; // ESC or DrawGLScene Signalled A Quit 540 | } 541 | else // Not Time To Quit, Update Screen 542 | { 543 | SwapBuffers(hDC); // Swap Buffers (Double Buffering) 544 | } 545 | 546 | if (keys[VK_F1]) // Is F1 Being Pressed? 547 | { 548 | keys[VK_F1]=FALSE; // If So Make Key FALSE 549 | KillGLWindow(); // Kill Our Current Window 550 | fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode 551 | // Recreate Our OpenGL Window 552 | if (!CreateGLWindow("LibGizmo Test application Base on NeHe's Texture Mapping Tutorial",640,480,16,fullscreen)) 553 | { 554 | return 0; // Quit If Window Was Not Created 555 | } 556 | } 557 | } 558 | } 559 | 560 | // Shutdown 561 | KillGLWindow(); // Kill The Window 562 | return (msg.wParam); // Exit The Program 563 | } 564 | -------------------------------------------------------------------------------- /src/win32example/NeHe's Readme.txt: -------------------------------------------------------------------------------- 1 | ========================================================================== 2 | OpenGL Lesson 06: Texture Mapping 3 | ========================================================================== 4 | 5 | Authors Name: Jeff Molofee ( NeHe ) 6 | 7 | Disclaimer: 8 | 9 | This program may crash your system or run poorly depending on your 10 | hardware. The program and code contained in this archive was scanned 11 | for virii and has passed all test before it was put online. If you 12 | use this code in project of your own, send a shout out to the author! 13 | 14 | ========================================================================== 15 | NeHe Productions 1997-2004 16 | ========================================================================== 17 | -------------------------------------------------------------------------------- /src/win32example/Release/CL.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/CL.read.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/CL.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/CL.write.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/Lesson6.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/Lesson6.obj -------------------------------------------------------------------------------- /src/win32example/Release/cl.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/cl.command.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/link.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/link.command.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/link.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/link.read.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/link.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/link.write.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/mt.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/mt.command.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/mt.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/mt.read.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/mt.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/mt.write.1.tlog -------------------------------------------------------------------------------- /src/win32example/Release/vc100.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CedricGuillemet/LibGizmo/4b097bd7aa32d98f479cd34b78a8b3a670a9372b/src/win32example/Release/vc100.pdb -------------------------------------------------------------------------------- /src/win32example/Release/win32example.exe.intermediate.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/win32example/Release/win32example.lastbuildstate: -------------------------------------------------------------------------------- 1 | #v4.0:v100:false 2 | Release|Win32|D:\Projects\releases\libgizmo\libGizmo\src\| 3 | -------------------------------------------------------------------------------- /src/win32example/Release/win32example.log: -------------------------------------------------------------------------------- 1 | Build started 15/02/2012 15:14:26. 2 | 1>Project "D:\Projects\releases\libgizmo\libGizmo\src\win32example\win32example.vcxproj" on node 3 (build target(s)). 3 | 1>InitializeBuildStatus: 4 | Creating "Release\win32example.unsuccessfulbuild" because "AlwaysCreate" was specified. 5 | ClCompile: 6 | All outputs are up-to-date. 7 | 1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(990,5): warning MSB8012: TargetPath(D:\Projects\releases\libgizmo\libGizmo\src\Release\win32example.exe) does not match the Linker's OutputFile property value (D:\Projects\releases\libgizmo\libGizmo\bin\win32example.exe). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile). 8 | Link: 9 | C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"D:\Projects\releases\libgizmo\libGizmo\src\\..\bin\win32example.exe" /INCREMENTAL:NO /NOLOGO opengl32.lib D:\Projects\releases\libgizmo\libGizmo\src\\..\lib\libgizmo.lib glu32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Release\win32example.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"D:\Projects\releases\libgizmo\libGizmo\src\Release\win32example.pdb" /SUBSYSTEM:WINDOWS /OPT:REF /OPT:ICF /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"D:\Projects\releases\libgizmo\libGizmo\src\Release\win32example.lib" /MACHINE:X86 Release\Lesson6.obj 10 | 1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library 11 | Generating code 12 | Finished generating code 13 | win32example.vcxproj -> D:\Projects\releases\libgizmo\libGizmo\src\Release\win32example.exe 14 | Manifest: 15 | C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mt.exe /nologo /verbose /outputresource:"D:\Projects\releases\libgizmo\libGizmo\src\\..\bin\win32example.exe;#1" /manifest Release\win32example.exe.intermediate.manifest 16 | FinalizeBuildStatus: 17 | Deleting file "Release\win32example.unsuccessfulbuild". 18 | Touching "Release\win32example.lastbuildstate". 19 | 1>Done Building Project "D:\Projects\releases\libgizmo\libGizmo\src\win32example\win32example.vcxproj" (build target(s)). 20 | 21 | Build succeeded. 22 | 23 | Time Elapsed 00:00:02.57 24 | -------------------------------------------------------------------------------- /src/win32example/win32example.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {FF6718FF-BFDA-4832-B526-F8223ABB74F2} 15 | Win32Proj 16 | win32example 17 | 18 | 19 | 20 | Application 21 | true 22 | MultiByte 23 | 24 | 25 | Application 26 | false 27 | true 28 | MultiByte 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | true 42 | 43 | 44 | false 45 | 46 | 47 | 48 | 49 | 50 | Level3 51 | Disabled 52 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) 53 | $(SolutionDir)\..\inc 54 | 55 | 56 | Windows 57 | true 58 | $(SolutionDir)\..\bin\$(TargetName)Debug$(TargetExt) 59 | opengl32.lib;$(SolutionDir)\..\lib\libgizmoDebug.lib;glu32.lib;%(AdditionalDependencies) 60 | 61 | 62 | 63 | 64 | Level3 65 | 66 | 67 | MaxSpeed 68 | true 69 | true 70 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) 71 | $(SolutionDir)\..\inc 72 | 73 | 74 | Windows 75 | true 76 | true 77 | true 78 | $(SolutionDir)\..\bin\$(TargetName)$(TargetExt) 79 | opengl32.lib;$(SolutionDir)\..\lib\libgizmo.lib;glu32.lib;%(AdditionalDependencies) 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /src/win32example/win32example.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/win32example/win32example.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | $(SolutionDir)\..\bin\$(TargetName)Debug$(TargetExt) 5 | WindowsLocalDebugger 6 | 7 | --------------------------------------------------------------------------------