/
cam.cpp
204 lines (179 loc) · 5.46 KB
/
cam.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
#include "cam.h"
#include <math.h>
myCam::myCam(float fov, float aspect){
this->projMatrix.setToIdentity();
this->projMatrix.perspective(fov, aspect, 0.01f, 1000.0f);
this->viewMatrix.setToIdentity();
this->viewMatrix.lookAt(QVector3D(0,50,30),QVector3D(0,0,0),QVector3D(0,1,0));
this->activePlaymode = true;
this->freeCameramode = false;
this->angleX = 0;
this->angleY = 0;
this->distanz = 30;
this->isMoving = false;
this->animations = std::vector<Animation>();
this->isCubeCamera = false;
}
void myCam::queueAnimation(QVector3D ziel, QVector3D zielLookat, int duration)
{
Animation aniNew = Animation(ziel,zielLookat,duration);
this->animations.push_back(aniNew);
if (!this->isMoving)
this->nextAnimation();
}
QVector3D myCam::getPositionFromViewMatrix(QMatrix4x4 matrix)
{
QMatrix4x4 viewRot = matrix;
viewRot.setColumn(3, QVector4D(0,0,0,1));
QVector4D p = -(viewRot.transposed() * matrix.column(3));
return QVector3D(p.x(), p.y(), p.z());
}
void myCam::moveStep(int time)
{
if(this->isMoving)
{
this->moveTime += time;
if (this->moveTime >= this->moveDuration)
{
this->viewMatrix.setToIdentity();
this->viewMatrix.lookAt(this->moveZiel,this->moveLookatZiel,QVector3D(0,1,0));
this->isMoving = false;
if (!this->animations.empty())
this->nextAnimation();
}
else
{
//QVector3D iamAt = (this->moveZiel-this->moveStart) * sin((this->moveTime/this->moveDuration)*3.1415926/2.0)+this->moveStart;
QVector3D iamAt = (this->moveZiel-this->moveStart) * (-1.0/2.0*cos((this->moveTime/this->moveDuration)*3.1415926)+0.5)+this->moveStart;
QVector3D ilookAt = (this->moveLookatZiel-this->moveLookatStart) * (-1.0/2.0*cos((this->moveTime/this->moveDuration)*3.1415926)+0.5)+this->moveLookatStart;
this->viewMatrix.setToIdentity();
this->viewMatrix.lookAt(iamAt, ilookAt, QVector3D(0,1,0));
}
}
}
void myCam::nextAnimation()
{
if (!this->animations.empty())
{
this->isMoving = true;
Animation ani = this->animations.at(this->animations.size()-1);
this->animations.pop_back();
this->moveZiel = ani.moveZiel;
this->moveStart = getPositionFromViewMatrix(this->viewMatrix);
this->moveLookatZiel = ani.moveLookatZiel;
this->moveTime = 0;
this->moveDuration = ani.moveDuration;
float dist = (ani.moveZiel.distanceToPoint(ani.moveLookatZiel));
QMatrix4x4 matrixcopy;
matrixcopy.translate(0,0,dist);
matrixcopy = matrixcopy * this->viewMatrix;
this->moveLookatStart = this->getPositionFromViewMatrix(matrixcopy);
}
else
this->isMoving = false;
}
void myCam::aktivatePlaymode(QVector3D kugelWhite, int abstand)
{
this->activePlaymode = true;
this->distanz = abstand;
this->kugelWhite = kugelWhite;
QVector3D position;
position.setX(kugelWhite.x());
position.setY(kugelWhite.y());
position.setZ(kugelWhite.z());
if(position.x()==0 && position.y()==0 && position.z() == 0)
{
position.setZ(-1);
}
position.normalize();
position.setX(position.x()*abstand + kugelWhite.x());
position.setY(8);
position.setZ(position.z()*abstand + kugelWhite.z());
//this->viewMatrix.setToIdentity();
//this->viewMatrix.lookAt(position,kugelWhite,QVector3D(0,1,0));
this->queueAnimation(position,kugelWhite,100);
position = position-kugelWhite;
if(position.x()>0 && position.z()>0)
{
this->angleY = atan(position.z()/position.x())*180/3.1415926-90;
}
else if(position.x()<0 && position.z()>0)
{
this->angleY = atan(-position.x()/position.z())*180/3.1415926;
}
else if(position.x()<0 && position.z()<0)
{
this->angleY = atan(position.z()/position.x())*180/3.1415926+90;
}
else if(position.x()>0 && position.z()<0)
{
this->angleY = atan(position.x()/-position.z())*180/3.1415926+180;
}
else if(position.x() == 0)
{
if(position.z()>0)
this->angleY = 0;
else
this->angleY = 180;
}
else if(position.z() == 0)
{
if(position.x()>0)
this->angleY = 270;
else
this->angleY = 90;
}
this->angleX = 15;
this->camRotate(0,0);
this->freeCameramode = false;
}
void myCam::aktivateWatchmode()
{
this->activePlaymode = false;
this->freeCameramode = true;
}
float myCam::getCamAngle()
{
return this->angleY;
}
void myCam::camRotate(int x,int y)
{
if(this->freeCameramode)
{
QMatrix4x4 rot1;
rot1.setToIdentity();
rot1.rotate(y*0.1,1,0,0);
QMatrix4x4 rot2;
rot2.setToIdentity();
rot2.rotate(x*0.1,0,1,0);
this->viewMatrix = rot1*rot2 * this->viewMatrix;
}
else
{
this->angleX -= y*0.1;
this->angleY += x*0.1;
if(this->angleX > 60)
{
this->angleX = 60;
}
if(this->angleX<15)
{
this->angleX = 15;
}
this->viewMatrix.setToIdentity();
this->viewMatrix.translate(0,0,-distanz);
this->viewMatrix.rotate(angleX,1,0,0);
this->viewMatrix.rotate(angleY,0,1,0);
this->viewMatrix.translate(-kugelWhite);
}
}
void myCam::camMove(int x, int y)
{
if(this->freeCameramode == true)
{
QMatrix4x4 move;
move.setToIdentity();
move.translate(x,0,y);
this->viewMatrix = move * this->viewMatrix;
}
}