Commit 305895dc authored by chili's avatar chili
Browse files

bad camera rotation free mouse look

parent 2a6d9115
...@@ -30,8 +30,14 @@ void App::DoFrame() ...@@ -30,8 +30,14 @@ void App::DoFrame()
while( const auto e = wnd.kbd.ReadKey() ) while( const auto e = wnd.kbd.ReadKey() )
{ {
if( e->IsPress() && e->GetCode() == VK_INSERT ) if( !e->IsPress() )
{ {
continue;
}
switch( e->GetCode() )
{
case VK_ESCAPE:
if( wnd.CursorEnabled() ) if( wnd.CursorEnabled() )
{ {
wnd.DisableCursor(); wnd.DisableCursor();
...@@ -42,6 +48,46 @@ void App::DoFrame() ...@@ -42,6 +48,46 @@ void App::DoFrame()
wnd.EnableCursor(); wnd.EnableCursor();
wnd.mouse.DisableRaw(); wnd.mouse.DisableRaw();
} }
break;
case VK_F1:
showDemoWindow = true;
break;
}
}
if( !wnd.CursorEnabled() )
{
if( wnd.kbd.KeyIsPressed( 'W' ) )
{
cam.Translate( { 0.0f,0.0f,dt } );
}
if( wnd.kbd.KeyIsPressed( 'A' ) )
{
cam.Translate( { -dt,0.0f,0.0f } );
}
if( wnd.kbd.KeyIsPressed( 'S' ) )
{
cam.Translate( { 0.0f,0.0f,-dt } );
}
if( wnd.kbd.KeyIsPressed( 'D' ) )
{
cam.Translate( { dt,0.0f,0.0f } );
}
if( wnd.kbd.KeyIsPressed( 'R' ) )
{
cam.Translate( { 0.0f,dt,0.0f } );
}
if( wnd.kbd.KeyIsPressed( 'F' ) )
{
cam.Translate( { 0.0f,-dt,0.0f } );
}
}
while( const auto delta = wnd.mouse.ReadRawDelta() )
{
if( !wnd.CursorEnabled() )
{
cam.Rotate( delta->x,delta->y );
} }
} }
...@@ -50,7 +96,6 @@ void App::DoFrame() ...@@ -50,7 +96,6 @@ void App::DoFrame()
light.SpawnControlWindow(); light.SpawnControlWindow();
ShowImguiDemoWindow(); ShowImguiDemoWindow();
nano.ShowWindow(); nano.ShowWindow();
ShowRawInputWindow();
// present // present
wnd.Gfx().EndFrame(); wnd.Gfx().EndFrame();
...@@ -58,26 +103,10 @@ void App::DoFrame() ...@@ -58,26 +103,10 @@ void App::DoFrame()
void App::ShowImguiDemoWindow() void App::ShowImguiDemoWindow()
{ {
static bool show_demo_window = true; if( showDemoWindow )
if( show_demo_window )
{
ImGui::ShowDemoWindow( &show_demo_window );
}
}
void App::ShowRawInputWindow()
{
while( const auto d = wnd.mouse.ReadRawDelta() )
{
x += d->x;
y += d->y;
}
if( ImGui::Begin( "Raw Input" ) )
{ {
ImGui::Text( "Tally: (%d,%d)",x,y ); ImGui::ShowDemoWindow( &showDemoWindow );
ImGui::Text( "Cursor: %s",wnd.CursorEnabled()?"enabled":"disabled" );
} }
ImGui::End();
} }
App::~App() App::~App()
......
...@@ -17,9 +17,8 @@ public: ...@@ -17,9 +17,8 @@ public:
private: private:
void DoFrame(); void DoFrame();
void ShowImguiDemoWindow(); void ShowImguiDemoWindow();
void ShowRawInputWindow();
private: private:
int x = 0,y = 0; bool showDemoWindow = false;
ImguiManager imgui; ImguiManager imgui;
Window wnd; Window wnd;
ChiliTimer timer; ChiliTimer timer;
......
#include "Camera.h" #include "Camera.h"
#include "imgui/imgui.h" #include "imgui/imgui.h"
#include "ChiliMath.h"
namespace dx = DirectX; namespace dx = DirectX;
Camera::Camera() noexcept
{
Reset();
}
DirectX::XMMATRIX Camera::GetMatrix() const noexcept DirectX::XMMATRIX Camera::GetMatrix() const noexcept
{ {
const auto pos = dx::XMVector3Transform( return dx::XMMatrixTranslation( -pos.x,-pos.y,-pos.z ) *
dx::XMVectorSet( 0.0f,0.0f,-r,0.0f ), dx::XMMatrixRotationRollPitchYaw( -pitch,-yaw,0.0f );
dx::XMMatrixRotationRollPitchYaw( phi,-theta,0.0f )
);
return dx::XMMatrixLookAtLH(
pos,dx::XMVectorZero(),
dx::XMVectorSet( 0.0f,1.0f,0.0f,0.0f )
) * dx::XMMatrixRotationRollPitchYaw(
pitch,-yaw,roll
);
} }
void Camera::SpawnControlWindow() noexcept void Camera::SpawnControlWindow() noexcept
...@@ -22,12 +20,11 @@ void Camera::SpawnControlWindow() noexcept ...@@ -22,12 +20,11 @@ void Camera::SpawnControlWindow() noexcept
if( ImGui::Begin( "Camera" ) ) if( ImGui::Begin( "Camera" ) )
{ {
ImGui::Text( "Position" ); ImGui::Text( "Position" );
ImGui::SliderFloat( "R",&r,0.2f,80.0f,"%.1f" ); ImGui::SliderFloat( "X",&pos.x,-80.0f,80.0f,"%.1f" );
ImGui::SliderAngle( "Theta",&theta,-180.0f,180.0f ); ImGui::SliderFloat( "Y",&pos.y,-80.0f,80.0f,"%.1f" );
ImGui::SliderAngle( "Phi",&phi,-89.0f,89.0f ); ImGui::SliderFloat( "Z",&pos.z,-80.0f,80.0f,"%.1f" );
ImGui::Text( "Orientation" ); ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&roll,-180.0f,180.0f ); ImGui::SliderAngle( "Pitch",&pitch,-90.0f,90.0f );
ImGui::SliderAngle( "Pitch",&pitch,-180.0f,180.0f );
ImGui::SliderAngle( "Yaw",&yaw,-180.0f,180.0f ); ImGui::SliderAngle( "Yaw",&yaw,-180.0f,180.0f );
if( ImGui::Button( "Reset" ) ) if( ImGui::Button( "Reset" ) )
{ {
...@@ -39,10 +36,27 @@ void Camera::SpawnControlWindow() noexcept ...@@ -39,10 +36,27 @@ void Camera::SpawnControlWindow() noexcept
void Camera::Reset() noexcept void Camera::Reset() noexcept
{ {
r = 20.0f; pos = { 0.0f,7.5f,-18.0f };
theta = 0.0f;
phi = 0.0f;
pitch = 0.0f; pitch = 0.0f;
yaw = 0.0f; yaw = 0.0f;
roll = 0.0f; }
void Camera::Rotate( float dx,float dy ) noexcept
{
yaw = wrap_angle( yaw + dx * rotationSpeed );
pitch = std::clamp( pitch + dy * rotationSpeed,-PI / 2.0f,PI / 2.0f );
}
void Camera::Translate( DirectX::XMFLOAT3 translation ) noexcept
{
dx::XMStoreFloat3( &translation,dx::XMVector3Transform(
dx::XMLoadFloat3( &translation ),
dx::XMMatrixRotationRollPitchYaw( pitch,yaw,0.0f ) *
dx::XMMatrixScaling( travelSpeed,travelSpeed,travelSpeed )
) );
pos = {
pos.x + translation.x,
pos.y + translation.y,
pos.z + translation.z
};
} }
...@@ -4,14 +4,16 @@ ...@@ -4,14 +4,16 @@
class Camera class Camera
{ {
public: public:
Camera() noexcept;
DirectX::XMMATRIX GetMatrix() const noexcept; DirectX::XMMATRIX GetMatrix() const noexcept;
void SpawnControlWindow() noexcept; void SpawnControlWindow() noexcept;
void Reset() noexcept; void Reset() noexcept;
void Rotate( float dx,float dy ) noexcept;
void Translate( DirectX::XMFLOAT3 translation ) noexcept;
private: private:
float r = 20.0f; DirectX::XMFLOAT3 pos;
float theta = 0.0f; float pitch;
float phi = 0.0f; float yaw;
float pitch = 0.0f; static constexpr float travelSpeed = 12.0f;
float yaw = 0.0f; static constexpr float rotationSpeed = 0.004f;
float roll = 0.0f;
}; };
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -39,7 +39,7 @@ void PointLight::SpawnControlWindow() noexcept ...@@ -39,7 +39,7 @@ void PointLight::SpawnControlWindow() noexcept
void PointLight::Reset() noexcept void PointLight::Reset() noexcept
{ {
cbData = { cbData = {
{ 0.0f,0.0f,0.0f }, { 1.5f,14.0f,-4.5f },
{ 0.05f,0.05f,0.05f }, { 0.05f,0.05f,0.05f },
{ 1.0f,1.0f,1.0f }, { 1.0f,1.0f,1.0f },
1.0f, 1.0f,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment