-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cpp
179 lines (151 loc) · 5.35 KB
/
Controller.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
#include "Controller.h"
#include "CameraBehaviors.hpp"
#include "GLFW/glfw3.h"
#include "TextRendering.hpp"
#include "sgraph/IScenegraph.h"
#include "sgraph/Scenegraph.h"
#include "sgraph/GroupNode.h"
#include "sgraph/LeafNode.h"
#include "sgraph/ScaleTransform.h"
#include "ObjImporter.h"
using namespace sgraph;
#include <iostream>
using namespace std;
#include "sgraph/ScenegraphExporter.h"
#include "sgraph/ScenegraphImporter.h"
#include <glm/ext/quaternion_trigonometric.hpp>
Controller::Controller(istream& scenegraphFile, Model& m,View& v): model(m), view(v) {
initScenegraph(scenegraphFile);
globalCamera = new StationaryCamera(glm::vec3(50.0, 75.0, 100.0));
globalCamera->LookAt(glm::vec3(0.0,0.0,0.0));
cameras.push_back(globalCamera);
helicopterCamera = new HelicopterCamera(175, 150, glm::radians(30.0), 25);
cameras.push_back(helicopterCamera);
firstPersonCamera = new FirstPersonCamera(glm::vec3{0,30,75}, 10, glm::radians(60.0));
cameras.push_back(firstPersonCamera);
}
void Controller::initScenegraph(istream& scenegraphFile) {
sgraph::ScenegraphImporter importer;
//read in the file of commands
IScenegraph *scenegraph = importer.parse(scenegraphFile);
//scenegraph->setMeshes(meshes);
model.setScenegraph(scenegraph);
cout <<"Scenegraph made" << endl;
TextRendering tr;
scenegraph->getRoot()->accept((sgraph::SGNodeVisitor *)&tr);
}
Controller::~Controller()
{
if (globalCamera) delete globalCamera;
if (helicopterCamera) delete helicopterCamera;
if (firstPersonCamera) delete firstPersonCamera;
}
void Controller::run()
{
sgraph::IScenegraph * scenegraph = model.getScenegraph();
map<string,util::PolygonMesh<VertexAttrib> > meshes = scenegraph->getMeshes();
view.init(this,model);
float deltaTime = 0;
while (!view.shouldWindowClose()) {
for(auto& camera : cameras) {
camera->Update(deltaTime);
}
deltaTime = view.display(scenegraph, cameras, cameras[activeCameraIndex]);
if (deltaTime < 0) break;
}
view.closeWindow();
exit(EXIT_SUCCESS);
}
void Controller::onkey(int key, int scancode, int action, int mods)
{
// cout << (char)key << " pressed" << endl;
switch (key) {
// Camera Switching
case GLFW_KEY_1:
if (action == GLFW_PRESS) activeCameraIndex = 0;
break;
case GLFW_KEY_2:
if (action == GLFW_PRESS) activeCameraIndex = 1;
break;
case GLFW_KEY_3:
if (action == GLFW_PRESS) activeCameraIndex = 2;
break;
// FPS Camera Movement
case GLFW_KEY_MINUS:
if (action == GLFW_PRESS)
firstPersonCamera->PressedButton(FirstPersonCamera::Input::MoveBackward);
else if (action == GLFW_RELEASE)
firstPersonCamera->ReleasedButton(FirstPersonCamera::Input::MoveBackward);
break;
case GLFW_KEY_EQUAL:
if (action == GLFW_PRESS)
firstPersonCamera->PressedButton(FirstPersonCamera::Input::MoveForward);
else if (action == GLFW_RELEASE)
firstPersonCamera->ReleasedButton(FirstPersonCamera::Input::MoveForward);
break;
case GLFW_KEY_UP:
if (action == GLFW_PRESS)
firstPersonCamera->PressedButton(FirstPersonCamera::Input::LookUp);
else if (action == GLFW_RELEASE)
firstPersonCamera->ReleasedButton(FirstPersonCamera::Input::LookUp);
break;
case GLFW_KEY_DOWN:
if (action == GLFW_PRESS)
firstPersonCamera->PressedButton(FirstPersonCamera::Input::LookDown);
else if (action == GLFW_RELEASE)
firstPersonCamera->ReleasedButton(FirstPersonCamera::Input::LookDown);
break;
case GLFW_KEY_LEFT:
if (action == GLFW_PRESS)
firstPersonCamera->PressedButton(FirstPersonCamera::Input::LookLeft);
else if (action == GLFW_RELEASE)
firstPersonCamera->ReleasedButton(FirstPersonCamera::Input::LookLeft);
break;
case GLFW_KEY_RIGHT:
if (action == GLFW_PRESS)
firstPersonCamera->PressedButton(FirstPersonCamera::Input::LookRight);
else if (action == GLFW_RELEASE)
firstPersonCamera->ReleasedButton(FirstPersonCamera::Input::LookRight);
break;
case GLFW_KEY_R:
if (action == GLFW_PRESS) firstPersonCamera->Reset();
break;
}
}
void Controller::mousePosition(double xpos, double ypos) {
// cout << xpos << ',' << ypos << endl;
glm::vec2 currentDelta = glm::vec2(xpos, ypos) - currentMousePos;
currentMousePos.x = xpos;
currentMousePos.y = ypos;
if (isMousePressed)
{
if (currentDelta == currentMousePos) return;
glm::vec3 rotAxis = glm::vec3(currentDelta.y, currentDelta.x, 0);
rotAxis = glm::normalize(rotAxis);
// cout << "deltaMousePos: " << deltaMousePos.x << ", " << deltaMousePos.y << endl;
}
}
void Controller::reshape(int width, int height)
{
cout <<"Window reshaped to width=" << width << " and height=" << height << endl;
view.resize();
}
void Controller::dispose()
{
view.closeWindow();
}
void Controller::error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
void Controller::mouseButton(int button, int action, int mods) {
if (button != GLFW_MOUSE_BUTTON_LEFT) return;
// cout << "button: " << button << " action: " << action << " mods: " << mods << endl;
// Left click pressed.
if (action == GLFW_PRESS)
isMousePressed = true;
// Left click released.
else if (action == GLFW_RELEASE)
isMousePressed = false;
// cout << "isMousePressed: " << isMousePressed << endl;
}