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 {
66 program.SetLightDirection({ 1.0f, 3.0f, 5.0f });
67 // disable distance fog
68 program.SetFogDensity(0.0f);
69 GLContext::ClearDepthBuffer();
71 program.SetVP(view, projection);
74 program.SetM(block_transform);
78 program.SetM(crosshair_transform);
83 Interface::Interface(const Config &config, World &world)
85 , ctrl(world.Player())
86 , hud(world.BlockTypes())
87 , aim{{ 0, 0, 0 }, { 0, 0, -1 }}
92 , outline_transform(1.0f)
100 hud.Viewport(960, 600);
101 hud.Display(selection);
105 void Interface::HandlePress(const SDL_KeyboardEvent &event) {
106 if (config.keyboard_disabled) return;
108 switch (event.keysym.sym) {
149 PrintSelectionInfo();
154 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
155 if (config.keyboard_disabled) return;
157 switch (event.keysym.sym) {
179 void Interface::FaceBlock() {
180 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
181 hud.Display(selection);
184 void Interface::TurnBlock() {
185 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
186 hud.Display(selection);
189 void Interface::ToggleCollision() {
190 ctrl.Controlled().WorldCollidable(!ctrl.Controlled().WorldCollidable());
191 std::cout << "collision " << (ctrl.Controlled().WorldCollidable() ? "on" : "off") << std::endl;
194 void Interface::PrintBlockInfo() {
195 std::cout << std::endl;
197 std::cout << "not looking at any block" << std::endl;
198 Ray aim = ctrl.Aim();
199 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
202 std::cout << "looking at block " << aim_block
203 << " " << Chunk::ToCoords(aim_block)
204 << " of chunk " << aim_chunk->Position()
206 Print(aim_chunk->BlockAt(aim_block));
209 void Interface::PrintChunkInfo() {
210 std::cout << std::endl;
212 std::cout << "not looking at any block" << std::endl;
215 std::cout << "looking at chunk "
216 << aim_chunk->Position()
219 std::cout << " neighbors:" << std::endl;
220 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
221 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
223 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
224 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
226 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
227 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
229 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
230 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
232 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
233 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
235 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
236 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
238 std::cout << std::endl;
241 void Interface::PrintLightInfo() {
243 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
244 << " at position " << world.Player().Position()
248 void Interface::PrintSelectionInfo() {
249 std::cout << std::endl;
253 void Interface::Print(const Block &block) {
254 std::cout << "type: " << block.type
255 << ", face: " << block.GetFace()
256 << ", turn: " << block.GetTurn()
261 void Interface::Handle(const SDL_MouseMotionEvent &event) {
262 if (config.mouse_disabled) return;
263 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
264 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
267 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
268 if (config.mouse_disabled) return;
270 if (event.button == SDL_BUTTON_LEFT) {
272 remove_timer.Start();
273 } else if (event.button == SDL_BUTTON_MIDDLE) {
275 } else if (event.button == SDL_BUTTON_RIGHT) {
281 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
282 if (config.mouse_disabled) return;
284 if (event.button == SDL_BUTTON_LEFT) {
286 } else if (event.button == SDL_BUTTON_RIGHT) {
291 void Interface::PickBlock() {
292 if (!aim_chunk) return;
293 selection = aim_chunk->BlockAt(aim_block);
294 hud.Display(selection);
297 void Interface::PlaceBlock() {
298 if (!aim_chunk) return;
299 Chunk *mod_chunk = aim_chunk;
300 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
301 if (!Chunk::InBounds(next_pos)) {
302 mod_chunk = &world.Next(*aim_chunk, aim_normal);
303 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
305 mod_chunk->SetBlock(next_pos, selection);
306 mod_chunk->Invalidate();
309 void Interface::RemoveBlock() noexcept {
310 if (!aim_chunk) return;
311 aim_chunk->SetBlock(aim_block, remove);
312 aim_chunk->Invalidate();
316 void Interface::Handle(const SDL_MouseWheelEvent &event) {
317 if (config.mouse_disabled) return;
321 } else if (event.y > 0) {
326 void Interface::SelectNext() {
328 if (size_t(selection.type) >= world.BlockTypes().Size()) {
331 hud.Display(selection);
334 void Interface::SelectPrevious() {
336 if (selection.type <= 0) {
337 selection.type = world.BlockTypes().Size() - 1;
339 hud.Display(selection);
342 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
343 if (event.event == SDL_WINDOWEVENT_RESIZED) {
344 hud.Viewport(event.data1, event.data2);
349 void Interface::Update(int dt) {
350 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
353 place_timer.Update(dt);
354 remove_timer.Update(dt);
359 if (remove_timer.Hit()) {
364 if (place_timer.Hit()) {
370 void Interface::CheckAim() {
372 if (world.Intersection(aim, glm::mat4(1.0f), aim_chunk, aim_block, dist, aim_normal)) {
374 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
375 outline_transform = glm::scale(glm::vec3(1.0002f));
376 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
377 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
384 void Interface::Render(DirectionalLighting &program) noexcept {
385 if (config.visual_disabled) return;
388 program.SetM(outline_transform);