-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayMode.cpp
218 lines (182 loc) · 7.02 KB
/
PlayMode.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
#include "PlayMode.hpp"
#include "LitColorTextureProgram.hpp"
#include "DrawLines.hpp"
#include "Mesh.hpp"
#include "Load.hpp"
#include "gl_errors.hpp"
#include "data_path.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <random>
GLuint hexapod_meshes_for_lit_color_texture_program = 0;
Load< MeshBuffer > hexapod_meshes(LoadTagDefault, []() -> MeshBuffer const * {
MeshBuffer const *ret = new MeshBuffer(data_path("hexapod.pnct"));
hexapod_meshes_for_lit_color_texture_program = ret->make_vao_for_program(lit_color_texture_program->program);
return ret;
});
Load< Scene > hexapod_scene(LoadTagDefault, []() -> Scene const * {
return new Scene(data_path("hexapod.scene"), [&](Scene &scene, Scene::Transform *transform, std::string const &mesh_name){
Mesh const &mesh = hexapod_meshes->lookup(mesh_name);
scene.drawables.emplace_back(transform);
Scene::Drawable &drawable = scene.drawables.back();
drawable.pipeline = lit_color_texture_program_pipeline;
drawable.pipeline.vao = hexapod_meshes_for_lit_color_texture_program;
drawable.pipeline.type = mesh.type;
drawable.pipeline.start = mesh.start;
drawable.pipeline.count = mesh.count;
});
});
PlayMode::PlayMode() : scene(*hexapod_scene) {
//get pointers to leg for convenience:
for (auto &transform : scene.transforms) {
if (transform.name == "Hip.FL") hip = &transform;
else if (transform.name == "UpperLeg.FL") upper_leg = &transform;
else if (transform.name == "LowerLeg.FL") lower_leg = &transform;
}
if (hip == nullptr) throw std::runtime_error("Hip not found.");
if (upper_leg == nullptr) throw std::runtime_error("Upper leg not found.");
if (lower_leg == nullptr) throw std::runtime_error("Lower leg not found.");
hip_base_rotation = hip->rotation;
upper_leg_base_rotation = upper_leg->rotation;
lower_leg_base_rotation = lower_leg->rotation;
//get pointer to camera for convenience:
if (scene.cameras.size() != 1) throw std::runtime_error("Expecting scene to have exactly one camera, but it has " + std::to_string(scene.cameras.size()));
camera = &scene.cameras.front();
}
PlayMode::~PlayMode() {
}
bool PlayMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size) {
if (evt.type == SDL_KEYDOWN) {
if (evt.key.keysym.sym == SDLK_ESCAPE) {
SDL_SetRelativeMouseMode(SDL_FALSE);
return true;
} else if (evt.key.keysym.sym == SDLK_a) {
left.downs += 1;
left.pressed = true;
return true;
} else if (evt.key.keysym.sym == SDLK_d) {
right.downs += 1;
right.pressed = true;
return true;
} else if (evt.key.keysym.sym == SDLK_w) {
up.downs += 1;
up.pressed = true;
return true;
} else if (evt.key.keysym.sym == SDLK_s) {
down.downs += 1;
down.pressed = true;
return true;
}
} else if (evt.type == SDL_KEYUP) {
if (evt.key.keysym.sym == SDLK_a) {
left.pressed = false;
return true;
} else if (evt.key.keysym.sym == SDLK_d) {
right.pressed = false;
return true;
} else if (evt.key.keysym.sym == SDLK_w) {
up.pressed = false;
return true;
} else if (evt.key.keysym.sym == SDLK_s) {
down.pressed = false;
return true;
}
} else if (evt.type == SDL_MOUSEBUTTONDOWN) {
if (SDL_GetRelativeMouseMode() == SDL_FALSE) {
SDL_SetRelativeMouseMode(SDL_TRUE);
return true;
}
} else if (evt.type == SDL_MOUSEMOTION) {
if (SDL_GetRelativeMouseMode() == SDL_TRUE) {
glm::vec2 motion = glm::vec2(
evt.motion.xrel / float(window_size.y),
-evt.motion.yrel / float(window_size.y)
);
camera->transform->rotation = glm::normalize(
camera->transform->rotation
* glm::angleAxis(-motion.x * camera->fovy, glm::vec3(0.0f, 1.0f, 0.0f))
* glm::angleAxis(motion.y * camera->fovy, glm::vec3(1.0f, 0.0f, 0.0f))
);
return true;
}
}
return false;
}
void PlayMode::update(float elapsed) {
//slowly rotates through [0,1):
wobble += elapsed / 10.0f;
wobble -= std::floor(wobble);
hip->rotation = hip_base_rotation * glm::angleAxis(
glm::radians(5.0f * std::sin(wobble * 2.0f * float(M_PI))),
glm::vec3(0.0f, 1.0f, 0.0f)
);
upper_leg->rotation = upper_leg_base_rotation * glm::angleAxis(
glm::radians(7.0f * std::sin(wobble * 2.0f * 2.0f * float(M_PI))),
glm::vec3(0.0f, 0.0f, 1.0f)
);
lower_leg->rotation = lower_leg_base_rotation * glm::angleAxis(
glm::radians(10.0f * std::sin(wobble * 3.0f * 2.0f * float(M_PI))),
glm::vec3(0.0f, 0.0f, 1.0f)
);
//move camera:
{
//combine inputs into a move:
constexpr float PlayerSpeed = 30.0f;
glm::vec2 move = glm::vec2(0.0f);
if (left.pressed && !right.pressed) move.x =-1.0f;
if (!left.pressed && right.pressed) move.x = 1.0f;
if (down.pressed && !up.pressed) move.y =-1.0f;
if (!down.pressed && up.pressed) move.y = 1.0f;
//make it so that moving diagonally doesn't go faster:
if (move != glm::vec2(0.0f)) move = glm::normalize(move) * PlayerSpeed * elapsed;
glm::mat4x3 frame = camera->transform->make_local_to_parent();
glm::vec3 frame_right = frame[0];
//glm::vec3 up = frame[1];
glm::vec3 frame_forward = -frame[2];
camera->transform->position += move.x * frame_right + move.y * frame_forward;
}
//reset button press counters:
left.downs = 0;
right.downs = 0;
up.downs = 0;
down.downs = 0;
}
void draw_linearity_check();
void PlayMode::draw(glm::uvec2 const &drawable_size) {
//update camera aspect ratio for drawable:
camera->aspect = float(drawable_size.x) / float(drawable_size.y);
//set up light type and position for lit_color_texture_program:
// TODO: consider using the Light(s) in the scene to do this
glUseProgram(lit_color_texture_program->program);
glUniform1i(lit_color_texture_program->LIGHT_TYPE_int, 1);
glUniform3fv(lit_color_texture_program->LIGHT_DIRECTION_vec3, 1, glm::value_ptr(glm::vec3(0.0f, 0.0f,-1.0f)));
glUniform3fv(lit_color_texture_program->LIGHT_ENERGY_vec3, 1, glm::value_ptr(glm::vec3(1.0f, 1.0f, 0.95f)));
glUseProgram(0);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClearDepth(1.0f); //1.0 is actually the default value to clear the depth buffer to, but FYI you can change it.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); //this is the default depth comparison function, but FYI you can change it.
GL_ERRORS(); //print any errors produced by this setup code
scene.draw(*camera);
{ //use DrawLines to overlay some text:
glDisable(GL_DEPTH_TEST);
float aspect = float(drawable_size.x) / float(drawable_size.y);
DrawLines lines(glm::mat4(
1.0f / aspect, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
));
constexpr float H = 0.09f;
lines.draw_text("Mouse motion rotates camera; WASD moves; escape ungrabs mouse",
glm::vec3(-aspect + 0.1f * H, -1.0 + 0.1f * H, 0.0),
glm::vec3(H, 0.0f, 0.0f), glm::vec3(0.0f, H, 0.0f),
glm::u8vec4(0x00, 0x00, 0x00, 0x00));
float ofs = 2.0f / drawable_size.y;
lines.draw_text("Mouse motion rotates camera; WASD moves; escape ungrabs mouse",
glm::vec3(-aspect + 0.1f * H + ofs, -1.0 + 0.1f * H + ofs, 0.0),
glm::vec3(H, 0.0f, 0.0f), glm::vec3(0.0f, H, 0.0f),
glm::u8vec4(0xff, 0xff, 0xff, 0x00));
}
draw_linearity_check();
}