/
Cameranode.cpp
122 lines (109 loc) · 2.1 KB
/
Cameranode.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
#include "scenegraph/Cameranode.h"
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_opengl.h>
#include <GL/glu.h>
Cameranode::Cameranode()
:lookat(false)
,rotate_around_world_origo(false)
{}
void Cameranode::Look_at(Vector3 p)
{
lookat_target = p;
/* Vector3 front;
Vector3 right;
Vector3 up;
*/ front = p - position;
front.Normalize();
// right hand rule cross products
right = Vector3(0, 1, 0).CrossProduct(front);
right.Normalize();
up = front.CrossProduct(right);
/* right = -right;
up = -up;
front = -front;
*/
lookat = true;
matrix[0] = right.x;
matrix[1] = right.y;
matrix[2] = right.z;
matrix[3] = 0;
matrix[4] = up.x;
matrix[5] = up.y;
matrix[6] = up.z;
matrix[7] = 0;
matrix[8] = front.x;
matrix[9] = front.y;
matrix[10] = front.z;
matrix[11] = 0;
matrix[12] = -position.x;
matrix[13] = -position.y;
matrix[14] = -position.z;
matrix[15] = 1;
}
Vector3 Cameranode::Get_up()
{
return up;
}
Vector3 Cameranode::Get_front()
{
return front;
}
Vector3 Cameranode::Get_right()
{
return right;
}
void Cameranode::Set_position(Vector3 v)
{
position = v;
}
void Cameranode::Set_rotation(Vector3 v)
{
rotation = v;
}
Vector3 Cameranode::Get_position()
{
return position;
}
Vector3 Cameranode::Get_rotation()
{
return rotation;
}
void Cameranode::Set_rotate_around_world_origo(bool t)
{
rotate_around_world_origo = t;
}
void Cameranode::Prerender()
{
glPushMatrix();
if(!lookat)
{
if(!rotate_around_world_origo)
{
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
glTranslatef(-position.x, -position.y, -position.z);
}
else
{
glTranslatef(-position.x, -position.y, -position.z);
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.z, 0, 0, 1);
}
}
else
{
// glMultMatrixf(matrix);
gluLookAt( position.x, position.y, position.z,
lookat_target.x, lookat_target.y, lookat_target.z,
up.x, up.y, up.z );
}
}
void Cameranode::Postrender()
{
glPopMatrix();
}