-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjeltzfly.cpp
274 lines (255 loc) · 7.4 KB
/
jeltzfly.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#include "prec.h"
#include "jeltzfly.h"
#include <fstream>
#include <ostream>
#include "includegl.h"
#include "shader.h"
#include "util.h"
using namespace std;
const float to_rad = pi/180.0f;
JeltzFly::JeltzFly()
{
camera.setPerspective(60.0f * (pi/180.0f));
camera.setDistance(0.01f, 100.0f);
camera.regen();
target = NULL;
sensitivity = 0.5;
speed = 1.0;
interpolating = false;
snapSpeed = 5.0;
orbit = vec3f(0.0f);
saveOnExit = true;
loadCamera();
}
JeltzFly::~JeltzFly()
{
if (saveOnExit)
saveCamera();
}
void JeltzFly::update(float dt)
{
Camera& camera = target ? *target : this->camera;
bool moved = false;
float mspeed = speed;
//increase wasd speed with shift
if (jeltz->button("Left Ctrl"))
mspeed *= 0.1;
if (jeltz->button("Left Shift"))
mspeed *= 10.0;
//standard wasd fly movement
if (jeltz->button("A"))
{camera.move(camera.rightVec() * mspeed * -dt); moved = true;}
if (jeltz->button("D"))
{camera.move(camera.rightVec() * mspeed * dt); moved = true;}
if (jeltz->button("S"))
{camera.move(camera.toVec() * mspeed * -dt); moved = true;}
if (jeltz->button("W"))
{camera.move(camera.toVec() * mspeed * dt); moved = true;}
//un-orbit with wasd, toggle wth space
bool toggleOrbit = jeltz->buttonDown("Space");
bool orbiting = camera.getZoom() > 0.0;
if ((toggleOrbit || moved) && orbiting)
{
camera.setPosition(camera.getZoomPos());
camera.setZoom(0.0);
moved = true;
}
else if (toggleOrbit && !orbiting)
{
interpolating = true;
//interpFrom = Quat(camera.getTransform()).inverse();
interpFrom = Quat::dirUp(camera.toVec(), camera.upVec());
//interpFrom = Quat::fromEuler(camera.getRotation());
vec3f rotateTowards = (orbit - camera.getPosition()).unit();
if (myabs(rotateTowards.y) > 0.9999f)
{
if (myabs(camera.upVec().y) > 0.9999f)
{
printf("Using final.up = initial.forward\n");
interpTo = Quat::dirUp(rotateTowards, camera.toVec() * mysign(rotateTowards.y));
}
else
{
printf("Using final.up = initial.up\n");
interpTo = Quat::dirUp(rotateTowards, camera.upVec() * mysign(camera.upVec().y));
}
}
else
{
//printf("Using final.up = (0,1,0)\n");
interpTo = Quat::dirUp(rotateTowards);
}
camera.setOffset(Quat::identity());
//interpTo = camera.getOffset().inverse() * interpTo;
interpFrom.normalize();
interpTo.normalize();
interpRatio = 0.0f;
float a = (interpTo * interpFrom.inverse()).unit().getAngle();
printf("Interp Angle = %.2fdeg\n", a * 180.0f/pi);
a = std::max(0.01f, a);
msnapSpeed = snapSpeed / a;
camera.lock(false);
}
//middle mouse pan
if (jeltz->button("MButton"))
{
vec2i move = jeltz->mouseMove();
if (move.x != 0 || move.y != 0)
{
float msensitivity = 1.0f;
if (jeltz->button("Left Ctrl"))
msensitivity *= 0.1f;
if (jeltz->button("Left Shift"))
msensitivity *= 10.0f;
vec2f dir;
if (camera.getZoom() > 0.1f)
dir = vec2f(tan(camera.getFOVX()*0.5f), tan(camera.getFOVY()*0.5f)) * (2.0f * camera.getZoom() * msensitivity) * vec2f(-move.x, move.y) / jeltz->winSize();
else
dir = vec2f(-move.x, move.y) * msensitivity * mspeed * sensitivity * 0.01f;
camera.move(camera.upVec() * dir.y + camera.rightVec() * dir.x);
moved = true;
}
}
//left mouse rotate
if (jeltz->button("LButton"))
{
vec2i move = jeltz->mouseMove();
if (move.x != 0 || move.y != 0)
{
float msensitivity = sensitivity;
if (jeltz->button("Left Ctrl"))
msensitivity *= 0.1;
camera.rotate(vec2f(-move.y, -move.x) * to_rad * msensitivity);
moved = true;
}
}
//right mouse zoom
if (jeltz->button("RButton"))
{
vec2i move = jeltz->mouseMove();
if (move.y != 0)
{
float msensitivity = sensitivity * speed * 0.05;
if (jeltz->button("Left Ctrl"))
msensitivity *= 0.1;
if (jeltz->button("Left Shift"))
msensitivity *= 10.0;
camera.zoom(-move.y * (camera.getZoom() + 1.0) * msensitivity);
if (camera.getZoom() == 0.0 && move.y > 0.0 && jeltz->button("Left Shift"))
camera.move(camera.toVec() * 0.1f);
moved = true;
}
}
//smooth snap to orbit position
if (interpolating)
{
if (moved)
{
//remove roll and stop interpolating
camera.lock(true);
camera.setRotation(camera.getEuler() * vec3f(1,1,0));
interpolating = false;
}
else
{
interpRatio += dt * msnapSpeed;
if (interpRatio >= 1.0f)
{
interpolating = false;
interpRatio = 1.0f;
camera.lock(true);
camera.zoomAt(camera.getPosition(), orbit);
vec3f euler = camera.getEuler();
if (myabs(euler.x) > pi*0.49f)
{
euler.y = (Quat(pi*0.5f,vec3f(1,0,0))*interpTo).euler().z;
printf("Fixing final yaw: %.2fdeg\n", euler.y * 180.0f/pi);
}
camera.setRotation(euler);
//camera.setRotation(camera.getOffset().inverse() * camera.getRotation());
//camera.setRotation(interpTo);
//camera.setZoom((camera.getPosition() - orbit).size());
//camera.setPosition(orbit);
//camera.setOffset(Quat::identity());
}
else
{
float smoothRatio = 1.0f - (cos(pi*interpRatio)*0.5f+0.5f);
//float smoothRatio = mysign(interpRatio*2.0f-1.0f)*pow(myabs(interpRatio*2.0f-1.0f),0.5)*0.5+0.5;
interpPos = interpFrom.slerp(interpTo, smoothRatio);
camera.setRotation(interpPos);
}
//camera.lookAt(camera.getPosition(), camera.getPosition() + interpPos * vec3f(0,0,-1));
//camera.setOffset(interpPos);
moved = true;
}
}
//only regen matrix if the camera has changed
if (moved)
camera.regenCamera();
//update projection aspect ratio if window resized
if (jeltz->resized())
{
this->camera.setAspectRatio(jeltz->winSize().x/(float)jeltz->winSize().y);
this->camera.regenProjection();
}
}
void JeltzFly::loadCamera(std::string filename)
{
vec3f pos;
vec3f rot;
float z;
ifstream ifile(filename.c_str());
if (ifile.is_open())
{
ifile >> pos.x >> pos.y >> pos.z;
ifile >> rot.x >> rot.y >> rot.z;
ifile >> z;
ifile.close();
camera.setZoom(z);
camera.setRotation(rot);
camera.setPosition(pos);
camera.regenCamera();
}
}
void JeltzFly::saveCamera(std::string filename)
{
vec3f pos = camera.getPosition();
vec3f rot = camera.getEuler();
float z = camera.getZoom();
ofstream ofile(filename.c_str());
ofile << pos.x << " " << pos.y << " " << pos.z << endl;
ofile << rot.x << " " << rot.y << " " << rot.z << endl;
ofile << z << endl;
ofile.close();
printf("Saved Camera\n");
}
void JeltzFly::uploadCamera(Shader* shader)
{
if (shader->active == shader)
{
shader->set("normalMat", mat33(camera.getTransform().transpose()));
shader->set("modelviewMat", camera.getInverse());
shader->set("projectionMat", camera.getProjection());
}
else if (!shader->error())
printf("Please \"%s\".use() before .uploadCamera()\n", shader->name().c_str());
}
void JeltzFly::uploadCamera()
{
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(camera.getProjection().m);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(camera.getInverse().m);
}
void JeltzFly::uploadOrtho()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, jeltz->winSize().x, 0, jeltz->winSize().y, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, jeltz->winSize().y, 0);
glScalef(1, -1, 1);
}