Repository navigation
Expand file tree
/
Copy pathcamera.cpp
More file actions
126 lines (97 loc) · 3.09 KB
/
Copy pathcamera.cpp
File metadata and controls
126 lines (97 loc) · 3.09 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
/*
This program 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.
This program 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/>.
*/
#include "gpustatemanager.h"
#include "camera.h"
#include "../grinliz/glgeometry.h"
#include "../grinliz/glmatrix.h"
#include "serialize.h"
#include "../xarchive/glstreamer.h"
using namespace grinliz;
Camera::Camera() :
valid( false )
{
posWC.Set( 0.f, 0.f, 0.f );
TiltRotationToQuat( 45.0f, 0. );
}
void Camera::Serialize( XStream* xs )
{
XarcOpen( xs, "Camera" );
XARC_SER( xs, posWC );
XARC_SER( xs, quat );
XarcClose( xs );
}
void Camera::TiltRotationToQuat( float tilt, float yRotation )
{
Matrix4 rotationY, rotationTilt;
rotationY.SetYRotation( yRotation );
rotationTilt.SetXRotation( tilt );
// Done in world: we tilt it down, turn it around y, then move it.
Matrix4 m = rotationY * rotationTilt;
quat.FromRotationMatrix( m );
valid = false;
}
void Camera::SetDir( const grinliz::Vector3F& _dir, const grinliz::Vector3F& up )
{
Matrix4 m;
Vector3F dir = _dir;
dir.Normalize();
static const Vector3F zero = { 0, 0, 0 };
LookAt( true, zero, dir, up, &m, 0, 0 );
quat.FromRotationMatrix( m );
}
void Camera::CalcWorldXForm()
{
if ( !valid ) {
Matrix4 translation, rotation;
translation.SetTranslation( posWC );
quat.Normalize();
quat.ToMatrix( &rotation );
worldXForm = translation * rotation;
CalcEyeDir();
// Calc the View Matrix
worldXForm.Invert( &viewMatrix );
valid = true;
}
}
void Camera::CalcEyeDir()
{
Vector4F dir[3] = {
{ 0.0f, 0.0f, -1.0f, 0.0f }, // ray
{ 0.0f, 1.0f, 0.0f, 0.0f }, // up (y axis)
{ 1.0f, 0.0f, 0.0f, 0.0f }, // right (x axis)
};
for( int i=0; i<3; ++i ) {
eyeDir[i] = worldXForm * dir[i];
eyeDir3[i].Set( eyeDir[i].x, eyeDir[i].y, eyeDir[i].z );
}
}
void Camera::Orbit( float rotation )
{
// This is kind of a tricky function. We want to rotate around
// what the camera is looking at, not the origin of the camera.
// It's a useful function, but a pain in the ass.
EyeDir(); // bring cache current
Vector3F pole; // the pole is up; this is the base of the pole, essentially
if ( IntersectRayPlane( posWC, eyeDir3[0], 1, 0, &pole ) == INTERSECT ) {
Vector3F delta = posWC - pole;
// Append the rotation
Matrix4 rotMat, current, m;
rotMat.SetYRotation( rotation );
quat.ToMatrix( ¤t );
m = rotMat * current;
quat.FromRotationMatrix( m );
// Move the origin to componensate
posWC = pole + rotMat * delta;
}
valid = false;
}