forked from eu07/maszyna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
212 lines (184 loc) · 7 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
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
206
207
208
209
210
211
212
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "system.hpp"
#include "classes.hpp"
#include "opengl/glew.h"
#include "opengl/glut.h"
#pragma hdrstop
#include "Camera.h"
#include "Usefull.h"
#include "Globals.h"
#include "Timer.h"
#include "mover.h"
#include "Console.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
// TViewPyramid TCamera::OrgViewPyramid;
//={vector3(-1,1,1),vector3(1,1,1),vector3(-1,-1,1),vector3(1,-1,1),vector3(0,0,0)};
const vector3 OrgCrossPos = vector3(0, -10, 0);
void TCamera::Init(vector3 NPos, vector3 NAngle)
{
pOffset = vector3(-0.0, 0, 0);
vUp = vector3(0, 1, 0);
// pOffset= vector3(-0.8,0,0);
CrossPos = OrgCrossPos;
CrossDist = 10;
Velocity = vector3(0, 0, 0);
OldVelocity = vector3(0, 0, 0);
Pitch = NAngle.x;
Yaw = NAngle.y;
Roll = NAngle.z;
Pos = NPos;
// Type= tp_Follow;
Type = (Global::bFreeFly ? tp_Free : tp_Follow);
// Type= tp_Free;
};
void TCamera::OnCursorMove(double x, double y)
{
// McZapkie-170402: zeby mysz dzialala zawsze if (Type==tp_Follow)
Pitch += y;
Yaw += -x;
if (Yaw > M_PI)
Yaw -= 2 * M_PI;
else if (Yaw < -M_PI)
Yaw += 2 * M_PI;
if (Type == tp_Follow) // je¿eli jazda z pojazdem
{
Fix(Pitch, -M_PI_4, M_PI_4); // ograniczenie k¹ta spogl¹dania w dó³ i w górê
// Fix(Yaw,-M_PI,M_PI);
}
}
void TCamera::Update()
{
// ABu: zmiana i uniezaleznienie predkosci od FPS
double a = (Console::Pressed(VK_SHIFT) ? 5.00 : 1.00);
if (Console::Pressed(VK_CONTROL))
a = a * 100;
// OldVelocity=Velocity;
if (FreeFlyModeFlag == true)
Type = tp_Free;
else
Type = tp_Follow;
if (Type == tp_Free)
{
if (Console::Pressed(Global::Keys[k_MechUp]))
Velocity.y += a;
if (Console::Pressed(Global::Keys[k_MechDown]))
Velocity.y -= a;
// McZapkie-170402: zeby nie bylo konfliktow
/*
if (Console::Pressed(VkKeyScan('d')))
Velocity.x+= a*Timer::GetDeltaTime();
if (Console::Pressed(VkKeyScan('a')))
Velocity.x-= a*Timer::GetDeltaTime();
if (Console::Pressed(VkKeyScan('w')))
Velocity.z-= a*Timer::GetDeltaTime();
if (Console::Pressed(VkKeyScan('s')))
Velocity.z+= a*Timer::GetDeltaTime();
if (Console::Pressed(VK_NUMPAD4) || Console::Pressed(VK_NUMPAD7) ||
Console::Pressed(VK_NUMPAD1))
Yaw+= +1*M_PI*Timer::GetDeltaTime();
if (Console::Pressed(VK_NUMPAD6) || Console::Pressed(VK_NUMPAD9) ||
Console::Pressed(VK_NUMPAD3))
Yaw+= -1*M_PI*Timer::GetDeltaTime();
if (Pressed(VK_NUMPAD2) || Console::Pressed(VK_NUMPAD1) ||
Console::Pressed(VK_NUMPAD3))
Pitch+= -1*M_PI*Timer::GetDeltaTime();
if (Console::Pressed(VK_NUMPAD8) || Console::Pressed(VK_NUMPAD7) ||
Console::Pressed(VK_NUMPAD9))
Pitch+= +1*M_PI*Timer::GetDeltaTime();
if (Console::Pressed(VkKeyScan('.')))
Roll+= -1*M_PI*Timer::GetDeltaTime();
if (Console::Pressed(VkKeyScan(',')))
Roll+= +1*M_PI*Timer::GetDeltaTime();
if (Console::Pressed(VK_NUMPAD5))
Pitch=Roll= 0.0f;
*/
// McZapkie-170402: poruszanie i rozgladanie we free takie samo jak w follow
if (Console::Pressed(Global::Keys[k_MechRight]))
Velocity.x += a;
if (Console::Pressed(Global::Keys[k_MechLeft]))
Velocity.x -= a;
if (Console::Pressed(Global::Keys[k_MechForward]))
Velocity.z -= a;
if (Console::Pressed(Global::Keys[k_MechBackward]))
Velocity.z += a;
// gora-dol
// if (Console::Pressed(VK_NUMPAD9)) Pos.y+=0.1;
// if (Console::Pressed(VK_NUMPAD3)) Pos.y-=0.1;
// McZapkie: zeby nie hustalo przy malym FPS:
// Velocity= (Velocity+OldVelocity)/2;
// matrix4x4 mat;
vector3 Vec = Velocity;
Vec.RotateY(Yaw);
Pos = Pos + Vec * Timer::GetDeltaRenderTime(); // czas bez pauzy
Velocity = Velocity / 2; // p³ynne hamowanie ruchu
// double tmp= 10*DeltaTime;
// Velocity+= -Velocity*10 * Timer::GetDeltaTime();//( tmp<1 ? tmp : 1 );
// Type= tp_Free;
}
}
vector3 TCamera::GetDirection()
{
matrix4x4 mat;
vector3 Vec;
Vec = vector3(0, 0, 1);
Vec.RotateY(Yaw);
return (Normalize(Vec));
}
// bool TCamera::GetMatrix(matrix4x4 &Matrix)
bool TCamera::SetMatrix()
{
glRotated(-Roll * 180.0f / M_PI, 0, 0, 1); // po wy³¹czeniu tego krêci siê pojazd, a sceneria
// nie
glRotated(-Pitch * 180.0f / M_PI, 1, 0, 0);
glRotated(-Yaw * 180.0f / M_PI, 0, 1, 0); // w zewnêtrznym widoku: kierunek patrzenia
if (Type == tp_Follow)
{
// gluLookAt(Pos.x+pOffset.x,Pos.y+pOffset.y,Pos.z+pOffset.z,
// LookAt.x+pOffset.x,LookAt.y+pOffset.y,LookAt.z+pOffset.z,vUp.x,vUp.y,vUp.z);
// gluLookAt(Pos.x+pOffset.x,Pos.y+pOffset.y,Pos.z+pOffset.z,
// LookAt.x+pOffset.x,LookAt.y+pOffset.y,LookAt.z+pOffset.z,vUp.x,vUp.y,vUp.z);
gluLookAt(Pos.x, Pos.y, Pos.z, LookAt.x, LookAt.y, LookAt.z, vUp.x, vUp.y,
vUp.z); // Ra: pOffset is zero
// gluLookAt(Pos.x,Pos.y,Pos.z,Pos.x+Velocity.x,Pos.y+Velocity.y,Pos.z+Velocity.z,0,1,0);
// return true;
}
if (Type == tp_Satelite)
Pitch = M_PI * 0.5;
if (Type != tp_Follow)
{
glTranslated(-Pos.x, -Pos.y, -Pos.z); // nie zmienia kierunku patrzenia
}
Global::SetCameraPosition(Pos); // by³o +pOffset
return true;
}
void TCamera::SetCabMatrix(vector3 &p)
{ // ustawienie widoku z kamery bez przesuniêcia robionego przez OpenGL - nie powinno tak trz¹œæ
glRotated(-Roll * 180.0f / M_PI, 0, 0, 1);
glRotated(-Pitch * 180.0f / M_PI, 1, 0, 0);
glRotated(-Yaw * 180.0f / M_PI, 0, 1, 0); // w zewnêtrznym widoku: kierunek patrzenia
if (Type == tp_Follow)
gluLookAt(Pos.x - p.x, Pos.y - p.y, Pos.z - p.z, LookAt.x - p.x, LookAt.y - p.y,
LookAt.z - p.z, vUp.x, vUp.y, vUp.z); // Ra: pOffset is zero
}
void TCamera::RaLook()
{ // zmiana kierunku patrzenia - przelicza Yaw
vector3 where = LookAt - Pos + vector3(0, 3, 0); // trochê w górê od szyn
if ((where.x != 0.0) || (where.z != 0.0))
Yaw = atan2(-where.x, -where.z); // k¹t horyzontalny
double l = Length3(where);
if (l > 0.0)
Pitch = asin(where.y / l); // k¹t w pionie
};
void TCamera::Stop()
{ // wy³¹cznie bezw³adnego ruchu po powrocie do kabiny
Type = tp_Follow;
Velocity = vector3(0, 0, 0);
};