You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

374 lines
11 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #include "stdafx.h"
  35. #include "assimp_view.h"
  36. namespace AssimpView {
  37. //-------------------------------------------------------------------------------
  38. // Handle mouse input for the FPS input behaviour
  39. //
  40. // Movement in x and y axis is possible
  41. //-------------------------------------------------------------------------------
  42. void HandleMouseInputFPS( void )
  43. {
  44. POINT mousePos;
  45. GetCursorPos( &mousePos );
  46. ScreenToClient( GetDlgItem(g_hDlg,IDC_RT), &mousePos );
  47. g_mousePos.x = mousePos.x;
  48. g_mousePos.y = mousePos.y;
  49. D3DXMATRIX matRotation;
  50. if (g_bMousePressed)
  51. {
  52. int nXDiff = (g_mousePos.x - g_LastmousePos.x);
  53. int nYDiff = (g_mousePos.y - g_LastmousePos.y);
  54. if( 0 != nYDiff)
  55. {
  56. D3DXMatrixRotationAxis( &matRotation, (D3DXVECTOR3*)& g_sCamera.vRight, D3DXToRadian((float)nYDiff / 6.0f));
  57. D3DXVec3TransformCoord( (D3DXVECTOR3*)&g_sCamera.vLookAt, (D3DXVECTOR3*)& g_sCamera.vLookAt, &matRotation );
  58. D3DXVec3TransformCoord( (D3DXVECTOR3*)&g_sCamera.vUp, (D3DXVECTOR3*)&g_sCamera.vUp, &matRotation );
  59. }
  60. if( 0 != nXDiff )
  61. {
  62. D3DXVECTOR3 v(0,1,0);
  63. D3DXMatrixRotationAxis( &matRotation, (D3DXVECTOR3*)&g_sCamera.vUp, D3DXToRadian((float)nXDiff / 6.0f) );
  64. D3DXVec3TransformCoord( (D3DXVECTOR3*)&g_sCamera.vLookAt, (D3DXVECTOR3*)&g_sCamera.vLookAt, &matRotation );
  65. D3DXVec3TransformCoord( (D3DXVECTOR3*)&g_sCamera.vRight,(D3DXVECTOR3*) &g_sCamera.vRight, &matRotation );
  66. }
  67. }
  68. g_LastmousePos.x = g_mousePos.x;
  69. g_LastmousePos.y = g_mousePos.y;
  70. }
  71. //-------------------------------------------------------------------------------
  72. // Handle mouse input for the FPS input behaviour
  73. //
  74. // Movement in x and y axis is possible
  75. //-------------------------------------------------------------------------------
  76. void HandleMouseInputTextureView( void )
  77. {
  78. POINT mousePos;
  79. GetCursorPos( &mousePos );
  80. ScreenToClient( GetDlgItem(g_hDlg,IDC_RT), &mousePos );
  81. g_mousePos.x = mousePos.x;
  82. g_mousePos.y = mousePos.y;
  83. D3DXMATRIX matRotation;
  84. if (g_bMousePressed)
  85. {
  86. CDisplay::Instance().SetTextureViewOffsetX((float)(g_mousePos.x - g_LastmousePos.x));
  87. CDisplay::Instance().SetTextureViewOffsetY((float)(g_mousePos.y - g_LastmousePos.y));
  88. }
  89. g_LastmousePos.x = g_mousePos.x;
  90. g_LastmousePos.y = g_mousePos.y;
  91. }
  92. //-------------------------------------------------------------------------------
  93. // handle mouse input for the light rotation
  94. //
  95. // Axes: global x/y axis
  96. //-------------------------------------------------------------------------------
  97. void HandleMouseInputLightRotate( void )
  98. {
  99. POINT mousePos;
  100. GetCursorPos( &mousePos );
  101. ScreenToClient( GetDlgItem(g_hDlg,IDC_RT), &mousePos );
  102. g_mousePos.x = mousePos.x;
  103. g_mousePos.y = mousePos.y;
  104. if (g_bMousePressedR)
  105. {
  106. int nXDiff = -(g_mousePos.x - g_LastmousePos.x);
  107. int nYDiff = -(g_mousePos.y - g_LastmousePos.y);
  108. aiVector3D v = aiVector3D(1.0f,0.0f,0.0f);
  109. aiMatrix4x4 mTemp;
  110. D3DXMatrixRotationAxis( (D3DXMATRIX*) &mTemp, (D3DXVECTOR3*)&v, D3DXToRadian((float)nYDiff / 2.0f));
  111. D3DXVec3TransformCoord((D3DXVECTOR3*)&g_avLightDirs[0],
  112. (const D3DXVECTOR3*)&g_avLightDirs[0],(const D3DXMATRIX*)&mTemp);
  113. v = aiVector3D(0.0f,1.0f,0.0f);
  114. D3DXMatrixRotationAxis( (D3DXMATRIX*) &mTemp, (D3DXVECTOR3*)&v, D3DXToRadian((float)nXDiff / 2.0f));
  115. D3DXVec3TransformCoord((D3DXVECTOR3*)&g_avLightDirs[0],
  116. (const D3DXVECTOR3*)&g_avLightDirs[0],(const D3DXMATRIX*)&mTemp);
  117. }
  118. return;
  119. }
  120. //-------------------------------------------------------------------------------
  121. // Handle mouse input for movements of the skybox
  122. //
  123. // The skybox can be moved by holding both the left and the right mouse button
  124. // pressed. Rotation is possible in x and y direction.
  125. //-------------------------------------------------------------------------------
  126. void HandleMouseInputSkyBox( void )
  127. {
  128. POINT mousePos;
  129. GetCursorPos( &mousePos );
  130. ScreenToClient( GetDlgItem(g_hDlg,IDC_RT), &mousePos );
  131. g_mousePos.x = mousePos.x;
  132. g_mousePos.y = mousePos.y;
  133. aiMatrix4x4 matRotation;
  134. if (g_bMousePressedBoth )
  135. {
  136. int nXDiff = -(g_mousePos.x - g_LastmousePos.x);
  137. int nYDiff = -(g_mousePos.y - g_LastmousePos.y);
  138. aiMatrix4x4 matWorld;
  139. if( 0 != nYDiff)
  140. {
  141. aiVector3D v = aiVector3D(1.0f,0.0f,0.0f);
  142. D3DXMatrixRotationAxis( (D3DXMATRIX*) &matWorld, (D3DXVECTOR3*)&v, D3DXToRadian((float)nYDiff / 2.0f));
  143. CBackgroundPainter::Instance().RotateSB(&matWorld);
  144. }
  145. if( 0 != nXDiff)
  146. {
  147. aiMatrix4x4 matWorldOld;
  148. if( 0 != nYDiff)
  149. {
  150. matWorldOld = matWorld;
  151. }
  152. aiVector3D v = aiVector3D(0.0f,1.0f,0.0f);
  153. D3DXMatrixRotationAxis( (D3DXMATRIX*)&matWorld, (D3DXVECTOR3*)&v, D3DXToRadian((float)nXDiff / 2.0f) );
  154. matWorld = matWorldOld * matWorld;
  155. CBackgroundPainter::Instance().RotateSB(&matWorld);
  156. }
  157. }
  158. }
  159. //-------------------------------------------------------------------------------
  160. //-------------------------------------------------------------------------------
  161. void HandleMouseInputLightIntensityAndColor( void )
  162. {
  163. POINT mousePos;
  164. GetCursorPos( &mousePos );
  165. ScreenToClient( GetDlgItem(g_hDlg,IDC_RT), &mousePos );
  166. g_mousePos.x = mousePos.x;
  167. g_mousePos.y = mousePos.y;
  168. if (g_bMousePressedM)
  169. {
  170. int nXDiff = -(g_mousePos.x - g_LastmousePos.x);
  171. int nYDiff = -(g_mousePos.y - g_LastmousePos.y);
  172. g_fLightIntensity -= (float)nXDiff / 400.0f;
  173. if ((nYDiff > 2 || nYDiff < -2) && (nXDiff < 20 && nXDiff > -20))
  174. {
  175. if (!g_bFPSView)
  176. {
  177. g_sCamera.vPos.z += nYDiff / 120.0f;
  178. }
  179. else
  180. {
  181. g_sCamera.vPos += (nYDiff / 120.0f) * g_sCamera.vLookAt.Normalize();
  182. }
  183. }
  184. }
  185. return;
  186. }
  187. //-------------------------------------------------------------------------------
  188. //-------------------------------------------------------------------------------
  189. void HandleMouseInputLocal( void )
  190. {
  191. POINT mousePos;
  192. GetCursorPos( &mousePos );
  193. ScreenToClient( GetDlgItem(g_hDlg,IDC_RT), &mousePos );
  194. g_mousePos.x = mousePos.x;
  195. g_mousePos.y = mousePos.y;
  196. aiMatrix4x4 matRotation;
  197. if (g_bMousePressed)
  198. {
  199. int nXDiff = -(g_mousePos.x - g_LastmousePos.x);
  200. int nYDiff = -(g_mousePos.y - g_LastmousePos.y);
  201. aiMatrix4x4 matWorld;
  202. if (g_eClick != EClickPos_Outside)
  203. {
  204. if( 0 != nYDiff && g_eClick != EClickPos_CircleHor)
  205. {
  206. aiVector3D v = aiVector3D(1.0f,0.0f,0.0f);
  207. D3DXMatrixRotationAxis( (D3DXMATRIX*) &matWorld, (D3DXVECTOR3*)&v, D3DXToRadian((float)nYDiff / 2.0f));
  208. g_mWorldRotate = g_mWorldRotate * matWorld;
  209. }
  210. if( 0 != nXDiff && g_eClick != EClickPos_CircleVert)
  211. {
  212. aiVector3D v = aiVector3D(0.0f,1.0f,0.0f);
  213. D3DXMatrixRotationAxis( (D3DXMATRIX*)&matWorld, (D3DXVECTOR3*)&v, D3DXToRadian((float)nXDiff / 2.0f) );
  214. g_mWorldRotate = g_mWorldRotate * matWorld;
  215. }
  216. }
  217. else
  218. {
  219. if(0 != nYDiff || 0 != nXDiff)
  220. {
  221. // rotate around the z-axis
  222. RECT sRect;
  223. GetWindowRect(GetDlgItem(g_hDlg,IDC_RT),&sRect);
  224. sRect.right -= sRect.left;
  225. sRect.bottom -= sRect.top;
  226. int xPos = g_mousePos.x - sRect.right/2;
  227. int yPos = g_mousePos.y - sRect.bottom/2;
  228. float fXDist = (float)xPos;
  229. float fYDist = (float)yPos / sqrtf((float)(yPos * yPos + xPos * xPos));
  230. bool bSign1;
  231. if (fXDist < 0.0f)bSign1 = false;
  232. else bSign1 = true;
  233. float fAngle = asin(fYDist);
  234. xPos = g_LastmousePos.x - sRect.right/2;
  235. yPos = g_LastmousePos.y - sRect.bottom/2;
  236. fXDist = (float)xPos;
  237. fYDist = (float)yPos / sqrtf((float)(yPos * yPos + xPos * xPos));
  238. bool bSign2;
  239. if (fXDist < 0.0f)bSign2 = false;
  240. else bSign2 = true;
  241. float fAngle2 = asin(fYDist);
  242. fAngle -= fAngle2;
  243. if (bSign1 != bSign2)
  244. {
  245. g_bInvert = !g_bInvert;
  246. }
  247. if (g_bInvert)fAngle *= -1.0f;
  248. aiVector3D v = aiVector3D(0.0f,0.0f,1.0f);
  249. D3DXMatrixRotationAxis( (D3DXMATRIX*)&matWorld, (D3DXVECTOR3*)&v, (float) (fAngle * 1.2) );
  250. g_mWorldRotate = g_mWorldRotate * matWorld;
  251. }
  252. }
  253. }
  254. g_LastmousePos.x = g_mousePos.x;
  255. g_LastmousePos.y = g_mousePos.y;
  256. }
  257. //-------------------------------------------------------------------------------
  258. //-------------------------------------------------------------------------------
  259. void HandleKeyboardInputFPS( void )
  260. {
  261. unsigned char keys[256];
  262. GetKeyboardState( keys );
  263. aiVector3D tmpLook = g_sCamera.vLookAt;
  264. aiVector3D tmpRight = g_sCamera.vRight;
  265. aiVector3D vOldPos = g_sCamera.vPos;
  266. // Up Arrow Key - View moves forward
  267. if( keys[VK_UP] & 0x80 )
  268. g_sCamera.vPos -= (tmpLook*-MOVE_SPEED)*g_fElpasedTime;
  269. // Down Arrow Key - View moves backward
  270. if( keys[VK_DOWN] & 0x80 )
  271. g_sCamera.vPos += (tmpLook*-MOVE_SPEED)*g_fElpasedTime;
  272. // Left Arrow Key - View side-steps or strafes to the left
  273. if( keys[VK_LEFT] & 0x80 )
  274. g_sCamera.vPos -= (tmpRight*MOVE_SPEED)*g_fElpasedTime;
  275. // Right Arrow Key - View side-steps or strafes to the right
  276. if( keys[VK_RIGHT] & 0x80 )
  277. g_sCamera.vPos += (tmpRight*MOVE_SPEED)*g_fElpasedTime;
  278. // Home Key - View elevates up
  279. if( keys[VK_HOME] & 0x80 )
  280. g_sCamera.vPos .y += MOVE_SPEED*g_fElpasedTime;
  281. // End Key - View elevates down
  282. if( keys[VK_END] & 0x80 )
  283. g_sCamera.vPos.y -= MOVE_SPEED*g_fElpasedTime;
  284. }
  285. //-------------------------------------------------------------------------------
  286. //-------------------------------------------------------------------------------
  287. void HandleKeyboardInputTextureView( void )
  288. {
  289. unsigned char keys[256];
  290. GetKeyboardState( keys );
  291. // Up Arrow Key
  292. if( keys[VK_UP] & 0x80 )
  293. CDisplay::Instance().SetTextureViewOffsetY ( g_fElpasedTime * 150.0f );
  294. // Down Arrow Key
  295. if( keys[VK_DOWN] & 0x80 )
  296. CDisplay::Instance().SetTextureViewOffsetY ( -g_fElpasedTime * 150.0f );
  297. // Left Arrow Key
  298. if( keys[VK_LEFT] & 0x80 )
  299. CDisplay::Instance().SetTextureViewOffsetX ( g_fElpasedTime * 150.0f );
  300. // Right Arrow Key
  301. if( keys[VK_RIGHT] & 0x80 )
  302. CDisplay::Instance().SetTextureViewOffsetX ( -g_fElpasedTime * 150.0f );
  303. }
  304. };