DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
camera.cpp
1#pragma helper_mask_fpr 0x1000
2
3#include "camera.hpp"
4
5#include <libvu0.h>
6
7#include <cmath>
8
9#include "mathutil.hpp"
10
12
13void CCamera::Step(int steps) {
14 float dir[4];
15 float flat[4];
16 int i;
17 int step;
18 int axis;
19
21 return;
22 }
23
24 if (this->speed <= 0.0f) {
25 this->speed = 1.0f;
26 }
27
28 // A step count below zero puts the eye and the look-at point onto their
29 // positions without any of the steps in between.
30 if (steps < 0) {
31 for (i = 0; i < 3; i++) {
32 this->pos[i] = this->next_pos[i];
33 this->ref[i] = this->next_ref[i];
34 }
35 } else {
36 for (step = 0; step < steps; step++) {
37 for (axis = 0; axis < 3; axis++) {
38 float move_pos;
39 float move_ref;
40 float left_pos;
41 float left_ref;
42 float speed = this->speed;
43
44 if (speed == 1.0f) {
45 this->pos[axis] = this->next_pos[axis];
46 this->ref[axis] = this->next_ref[axis];
47 } else {
48 move_pos = (this->next_pos[axis] - this->pos[axis]) / speed;
49
50 if (speed < 1.0f) {
51 speed = 1.0f;
52 }
53
54 move_ref = (this->next_ref[axis] - this->ref[axis]) / speed;
55
56 if (this->limit_move == 1) {
57 if (move_pos > 2.0f) {
58 move_pos = 2.0f;
59 }
60
61 if (move_pos < -2.0f) {
62 move_pos = -2.0f;
63 }
64 }
65
66 this->pos[axis] += move_pos;
67 this->ref[axis] += move_ref;
68
69 // The last step of the movement is not exact, so the eye
70 // and the look-at point snap on once they are near enough.
71 left_pos = this->pos[axis] - this->next_pos[axis];
72 left_ref = this->ref[axis] - this->next_ref[axis];
73
74 if (left_pos < this->snap_range && left_pos > -this->snap_range) {
75 this->pos[axis] = this->next_pos[axis];
76 }
77
78 if (left_ref < this->snap_range && left_ref > -this->snap_range) {
79 this->ref[axis] = this->next_ref[axis];
80 }
81 }
82 }
83 }
84 }
85
86 // The angles of the view direction; the horizontal one comes from the
87 // direction with its height taken out.
88 this->GetDir(dir);
89 flat[0] = dir[0];
90 flat[1] = 0.0f;
91 flat[2] = dir[2];
92 flat[3] = 0.0f;
93 sceVu0Normalize(flat, flat);
94 this->angle_h = atan2f(-flat[0], -flat[2]);
95 this->angle_v = -atan2f(dir[1], sqrtf(dir[0] * dir[0] + dir[2] * dir[2]));
96}
97
99 sceVu0CopyVector(this->next_pos, this->pos);
100 sceVu0CopyVector(this->next_ref, this->ref);
101}
102
103void CCamera::SetPos(CFrame *parent, float x, float y, float z) {
104 this->next_pos[0] = x;
105 this->pos[0] = x;
106 this->next_pos[1] = y;
107 this->pos[1] = y;
108 this->next_pos[2] = z;
109 this->pos[2] = z;
110 this->next_pos[3] = 1.0f;
111 this->pos[3] = 1.0f;
112}
113
114void CCamera::SetPos(float *pos) {
115 this->SetPos(NULL, pos[0], pos[1], pos[2]);
116}
117
118void CCamera::SetNextPos(CFrame *parent, float x, float y, float z) {
119 this->next_pos[0] = x;
120 this->next_pos[1] = y;
121 this->next_pos[2] = z;
122}
123
124void CCamera::SetRef(CFrame *parent, float x, float y, float z) {
125 this->ref[0] = x;
126 this->next_ref[0] = x;
127 this->ref[1] = y;
128 this->next_ref[1] = y;
129 this->ref[2] = z;
130 this->next_ref[2] = z;
131}
132
133void CCamera::SetRef(float *ref) {
134 this->SetRef(NULL, ref[0], ref[1], ref[2]);
135}
136
137void CCamera::SetRef(float x, float y, float z) {
138 this->SetRef(NULL, x, y, z);
139}
140
141void CCamera::SetNextRef(CFrame *parent, float x, float y, float z) {
142 this->next_ref[0] = x;
143 this->next_ref[1] = y;
144 this->next_ref[2] = z;
145}
146
147void CCamera::GetDir(float *dir) {
148 dir[0] = this->ref[0] - this->pos[0];
149 dir[1] = this->ref[1] - this->pos[1];
150 dir[2] = this->ref[2] - this->pos[2];
151}
152
153void CCamera::GetCameraMatrix(float (*matrix)[4]) {
154 float dir[4];
155 float roll_matrix[4][4];
156 float length;
157 float flat_length;
158 float sin_h;
159 float cos_h;
160 float cos_v;
161 float sin_v;
162 float sin_roll;
163 float cos_roll;
164
165 // The view looks down the negative Z axis, so the direction turns around.
166 this->GetDir(dir);
167 dir[1] = -dir[1];
168 dir[2] = -dir[2];
169
170 length = sqrtf(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
171 flat_length = sqrtf(dir[0] * dir[0] + dir[2] * dir[2]);
172
173 if (flat_length == 0.0f) {
174 flat_length = 1.0f;
175 }
176
177 if (length == 0.0f) {
178 length = 1.0f;
179 }
180
181 sin_h = dir[2] / flat_length;
182 cos_h = dir[0] / flat_length;
183 cos_v = flat_length / length;
184 sin_v = dir[1] / length;
185
186 matrix[0][0] = sin_h;
187 matrix[0][1] = -cos_h * sin_v;
188 matrix[0][2] = cos_h * cos_v;
189 matrix[0][3] = 0.0f;
190 matrix[1][0] = 0.0f;
191 matrix[1][1] = -cos_v;
192 matrix[1][2] = -sin_v;
193 matrix[1][3] = 0.0f;
194 matrix[2][0] = cos_h;
195 matrix[2][1] = sin_h * sin_v;
196 matrix[2][2] = -sin_h * cos_v;
197 matrix[2][3] = 0.0f;
198 matrix[3][0] = -this->pos[0] * sin_h - this->pos[2] * cos_h;
199 matrix[3][1] = sin_v * (this->pos[0] * cos_h) + this->pos[1] * cos_v - sin_v * (this->pos[2] * sin_h);
200 matrix[3][2] = cos_v * (-this->pos[0] * cos_h) + this->pos[1] * sin_v + cos_v * (this->pos[2] * sin_h);
201 matrix[3][3] = 1.0f;
202
203 // The roll turns the view about the direction that it looks along.
204 if (this->roll > PI) {
205 this->roll -= TWO_PI;
206 }
207
208 if (this->roll < -PI) {
209 this->roll += TWO_PI;
210 }
211
212 sceVu0UnitMatrix(roll_matrix);
213 sin_roll = sinf(this->roll);
214 cos_roll = cosf(this->roll);
215 roll_matrix[0][0] = cos_roll;
216 roll_matrix[1][0] = sin_roll;
217 roll_matrix[0][1] = -sin_roll;
218 roll_matrix[1][1] = cos_roll;
219 MulMatrix(matrix, roll_matrix, matrix);
220}
221
222void CCamera::SetSpeed(float speed) {
223 this->speed = speed;
224}
225
226void CCamera::SetRoll(float roll) {
227 this->roll = roll;
228}
229
230void CCamera::GetPos(float *pos) {
231 sceVu0CopyVector(pos, this->pos);
232}
233
234void CCamera::GetRef(float *ref) {
235 sceVu0CopyVector(ref, this->ref);
236}
237
239 return this->angle_h;
240}
241
243 return this->roll;
244}
245
246CCamera::CCamera(float speed) {
247 this->speed = speed;
248
249 float zero = 0.0f;
250
251 if (this->speed <= zero) {
252 this->speed = 1.0f;
253 }
254
255 this->limit_move = 0;
256 this->roll = zero;
257 this->snap_range = 0.01f;
258}
259
virtual ~CCamera()
Definition camera.cpp:260
float speed
Definition camera.hpp:26
virtual void Step(int steps)
Definition camera.cpp:13
void SetNextRef(CFrame *parent, float x, float y, float z)
Definition camera.cpp:141
float roll
Definition camera.hpp:24
static s32 StopCamera
Definition camera.hpp:38
void GetCameraMatrix(float(*matrix)[4])
Definition camera.cpp:153
float snap_range
Definition camera.hpp:29
void GetRef(float *ref)
Definition camera.cpp:234
s32 limit_move
Definition camera.hpp:25
float next_ref[4]
Definition camera.hpp:23
float angle_h
Definition camera.hpp:27
void SetRef(CFrame *parent, float x, float y, float z)
Definition camera.cpp:124
float GetAngleH()
Definition camera.cpp:238
void SetPos(CFrame *parent, float x, float y, float z)
Definition camera.cpp:103
float pos[4]
Definition camera.hpp:20
float ref[4]
Definition camera.hpp:21
CCamera(float speed)
Definition camera.cpp:246
void SetSpeed(float speed)
Definition camera.cpp:222
float GetRoll()
Definition camera.cpp:242
float angle_v
Definition camera.hpp:28
virtual void Stay()
Definition camera.cpp:98
float next_pos[4]
Definition camera.hpp:22
void GetPos(float *pos)
Definition camera.cpp:230
void SetNextPos(CFrame *parent, float x, float y, float z)
Definition camera.cpp:118
void GetDir(float *dir)
Definition camera.cpp:147
void SetRoll(float roll)
Definition camera.cpp:226