-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengl_camera.cpp
87 lines (66 loc) · 2.11 KB
/
opengl_camera.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
#include "opengl_camera.h"
#include "vmath.h"
#include "utility.h"
/***************************************************************************************/
Camera::Camera()
{
//Init with standard OGL values:
Position = Vector3f (0.0, 0.0, 0.0);
ViewDir = Vector3f( 0.0, 0.0, -1.0);
RightVector = Vector3f (1.0, 0.0, 0.0);
UpVector = Vector3f (0.0, 1.0, 0.0);
//Only to be sure:
RotatedX = RotatedY = RotatedZ = 0.0;
}
void Camera::Move(Vector3f Direction)
{
Position = Position + Direction;
}
void Camera::RotateX (GLfloat Angle)
{
RotatedX += Angle;
//Rotate viewdir around the right vector:
ViewDir = ViewDir * cos(Angle * Utility::PIdiv180) + UpVector * sin(Angle * Utility::PIdiv180);
ViewDir.normalize();
//now compute the new UpVector (by cross product)
UpVector = ViewDir.crossProduct(RightVector) * -1;
}
void Camera::RotateY (GLfloat Angle)
{
RotatedY += Angle;
//Rotate viewdir around the up vector:
ViewDir = ViewDir * cos(Angle * Utility::PIdiv180) - RightVector * sin(Angle * Utility::PIdiv180);
ViewDir.normalize();
//now compute the new RightVector (by cross product)
RightVector = ViewDir.crossProduct(UpVector);
}
void Camera::RotateZ (GLfloat Angle)
{
RotatedZ += Angle;
//Rotate viewdir around the right vector:
RightVector = RightVector * cos(Angle * Utility::PIdiv180) + UpVector * sin(Angle*Utility::PIdiv180);
RightVector.normalize();
//now compute the new UpVector (by cross product)
UpVector = ViewDir.crossProduct(RightVector) * -1;
}
void Camera::Render( void )
{
//The point at which the camera looks:
Vector3f ViewPoint = Position+ViewDir;
//as we know the up vector, we can easily use gluLookAt:
gluLookAt( Position.x,Position.y,Position.z,
ViewPoint.x,ViewPoint.y,ViewPoint.z,
UpVector.x,UpVector.y,UpVector.z);
}
void Camera::MoveForward( GLfloat Distance )
{
Position = Position + (ViewDir*-Distance);
}
void Camera::StrafeRight ( GLfloat Distance )
{
Position = Position + (RightVector*Distance);
}
void Camera::MoveUpward( GLfloat Distance )
{
Position = Position + (UpVector*Distance);
}