diff --git a/include/view.h b/include/view.h index af6942b..5ee247f 100644 --- a/include/view.h +++ b/include/view.h @@ -6,6 +6,8 @@ namespace view { XMVECTOR eye; XMVECTOR forward; XMVECTOR direction; + XMVECTOR at; + XMVECTOR normal; float fov; float pitch; @@ -15,7 +17,12 @@ namespace view { XMFLOAT4X4 float_transform; }; + extern view_state state; + void apply_translation(float forward, float strafe, float elevation); + void apply_yaw_pitch(float delta_yaw, float delta_pitch); + void apply_fov(float delta); void update_transforms(); + void load(); } diff --git a/src/test.cpp b/src/test.cpp index 37598aa..e5f7cbd 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -598,13 +598,11 @@ void load(const char * source_path) load_textures(); load_texture_id_uniform_buffer(); - view::state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); - view::state.eye = XMVectorSet(-55.5f, 48.25f, 50.0f, 1); - view::state.forward = XMVectorSet(-0.63, 0.78, 0, 0); - view::state.direction = view::state.forward; - view::state.pitch = -0.11; + ////////////////////////////////////////////////////////////////////// + // view + ////////////////////////////////////////////////////////////////////// - view::state.fov = 1.5; + view::load(); ////////////////////////////////////////////////////////////////////// // font @@ -667,9 +665,9 @@ light_parameters lighting = { void update_keyboard(int up, int down, int left, int right) { - XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view::state.forward, view::state.up)); - view::state.eye += view::state.forward * (0.1f * up + -0.1f * down); - view::state.eye += normal * (-0.1f * left + 0.1f * right); + float forward = (0.1f * up + -0.1f * down); + float strafe = (-0.1f * left + 0.1f * right); + view::apply_translation(forward, strafe, 0); } const int max_joysticks = 8; @@ -682,25 +680,13 @@ void update_joystick(int joystick_index, int leftshoulder, int rightshoulder, int start) { - //view::state.yaw += rx; - XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035); + float forward = -ly; + float strafe = lx; + float elevation = tl - tr; + view::apply_yaw_pitch(rx * -0.035, ry * -0.035); + view::apply_translation(forward, strafe, elevation); + view::apply_fov(0.01 * up + -0.01 * down); - view::state.forward = XMVector3Transform(XMVector3NormalizeEst(view::state.forward), mrz); - XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view::state.forward, view::state.up)); - - view::state.pitch += ry * -0.035; - if (view::state.pitch > 1.57f) view::state.pitch = 1.57f; - if (view::state.pitch < -1.57f) view::state.pitch = -1.57f; - - XMMATRIX mrn = XMMatrixRotationAxis(normal, view::state.pitch); - view::state.direction = XMVector3Transform(view::state.forward, mrn); - - view::state.eye += view::state.forward * -ly + normal * lx + view::state.up * (tl - tr); - - float new_fov = view::state.fov + 0.01 * up + -0.01 * down; - if (new_fov > 0.00001f) { - view::state.fov = new_fov; - } lighting.quadratic += 0.01 * a + -0.01 * b; if (lighting.quadratic < 0.0f) lighting.quadratic = 0.0f; diff --git a/src/view.cpp b/src/view.cpp index ccc109b..e306b27 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -1,22 +1,11 @@ #include "directxmath/directxmath.h" #include "window.h" +#include "view.h" + +constexpr bool third_person = false; namespace view { - struct view_state { - XMVECTOR up; - XMVECTOR eye; - XMVECTOR forward; - XMVECTOR direction; - float fov; - float pitch; - - XMMATRIX projection_transform; - XMMATRIX view_transform; - XMMATRIX transform; - XMFLOAT4X4 float_transform; - }; - view_state state; static inline XMMATRIX current_projection() @@ -31,11 +20,39 @@ namespace view { static inline XMMATRIX current_view() { - XMVECTOR at = XMVectorAdd(state.eye, state.direction); - XMMATRIX view = XMMatrixLookAtRH(state.eye, at, state.up); + state.at = XMVectorAdd(state.eye, state.direction); + + XMMATRIX view = XMMatrixLookAtRH(state.eye, state.at, state.up); return view; } + void apply_translation(float forward, float strafe, float elevation) + { + state.eye += state.forward * forward + state.normal * strafe + state.up * elevation; + } + + void apply_yaw_pitch(float delta_yaw, float delta_pitch) + { + XMMATRIX mrz = XMMatrixRotationZ(delta_yaw); + state.forward = XMVector3Transform(state.forward, mrz); + state.normal = XMVector3NormalizeEst(XMVector3Cross(state.forward, state.up)); + + state.pitch += delta_pitch; + if (state.pitch > 1.57f) state.pitch = 1.57f; + if (state.pitch < -1.57f) state.pitch = -1.57f; + + XMMATRIX mrn = XMMatrixRotationAxis(state.normal, state.pitch); + state.direction = XMVector3Transform(state.forward, mrn); + } + + void apply_fov(float delta) + { + float new_fov = state.fov + delta; + if (new_fov > 0.00001f) { + state.fov = new_fov; + } + } + void update_transforms() { state.projection_transform = current_projection(); @@ -43,4 +60,17 @@ namespace view { state.transform = state.view_transform * state.projection_transform; XMStoreFloat4x4(&state.float_transform, state.transform); } + + void load() + { + state.up = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); + state.eye = XMVectorSet(-55.5f, 48.25f, 50.0f, 1); + state.forward = XMVector3Normalize(XMVectorSet(-0.63, 0.78, 0, 0)); + state.direction = state.forward; + state.normal = XMVector3Normalize(XMVector3Cross(state.forward, state.up)); + + state.fov = 1.5; + + state.pitch = 0; + } }