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) {
141 PrintSelectionInfo();
146 void Interface::HandleRelease(const SDL_KeyboardEvent &event) {
147 if (config.keyboard_disabled) return;
149 switch (event.keysym.sym) {
171 void Interface::FaceBlock() {
172 selection.SetFace(Block::Face((selection.GetFace() + 1) % Block::FACE_COUNT));
173 hud.Display(selection);
176 void Interface::TurnBlock() {
177 selection.SetTurn(Block::Turn((selection.GetTurn() + 1) % Block::TURN_COUNT));
178 hud.Display(selection);
181 void Interface::PrintBlockInfo() {
182 std::cout << std::endl;
184 std::cout << "not looking at any block" << std::endl;
185 Ray aim = ctrl.Aim();
186 std::cout << "aim ray: " << aim.orig << ", " << aim.dir << std::endl;
189 std::cout << "looking at block " << aim_block
190 << " " << Chunk::ToCoords(aim_block)
191 << " of chunk " << aim_chunk->Position()
193 Print(aim_chunk->BlockAt(aim_block));
196 void Interface::PrintChunkInfo() {
197 std::cout << std::endl;
199 std::cout << "not looking at any block" << std::endl;
202 std::cout << "looking at chunk "
203 << aim_chunk->Position()
206 std::cout << " neighbors:" << std::endl;
207 if (aim_chunk->HasNeighbor(Block::FACE_LEFT)) {
208 std::cout << " left " << aim_chunk->GetNeighbor(Block::FACE_LEFT).Position() << std::endl;
210 if (aim_chunk->HasNeighbor(Block::FACE_RIGHT)) {
211 std::cout << " right " << aim_chunk->GetNeighbor(Block::FACE_RIGHT).Position() << std::endl;
213 if (aim_chunk->HasNeighbor(Block::FACE_UP)) {
214 std::cout << " up " << aim_chunk->GetNeighbor(Block::FACE_UP).Position() << std::endl;
216 if (aim_chunk->HasNeighbor(Block::FACE_DOWN)) {
217 std::cout << " down " << aim_chunk->GetNeighbor(Block::FACE_DOWN).Position() << std::endl;
219 if (aim_chunk->HasNeighbor(Block::FACE_FRONT)) {
220 std::cout << " front " << aim_chunk->GetNeighbor(Block::FACE_FRONT).Position() << std::endl;
222 if (aim_chunk->HasNeighbor(Block::FACE_BACK)) {
223 std::cout << " back " << aim_chunk->GetNeighbor(Block::FACE_BACK).Position() << std::endl;
225 std::cout << std::endl;
228 void Interface::PrintLightInfo() {
230 << "light level " << world.PlayerChunk().GetLight(world.Player().Position())
231 << " at position " << world.Player().Position()
235 void Interface::PrintSelectionInfo() {
236 std::cout << std::endl;
240 void Interface::Print(const Block &block) {
241 std::cout << "type: " << block.type
242 << ", face: " << block.GetFace()
243 << ", turn: " << block.GetTurn()
248 void Interface::Handle(const SDL_MouseMotionEvent &event) {
249 if (config.mouse_disabled) return;
250 ctrl.RotateYaw(event.xrel * config.yaw_sensitivity);
251 ctrl.RotatePitch(event.yrel * config.pitch_sensitivity);
254 void Interface::HandlePress(const SDL_MouseButtonEvent &event) {
255 if (config.mouse_disabled) return;
257 if (event.button == SDL_BUTTON_LEFT) {
259 remove_timer.Start();
260 } else if (event.button == SDL_BUTTON_MIDDLE) {
262 } else if (event.button == SDL_BUTTON_RIGHT) {
268 void Interface::HandleRelease(const SDL_MouseButtonEvent &event) {
269 if (config.mouse_disabled) return;
271 if (event.button == SDL_BUTTON_LEFT) {
273 } else if (event.button == SDL_BUTTON_RIGHT) {
278 void Interface::PickBlock() {
279 if (!aim_chunk) return;
280 selection = aim_chunk->BlockAt(aim_block);
281 hud.Display(selection);
284 void Interface::PlaceBlock() {
285 if (!aim_chunk) return;
286 Chunk *mod_chunk = aim_chunk;
287 glm::vec3 next_pos = Chunk::ToCoords(aim_block) + aim_normal;
288 if (!Chunk::InBounds(next_pos)) {
289 mod_chunk = &world.Next(*aim_chunk, aim_normal);
290 next_pos -= aim_normal * glm::vec3(Chunk::Extent());
292 mod_chunk->SetBlock(next_pos, selection);
293 mod_chunk->Invalidate();
296 void Interface::RemoveBlock() noexcept {
297 if (!aim_chunk) return;
298 aim_chunk->SetBlock(aim_block, remove);
299 aim_chunk->Invalidate();
303 void Interface::Handle(const SDL_MouseWheelEvent &event) {
304 if (config.mouse_disabled) return;
308 } else if (event.y > 0) {
313 void Interface::SelectNext() {
315 if (size_t(selection.type) >= world.BlockTypes().Size()) {
318 hud.Display(selection);
321 void Interface::SelectPrevious() {
323 if (selection.type <= 0) {
324 selection.type = world.BlockTypes().Size() - 1;
326 hud.Display(selection);
329 void Interface::Handle(const SDL_WindowEvent &event) noexcept {
330 if (event.event == SDL_WINDOWEVENT_RESIZED) {
331 hud.Viewport(event.data1, event.data2);
336 void Interface::Update(int dt) {
337 ctrl.Velocity(glm::vec3(fwd - rev) * config.move_velocity);
340 place_timer.Update(dt);
341 remove_timer.Update(dt);
346 if (remove_timer.Hit()) {
351 if (place_timer.Hit()) {
357 void Interface::CheckAim() {
359 if (world.Intersection(aim, glm::mat4(1.0f), &aim_chunk, &aim_block, &dist, &aim_normal)) {
361 aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outline);
362 outline_transform = glm::scale(glm::vec3(1.0002f));
363 outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
364 outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
371 void Interface::Render(DirectionalLighting &program) noexcept {
372 if (config.visual_disabled) return;
375 program.SetM(outline_transform);