-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera.c~
206 lines (158 loc) · 7.28 KB
/
camera.c~
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
camera.c
This is heavily inspired from the camera class from GameTutorials OpenGL tutorials
Original source:
Ben Humphrey (DigiBen)
Game Programmer
Co-Web Host of www.GameTutorials.com
Additional work and port:
Copyright 2007 Sebastien Delestaing
This file is part of "the_cube".
"the_cube" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
"the_cube" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/************************************ INCLUDES ***************************************/
#include <pspgu.h>
#include "camera.h"
/********************************* LOCAL FUNCTIONS ***********************************/
// Computes vector magnitude
static float Magnitude (ScePspFVector3 vNormal)
{
// Here is the equation: magnitude = sqrt(V.x^2 + V.y^2 + V.z^2) : Where V is the vector
return (float)sqrt( (vNormal.x * vNormal.x) +
(vNormal.y * vNormal.y) +
(vNormal.z * vNormal.z) );
}
// Substract 2 vectors
static ScePspFVector3 Substract (ScePspFVector3 vVector1, ScePspFVector3 vVector2)
{
ScePspFVector3 vSub;
vSub.x = vVector1.x - vVector2.x;
vSub.y = vVector1.y - vVector2.y;
vSub.z = vVector1.z - vVector2.z;
return vSub;
}
// Add 2 vectors
static ScePspFVector3 Add (ScePspFVector3 vVector1, ScePspFVector3 vVector2)
{
ScePspFVector3 vSum;
vSum.x = vVector1.x + vVector2.x;
vSum.y = vVector1.y + vVector2.y;
vSum.z = vVector1.z + vVector2.z;
return vSum;
}
/******************************* EXPORTED FUNCTIONS **********************************/
// Allocates camera and initialize to default value
cam_t *cameraInit (void)
{
cam_t *new;
ScePspFVector3 vZero = {0.0, 0.0, 0.0}; // Init a vVector to 0 0 0 for our position
ScePspFVector3 vView = {0.0, 1.0, 0.5}; // Init a starting view vVector (looking up and out the screen)
ScePspFVector3 vUp = {0.0, 0.0, 1.0}; // Init a standard up vVector (Rarely ever changes)
new = (cam_t *)malloc (sizeof (cam_t));
new->position = vZero; // Init the position to zero
new->view = vView; // Init the view to a std starting view
new->up_vector = vUp; // Init the UpVector
return new;
}
// Frees camera
void cameraDispose (cam_t *camera)
{
free (camera);
}
// Changes the position, view, and up vector of the camera.
void cameraPosition (cam_t *camera, ScePspFVector3 *vPosition, ScePspFVector3 *vView, ScePspFVector3 *vUpVector)
{
camera->position = *vPosition; // Assign the position
camera->view = *vView; // Assign the view
camera->up_vector = *vUpVector; // Assign the up vector
}
// Rotates the view around the position using an axis-angle rotation
void cameraRotateView (cam_t *camera, float angle, float x, float y, float z)
{
ScePspFVector3 vNewView;
// Get the view vector (The direction we are facing)
ScePspFVector3 vView = Substract (camera->view, camera->position);
// Calculate the sine and cosine of the angle once
float cosTheta = (float)cosf (angle);
float sinTheta = (float)sinf (angle);
// Find the new x position for the new rotated point
vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x;
vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y;
vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z;
// Find the new y position for the new rotated point
vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x;
vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y;
vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z;
// Find the new z position for the new rotated point
vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x;
vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y;
vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z;
// Now we just add the newly rotated vector to our position to set
// our new rotated view of our camera.
camera->view = Add (camera->position, vNewView);
}
///////////////////////////////// ROTATE AROUND POINT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This rotates the position around {0, 0, 0} point
/////
///////////////////////////////// ROTATE AROUND POINT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void cameraRotateAroundOrigin (cam_t *camera, float theta, float phi)
{
float cos_theta, sin_theta, cos_phi, sin_phi, camera_distance;
cos_phi = cosf (phi * GU_PI / 180.0f);
sin_phi = sinf (phi * GU_PI / 180.0f);
cos_theta = cosf (theta * GU_PI / 180.0f);
sin_theta = sinf (theta * GU_PI / 180.0f);
camera_distance = Magnitude (camera->position);
camera->position.x = camera_distance * sin_phi * sin_theta;
camera->position.y = camera_distance * cos_phi;
camera->position.z = camera_distance * sin_phi * cos_theta;
}
///////////////////////////////// STRAFE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This strafes the camera left and right depending on the speed (-/+)
/////
///////////////////////////////// STRAFE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void cameraStrafe (cam_t *camera, float speed)
{
// Add the strafe vector to our position
camera->position.x += camera->strafe.x * speed;
camera->position.z += camera->strafe.z * speed;
// Add the strafe vector to our view
camera->view.x += camera->strafe.x * speed;
camera->view.z += camera->strafe.z * speed;
}
///////////////////////////////// MOVE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This will move the camera forward or backward depending on the speed
/////
///////////////////////////////// MOVE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void cameraMove (cam_t *camera, float speed)
{
// Get the current view vector (the direction we are looking)
ScePspFVector3 vVector = Substract (camera->view, camera->position);
gumNormalize (&vVector);
camera->position.x += vVector.x * speed; // Add our acceleration to our position's X
camera->position.y += vVector.y * speed; // Add our acceleration to our position's Y
camera->position.z += vVector.z * speed; // Add our acceleration to our position's Z
camera->view.x += vVector.x * speed; // Add our acceleration to our view's X
camera->view.y += vVector.y * speed; // Add our acceleration to our view's Y
camera->view.z += vVector.z * speed; // Add our acceleration to our view's Z
}
void cameraLook (cam_t *camera)
{
// Give PSP our camera position, then camera view, then camera up vector
sceGumLookAt (&(camera->position),
&(camera->view),
&(camera->up_vector));
}