-
Notifications
You must be signed in to change notification settings - Fork 0
/
CratesMode.cpp
224 lines (194 loc) · 7.59 KB
/
CratesMode.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
#include "CratesMode.hpp"
#include "MenuMode.hpp"
#include "Load.hpp"
#include "Sound.hpp"
#include "MeshBuffer.hpp"
#include "gl_errors.hpp" //helper for dumpping OpenGL error messages
#include "read_chunk.hpp" //helper for reading a vector of structures from a file
#include "data_path.hpp" //helper to get paths relative to executable
#include "compile_program.hpp" //helper to compile opengl shader programs
#include "draw_text.hpp" //helper to... um.. draw text
#include "vertex_color_program.hpp"
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <fstream>
#include <map>
#include <cstddef>
#include <random>
Load< MeshBuffer > crates_meshes(LoadTagDefault, [](){
return new MeshBuffer(data_path("crates.pnc"));
});
Load< GLuint > crates_meshes_for_vertex_color_program(LoadTagDefault, [](){
return new GLuint(crates_meshes->make_vao_for_program(vertex_color_program->program));
});
Load< Sound::Sample > sample_dot(LoadTagDefault, [](){
return new Sound::Sample(data_path("dot.wav"));
});
Load< Sound::Sample > sample_loop(LoadTagDefault, [](){
return new Sound::Sample(data_path("loop.wav"));
});
CratesMode::CratesMode() {
//----------------
//set up scene:
//TODO: this should load the scene from a file!
auto attach_object = [this](Scene::Transform *transform, std::string const &name) {
Scene::Object *object = scene.new_object(transform);
object->program = vertex_color_program->program;
object->program_mvp_mat4 = vertex_color_program->object_to_clip_mat4;
object->program_mv_mat4x3 = vertex_color_program->object_to_light_mat4x3;
object->program_itmv_mat3 = vertex_color_program->normal_to_light_mat3;
object->vao = *crates_meshes_for_vertex_color_program;
MeshBuffer::Mesh const &mesh = crates_meshes->lookup(name);
object->start = mesh.start;
object->count = mesh.count;
return object;
};
{ //build some sort of content:
//Crate at the origin:
Scene::Transform *transform1 = scene.new_transform();
transform1->position = glm::vec3(1.0f, 0.0f, 0.0f);
large_crate = attach_object(transform1, "Crate");
//smaller crate on top:
Scene::Transform *transform2 = scene.new_transform();
transform2->set_parent(transform1);
transform2->position = glm::vec3(0.0f, 0.0f, 1.5f);
transform2->scale = glm::vec3(0.5f);
small_crate = attach_object(transform2, "Crate");
}
{ //Camera looking at the origin:
Scene::Transform *transform = scene.new_transform();
transform->position = glm::vec3(0.0f, -10.0f, 1.0f);
//Cameras look along -z, so rotate view to look at origin:
transform->rotation = glm::angleAxis(glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
camera = scene.new_camera(transform);
}
//start the 'loop' sample playing at the large crate:
loop = sample_loop->play(large_crate->transform->position, 1.0f, Sound::Loop);
}
CratesMode::~CratesMode() {
if (loop) loop->stop();
}
bool CratesMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size) {
//ignore any keys that are the result of automatic key repeat:
if (evt.type == SDL_KEYDOWN && evt.key.repeat) {
return false;
}
//handle tracking the state of WSAD for movement control:
if (evt.type == SDL_KEYDOWN || evt.type == SDL_KEYUP) {
if (evt.key.keysym.scancode == SDL_SCANCODE_W) {
controls.forward = (evt.type == SDL_KEYDOWN);
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_S) {
controls.backward = (evt.type == SDL_KEYDOWN);
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_A) {
controls.left = (evt.type == SDL_KEYDOWN);
return true;
} else if (evt.key.keysym.scancode == SDL_SCANCODE_D) {
controls.right = (evt.type == SDL_KEYDOWN);
return true;
}
}
//handle tracking the mouse for rotation control:
if (!mouse_captured) {
if (evt.type == SDL_KEYDOWN && evt.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
Mode::set_current(nullptr);
return true;
}
if (evt.type == SDL_MOUSEBUTTONDOWN) {
SDL_SetRelativeMouseMode(SDL_TRUE);
mouse_captured = true;
return true;
}
} else if (mouse_captured) {
if (evt.type == SDL_KEYDOWN && evt.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
SDL_SetRelativeMouseMode(SDL_FALSE);
mouse_captured = false;
return true;
}
if (evt.type == SDL_MOUSEMOTION) {
//Note: float(window_size.y) * camera->fovy is a pixels-to-radians conversion factor
float yaw = evt.motion.xrel / float(window_size.y) * camera->fovy;
float pitch = evt.motion.yrel / float(window_size.y) * camera->fovy;
yaw = -yaw;
pitch = -pitch;
camera->transform->rotation = glm::normalize(
camera->transform->rotation
* glm::angleAxis(yaw, glm::vec3(0.0f, 1.0f, 0.0f))
* glm::angleAxis(pitch, glm::vec3(1.0f, 0.0f, 0.0f))
);
return true;
}
}
return false;
}
void CratesMode::update(float elapsed) {
glm::mat3 directions = glm::mat3_cast(camera->transform->rotation);
float amt = 5.0f * elapsed;
if (controls.right) camera->transform->position += amt * directions[0];
if (controls.left) camera->transform->position -= amt * directions[0];
if (controls.backward) camera->transform->position += amt * directions[2];
if (controls.forward) camera->transform->position -= amt * directions[2];
{ //set sound positions:
glm::mat4 cam_to_world = camera->transform->make_local_to_world();
Sound::listener.set_position( cam_to_world[3] );
//camera looks down -z, so right is +x:
Sound::listener.set_right( glm::normalize(cam_to_world[0]) );
if (loop) {
glm::mat4 large_crate_to_world = large_crate->transform->make_local_to_world();
loop->set_position( large_crate_to_world * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f) );
}
}
dot_countdown -= elapsed;
if (dot_countdown <= 0.0f) {
dot_countdown = (rand() / float(RAND_MAX) * 2.0f) + 0.5f;
glm::mat4x3 small_crate_to_world = small_crate->transform->make_local_to_world();
sample_dot->play( small_crate_to_world * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f) );
}
}
void CratesMode::draw(glm::uvec2 const &drawable_size) {
//set up basic OpenGL state:
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//set up light position + color:
glUseProgram(vertex_color_program->program);
glUniform3fv(vertex_color_program->sun_color_vec3, 1, glm::value_ptr(glm::vec3(0.81f, 0.81f, 0.76f)));
glUniform3fv(vertex_color_program->sun_direction_vec3, 1, glm::value_ptr(glm::normalize(glm::vec3(-0.2f, 0.2f, 1.0f))));
glUniform3fv(vertex_color_program->sky_color_vec3, 1, glm::value_ptr(glm::vec3(0.4f, 0.4f, 0.45f)));
glUniform3fv(vertex_color_program->sky_direction_vec3, 1, glm::value_ptr(glm::vec3(0.0f, 1.0f, 0.0f)));
glUseProgram(0);
//fix aspect ratio of camera
camera->aspect = drawable_size.x / float(drawable_size.y);
scene.draw(camera);
if (Mode::current.get() == this) {
glDisable(GL_DEPTH_TEST);
std::string message;
if (mouse_captured) {
message = "ESCAPE TO UNGRAB MOUSE * WASD MOVE";
} else {
message = "CLICK TO GRAB MOUSE * ESCAPE QUIT";
}
float height = 0.06f;
float width = text_width(message, height);
draw_text(message, glm::vec2(-0.5f * width,-0.99f), height, glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
draw_text(message, glm::vec2(-0.5f * width,-1.0f), height, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
glUseProgram(0);
}
GL_ERRORS();
}
void CratesMode::show_pause_menu() {
std::shared_ptr< MenuMode > menu = std::make_shared< MenuMode >();
std::shared_ptr< Mode > game = shared_from_this();
menu->background = game;
menu->choices.emplace_back("PAUSED");
menu->choices.emplace_back("RESUME", [game](){
Mode::set_current(game);
});
menu->choices.emplace_back("QUIT", [](){
Mode::set_current(nullptr);
});
menu->selected = 1;
Mode::set_current(menu);
}