Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------//
- // File: Lights.cpp //
- //------------------//
- #include <Windows.h>
- #include <mmsystem.h>
- #include <d3dx9.h>
- #pragma warning( disable : 4996 ) // disable deprecated warning
- #include <strsafe.h>
- #pragma warning( default : 4996 )
- #include "resource.h"
- #include "D3DInit.h"
- #include "Camera.h"
- //-----------------------------------------------------------------------------
- // Global variables
- //-----------------------------------------------------------------------------
- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
- LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
- D3DXVECTOR3 m_vDownPt; // starting point of rotation arc
- D3DXVECTOR3 m_vCurrentPt; // current point of rotation arc
- D3DXQUATERNION m_qDown; // Quaternion before button down
- D3DXQUATERNION m_qNow; // Composite quaternion for current drag
- D3DXMATRIXA16 matView;
- D3DXMATRIXA16 matProj;
- D3DXMATRIX m_mRotation;
- D3DXMATRIX m_mtxScaling;
- CCamera m_pCamera; // Our current camera object
- bool m_bDrag; // Whether user is dragging arc ball
- HWND hWnd;
- D3DVIEWPORT9 m_Viewport; // The viewport details into which we are rendering.
- D3DInit d3di;
- CD3DSettings m_D3DSettings; // The settings used to initialize D3D
- POINT m_OldCursorPos; // Old cursor position for tracking
- void ProcessInput( );
- #ifndef _COBJECT_H_
- #define _COBJECT_H_
- // A structure for our custom vertex type. We added a normal, and omitted the
- // color (which is provided by the material)
- struct CUSTOMVERTEX
- {
- D3DXVECTOR3 position; // The 3D position for the vertex
- D3DXVECTOR3 normal; // The surface normal for the vertex
- CUSTOMVERTEX( D3DXVECTOR3 x, D3DXVECTOR3 x1 ) { position = x; normal = x1; }
- };
- D3DXVECTOR3 ScreenToVector( float fScreenPtX, float fScreenPtY )
- {
- RECT r;
- GetWindowRect(hWnd, &r);
- // Scale to screen
- FLOAT x = -( fScreenPtX - r.left - (r.right-r.left) / 2 ) / ( 1.0f * (r.right-r.left) / 2 );
- FLOAT y = ( fScreenPtY - r.top - (r.bottom-r.top) / 2 ) / ( 1.0f * (r.bottom-r.top) / 2 );
- FLOAT z = 0.0f;
- FLOAT mag = x * x + y * y;
- if( mag > 1.0f )
- {
- FLOAT scale = 1.0f / sqrtf( mag );
- x *= scale;
- y *= scale;
- }
- else
- z = sqrtf( 1.0f - mag );
- // Return vector
- return D3DXVECTOR3( x, y, z );
- }
- D3DXQUATERNION QuatFromBallPoints( const D3DXVECTOR3& vFrom, const D3DXVECTOR3& vTo )
- {
- D3DXVECTOR3 vPart;
- float fDot = D3DXVec3Dot( &vFrom, &vTo );
- D3DXVec3Cross( &vPart, &vFrom, &vTo );
- return D3DXQUATERNION( vPart.x, vPart.y, vPart.z, fDot );
- }
- class CObject
- {
- public:
- //-------------------------------------------------------------------------
- // Public Variables for This Class
- //-------------------------------------------------------------------------
- D3DXMATRIX m_mtxWorld; // Objects world matrix
- LPDIRECT3DVERTEXBUFFER9 m_pVertexBuffer; // Vertex Buffer we are instancing
- //-------------------------------------------------------------------------
- // Constructors & Destructors for This Class.
- //-------------------------------------------------------------------------
- CObject( LPDIRECT3DVERTEXBUFFER9 pVertexBuffer )
- {
- // Reset / Clear all required values
- D3DXMatrixIdentity( &m_mtxWorld );
- }
- CObject( )
- {
- // Reset / Clear all required values
- m_pVertexBuffer = NULL;
- D3DXMatrixIdentity( &m_mtxWorld );
- }
- virtual ~CObject( )
- {
- // Release our vertex buffer (de-reference)
- if ( m_pVertexBuffer ) m_pVertexBuffer->Release();
- m_pVertexBuffer = NULL;
- }
- void OnRotate( int nX, int nY )
- {
- if( m_bDrag )
- {
- g_pd3dDevice->GetTransform(D3DTS_WORLD, &m_mtxWorld);
- m_vCurrentPt = ScreenToVector( ( float )nX, ( float )nY );
- m_qNow = m_qDown * QuatFromBallPoints( m_vDownPt, m_vCurrentPt );
- D3DXMatrixRotationQuaternion(&m_mRotation, &m_qNow);
- D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &m_mRotation);
- }
- }
- void OnScaling( int nX, int nY )
- {
- m_vCurrentPt = ScreenToVector( ( float )nX, ( float )nY );
- float dx = (m_vCurrentPt.x - m_vDownPt.x);
- //(m_vCurrentPt.y - m_vDownPt.y) +
- //(m_vCurrentPt.z - m_vDownPt.z)) / 3.0f;
- D3DXMatrixScaling( &m_mtxScaling, 1.0f + dx, 1.0f + dx, 1.0f + dx );
- D3DXMatrixMultiply(&m_mtxWorld, &m_mtxWorld, &m_mtxScaling );
- }
- void OnBegin( int nX, int nY )
- {
- RECT r1;
- GetWindowRect(hWnd, &r1);
- // Only enter the drag state if the click falls
- // inside the click rectangle.
- if( nX >= r1.left &&
- nX < r1.left + (r1.right - r1.left) &&
- nY >= r1.top &&
- nY < r1.top + (r1.bottom - r1.top))
- {
- m_bDrag = true;
- m_qDown = m_qNow;
- m_vDownPt = ScreenToVector( ( float )nX, ( float )nY );
- }
- }
- //-------------------------------------------------------------------------
- // Public Functions for This Class
- //-------------------------------------------------------------------------
- void SetVertexBuffer ( LPDIRECT3DVERTEXBUFFER9 pVertexBuffer );
- };
- struct MATR3x3
- {
- FLOAT _11; FLOAT _12; FLOAT _13;
- FLOAT _21; FLOAT _22; FLOAT _23;
- FLOAT _31; FLOAT _32; FLOAT _33;
- D3DXVECTOR3 x(D3DXVECTOR3 *v);
- };
- #endif // !_COBJECT_H_
- D3DXVECTOR3 MATR3x3::x(D3DXVECTOR3 *v)
- {
- 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);
- }
- CObject m_pObject[2]; // Objects storing mesh instances
- // Our custom FVF, which describes our custom vertex structure
- #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
- //-----------------------------------------------------------------------------
- // Name: InitD3D()
- // Desc: Initializes Direct3D
- //-----------------------------------------------------------------------------
- HRESULT InitD3D( HWND hWnd )
- {
- // Create the D3D object.
- if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
- return E_FAIL;
- m_D3DSettings.Windowed_Settings.AdapterOrdinal = D3DADAPTER_DEFAULT;
- m_D3DSettings.Windowed_Settings.BackBufferFormat = D3DFMT_A8R8G8B8;
- m_D3DSettings.Windowed_Settings.DepthStencilFormat = D3DFMT_D16;
- m_D3DSettings.Windowed_Settings.DeviceType = D3DDEVTYPE_HAL;
- g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &m_D3DSettings.Windowed_Settings.DisplayMode);
- m_D3DSettings.Windowed_Settings.VertexProcessingType = HARDWARE_VP;
- m_D3DSettings.Windowed = TRUE;
- //D3DInit Initialize;
- d3di.BuildPresentParameters(m_D3DSettings);
- // Set up the structure used to create the D3DDevice. Since we are now
- // using more complex geometry, we will create a device with a zbuffer.
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory( &d3dpp, sizeof( d3dpp ) );
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
- d3dpp.EnableAutoDepthStencil = TRUE;
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- // Create the D3DDevice
- if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
- D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- &d3di.BuildPresentParameters(m_D3DSettings), &g_pd3dDevice ) ) )
- {
- return E_FAIL;
- }
- // Turn off culling
- g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
- // Turn on the zbuffer
- g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
- g_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
- g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
- g_pd3dDevice->SetRenderState (D3DRS_SPECULARENABLE, TRUE);
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: InitGeometry()
- // Desc: Creates the scene geometry
- //-----------------------------------------------------------------------------
- HRESULT InitGeometry()
- {
- D3DXVECTOR3 VEC1(1.5f, 2.0f, 1.5f);
- D3DXVECTOR3 VEC2(0.7f, 0.8f, 0.7f);
- D3DXVECTOR3 VEC3(1.5f, 0.0f, 1.5f);
- D3DXVECTOR3 VEC4(1.0f, -1.5f, 1.0f);
- // Create the vertex buffer.
- if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4 * sizeof( CUSTOMVERTEX ),
- 0, D3DFVF_XYZ,
- D3DPOOL_MANAGED, &m_pObject[0].m_pVertexBuffer, NULL ) ) )
- {
- return E_FAIL;
- }
- if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50 * 6 * sizeof( CUSTOMVERTEX ),
- 0, D3DFVF_CUSTOMVERTEX,
- D3DPOOL_MANAGED, &m_pObject[1].m_pVertexBuffer, NULL ) ) )
- {
- return E_FAIL;
- }
- // Fill the vertex buffer. We are algorithmically generating a cylinder
- // here, including the normals, which are used for lighting.
- CUSTOMVERTEX * pVertices = NULL;
- if( FAILED( m_pObject[0].m_pVertexBuffer->Lock( 0, 3 * sizeof( CUSTOMVERTEX ), ( void** )&pVertices, 0 ) ) )
- return E_FAIL;
- *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( -2.0f, -2.0f, -2.0f ), D3DXVECTOR3( -2.0f, -2.0f, -2.0f ) );
- *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( 2.0f, -2.0f, -2.0f ), D3DXVECTOR3( 2.0f, -2.0f, -2.0f ) );
- *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( -2.0f, -2.0f, 2.0f ), D3DXVECTOR3( -2.0f, -2.0f, 2.0f ) );
- *pVertices++ = CUSTOMVERTEX( D3DXVECTOR3( 2.0f, -2.0f, 2.0f ), D3DXVECTOR3( 2.0f, -2.0f, 2.0f ) );
- m_pObject[0].m_pVertexBuffer->Unlock();
- pVertices = NULL;
- if( FAILED( m_pObject[1].m_pVertexBuffer->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
- return E_FAIL;
- for( DWORD i = 0; i < 50; i++ )
- {
- FLOAT theta = ( 2 * D3DX_PI * i ) / 49;
- MATR3x3 m;
- m._11 = sinf( theta ); m._12 = 0; m._13 = 0;
- m._21 = 0; m._22 = 1; m._23 = 0;
- m._31 = 0; m._32 = 0; m._33 = cosf( theta );
- pVertices[2 * i + 0].position = m.x(&VEC1);
- pVertices[2 * i + 0].normal = m.x(&VEC1);
- pVertices[2 * i + 1].position = m.x(&VEC2);
- pVertices[2 * i + 1].normal = m.x(&VEC2);
- }
- for( DWORD i = 50; i < 100; i++ )
- {
- FLOAT theta = ( 2 * D3DX_PI * (i - 50) ) / 49;
- MATR3x3 m;
- m._11 = sinf( theta ); m._12 = 0; m._13 = 0;
- m._21 = 0; m._22 = 1; m._23 = 0;
- m._31 = 0; m._32 = 0; m._33 = cosf( theta );
- pVertices[2 * i + 0].position = m.x(&VEC2);
- pVertices[2 * i + 0].normal = m.x(&VEC2);
- pVertices[2 * i + 1].position = m.x(&VEC3);
- pVertices[2 * i + 1].normal = m.x(&VEC3);
- }
- for( DWORD i = 100; i < 150; i++ )
- {
- FLOAT theta = ( 2 * D3DX_PI * (i - 100) ) / 49;
- MATR3x3 m;
- m._11 = sinf( theta ); m._12 = 0; m._13 = 0;
- m._21 = 0; m._22 = 1; m._23 = 0;
- m._31 = 0; m._32 = 0; m._33 = cosf( theta );
- pVertices[2 * i + 0].position = m.x(&VEC3);
- pVertices[2 * i + 0].normal = m.x(&VEC3);
- pVertices[2 * i + 1].position = m.x(&VEC4);
- pVertices[2 * i + 1].normal = m.x(&VEC4);
- }
- m_pObject[1].m_pVertexBuffer->Unlock();
- return S_OK;
- }
- //-----------------------------------------------------------------------------
- // Name: Cleanup()
- // Desc: Releases all previously initialized objects
- //-----------------------------------------------------------------------------
- VOID Cleanup()
- {
- if( g_pd3dDevice != NULL )
- g_pd3dDevice->Release();
- if( g_pD3D != NULL )
- g_pD3D->Release();
- }
- //-----------------------------------------------------------------------------
- // Name: SetupLights()
- // Desc: Sets up the Lights and materials for the scene.
- //-----------------------------------------------------------------------------
- VOID SetupLights()
- {
- // Set up a material. The material here just has the diffuse and ambient
- // colors set to yellow. Note that only one material can be used at a time.
- D3DMATERIAL9 mtrl;
- ZeroMemory( &mtrl, sizeof( D3DMATERIAL9 ) );
- mtrl.Diffuse.r = mtrl.Ambient.r = mtrl.Specular.r = 1.0f;
- mtrl.Diffuse.g = mtrl.Ambient.g = mtrl.Specular.g = 1.0f;
- mtrl.Diffuse.b = mtrl.Ambient.b = mtrl.Specular.b = 0.0f;
- mtrl.Diffuse.a = mtrl.Ambient.a = mtrl.Specular.a = 1.0f;
- mtrl.Power = 10.0f;
- g_pd3dDevice->SetMaterial( &mtrl );
- // Set up a white, directional light, with an oscillating direction.
- // Note that many Lights may be active at a time (but each one slows down
- // the rendering of our scene). However, here we are just using one. Also,
- // we need to set the D3DRS_LIGHTING renderstate to enable lighting
- D3DXVECTOR3 vecDir;
- D3DLIGHT9 light;
- ZeroMemory( &light, sizeof( D3DLIGHT9 ) );
- light.Type = D3DLIGHT_DIRECTIONAL;
- light.Diffuse.r = 1.0f;
- light.Diffuse.g = 1.0f;
- light.Diffuse.b = 1.0f;
- vecDir = D3DXVECTOR3( 10.0f,
- 2.0f,
- 5.0f );
- D3DXVec3Normalize( ( D3DXVECTOR3* )&light.Direction, &vecDir );
- light.Range = sqrtf(FLT_MAX);
- g_pd3dDevice->SetLight( 0, &light );
- g_pd3dDevice->LightEnable( 0, TRUE );
- g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
- // Finally, turn on some ambient light.
- g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
- }
- //-----------------------------------------------------------------------------
- // Name: Render()
- // Desc: Draws the scene
- //-----------------------------------------------------------------------------
- VOID Render()
- {
- // Clear the backbuffer and the zbuffer
- g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
- D3DCOLOR_XRGB( 0, 127, 255 ), 1.0f, 0 );
- ProcessInput();
- // Begin the scene
- if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
- {
- // Setup the Lights and materials
- SetupLights();
- // Setup the world, view, and projection matrices
- g_pd3dDevice->SetTransform( D3DTS_VIEW, &m_pCamera.GetViewMatrix() );
- // Render the vertex buffer contents
- g_pd3dDevice->SetTransform( D3DTS_WORLD, &m_pObject[1].m_mtxWorld );
- g_pd3dDevice->SetStreamSource( 0, m_pObject[1].m_pVertexBuffer, 0, sizeof( CUSTOMVERTEX ) );
- g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 6 * 50 - 2 );
- g_pd3dDevice->SetTransform( D3DTS_WORLD, &m_pObject[0].m_mtxWorld );
- g_pd3dDevice->SetStreamSource( 0, m_pObject[0].m_pVertexBuffer, 0, sizeof( CUSTOMVERTEX ) );
- g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
- g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
- // End the scene
- g_pd3dDevice->EndScene();
- }
- // Present the backbuffer contents to the display
- g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
- }
- //-----------------------------------------------------------------------------
- // Name: MsgProc()
- // Desc: The window's message handler
- //-----------------------------------------------------------------------------
- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- int iMouseX = ( short )LOWORD( lParam );
- int iMouseY = ( short )HIWORD( lParam );
- POINT CursorPos;
- float X = 0.0f, Y = 0.0f;
- switch( msg )
- {
- case WM_SIZE:
- if ( wParam == SIZE_MINIMIZED )
- return TRUE; // App has been minimized
- else
- {
- ULONG VWidth = LOWORD( lParam );
- ULONG VHeight = HIWORD( lParam );
- RECT r; GetWindowRect(hWnd, &r);
- m_Viewport.X = r.left; m_Viewport.Y = r.top;
- m_Viewport.Width = VWidth; m_Viewport.Height = VHeight;
- m_Viewport.MinZ = 0.0f; m_Viewport.MaxZ = 1.0f;
- float m_fNearClip = 1.01f; float m_fFarClip = 5000.0f;
- if(g_pd3dDevice)
- {
- g_pd3dDevice->SetViewport(&m_Viewport);
- d3di.ResetDisplay(g_pd3dDevice, m_D3DSettings);
- D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, ( float )( r.right - r.left ) / ( r.bottom - r.top ), m_fNearClip, m_fFarClip );
- g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
- }
- }
- return TRUE;
- case WM_DESTROY:
- Cleanup();
- PostQuitMessage( 0 );
- return TRUE;
- case WM_RBUTTONDOWN:
- ShowCursor(FALSE);
- SetCapture( hWnd );
- m_pObject[1].OnBegin( iMouseX, iMouseY );
- return TRUE;
- case WM_LBUTTONDOWN:
- m_pObject[1].OnBegin( iMouseX, iMouseX );
- ShowCursor(FALSE);
- SetCapture( hWnd );
- GetCursorPos( &m_OldCursorPos );
- return TRUE;
- /*case WM_MOUSEMOVE:
- if( wParam == MK_LBUTTON )
- m_pObject[1].OnRotate( iMouseX, iMouseY );
- else if( wParam == MK_RBUTTON )
- m_pObject[1].OnScaling( iMouseX, iMouseY );
- return TRUE;*/
- case WM_RBUTTONUP:
- ReleaseCapture();
- ShowCursor(TRUE);
- m_bDrag = false;
- return TRUE;
- case WM_LBUTTONUP:
- ReleaseCapture();
- ShowCursor(TRUE);
- m_bDrag = false;
- return TRUE;
- case WM_KEYDOWN:
- switch (wParam)
- {
- case VK_LEFT:
- //D3DXMatrixTranslation( &m_pObject[ 0 ].m_mtxWorld,
- m_pObject[1].m_mtxWorld._41 -= 0.5f;
- return TRUE;
- case VK_RIGHT:
- m_pObject[1].m_mtxWorld._41 += 0.5f;
- return TRUE;
- }
- return TRUE;
- case WM_COMMAND:
- switch(wParam)
- {
- case ID_EXIT:
- Cleanup();
- PostQuitMessage( 0 );
- return TRUE;
- }
- return TRUE;
- }
- return DefWindowProc( hWnd, msg, wParam, lParam );
- }
- //-----------------------------------------------------------------------------
- // Name: WinMain()
- // Desc: The application's entry point
- //-----------------------------------------------------------------------------
- INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
- {
- // Register the window class
- WNDCLASSEX wc =
- {
- sizeof( WNDCLASSEX ), CS_BYTEALIGNCLIENT | CS_HREDRAW | CS_VREDRAW, MsgProc, 0L, 0L,
- GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
- L"D3D Tutorial", NULL
- };
- RegisterClassEx( &wc );
- HMENU hMenu = LoadMenu( hInst, MAKEINTRESOURCE( IDR_MENU1 ) );
- // Create the application's window
- hWnd = CreateWindow( L"D3D Tutorial", L"Лабораторная работа №2",
- WS_OVERLAPPEDWINDOW, 100, 100, 740, 580,
- NULL, hMenu, wc.hInstance, NULL );
- m_vCurrentPt = D3DXVECTOR3( 0, 0, 0 );
- m_vDownPt = D3DXVECTOR3( 0, 0, 0 );
- D3DXQuaternionIdentity( &m_qDown );
- D3DXQuaternionIdentity( &m_qNow );
- D3DXMatrixIdentity( &m_mRotation );
- D3DXMatrixIdentity( &m_mtxScaling );
- // Initialize Direct3D
- if( SUCCEEDED( InitD3D( hWnd ) ) )
- {
- // Create the geometry
- if( SUCCEEDED( InitGeometry() ) )
- {
- // Show the window
- ShowWindow( hWnd, SW_SHOWDEFAULT );
- UpdateWindow( hWnd );
- // Enter the message loop
- MSG msg;
- ZeroMemory( &msg, sizeof( msg ) );
- while( msg.message != WM_QUIT )
- {
- if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- else
- Render();
- }
- }
- }
- UnregisterClass( L"D3D Tutorial", wc.hInstance );
- return 0;
- }
- //-----------------------------------------------------------------------------
- // Name : ProcessInput ()
- // Desc : Simply polls the input devices and performs basic input operations
- //-----------------------------------------------------------------------------
- void ProcessInput( )
- {
- static UCHAR pKeyBuffer[ 256 ];
- ULONG Direction = 0;
- POINT CursorPos;
- float X = 0.0f, Y = 0.0f;
- // Retrieve keyboard state
- if ( !GetKeyboardState( pKeyBuffer ) ) return;
- // Check the relevant keys
- /*if ( pKeyBuffer[ VK_UP ] & 0xF0 ) Direction |= CPlayer::DIR_FORWARD;
- if ( pKeyBuffer[ VK_DOWN ] & 0xF0 ) Direction |= CPlayer::DIR_BACKWARD;
- if ( pKeyBuffer[ VK_LEFT ] & 0xF0 ) Direction |= CPlayer::DIR_LEFT;
- if ( pKeyBuffer[ VK_RIGHT ] & 0xF0 ) Direction |= CPlayer::DIR_RIGHT;
- if ( pKeyBuffer[ VK_PRIOR ] & 0xF0 ) Direction |= CPlayer::DIR_UP;
- if ( pKeyBuffer[ VK_NEXT ] & 0xF0 ) Direction |= CPlayer::DIR_DOWN;*/
- // Now process the mouse (if the button is pressed)
- if ( GetCapture() == hWnd )
- {
- // Retrieve the cursor position
- GetCursorPos( &CursorPos );
- // Calculate mouse rotational values
- X = (float)(CursorPos.x - m_OldCursorPos.x) / 3.0f;
- Y = (float)(CursorPos.y - m_OldCursorPos.y) / 3.0f;
- // Reset our cursor position so we can keep going forever :)
- SetCursorPos( m_OldCursorPos.x, m_OldCursorPos.y );
- if ( X != 0.0f || Y != 0.0f )
- {
- // Rotate our camera
- m_pCamera.Rotate( Y, X, 0.0f );
- // Update the device matrix
- m_pCamera.UpdateRenderView( g_pd3dDevice );
- // End if any rotation
- }
- } // End if Captured
- }
Add Comment
Please, Sign In to add comment