Guest User

Untitled

a guest
Jul 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.87 KB | None | 0 0
  1. //------------------//
  2. // File: Lights.cpp //
  3. //------------------//
  4. #include <Windows.h>
  5. #include <mmsystem.h>
  6. #include <d3dx9.h>
  7. #pragma warning( disable : 4996 ) // disable deprecated warning
  8. #include <strsafe.h>
  9. #pragma warning( default : 4996 )
  10. #include "resource.h"
  11. #include "D3DInit.h"
  12. #include "Camera.h"
  13.  
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Global variables
  19. //-----------------------------------------------------------------------------
  20. LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
  21. LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
  22. D3DXVECTOR3 m_vDownPt; // starting point of rotation arc
  23. D3DXVECTOR3 m_vCurrentPt; // current point of rotation arc
  24. D3DXQUATERNION m_qDown; // Quaternion before button down
  25. D3DXQUATERNION m_qNow; // Composite quaternion for current drag
  26. D3DXMATRIXA16 matView;
  27. D3DXMATRIXA16 matProj;
  28. D3DXMATRIX m_mRotation;
  29. D3DXMATRIX m_mtxScaling;
  30. CCamera m_pCamera; // Our current camera object
  31. bool m_bDrag; // Whether user is dragging arc ball
  32. HWND hWnd;
  33. D3DVIEWPORT9 m_Viewport; // The viewport details into which we are rendering.
  34. D3DInit d3di;
  35. CD3DSettings m_D3DSettings; // The settings used to initialize D3D
  36. POINT m_OldCursorPos; // Old cursor position for tracking
  37. void ProcessInput( );
  38.  
  39. #ifndef _COBJECT_H_
  40. #define _COBJECT_H_
  41.  
  42. // A structure for our custom vertex type. We added a normal, and omitted the
  43. // color (which is provided by the material)
  44.  
  45. struct CUSTOMVERTEX
  46. {
  47. D3DXVECTOR3 position; // The 3D position for the vertex
  48. D3DXVECTOR3 normal; // The surface normal for the vertex
  49. CUSTOMVERTEX( D3DXVECTOR3 x, D3DXVECTOR3 x1 ) { position = x; normal = x1; }
  50. };
  51.  
  52. D3DXVECTOR3 ScreenToVector( float fScreenPtX, float fScreenPtY )
  53. {
  54. RECT r;
  55. GetWindowRect(hWnd, &r);
  56. // Scale to screen
  57. FLOAT x = -( fScreenPtX - r.left - (r.right-r.left) / 2 ) / ( 1.0f * (r.right-r.left) / 2 );
  58. FLOAT y = ( fScreenPtY - r.top - (r.bottom-r.top) / 2 ) / ( 1.0f * (r.bottom-r.top) / 2 );
  59.  
  60. FLOAT z = 0.0f;
  61. FLOAT mag = x * x + y * y;
  62.  
  63. if( mag > 1.0f )
  64. {
  65. FLOAT scale = 1.0f / sqrtf( mag );
  66. x *= scale;
  67. y *= scale;
  68. }
  69. else
  70. z = sqrtf( 1.0f - mag );
  71.  
  72. // Return vector
  73. return D3DXVECTOR3( x, y, z );
  74. }
  75.  
  76. D3DXQUATERNION QuatFromBallPoints( const D3DXVECTOR3& vFrom, const D3DXVECTOR3& vTo )
  77. {
  78. D3DXVECTOR3 vPart;
  79. float fDot = D3DXVec3Dot( &vFrom, &vTo );
  80. D3DXVec3Cross( &vPart, &vFrom, &vTo );
  81.  
  82. return D3DXQUATERNION( vPart.x, vPart.y, vPart.z, fDot );
  83. }
  84.  
  85. class CObject
  86. {
  87. public:
  88. //-------------------------------------------------------------------------
  89. // Public Variables for This Class
  90. //-------------------------------------------------------------------------
  91. D3DXMATRIX m_mtxWorld; // Objects world matrix
  92. LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer; // Vertex Buffer we are instancing
  93.  
  94. //-------------------------------------------------------------------------
  95. // Constructors & Destructors for This Class.
  96. //-------------------------------------------------------------------------
  97. CObject( LPDIRECT3DVERTEXBUFFER9 pVertexBuffer )
  98. {
  99. // Reset / Clear all required values
  100. D3DXMatrixIdentity( &m_mtxWorld );
  101. }
  102. CObject( )
  103. {
  104. // Reset / Clear all required values
  105. m_pVertexBuffer = NULL;
  106. D3DXMatrixIdentity( &m_mtxWorld );
  107. }
  108. virtual ~CObject( )
  109. {
  110. // Release our vertex buffer (de-reference)
  111. if ( m_pVertexBuffer ) m_pVertexBuffer->Release();
  112. m_pVertexBuffer = NULL;
  113. }
  114. void OnRotate( int nX, int nY )
  115. {
  116. if( m_bDrag )
  117. {
  118. g_pd3dDevice->GetTransform(D3DTS_WORLD, &m_mtxWorld);
  119. m_vCurrentPt = ScreenToVector( ( float )nX, ( float )nY );
  120. m_qNow = m_qDown * QuatFromBallPoints( m_vDownPt, m_vCurrentPt );
  121. D3DXMatrixRotationQuaternion(&m_mRotation, &m_qNow);
  122. D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &m_mRotation);
  123. }
  124. }
  125. void OnScaling( int nX, int nY )
  126. {
  127. m_vCurrentPt = ScreenToVector( ( float )nX, ( float )nY );
  128. float dx = (m_vCurrentPt.x - m_vDownPt.x);
  129. //(m_vCurrentPt.y - m_vDownPt.y) +
  130. //(m_vCurrentPt.z - m_vDownPt.z)) / 3.0f;
  131. D3DXMatrixScaling( &m_mtxScaling, 1.0f + dx, 1.0f + dx, 1.0f + dx );
  132. D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &m_mtxScaling );
  133. }
  134. void OnBegin( int nX, int nY )
  135. {
  136. RECT r1;
  137. GetWindowRect(hWnd, &r1);
  138. // Only enter the drag state if the click falls
  139. // inside the click rectangle.
  140. if( nX >= r1.left &&
  141. nX < r1.left + (r1.right - r1.left) &&
  142. nY >= r1.top &&
  143. nY < r1.top + (r1.bottom - r1.top))
  144. {
  145. m_bDrag = true;
  146. m_qDown = m_qNow;
  147. m_vDownPt = ScreenToVector( ( float )nX, ( float )nY );
  148. }
  149. }
  150.  
  151. //-------------------------------------------------------------------------
  152. // Public Functions for This Class
  153. //-------------------------------------------------------------------------
  154. void SetVertexBuffer ( LPDIRECT3DVERTEXBUFFER9 pVertexBuffer );
  155. };
  156.  
  157. struct MATR3x3
  158. {
  159. FLOAT _11; FLOAT _12; FLOAT _13;
  160. FLOAT _21; FLOAT _22; FLOAT _23;
  161. FLOAT _31; FLOAT _32; FLOAT _33;
  162. D3DXVECTOR3 x(D3DXVECTOR3 *v);
  163. };
  164.  
  165. #endif // !_COBJECT_H_
  166.  
  167. D3DXVECTOR3 MATR3x3::x(D3DXVECTOR3 *v)
  168. {
  169. return D3DXVECTOR3(v->x*_11+v->y*_21+v->z*_31, v->x*_12+v->y*_22+v->z*_32, v->x*_13+v->y*_32+v->z*_33);
  170. }
  171.  
  172. CObject m_pObject[2]; // Objects storing mesh instances
  173.  
  174. // Our custom FVF, which describes our custom vertex structure
  175. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
  176.  
  177.  
  178. //-----------------------------------------------------------------------------
  179. // Name: InitD3D()
  180. // Desc: Initializes Direct3D
  181. //-----------------------------------------------------------------------------
  182. HRESULT InitD3D( HWND hWnd )
  183. {
  184. // Create the D3D object.
  185. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
  186. return E_FAIL;
  187.  
  188. m_D3DSettings.Windowed_Settings.AdapterOrdinal = D3DADAPTER_DEFAULT;
  189. m_D3DSettings.Windowed_Settings.BackBufferFormat = D3DFMT_A8R8G8B8;
  190. m_D3DSettings.Windowed_Settings.DepthStencilFormat = D3DFMT_D16;
  191. m_D3DSettings.Windowed_Settings.DeviceType = D3DDEVTYPE_HAL;
  192. g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &m_D3DSettings.Windowed_Settings.DisplayMode);
  193. m_D3DSettings.Windowed_Settings.VertexProcessingType = HARDWARE_VP;
  194. m_D3DSettings.Windowed = TRUE;
  195.  
  196. //D3DInit Initialize;
  197. d3di.BuildPresentParameters(m_D3DSettings);
  198.  
  199.  
  200. // Set up the structure used to create the D3DDevice. Since we are now
  201. // using more complex geometry, we will create a device with a zbuffer.
  202. D3DPRESENT_PARAMETERS d3dpp;
  203. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  204. d3dpp.Windowed = TRUE;
  205. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  206. d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
  207. d3dpp.EnableAutoDepthStencil = TRUE;
  208. d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  209.  
  210. // Create the D3DDevice
  211. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  212. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  213. &d3di.BuildPresentParameters(m_D3DSettings), &g_pd3dDevice ) ) )
  214. {
  215. return E_FAIL;
  216. }
  217.  
  218. // Turn off culling
  219. g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  220.  
  221. // Turn on the zbuffer
  222. g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  223.  
  224. g_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
  225.  
  226. g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
  227.  
  228. g_pd3dDevice->SetRenderState (D3DRS_SPECULARENABLE, TRUE);
  229.  
  230. return S_OK;
  231. }
  232.  
  233.  
  234.  
  235.  
  236. //-----------------------------------------------------------------------------
  237. // Name: InitGeometry()
  238. // Desc: Creates the scene geometry
  239. //-----------------------------------------------------------------------------
  240. HRESULT InitGeometry()
  241. {
  242. D3DXVECTOR3 VEC1(1.5f, 2.0f, 1.5f);
  243. D3DXVECTOR3 VEC2(0.7f, 0.8f, 0.7f);
  244. D3DXVECTOR3 VEC3(1.5f, 0.0f, 1.5f);
  245. D3DXVECTOR3 VEC4(1.0f, -1.5f, 1.0f);
  246.  
  247. // Create the vertex buffer.
  248. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4 * sizeof( CUSTOMVERTEX ),
  249. 0, D3DFVF_XYZ,
  250. D3DPOOL_MANAGED, &m_pObject[0].m_pVertexBuffer, NULL ) ) )
  251. {
  252. return E_FAIL;
  253. }
  254.  
  255. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50 * 6 * sizeof( CUSTOMVERTEX ),
  256. 0, D3DFVF_CUSTOMVERTEX,
  257. D3DPOOL_MANAGED, &m_pObject[1].m_pVertexBuffer, NULL ) ) )
  258. {
  259. return E_FAIL;
  260. }
  261.  
  262. // Fill the vertex buffer. We are algorithmically generating a cylinder
  263. // here, including the normals, which are used for lighting.
  264. CUSTOMVERTEX * pVertices = NULL;
  265. if( FAILED( m_pObject[0].m_pVertexBuffer->Lock( 0, 3 * sizeof( CUSTOMVERTEX ), ( void** )&pVertices, 0 ) ) )
  266. return E_FAIL;
  267. *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( -2.0f, -2.0f, -2.0f ), D3DXVECTOR3( -2.0f, -2.0f, -2.0f ) );
  268. *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( 2.0f, -2.0f, -2.0f ), D3DXVECTOR3( 2.0f, -2.0f, -2.0f ) );
  269. *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( -2.0f, -2.0f, 2.0f ), D3DXVECTOR3( -2.0f, -2.0f, 2.0f ) );
  270. *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( 2.0f, -2.0f, 2.0f ), D3DXVECTOR3( 2.0f, -2.0f, 2.0f ) );
  271. m_pObject[0].m_pVertexBuffer->Unlock();
  272.  
  273. pVertices = NULL;
  274. if( FAILED( m_pObject[1].m_pVertexBuffer->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
  275. return E_FAIL;
  276. for( DWORD i = 0; i < 50; i++ )
  277. {
  278. FLOAT theta = ( 2 * D3DX_PI * i ) / 49;
  279. MATR3x3 m;
  280. m._11 = sinf( theta ); m._12 = 0; m._13 = 0;
  281. m._21 = 0; m._22 = 1; m._23 = 0;
  282. m._31 = 0; m._32 = 0; m._33 = cosf( theta );
  283.  
  284. pVertices[2 * i + 0].position = m.x(&VEC1);
  285. pVertices[2 * i + 0].normal = m.x(&VEC1);
  286. pVertices[2 * i + 1].position = m.x(&VEC2);
  287. pVertices[2 * i + 1].normal = m.x(&VEC2);
  288. }
  289.  
  290. for( DWORD i = 50; i < 100; i++ )
  291. {
  292. FLOAT theta = ( 2 * D3DX_PI * (i - 50) ) / 49;
  293. MATR3x3 m;
  294. m._11 = sinf( theta ); m._12 = 0; m._13 = 0;
  295. m._21 = 0; m._22 = 1; m._23 = 0;
  296. m._31 = 0; m._32 = 0; m._33 = cosf( theta );
  297. pVertices[2 * i + 0].position = m.x(&VEC2);
  298. pVertices[2 * i + 0].normal = m.x(&VEC2);
  299. pVertices[2 * i + 1].position = m.x(&VEC3);
  300. pVertices[2 * i + 1].normal = m.x(&VEC3);
  301. }
  302.  
  303. for( DWORD i = 100; i < 150; i++ )
  304. {
  305. FLOAT theta = ( 2 * D3DX_PI * (i - 100) ) / 49;
  306. MATR3x3 m;
  307. m._11 = sinf( theta ); m._12 = 0; m._13 = 0;
  308. m._21 = 0; m._22 = 1; m._23 = 0;
  309. m._31 = 0; m._32 = 0; m._33 = cosf( theta );
  310. pVertices[2 * i + 0].position = m.x(&VEC3);
  311. pVertices[2 * i + 0].normal = m.x(&VEC3);
  312. pVertices[2 * i + 1].position = m.x(&VEC4);
  313. pVertices[2 * i + 1].normal = m.x(&VEC4);
  314. }
  315. m_pObject[1].m_pVertexBuffer->Unlock();
  316. return S_OK;
  317. }
  318.  
  319.  
  320. //-----------------------------------------------------------------------------
  321. // Name: Cleanup()
  322. // Desc: Releases all previously initialized objects
  323. //-----------------------------------------------------------------------------
  324. VOID Cleanup()
  325. {
  326. if( g_pd3dDevice != NULL )
  327. g_pd3dDevice->Release();
  328.  
  329. if( g_pD3D != NULL )
  330. g_pD3D->Release();
  331. }
  332.  
  333. //-----------------------------------------------------------------------------
  334. // Name: SetupLights()
  335. // Desc: Sets up the Lights and materials for the scene.
  336. //-----------------------------------------------------------------------------
  337. VOID SetupLights()
  338. {
  339. // Set up a material. The material here just has the diffuse and ambient
  340. // colors set to yellow. Note that only one material can be used at a time.
  341. D3DMATERIAL9 mtrl;
  342. ZeroMemory( &mtrl, sizeof( D3DMATERIAL9 ) );
  343. mtrl.Diffuse.r = mtrl.Ambient.r = mtrl.Specular.r = 1.0f;
  344. mtrl.Diffuse.g = mtrl.Ambient.g = mtrl.Specular.g = 1.0f;
  345. mtrl.Diffuse.b = mtrl.Ambient.b = mtrl.Specular.b = 0.0f;
  346. mtrl.Diffuse.a = mtrl.Ambient.a = mtrl.Specular.a = 1.0f;
  347. mtrl.Power = 10.0f;
  348. g_pd3dDevice->SetMaterial( &mtrl );
  349.  
  350. // Set up a white, directional light, with an oscillating direction.
  351. // Note that many Lights may be active at a time (but each one slows down
  352. // the rendering of our scene). However, here we are just using one. Also,
  353. // we need to set the D3DRS_LIGHTING renderstate to enable lighting
  354. D3DXVECTOR3 vecDir;
  355. D3DLIGHT9 light;
  356. ZeroMemory( &light, sizeof( D3DLIGHT9 ) );
  357. light.Type = D3DLIGHT_DIRECTIONAL;
  358. light.Diffuse.r = 1.0f;
  359. light.Diffuse.g = 1.0f;
  360. light.Diffuse.b = 1.0f;
  361. vecDir = D3DXVECTOR3( 10.0f,
  362. 2.0f,
  363. 5.0f );
  364. D3DXVec3Normalize( ( D3DXVECTOR3* )&light.Direction, &vecDir );
  365. light.Range = sqrtf(FLT_MAX);
  366. g_pd3dDevice->SetLight( 0, &light );
  367. g_pd3dDevice->LightEnable( 0, TRUE );
  368. g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  369.  
  370. // Finally, turn on some ambient light.
  371. g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
  372. }
  373.  
  374. //-----------------------------------------------------------------------------
  375. // Name: Render()
  376. // Desc: Draws the scene
  377. //-----------------------------------------------------------------------------
  378.  
  379. VOID Render()
  380. {
  381. // Clear the backbuffer and the zbuffer
  382. g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
  383. D3DCOLOR_XRGB( 0, 127, 255 ), 1.0f, 0 );
  384.  
  385. ProcessInput();
  386. // Begin the scene
  387. if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
  388. {
  389. // Setup the Lights and materials
  390. SetupLights();
  391.  
  392. // Setup the world, view, and projection matrices
  393. g_pd3dDevice->SetTransform( D3DTS_VIEW, &m_pCamera.GetViewMatrix() );
  394.  
  395. // Render the vertex buffer contents
  396. g_pd3dDevice->SetTransform( D3DTS_WORLD, &m_pObject[1].m_mtxWorld );
  397. g_pd3dDevice->SetStreamSource( 0, m_pObject[1].m_pVertexBuffer, 0, sizeof( CUSTOMVERTEX ) );
  398. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  399. g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 6 * 50 - 2 );
  400.  
  401. g_pd3dDevice->SetTransform( D3DTS_WORLD, &m_pObject[0].m_mtxWorld );
  402. g_pd3dDevice->SetStreamSource( 0, m_pObject[0].m_pVertexBuffer, 0, sizeof( CUSTOMVERTEX ) );
  403. g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  404. g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  405.  
  406. // End the scene
  407. g_pd3dDevice->EndScene();
  408. }
  409. // Present the backbuffer contents to the display
  410. g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  411. }
  412.  
  413.  
  414.  
  415. //-----------------------------------------------------------------------------
  416. // Name: MsgProc()
  417. // Desc: The window's message handler
  418. //-----------------------------------------------------------------------------
  419. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  420. {
  421. int iMouseX = ( short )LOWORD( lParam );
  422. int iMouseY = ( short )HIWORD( lParam );
  423. POINT CursorPos;
  424.  
  425. float X = 0.0f, Y = 0.0f;
  426. switch( msg )
  427. {
  428. case WM_SIZE:
  429. if ( wParam == SIZE_MINIMIZED )
  430. return TRUE; // App has been minimized
  431. else
  432. {
  433. ULONG VWidth = LOWORD( lParam );
  434. ULONG VHeight = HIWORD( lParam );
  435. RECT r; GetWindowRect(hWnd, &r);
  436. m_Viewport.X = r.left; m_Viewport.Y = r.top;
  437. m_Viewport.Width = VWidth; m_Viewport.Height = VHeight;
  438. m_Viewport.MinZ = 0.0f; m_Viewport.MaxZ = 1.0f;
  439. float m_fNearClip = 1.01f; float m_fFarClip = 5000.0f;
  440. if(g_pd3dDevice)
  441. {
  442. g_pd3dDevice->SetViewport(&m_Viewport);
  443. d3di.ResetDisplay(g_pd3dDevice, m_D3DSettings);
  444. D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, ( float )( r.right - r.left ) / ( r.bottom - r.top ), m_fNearClip, m_fFarClip );
  445. g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  446. }
  447. }
  448. return TRUE;
  449.  
  450. case WM_DESTROY:
  451. Cleanup();
  452. PostQuitMessage( 0 );
  453. return TRUE;
  454.  
  455. case WM_RBUTTONDOWN:
  456. ShowCursor(FALSE);
  457. SetCapture( hWnd );
  458. m_pObject[1].OnBegin( iMouseX, iMouseY );
  459. return TRUE;
  460.  
  461. case WM_LBUTTONDOWN:
  462. m_pObject[1].OnBegin( iMouseX, iMouseX );
  463. ShowCursor(FALSE);
  464. SetCapture( hWnd );
  465. GetCursorPos( &m_OldCursorPos );
  466. return TRUE;
  467.  
  468. /*case WM_MOUSEMOVE:
  469. if( wParam == MK_LBUTTON )
  470. m_pObject[1].OnRotate( iMouseX, iMouseY );
  471. else if( wParam == MK_RBUTTON )
  472. m_pObject[1].OnScaling( iMouseX, iMouseY );
  473. return TRUE;*/
  474. case WM_RBUTTONUP:
  475. ReleaseCapture();
  476. ShowCursor(TRUE);
  477. m_bDrag = false;
  478. return TRUE;
  479.  
  480. case WM_LBUTTONUP:
  481. ReleaseCapture();
  482. ShowCursor(TRUE);
  483. m_bDrag = false;
  484. return TRUE;
  485.  
  486. case WM_KEYDOWN:
  487. switch (wParam)
  488. {
  489. case VK_LEFT:
  490. //D3DXMatrixTranslation( &m_pObject[ 0 ].m_mtxWorld,
  491. m_pObject[1].m_mtxWorld._41 -= 0.5f;
  492. return TRUE;
  493. case VK_RIGHT:
  494. m_pObject[1].m_mtxWorld._41 += 0.5f;
  495. return TRUE;
  496. }
  497. return TRUE;
  498.  
  499. case WM_COMMAND:
  500. switch(wParam)
  501. {
  502. case ID_EXIT:
  503. Cleanup();
  504. PostQuitMessage( 0 );
  505. return TRUE;
  506. }
  507. return TRUE;
  508. }
  509.  
  510. return DefWindowProc( hWnd, msg, wParam, lParam );
  511. }
  512.  
  513.  
  514.  
  515.  
  516. //-----------------------------------------------------------------------------
  517. // Name: WinMain()
  518. // Desc: The application's entry point
  519. //-----------------------------------------------------------------------------
  520. INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
  521. {
  522. // Register the window class
  523. WNDCLASSEX wc =
  524. {
  525. sizeof( WNDCLASSEX ), CS_BYTEALIGNCLIENT | CS_HREDRAW | CS_VREDRAW, MsgProc, 0L, 0L,
  526. GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
  527. L"D3D Tutorial", NULL
  528. };
  529. RegisterClassEx( &wc );
  530. HMENU hMenu = LoadMenu( hInst, MAKEINTRESOURCE( IDR_MENU1 ) );
  531.  
  532. // Create the application's window
  533. hWnd = CreateWindow( L"D3D Tutorial", L"Лабораторная работа №2",
  534. WS_OVERLAPPEDWINDOW, 100, 100, 740, 580,
  535. NULL, hMenu, wc.hInstance, NULL );
  536.  
  537. m_vCurrentPt = D3DXVECTOR3( 0, 0, 0 );
  538. m_vDownPt = D3DXVECTOR3( 0, 0, 0 );
  539. D3DXQuaternionIdentity( &m_qDown );
  540. D3DXQuaternionIdentity( &m_qNow );
  541. D3DXMatrixIdentity( &m_mRotation );
  542. D3DXMatrixIdentity( &m_mtxScaling );
  543. // Initialize Direct3D
  544. if( SUCCEEDED( InitD3D( hWnd ) ) )
  545. {
  546. // Create the geometry
  547. if( SUCCEEDED( InitGeometry() ) )
  548. {
  549. // Show the window
  550. ShowWindow( hWnd, SW_SHOWDEFAULT );
  551. UpdateWindow( hWnd );
  552.  
  553. // Enter the message loop
  554. MSG msg;
  555. ZeroMemory( &msg, sizeof( msg ) );
  556. while( msg.message != WM_QUIT )
  557. {
  558. if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  559. {
  560. TranslateMessage( &msg );
  561. DispatchMessage( &msg );
  562. }
  563. else
  564. Render();
  565. }
  566. }
  567. }
  568.  
  569. UnregisterClass( L"D3D Tutorial", wc.hInstance );
  570. return 0;
  571. }
  572.  
  573. //-----------------------------------------------------------------------------
  574. // Name : ProcessInput ()
  575. // Desc : Simply polls the input devices and performs basic input operations
  576. //-----------------------------------------------------------------------------
  577. void ProcessInput( )
  578. {
  579. static UCHAR pKeyBuffer[ 256 ];
  580. ULONG Direction = 0;
  581. POINT CursorPos;
  582. float X = 0.0f, Y = 0.0f;
  583.  
  584. // Retrieve keyboard state
  585. if ( !GetKeyboardState( pKeyBuffer ) ) return;
  586.  
  587. // Check the relevant keys
  588. /*if ( pKeyBuffer[ VK_UP ] & 0xF0 ) Direction |= CPlayer::DIR_FORWARD;
  589. if ( pKeyBuffer[ VK_DOWN ] & 0xF0 ) Direction |= CPlayer::DIR_BACKWARD;
  590. if ( pKeyBuffer[ VK_LEFT ] & 0xF0 ) Direction |= CPlayer::DIR_LEFT;
  591. if ( pKeyBuffer[ VK_RIGHT ] & 0xF0 ) Direction |= CPlayer::DIR_RIGHT;
  592. if ( pKeyBuffer[ VK_PRIOR ] & 0xF0 ) Direction |= CPlayer::DIR_UP;
  593. if ( pKeyBuffer[ VK_NEXT ] & 0xF0 ) Direction |= CPlayer::DIR_DOWN;*/
  594.  
  595. // Now process the mouse (if the button is pressed)
  596. if ( GetCapture() == hWnd )
  597. {
  598. // Retrieve the cursor position
  599. GetCursorPos( &CursorPos );
  600.  
  601. // Calculate mouse rotational values
  602. X = (float)(CursorPos.x - m_OldCursorPos.x) / 3.0f;
  603. Y = (float)(CursorPos.y - m_OldCursorPos.y) / 3.0f;
  604.  
  605. // Reset our cursor position so we can keep going forever :)
  606. SetCursorPos( m_OldCursorPos.x, m_OldCursorPos.y );
  607.  
  608. if ( X != 0.0f || Y != 0.0f )
  609. {
  610. // Rotate our camera
  611. m_pCamera.Rotate( Y, X, 0.0f );
  612. // Update the device matrix
  613. m_pCamera.UpdateRenderView( g_pd3dDevice );
  614. // End if any rotation
  615. }
  616. } // End if Captured
  617. }
Add Comment
Please, Sign In to add comment