-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxcam.cpp
159 lines (126 loc) · 3.5 KB
/
xcam.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
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
/**
* This file belongs to the 'xlab' game engine.
* Copyright 2009 xfacter
* Copyright 2016 wickles
* This work is licensed under the LGPLv3
* subject to all terms as reproduced in the included LICENSE file.
*/
#include <pspgu.h>
#include <pspgum.h>
#include "xmath.h"
#include "xcam.h"
xCamera::xCamera()
{
Reset(0.0f, 0.0f, 0.0f);
}
xCamera::xCamera(float x, float y, float z)
{
Reset(x, y, z);
}
void xCamera::Reset(float x, float y, float z)
{
gumLoadIdentity(&matrix);
rot.x = 0.0f; rot.y = 0.0f; rot.z = 0.0f;
pos.x = x; pos.y = y; pos.z = z;
}
void xCamera::LookAt(ScePspFVector3* at, float radius)
{
//
}
void xCamera::MoveTo(ScePspFVector3* _pos)
{
pos = *_pos;
}
void xCamera::Translate(ScePspFVector3* trans)
{
pos.x += trans->x;
pos.y += trans->y;
pos.z += trans->z;
}
void xCamera::Navigate(float right, float up, float fwd)
{
pos.x += right * matrix.x.x;
pos.y += right * matrix.y.x;
pos.z += right * matrix.z.x;
pos.x += up * matrix.x.y;
pos.y += up * matrix.y.y;
pos.z += up * matrix.z.y;
pos.x += fwd * matrix.x.z;
pos.y += fwd * matrix.y.z;
pos.z += fwd * matrix.z.z;
}
void xCamera::RotateX(float rad)
{
rot.x += rad;
}
void xCamera::RotateY(float rad)
{
rot.y += rad;
}
void xCamera::RotateZ(float rad)
{
rot.z += rad;
}
void xCamera::UpdateMatrix()
{
matrix.w.x = 0.0f; matrix.w.y = 0.0f; matrix.w.z = 0.0f;
ScePspFMatrix4 rot_matrix;
gumLoadIdentity(&rot_matrix);
gumRotateXYZ(&rot_matrix, &rot);
gumMultMatrix(&matrix, &rot_matrix, &matrix);
gumTranslate(&matrix, &pos);
rot.x = 0.0f; rot.y = 0.0f; rot.z = 0.0f;
}
void xCamera::SetViewMatrix(int no_update)
{
sceGumMatrixMode(GU_VIEW);
if (!no_update) UpdateMatrix();
sceGumLoadMatrix(&matrix);
sceGumMatrixMode(GU_MODEL);
}
ScePspFVector3* xCamera::GetPos()
{
return &pos;
}
xFollowCam::xFollowCam(ScePspFVector3* start, float max_dist)
{
pos = *start;
threshold = max_dist;
}
void xFollowCam::Update(ScePspFVector3* follow, float dt)
{
if (x_dist_test2(pos.x, pos.y, follow->x, follow->y, threshold))
{
ScePspFVector3 dir = {pos.x - follow->x, pos.y - follow->y, pos.z - follow->z};
x_normalize(&dir, 1.0f);
ScePspFVector3 move = {follow->x + threshold*dir.x, follow->y + threshold*dir.y, follow->z + threshold*dir.z};
x_ease_to_target3(&pos, &move, 0.99f, dt);
}
}
void xFollowCam::SetCamera(float height, ScePspFVector3* follow, ScePspFVector3* up)
{
ScePspFVector3 eye = {pos.x + height*up->x, pos.y + height*up->y, pos.z + height*up->z};
sceGumMatrixMode(GU_VIEW);
sceGumLoadIdentity();
sceGumLookAt(&eye, follow, up);
sceGumMatrixMode(GU_MODEL);
}
#if 0
// better C version
typedef struct {
ScePspFVector3 up;
ScePspFVector3 pos;
ScePspFVector3 dir;
} xCamera;
xCamSetMatrix(xCamera* cam);
xCamSetUpVector(xCamera* cam, float x, float y, float z);
xCamOrient(xCamera* cam, float px, float py, float pz, float dx, float dy, float dz);
xCamOrbit(xCamera* cam, float tx, float ty, float tz, float dx, float dy, float dz, float r);
xCamMove(xCamera* cam, float fwd, float left, float up);
xCamRotateX(xCamera* cam, float angle);
xCamRotateY(xCamera* cam, float angle);
xCamRotateZ(xCamera* cam, float angle);
xCamRotateAroundX(xCamera* cam, float angle, float tx, float ty, float tz);
xCamRotateAroundY(xCamera* cam, float angle, float tx, float ty, float tz);
xCamRotateAroundZ(xCamera* cam, float angle, float tx, float ty, float tz);
#endif