-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtour.cpp
More file actions
114 lines (106 loc) · 2.35 KB
/
Copy pathtour.cpp
File metadata and controls
114 lines (106 loc) · 2.35 KB
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include "helper.h"
#include "world.h"
#include "global.h"
#include "camera.h"
#include "bullet.h"
#include "tour.h"
using namespace std;
using namespace glm;
struct waypoint_t {
bool centred;
vec3 centre;
string id;
struct {
vec3 pos;
quat rot;
} camera;
float duration;
};
static vector<waypoint_t> waypoints;
static struct tour_t {
double timeElapsed;
unsigned int step;
} tour;
void loadTour()
{
ifstream datafs(DATA_TOUR);
if (!datafs) {
cerr << "Cannot open tour data file " DATA_TOUR << endl;
return;
}
string line;
waypoint_t wp;
while (getline(datafs, line)) {
if (line.empty() || line.at(0) == '#')
continue;
istringstream ss(line);
string type;
ss >> type;
if (type == "Waypoint") {
if (!wp.id.empty())
waypoints.push_back(wp);
ss >> wp.id;
wp.centred = false;
} else if (type == "Camera")
ss >> wp.camera.pos >> wp.camera.rot;
else if (type == "Duration")
ss >> wp.duration;
else if (type == "Centre") {
ss >> wp.centre;
wp.centred = true;
}
}
if (!wp.id.empty())
waypoints.push_back(wp);
}
static void loadWaypoint(int i)
{
waypoint_t &wp = waypoints[i];
camera.setPosition(wp.camera.pos);
camera.setRotation(wp.camera.rot);
}
void initTour()
{
if (waypoints.size() == 0)
return;
tour.step = 0;
status.pauseDuration = tour.timeElapsed = glfwGetTime();
status.mode = status_t::TourMode;
status.lines = false;
status.run = true;
loadWaypoint(0);
}
void updateTour()
{
double time = glfwGetTime();
double diff = time - tour.timeElapsed;
while (diff >= waypoints[tour.step].duration) {
tour.timeElapsed = time;
tour.step++;
diff = time - tour.timeElapsed;
if (tour.step + 1 == waypoints.size()) {
quitTour();
clog << "Tour finished." << endl;
return;
}
}
waypoint_t &wp = waypoints[tour.step];
waypoint_t &wpn = waypoints[tour.step + 1];
float ratio = diff / wp.duration;
//clog << diff << ", " << ratio << ", " << wp.duration << endl;
if (wp.centred)
camera.setPosition(polarMix(wp.camera.pos, wpn.camera.pos, wp.centre, ratio));
else
camera.setPosition(mix(wp.camera.pos, wpn.camera.pos, ratio));
camera.setRotation(mix(wp.camera.rot, wpn.camera.rot, ratio));
//clog << camera.position() << camera.rotation() << endl;
}
void quitTour()
{
status.mode = status_t::CameraMode;
}