GL - camera movement working

This commit is contained in:
Linda Andersson 2013-11-29 10:04:44 +01:00
parent f9a1aaf43c
commit 7a663b2e16
11 changed files with 110 additions and 61 deletions

View File

@ -39,6 +39,7 @@ HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT Render(float deltaTime);
HRESULT Update(float deltaTime);
HRESULT InitDirect3D();
HRESULT InitGame();
HRESULT CleanUp();
@ -78,16 +79,24 @@ void SetStdOutToNewConsole()
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
// for dynamic .dll loading
// path is relative to the .exe and .dll pos
// also change the VC directories - working dir is set to $(SolutionDir)..\Bin\Executable\Tester
// to fit with where the .obj files is
// linker/ input/ delayed load .dll - specify the .dll that should be loaded
BOOL success = SetDllDirectory(L"..\\..\\DLL");
if (success == 0)
{
return 0;
}
if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
return 0;
if( FAILED( InitDirect3D() ) )
return 0;
if( FAILED( InitGame() ) )
return 0;
@ -175,7 +184,19 @@ HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create Direct3D with Oyster Graphics
//--------------------------------------------------------------------------------------
HRESULT InitDirect3D()
{
if(Oyster::Graphics::API::Init(g_hWnd, false, false, Oyster::Math::Float2( 1024, 768)) != Oyster::Graphics::API::Sucsess)
return E_FAIL;
return S_OK;
}
//--------------------------------------------------------------------------------------
// Init the input and the game
//-------------------------------------------------------------------------------------
HRESULT InitGame()
{
inputObj = new InputClass;
@ -184,18 +205,13 @@ HRESULT InitGame()
MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK);
return false;
}
if(Oyster::Graphics::API::Init(g_hWnd, false, false, Oyster::Math::Float2( 1024, 768)) != Oyster::Graphics::API::Sucsess)
return E_FAIL;
game = new GameLogic::IGame();
game->Init();
game->StartGame();
return S_OK;
}
HRESULT Update(float deltaTime)
{
inputObj->Update();
@ -218,7 +234,17 @@ HRESULT Update(float deltaTime)
key = GameLogic::keyInput_D;
}
game->Update(key);
float pitch = 0;
float yaw = 0;
//if(inputObj->IsMousePressed())
//{
pitch = inputObj->GetPitch();
yaw = inputObj->GetYaw();
//}
game->Update(key, pitch, yaw);
return S_OK;
}
@ -232,18 +258,6 @@ HRESULT Render(float deltaTime)
//std::cout<<"test";
}
// test view and projection matrix
Oyster::Math::Float3 dir = Oyster::Math::Float3(0,0,-1);
Oyster::Math::Float3 up =Oyster::Math::Float3(0,1,0);
Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 100);
Oyster::Math::Float4x4 view =Oyster::Math3D::OrientationMatrix_LookAtDirection(dir, up, pos);
view = view.GetInverse();
Oyster::Math::Float4x4 proj = Oyster::Math3D::ProjectionMatrix_Perspective(3.14f/2, 1024/768, 1, 1000);
Oyster::Graphics::API::NewFrame(view, proj);
game->Render();
wchar_t title[255];
swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed));

View File

@ -117,7 +117,7 @@ Oyster::Math::Float4x4 Camera::ViewsProj()const
void Camera::Walk(float dist)
{
this->m_position += dist*Oyster::Math::Float3(1,0,0);
this->m_position += dist*this->mLook;
}
void Camera::Strafe(float dist)

View File

@ -3,7 +3,9 @@ using namespace GameLogic;
Game::Game(void)
{
player = NULL;
level = NULL;
camera = NULL;
}
@ -15,21 +17,52 @@ Game::~Game(void)
delete player;
player = NULL;
}
if(camera)
{
delete camera;
camera = NULL;
}
}
void Game::Init()
{
player = new Player();
camera = new Camera();
}
void Game::StartGame()
{
Oyster::Math::Float3 dir = Oyster::Math::Float3(0,0,-1);
Oyster::Math::Float3 up =Oyster::Math::Float3(0,1,0);
Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 100);
camera->LookAt(pos, dir, up);
camera->SetLens(3.14f/2, 1024/768, 1, 1000);
}
void Game::Update(keyInput keyPressed)
void Game::Update(keyInput keyPressed, float pitch, float yaw)
{
player->Update(keyPressed);
//player->Update(keyPressed);
camera->Yaw(yaw);
camera->Pitch(pitch);
if(keyPressed == keyInput_A)
{
camera->Strafe(-0.1);
}
if(keyPressed == keyInput_D)
{
camera->Strafe(0.1);
}
if(keyPressed == keyInput_S)
{
camera->Walk(-0.1);
}
if(keyPressed == keyInput_W)
{
camera->Walk(0.1);
}
camera->UpdateViewMatrix();
}
void Game::Render()
{
Oyster::Graphics::API::NewFrame(camera->View(), camera->Proj());
player->Render();
}

View File

@ -4,11 +4,10 @@
#include "Level.h"
#include "Player.h"
#include "IGame.h"
#include "Camera.h"
namespace GameLogic
{
class Game
{
public:
@ -17,15 +16,13 @@ namespace GameLogic
void Init();
void StartGame();
void Update(keyInput keyPressed);
void Update(keyInput keyPressed, float pitch, float yaw);
void Render();
private:
Level* level;
Player* player;
Camera* camera;
};
}
#endif

View File

@ -176,6 +176,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Camera.h" />
<ClInclude Include="CollisionManager.h" />
<ClInclude Include="DynamicObject.h" />
<ClInclude Include="Game.h" />
@ -189,6 +190,7 @@
<ClInclude Include="Weapon.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Camera.cpp" />
<ClCompile Include="CollisionManager.cpp" />
<ClCompile Include="DynamicObject.cpp" />
<ClCompile Include="Game.cpp" />

View File

@ -48,6 +48,9 @@
<ClInclude Include="CollisionManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Game.cpp">
@ -86,5 +89,8 @@
<ClCompile Include="CollisionManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -29,9 +29,9 @@ void IGame::StartGame()
{
gameModule->StartGame();
}
void IGame::Update(keyInput keyPressed)
void IGame::Update(keyInput keyPressed, float pitch, float yaw)
{
gameModule->Update(keyPressed);
gameModule->Update(keyPressed, pitch, yaw);
}
void IGame::Render()
{

View File

@ -38,7 +38,7 @@ namespace GameLogic
/************************************************************************/
/* Get key input to update the player */
/************************************************************************/
void Update(keyInput keyPressed);
void Update(keyInput keyPressed, float pitch, float yaw);
void Render();
Game* getGameModule();
private:

View File

@ -228,8 +228,17 @@ HRESULT Update(float deltaTime)
key = GameLogic::keyInput_D;
}
game->Update(key);
float pitch = 0;
float yaw = 0;
// move only when mouse is pressed
//if(inputObj->IsMousePressed())
//{
pitch = inputObj->GetPitch();
yaw = inputObj->GetYaw();
//}
game->Update(key, pitch, yaw);
return S_OK;
}
@ -242,18 +251,6 @@ HRESULT Render(float deltaTime)
//std::cout<<"test";
}
// test view and projection matrix
Oyster::Math::Float3 dir = Oyster::Math::Float3(0, 0, -1);
Oyster::Math::Float3 up = Oyster::Math::Float3(0, 1, 0);
Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 100);
Oyster::Math::Float4x4 view =Oyster::Math3D::OrientationMatrix_LookAtDirection(dir, up, pos);
view = view.GetInverse();
Oyster::Math::Float4x4 proj = Oyster::Math3D::ProjectionMatrix_Perspective(PI/2, 1024/768, 1, 1000);
Oyster::Graphics::API::NewFrame(view, proj);
game->Render();
wchar_t title[255];
swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed));
@ -266,7 +263,6 @@ HRESULT Render(float deltaTime)
HRESULT CleanUp()
{
SAFE_DELETE(game);
return S_OK;
}

View File

@ -172,20 +172,17 @@ bool InputClass::ReadMouse()
return true;
}
void InputClass::MouseMove(float &Pitch, float &RotateY )
float InputClass::GetPitch( )
{
//if left mouse button is pressed
if (m_mouseState.rgbButtons[0])
{
float dx = (static_cast<float>( m_mouseState.lX)/150);
float dy = (static_cast<float>( m_mouseState.lY)/150);
//
Pitch=dy;
RotateY=dx;
}
float dy = (static_cast<float>( m_mouseState.lY)/5);
return -dy;
}
float InputClass::GetYaw( )
{
float dX = (static_cast<float>( m_mouseState.lX)/5);
return -dX;
}
bool InputClass::IsMousePressed()
{
if (m_mouseState.rgbButtons[0])

View File

@ -44,7 +44,11 @@ public:
bool IsKeyPressed(int key);
bool IsMousePressed();
void MouseMove(float &Pitch, float &RoateY);
// Call if mouse is pressed
float GetYaw();
float GetPitch();
};
#endif