-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayMode.cpp
More file actions
287 lines (244 loc) · 9.68 KB
/
PlayMode.cpp
File metadata and controls
287 lines (244 loc) · 9.68 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
#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 <glm/gtx/quaternion.hpp>
#include <random>
GLuint phonebank_meshes_for_lit_color_texture_program = 0;
Load< MeshBuffer > phonebank_meshes(LoadTagDefault, []() -> MeshBuffer const * {
MeshBuffer const *ret = new MeshBuffer(data_path("phone-bank.pnct"));
phonebank_meshes_for_lit_color_texture_program = ret->make_vao_for_program(lit_color_texture_program->program);
return ret;
});
Load< Scene > phonebank_scene(LoadTagDefault, []() -> Scene const * {
return new Scene(data_path("phone-bank.scene"), [&](Scene &scene, Scene::Transform *transform, std::string const &mesh_name){
Mesh const &mesh = phonebank_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 = phonebank_meshes_for_lit_color_texture_program;
drawable.pipeline.type = mesh.type;
drawable.pipeline.start = mesh.start;
drawable.pipeline.count = mesh.count;
});
});
WalkMesh const *walkmesh = nullptr;
Load< WalkMeshes > phonebank_walkmeshes(LoadTagDefault, []() -> WalkMeshes const * {
WalkMeshes *ret = new WalkMeshes(data_path("phone-bank.w"));
walkmesh = &ret->lookup("WalkMesh");
return ret;
});
PlayMode::PlayMode() : scene(*phonebank_scene) {
//create a player transform:
scene.transforms.emplace_back();
player.transform = &scene.transforms.back();
//create a player camera attached to a child of the player transform:
scene.transforms.emplace_back();
scene.cameras.emplace_back(&scene.transforms.back());
player.camera = &scene.cameras.back();
player.camera->fovy = glm::radians(60.0f);
player.camera->near = 0.01f;
player.camera->transform->parent = player.transform;
//player's eyes are 1.8 units above the ground:
player.camera->transform->position = glm::vec3(0.0f, 0.0f, 1.8f);
//rotate camera facing direction (-z) to player facing direction (+y):
player.camera->transform->rotation = glm::angleAxis(glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
//start player walking at nearest walk point:
player.at = walkmesh->nearest_walk_point(player.transform->position);
}
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)
);
glm::vec3 upDir = walkmesh->to_world_smooth_normal(player.at);
player.transform->rotation = glm::angleAxis(-motion.x * player.camera->fovy, upDir) * player.transform->rotation;
float pitch = glm::pitch(player.camera->transform->rotation);
pitch += motion.y * player.camera->fovy;
//camera looks down -z (basically at the player's feet) when pitch is at zero.
pitch = std::min(pitch, 0.95f * 3.1415926f);
pitch = std::max(pitch, 0.05f * 3.1415926f);
player.camera->transform->rotation = glm::angleAxis(pitch, glm::vec3(1.0f, 0.0f, 0.0f));
return true;
}
}
return false;
}
void PlayMode::update(float elapsed) {
//player walking:
{
//combine inputs into a move:
constexpr float PlayerSpeed = 3.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;
//get move in world coordinate system:
glm::vec3 remain = player.transform->make_local_to_world() * glm::vec4(move.x, move.y, 0.0f, 0.0f);
//using a for() instead of a while() here so that if walkpoint gets stuck in
// some awkward case, code will not infinite loop:
for (uint32_t iter = 0; iter < 10; ++iter) {
if (remain == glm::vec3(0.0f)) break;
WalkPoint end;
float time;
walkmesh->walk_in_triangle(player.at, remain, &end, &time);
player.at = end;
if (time == 1.0f) {
//finished within triangle:
remain = glm::vec3(0.0f);
break;
}
//some step remains:
remain *= (1.0f - time);
//try to step over edge:
glm::quat rotation;
if (walkmesh->cross_edge(player.at, &end, &rotation)) {
//stepped to a new triangle:
player.at = end;
//rotate step to follow surface:
remain = rotation * remain;
} else {
//ran into a wall, bounce / slide along it:
glm::vec3 const &a = walkmesh->vertices[player.at.indices.x];
glm::vec3 const &b = walkmesh->vertices[player.at.indices.y];
glm::vec3 const &c = walkmesh->vertices[player.at.indices.z];
glm::vec3 along = glm::normalize(b-a);
glm::vec3 normal = glm::normalize(glm::cross(b-a, c-a));
glm::vec3 in = glm::cross(normal, along);
//check how much 'remain' is pointing out of the triangle:
float d = glm::dot(remain, in);
if (d < 0.0f) {
//bounce off of the wall:
remain += (-1.25f * d) * in;
} else {
//if it's just pointing along the edge, bend slightly away from wall:
remain += 0.01f * d * in;
}
}
}
if (remain != glm::vec3(0.0f)) {
std::cout << "NOTE: code used full iteration budget for walking." << std::endl;
}
//update player's position to respect walking:
player.transform->position = walkmesh->to_world_point(player.at);
{ //update player's rotation to respect local (smooth) up-vector:
glm::quat adjust = glm::rotation(
player.transform->rotation * glm::vec3(0.0f, 0.0f, 1.0f), //current up vector
walkmesh->to_world_smooth_normal(player.at) //smoothed up vector at walk location
);
player.transform->rotation = glm::normalize(adjust * player.transform->rotation);
}
/*
glm::mat4x3 frame = camera->transform->make_local_to_parent();
glm::vec3 right = frame[0];
//glm::vec3 up = frame[1];
glm::vec3 forward = -frame[2];
camera->transform->position += move.x * right + move.y * forward;
*/
}
//reset button press counters:
left.downs = 0;
right.downs = 0;
up.downs = 0;
down.downs = 0;
}
void PlayMode::draw(glm::uvec2 const &drawable_size) {
//update camera aspect ratio for drawable:
player.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.
scene.draw(*player.camera);
/* In case you are wondering if your walkmesh is lining up with your scene, try:
{
glDisable(GL_DEPTH_TEST);
DrawLines lines(player.camera->make_projection() * glm::mat4(player.camera->transform->make_world_to_local()));
for (auto const &tri : walkmesh->triangles) {
lines.draw(walkmesh->vertices[tri.x], walkmesh->vertices[tri.y], glm::u8vec4(0x88, 0x00, 0xff, 0xff));
lines.draw(walkmesh->vertices[tri.y], walkmesh->vertices[tri.z], glm::u8vec4(0x88, 0x00, 0xff, 0xff));
lines.draw(walkmesh->vertices[tri.z], walkmesh->vertices[tri.x], glm::u8vec4(0x88, 0x00, 0xff, 0xff));
}
}
*/
{ //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 looks; 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 looks; 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));
}
GL_ERRORS();
}