-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
90 lines (84 loc) · 2.72 KB
/
camera.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "camera.h"
#define _USE_MATH_DEFINES
#include "math.h"
const vec3 YAxis = { 0, 1, 0 };
void
UpdateCameraMatrices(camera* Camera)
{
Camera->ViewMatrix = Math::Mat4Camera(Camera->Position, Camera->Forward, YAxis);
Camera->ProjectionMatrix =
Math::Mat4Perspective(Camera->FieldOfView, (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT,
Camera->NearClipPlane, Camera->FarClipPlane);
Camera->VPMatrix = Math::MulMat4(Camera->ProjectionMatrix, Camera->ViewMatrix);
Camera->InvVPMatrix = Math::InvMat4(Camera->VPMatrix);
}
void
UpdateCameraDerivedFields(camera* Camera)
{
Camera->Rotation.X = ClampFloat(-Camera->MaxTiltAngle, Camera->Rotation.X, Camera->MaxTiltAngle);
Camera->Forward = Math::Normalized(
Math::MulMat3Vec3(Math::Mat3Rotate(Camera->Rotation.X, Camera->Rotation.Y, 0), { 0, 0, -1 }));
Camera->Right = Math::Normalized(Math::Cross(Camera->Forward, YAxis));
UpdateCameraMatrices(Camera);
}
void
UpdateCamera(camera* Camera, vec3 FollowPoint, const game_input* Input)
{
const float DegToRad = float(M_PI) / 180.0f;
if(!Input->IsMouseInEditorMode)
{
Camera->OrbitRotation.Y =
ClampFloat(-10 * DegToRad, Camera->OrbitRotation.Y - 0.003f * (float)Input->dMouseY,
70 * DegToRad);
Camera->OrbitRotation.X += 0.05f * (float)Input->dMouseX;
Camera->OrbitRadius = MaxFloat(0, Camera->OrbitRadius + 0.1f * Input->dMouseWheelScreen);
}
vec3 Dir = Math::Normalized(
vec3{ cosf(Camera->OrbitRotation.X * DegToRad), 0, sinf(Camera->OrbitRotation.X * DegToRad) });
Dir.Y = tan(Camera->OrbitRotation.Y);
Dir = -Math::Normalized(Dir);
Camera->Position = FollowPoint + -Dir * Camera->OrbitRadius;
Camera->Forward = Dir;
Camera->Right = Math::Normalized(Math::Cross(Camera->Forward, YAxis));
UpdateCameraMatrices(Camera);
}
void
UpdateCamera(camera* Camera, const game_input* Input)
{
if(Input->LeftShift.EndedDown)
{
if(Input->w.EndedDown)
{
Camera->Position += Input->dt * Camera->Speed * YAxis;
}
if(Input->s.EndedDown)
{
Camera->Position -= Input->dt * Camera->Speed * YAxis;
}
}
else
{
if(Input->w.EndedDown)
{
Camera->Position += Input->dt * Camera->Speed * Camera->Forward;
}
if(Input->s.EndedDown)
{
Camera->Position -= Input->dt * Camera->Speed * Camera->Forward;
}
}
if(Input->a.EndedDown)
{
Camera->Position -= Input->dt * Camera->Speed * Camera->Right;
}
if(Input->d.EndedDown)
{
Camera->Position += Input->dt * Camera->Speed * Camera->Right;
}
if(!Input->IsMouseInEditorMode)
{
Camera->Rotation.X += 0.05f * (float)Input->dMouseY;
Camera->Rotation.Y -= 0.05f * (float)Input->dMouseX;
}
UpdateCameraDerivedFields(Camera);
}