2 #include "Interface.hpp"
4 #include "../app/init.hpp"
5 #include "../graphics/DirectionalLighting.hpp"
6 #include "../model/shapes.hpp"
7 #include "../world/World.hpp"
10 #include <glm/gtc/matrix_transform.hpp>
11 #include <glm/gtx/io.hpp>
16 HUD::HUD(const BlockTypeRegistry &types)
20 , block_transform(1.0f)
21 , block_visible(false)
23 , crosshair_transform(1.0f)
26 , projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far))
27 , view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) {
28 block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f));
29 block_transform = glm::scale(block_transform, glm::vec3(50.0f));
30 block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f));
31 block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f));
33 crosshair.vertices = std::vector<glm::vec3>({
34 { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f },
35 { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f },
37 crosshair.indices = std::vector<OutlineModel::Index>({
40 crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f });
41 crosshair.Invalidate();
45 void HUD::Viewport(float width, float height) noexcept {
46 Viewport(0, 0, width, height);
49 void HUD::Viewport(float x, float y, float width, float height) noexcept {
50 projection = glm::ortho(x, width, height, y, near, far);
51 crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f));
55 void HUD::Display(const Block &b) {
56 const BlockType &type = types.Get(b.type);
59 type.FillModel(block_buf, b.Transform());
60 block.Update(block_buf);
61 block_visible = type.visible;
65 void HUD::Render(DirectionalLighting &program) noexcept {
67 program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
68 // disable distance fog
69 program.SetFogDensity(0.0f);
70 GLContext::ClearDepthBuffer();
71 program.SetMVP(block_transform, view, projection);
73 program.SetM(crosshair_transform);
79 Interface::Interface(const Config &config, World &world)
81 , ctrl(world.Player())
82 , hud(world.BlockTypes())
83 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
88 , outline_transform(1.0f)
96 hud.Viewport(960, 600);
97 hud.Display(selection);
101 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
102 if (config.keyboard_disabled) return;
104 switch (event.keysym.sym) {
145 PrintSelectionInfo();
150 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
151 if (config.keyboard_disabled) return;
153 switch (event.keysym.sym) {
175 void Interface::FaceBlock() {
176 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
177 hud.Display(selection);
180 void Interface::TurnBlock() {
181 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
182 hud.Display(selection);
185 void Interface::ToggleCollision() {
186 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
187 std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
190 void Interface::PrintBlockInfo() {
191 std::cout << std::endl;
193 std::cout << "not looking at any block" << std::endl;
194 Ray aim = ctrl.Aim();
195 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
198 std::cout << "looking at block " << aim_block
199 << " " << Chunk::ToCoords(aim_block)
200 << " of chunk " << aim_chunk->Position()
202 Print(aim_chunk->BlockAt(aim_block));
205 void Interface::PrintChunkInfo() {
206 std::cout << std::endl;
208 std::cout << "not looking at any block" << std::endl;
211 std::cout << "looking at chunk "
212 << aim_chunk->Position()
215 std::cout << " neighbors:" << std::endl;
216 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
217 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
219 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
220 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
222 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
223 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
225 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
226 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
228 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
229 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
231 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
232 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
234 std::cout << std::endl;
237 void Interface::PrintLightInfo() {
239 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
240 << " at position " << world.Player().Position()
244 void Interface::PrintSelectionInfo() {
245 std::cout << std::endl;
249 void Interface::Print(const Block &block) {
250 std::cout << "type: " << block.type
251 << ", face: " << block.GetFace()
252 << ", turn: " << block.GetTurn()
257 void Interface::Handle(const SDL_MouseMotionEvent &event) {
258 if (config.mouse_disabled) return;
259 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
260 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
263 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
264 if (config.mouse_disabled) return;
266 if (event.button == SDL_BUTTON_LEFT) {
268 remove_timer.Start();
269 } else if (event.button == SDL_BUTTON_MIDDLE) {
271 } else if (event.button == SDL_BUTTON_RIGHT) {
277 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
278 if (config.mouse_disabled) return;
280 if (event.button == SDL_BUTTON_LEFT) {
282 } else if (event.button == SDL_BUTTON_RIGHT) {
287 void Interface::PickBlock() {
288 if (!aim_chunk) return;
289 selection = aim_chunk->BlockAt(aim_block);
290 hud.Display(selection);
293 void Interface::PlaceBlock() {
294 if (!aim_chunk) return;
295 Chunk *mod_chunk = aim_chunk;
296 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
297 if (!Chunk::InBounds(next_pos)) {
298 mod_chunk = &world.Next(*aim_chunk, aim_normal);
299 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
301 mod_chunk->SetBlock(next_pos, selection);
302 mod_chunk->Invalidate();
305 void Interface::RemoveBlock() noexcept {
306 if (!aim_chunk) return;
307 aim_chunk->SetBlock(aim_block, remove);
308 aim_chunk->Invalidate();
312 void Interface::Handle(const SDL_MouseWheelEvent &event) {
313 if (config.mouse_disabled) return;
317 } else if (event.y > 0) {
322 void Interface::SelectNext() {
324 if (size_t(selection.type) >= world.BlockTypes().Size()) {
327 hud.Display(selection);
330 void Interface::SelectPrevious() {
332 if (selection.type <= 0) {
333 selection.type = world.BlockTypes().Size() - 1;
335 hud.Display(selection);
338 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
339 if (event.event == SDL_WINDOWEVENT_RESIZED) {
340 hud.Viewport(event.data1, event.data2);
345 void Interface::Update(int dt) {
346 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
349 place_timer.Update(dt);
350 remove_timer.Update(dt);
355 if (remove_timer.Hit()) {
360 if (place_timer.Hit()) {
366 void Interface::CheckAim() {
368 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
370 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
371 outline_transform = glm::scale(glm::vec3(1.0002f));
372 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
373 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
380 void Interface::Render(DirectionalLighting &program) noexcept {
381 if (config.visual_disabled) return;
384 program.SetM(outline_transform);