-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleCamera3D.cpp
More file actions
177 lines (132 loc) · 4.14 KB
/
ModuleCamera3D.cpp
File metadata and controls
177 lines (132 loc) · 4.14 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Globals.h"
#include "Application.h"
#include "PhysBody3D.h"
#include "ModuleCamera3D.h"
#include "ModulePlayer.h"
#include "PhysVehicle3D.h"
ModuleCamera3D::ModuleCamera3D(Application* app, bool start_enabled) : Module(app, start_enabled)
{
CalculateViewMatrix();
X = vec3(1.0f, 0.0f, 0.0f);
Y = vec3(0.0f, 1.0f, 0.0f);
Z = vec3(0.0f, 0.0f, 1.0f);
Position = vec3(0.0f, 0.0f, 5.0f);
Reference = vec3(0.0f, 0.0f, 0.0f);
}
ModuleCamera3D::~ModuleCamera3D()
{}
// -----------------------------------------------------------------
bool ModuleCamera3D::Start()
{
LOG("Setting up the camera");
bool ret = true;
return ret;
}
// -----------------------------------------------------------------
bool ModuleCamera3D::CleanUp()
{
LOG("Cleaning camera");
return true;
}
// -----------------------------------------------------------------
update_status ModuleCamera3D::Update(float dt)
{
// Implement a debug camera with keys and mouse
// Now we can make this movememnt frame rate independant!
/*vec3 newPos(0,0,0);
float speed = 3.0f * dt;
if(App->input->GetKey(SDL_SCANCODE_LSHIFT) == KEY_REPEAT)
speed = 8.0f * dt;
if(App->input->GetKey(SDL_SCANCODE_R) == KEY_REPEAT) newPos.y += speed;
if(App->input->GetKey(SDL_SCANCODE_F) == KEY_REPEAT) newPos.y -= speed;
if(App->input->GetKey(SDL_SCANCODE_W) == KEY_REPEAT) newPos -= Z * speed;
if(App->input->GetKey(SDL_SCANCODE_S) == KEY_REPEAT) newPos += Z * speed;
if(App->input->GetKey(SDL_SCANCODE_A) == KEY_REPEAT) newPos -= X * speed;
if(App->input->GetKey(SDL_SCANCODE_D) == KEY_REPEAT) newPos += X * speed;
Position += newPos;
Reference += newPos;
*/
// Mouse motion ----------------
if(App->input->GetMouseButton(SDL_BUTTON_RIGHT) == KEY_REPEAT)
{
int dx = -App->input->GetMouseXMotion();
int dy = -App->input->GetMouseYMotion();
float Sensitivity = 0.25f;
Position -= Reference;
if(dx != 0)
{
float DeltaX = (float)dx * Sensitivity;
X = rotate(X, DeltaX, vec3(0.0f, 1.0f, 0.0f));
Y = rotate(Y, DeltaX, vec3(0.0f, 1.0f, 0.0f));
Z = rotate(Z, DeltaX, vec3(0.0f, 1.0f, 0.0f));
}
if(dy != 0)
{
float DeltaY = (float)dy * Sensitivity;
Y = rotate(Y, DeltaY, X);
Z = rotate(Z, DeltaY, X);
if(Y.y < 0.0f)
{
Z = vec3(0.0f, Z.y > 0.0f ? 1.0f : -1.0f, 0.0f);
Y = cross(Z, X);
}
}
Position = Reference + Z * length(Position);
}
else
{
mat4x4 matrix;
App->player->vehicle->GetTransform(&matrix);
Position = matrix.translation();
X = vec3{ matrix[0],matrix[1],matrix[2] };
Y = vec3{ matrix[4], matrix[5], matrix[6] };
Z = vec3{ matrix[8], matrix[9],matrix[10] };
vec3 VehicleLocation = { matrix[12], matrix[13] + 7, matrix[14] };
Look((VehicleLocation)-Z * 15, VehicleLocation, true);
}
// Recalculate matrix -------------
CalculateViewMatrix();
return UPDATE_CONTINUE;
}
// -----------------------------------------------------------------
void ModuleCamera3D::Look(const vec3 &Position, const vec3 &Reference, bool RotateAroundReference)
{
this->Position = Position;
this->Reference = Reference;
Z = normalize(Position - Reference);
X = normalize(cross(vec3(0.0f, 1.0f, 0.0f), Z));
Y = cross(Z, X);
if(!RotateAroundReference)
{
this->Reference = this->Position;
this->Position += Z * 0.05f;
}
CalculateViewMatrix();
}
// -----------------------------------------------------------------
void ModuleCamera3D::LookAt( const vec3 &Spot)
{
Reference = Spot;
Z = normalize(Position - Reference);
X = normalize(cross(vec3(0.0f, 1.0f, 0.0f), Z));
Y = cross(Z, X);
CalculateViewMatrix();
}
// -----------------------------------------------------------------
void ModuleCamera3D::Move(const vec3 &Movement)
{
Position += Movement;
Reference += Movement;
CalculateViewMatrix();
}
// -----------------------------------------------------------------
float* ModuleCamera3D::GetViewMatrix()
{
return &ViewMatrix;
}
// -----------------------------------------------------------------
void ModuleCamera3D::CalculateViewMatrix()
{
ViewMatrix = mat4x4(X.x, Y.x, Z.x, 0.0f, X.y, Y.y, Z.y, 0.0f, X.z, Y.z, Z.z, 0.0f, -dot(X, Position), -dot(Y, Position), -dot(Z, Position), 1.0f);
ViewMatrixInverse = inverse(ViewMatrix);
}