view: factor out all view mutation

This commit is contained in:
Zack Buhman 2026-03-11 21:08:45 -05:00
parent e273a61277
commit 1b71cf4281
3 changed files with 66 additions and 43 deletions

View File

@ -6,6 +6,8 @@ namespace view {
XMVECTOR eye; XMVECTOR eye;
XMVECTOR forward; XMVECTOR forward;
XMVECTOR direction; XMVECTOR direction;
XMVECTOR at;
XMVECTOR normal;
float fov; float fov;
float pitch; float pitch;
@ -15,7 +17,12 @@ namespace view {
XMFLOAT4X4 float_transform; XMFLOAT4X4 float_transform;
}; };
extern view_state state; 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 update_transforms();
void load();
} }

View File

@ -598,13 +598,11 @@ void load(const char * source_path)
load_textures(); load_textures();
load_texture_id_uniform_buffer(); 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
view::state.forward = XMVectorSet(-0.63, 0.78, 0, 0); //////////////////////////////////////////////////////////////////////
view::state.direction = view::state.forward;
view::state.pitch = -0.11;
view::state.fov = 1.5; view::load();
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// font // font
@ -667,9 +665,9 @@ light_parameters lighting = {
void update_keyboard(int up, int down, int left, int right) void update_keyboard(int up, int down, int left, int right)
{ {
XMVECTOR normal = XMVector3NormalizeEst(XMVector3Cross(view::state.forward, view::state.up)); float forward = (0.1f * up + -0.1f * down);
view::state.eye += view::state.forward * (0.1f * up + -0.1f * down); float strafe = (-0.1f * left + 0.1f * right);
view::state.eye += normal * (-0.1f * left + 0.1f * right); view::apply_translation(forward, strafe, 0);
} }
const int max_joysticks = 8; const int max_joysticks = 8;
@ -682,25 +680,13 @@ void update_joystick(int joystick_index,
int leftshoulder, int rightshoulder, int leftshoulder, int rightshoulder,
int start) int start)
{ {
//view::state.yaw += rx; float forward = -ly;
XMMATRIX mrz = XMMatrixRotationZ(rx * -0.035); 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; lighting.quadratic += 0.01 * a + -0.01 * b;
if (lighting.quadratic < 0.0f) if (lighting.quadratic < 0.0f)
lighting.quadratic = 0.0f; lighting.quadratic = 0.0f;

View File

@ -1,22 +1,11 @@
#include "directxmath/directxmath.h" #include "directxmath/directxmath.h"
#include "window.h" #include "window.h"
#include "view.h"
constexpr bool third_person = false;
namespace view { 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; view_state state;
static inline XMMATRIX current_projection() static inline XMMATRIX current_projection()
@ -31,11 +20,39 @@ namespace view {
static inline XMMATRIX current_view() static inline XMMATRIX current_view()
{ {
XMVECTOR at = XMVectorAdd(state.eye, state.direction); state.at = XMVectorAdd(state.eye, state.direction);
XMMATRIX view = XMMatrixLookAtRH(state.eye, at, state.up);
XMMATRIX view = XMMatrixLookAtRH(state.eye, state.at, state.up);
return view; 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() void update_transforms()
{ {
state.projection_transform = current_projection(); state.projection_transform = current_projection();
@ -43,4 +60,17 @@ namespace view {
state.transform = state.view_transform * state.projection_transform; state.transform = state.view_transform * state.projection_transform;
XMStoreFloat4x4(&state.float_transform, state.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;
}
} }