-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
103 lines (75 loc) · 1.91 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "Camera.h"
float absf(float n)
{
if(n < 0)
return -n;
return n;
}
double rad(double deg)
{
return (3.14159265 * deg)/180.0;
}
Camera::Camera()
{
x = y = z = 0; // Positioning in the origin
rotX = rotY;
}
void Camera::rotateOnMouse(int X, int Y, int screenWidth, int screenHeight)
{
// Translate and rotate to other system coordinate, which has the origin in the centre
X = X - screenWidth/2;
Y = -Y + screenHeight/2;
float kY = (float)Y/(float)screenHeight;
float kX = (float)X/(float)screenWidth;
// First, analyse if the mouse pointer coordinates exceed the threshold
if(absf(kY) > 0.1) // Rotate X
{
rotX += 2 * kY;
}
if(absf(kX) > 0.1) // Rotate Y
{
rotY -= 2 * kX;
}
if(rotX > 360.0)
rotX -= 360.0;
else if(rotX < -360.0)
rotX += 360.0;
if(rotY > 360.0)
rotY -= 360.0;
else if(rotY < -360.0)
rotY += 360.0;
// printf("> rotX = %f.\n", rotX);
}
void Camera::positioning()
{
glRotated(-rotX, 1, 0, 0);
glRotated(-rotY, 0, 1, 0);
glTranslatef(x, y - 2, z);
}
void Camera::translateOnKeyboard(unsigned char key)
{
if((key == 'w') or (key == 'W')) // UP
{
x += sin(rad(rotY)) * 0.1;
y -= sin(rad(rotX)) * 0.06;
z += cos(rad(rotY)) * 0.1;
}
else if((key == 's') or (key == 'S')) // DOWN
{
x -= sin(rad(rotY)) * 0.1;
y += sin(rad(rotX)) * 0.06;
z -= cos(rad(rotY)) * 0.1;
}
else if((key == 'a') or (key == 'A')) // LEFT
{
x += cos(rad(rotY)) * 0.1;
z -= sin(rad(rotY)) * 0.1;
}
else if((key == 'd') or (key == 'D')) // RIGHT
{
x -= cos(rad(rotY)) * 0.1;
z += sin(rad(rotY)) * 0.1;
}
else
printf("< Nothing to do. >\n");
}