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()
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() )
{
wnd.DisableCursor();
......@@ -42,6 +48,46 @@ void App::DoFrame()
wnd.EnableCursor();
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()
light.SpawnControlWindow();
ShowImguiDemoWindow();
nano.ShowWindow();
ShowRawInputWindow();
// present
wnd.Gfx().EndFrame();
......@@ -58,26 +103,10 @@ void App::DoFrame()
void App::ShowImguiDemoWindow()
{
static bool show_demo_window = true;
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" ) )
if( showDemoWindow )
{
ImGui::Text( "Tally: (%d,%d)",x,y );
ImGui::Text( "Cursor: %s",wnd.CursorEnabled()?"enabled":"disabled" );
ImGui::ShowDemoWindow( &showDemoWindow );
}
ImGui::End();
}
App::~App()
......
......@@ -17,9 +17,8 @@ public:
private:
void DoFrame();
void ShowImguiDemoWindow();
void ShowRawInputWindow();
private:
int x = 0,y = 0;
bool showDemoWindow = false;
ImguiManager imgui;
Window wnd;
ChiliTimer timer;
......
#include "Camera.h"
#include "imgui/imgui.h"
#include "ChiliMath.h"
namespace dx = DirectX;
Camera::Camera() noexcept
{
Reset();
}
DirectX::XMMATRIX Camera::GetMatrix() const noexcept
{
const auto pos = dx::XMVector3Transform(
dx::XMVectorSet( 0.0f,0.0f,-r,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
);
return dx::XMMatrixTranslation( -pos.x,-pos.y,-pos.z ) *
dx::XMMatrixRotationRollPitchYaw( -pitch,-yaw,0.0f );
}
void Camera::SpawnControlWindow() noexcept
......@@ -22,12 +20,11 @@ void Camera::SpawnControlWindow() noexcept
if( ImGui::Begin( "Camera" ) )
{
ImGui::Text( "Position" );
ImGui::SliderFloat( "R",&r,0.2f,80.0f,"%.1f" );
ImGui::SliderAngle( "Theta",&theta,-180.0f,180.0f );
ImGui::SliderAngle( "Phi",&phi,-89.0f,89.0f );
ImGui::SliderFloat( "X",&pos.x,-80.0f,80.0f,"%.1f" );
ImGui::SliderFloat( "Y",&pos.y,-80.0f,80.0f,"%.1f" );
ImGui::SliderFloat( "Z",&pos.z,-80.0f,80.0f,"%.1f" );
ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&roll,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&pitch,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&pitch,-90.0f,90.0f );
ImGui::SliderAngle( "Yaw",&yaw,-180.0f,180.0f );
if( ImGui::Button( "Reset" ) )
{
......@@ -39,10 +36,27 @@ void Camera::SpawnControlWindow() noexcept
void Camera::Reset() noexcept
{
r = 20.0f;
theta = 0.0f;
phi = 0.0f;
pos = { 0.0f,7.5f,-18.0f };
pitch = 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 @@
class Camera
{
public:
Camera() noexcept;
DirectX::XMMATRIX GetMatrix() const noexcept;
void SpawnControlWindow() noexcept;
void Reset() noexcept;
void Rotate( float dx,float dy ) noexcept;
void Translate( DirectX::XMFLOAT3 translation ) noexcept;
private:
float r = 20.0f;
float theta = 0.0f;
float phi = 0.0f;
float pitch = 0.0f;
float yaw = 0.0f;
float roll = 0.0f;
DirectX::XMFLOAT3 pos;
float pitch;
float yaw;
static constexpr float travelSpeed = 12.0f;
static constexpr float rotationSpeed = 0.004f;
};
\ 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
void PointLight::Reset() noexcept
{
cbData = {
{ 0.0f,0.0f,0.0f },
{ 1.5f,14.0f,-4.5f },
{ 0.05f,0.05f,0.05f },
{ 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