From 88c16959037da4d12784797193a944591bd73fef Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 29 Jun 2022 14:03:36 +0300 Subject: [PATCH] Flipped UI, minor changes Flipped UI, removed extra Batch2D, added comments and minor changes --- src/graphics/Batch2D.cpp | 12 +++---- src/graphics/Font.cpp | 36 +++++++++------------ src/graphics/Font.h | 1 + src/voxel_engine.cpp | 3 +- src/window/Camera.cpp | 10 ++++-- src/window/Camera.h | 1 + src/world_render.h | 70 +++++++++++++++++++--------------------- 7 files changed, 67 insertions(+), 66 deletions(-) diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp index 9c114576..97c886de 100644 --- a/src/graphics/Batch2D.cpp +++ b/src/graphics/Batch2D.cpp @@ -74,13 +74,13 @@ void Batch2D::rect(float x, float y, float w, float h){ void Batch2D::rect(float x, float y, float w, float h, float u, float v, float tx, float ty, float r, float g, float b, float a){ - vertex(x, y, u, v, r,g,b,a); - vertex(x+w, y+h, u+tx, v+ty, r,g,b,a); - vertex(x, y+h, u, v+ty, r,g,b,a); + vertex(x, y, u, v+ty, r,g,b,a); + vertex(x+w, y+h, u+tx, v, r,g,b,a); + vertex(x, y+h, u, v, r,g,b,a); - vertex(x, y, u, v, r,g,b,a); - vertex(x+w, y, u+tx, v, r,g,b,a); - vertex(x+w, y+h, u+tx, v+ty, r,g,b,a); + vertex(x, y, u, v+ty, r,g,b,a); + vertex(x+w, y, u+tx, v+ty, r,g,b,a); + vertex(x+w, y+h, u+tx, v, r,g,b,a); } void Batch2D::render() { diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 0947357b..8a9e764a 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -9,15 +9,8 @@ Font::~Font(){ delete texture; } - -void Font::draw(Batch2D* batch, std::string text, int x, int y) { - for (char c : text){ - float u = (c % 16) / 16.0f; - float v = 1.0f - ((c / 16) / 16.0f) - 1.0f/16.0f; - batch->rect(x, y, 8, 8, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1); - - int gw = 7; - switch (c){ +int Font::getGlyphWidth(char c) { + switch (c){ case 'l': case 'i': case 'j': @@ -25,16 +18,19 @@ void Font::draw(Batch2D* batch, std::string text, int x, int y) { case '.': case ',': case ':': - case ';': - gw = 3; - break; - case 't': - gw = 5; - break; - case ' ': - gw = 3; - break; - } - x += gw; + case ';': return 3; + case 't': return 5; + case ' ': return 3; + } + return 7; +} + + +void Font::draw(Batch2D* batch, std::string text, int x, int y) { + for (char c : text){ + float u = (c % 16) / 16.0f; + float v = 1.0f - ((c / 16) / 16.0f) - 1.0f/16.0f; + batch->rect(x, y, 8, 8, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1); + x += getGlyphWidth(c); } } diff --git a/src/graphics/Font.h b/src/graphics/Font.h index c771b3e4..77a769a1 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -12,6 +12,7 @@ public: Font(Texture* texture); ~Font(); + int getGlyphWidth(char c); void draw(Batch2D* batch, std::string text, int x, int y); }; diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 28e79b7f..8dff8152 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -333,7 +333,8 @@ int main() { for (int i = 0; i < freeLoaders; i++) chunksController.loadVisible(wfile); - draw_world(player, camera, assets, chunks, occlusion, devdata, fps); + draw_world(player, camera, assets, chunks, occlusion); + draw_hud(player, assets, devdata, fps); Window::swapBuffers(); Events::pullEvents(); diff --git a/src/window/Camera.cpp b/src/window/Camera.cpp index 4875bd95..1fe9b77c 100644 --- a/src/window/Camera.cpp +++ b/src/window/Camera.cpp @@ -40,9 +40,15 @@ mat4 Camera::getProjection(){ if (perspective) return glm::perspective(fov*zoom, aspect, 0.05f, 1500.0f); else - return glm::ortho(0.0f, fov*aspect, 0.0f, fov); + if (flipped) + return glm::ortho(0.0f, fov*aspect, fov, 0.0f); + else + return glm::ortho(0.0f, fov*aspect, 0.0f, fov); } mat4 Camera::getView(){ - return glm::lookAt(position, position+front, up); + if (perspective) + return glm::lookAt(position, position+front, up); + else + return glm::mat4(1.0f); } diff --git a/src/window/Camera.h b/src/window/Camera.h index 4d8a4462..28aabe94 100644 --- a/src/window/Camera.h +++ b/src/window/Camera.h @@ -24,6 +24,7 @@ public: float zoom; mat4 rotation; bool perspective = true; + bool flipped = false; Camera(vec3 position, float fov); void rotate(float x, float y, float z); diff --git a/src/world_render.h b/src/world_render.h index 22d993df..0a029175 100644 --- a/src/world_render.h +++ b/src/world_render.h @@ -45,7 +45,6 @@ int uiscale = 2; LineBatch *lineBatch; Batch2D *batch; -Batch2D *activeBlockBatch; Camera *uicamera; void init_renderer(){ @@ -53,9 +52,9 @@ void init_renderer(){ lineBatch = new LineBatch(4096); batch = new Batch2D(1024); - activeBlockBatch = new Batch2D(1024); uicamera = new Camera(glm::vec3(), Window::height / uiscale); uicamera->perspective = false; + uicamera->flipped = true; } @@ -63,7 +62,6 @@ void finalize_renderer(){ delete crosshair; delete lineBatch; delete batch; - delete activeBlockBatch; } void draw_chunk(size_t index, Camera* camera, Shader* shader, bool occlusion){ @@ -112,17 +110,45 @@ bool chunks_comparator(size_t i, size_t j) { } -void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, - bool occlusion, bool devdata, int fps){ +void draw_hud(Player* player, Assets* assets, bool devdata, int fps){ + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + Shader* uishader = assets->getShader("ui"); + uishader->use(); + uishader->uniformMatrix("u_projview", uicamera->getProjection()*uicamera->getView()); + + // draw debug info + Font* font = assets->getFont("normal"); + batch->begin(); + batch->texture(font->texture); + if (devdata){ + font->draw(batch, "devdata does not exist", 16, 16); + font->draw(batch, std::to_string((int)player->camera->position.x), 10, 30); + font->draw(batch, std::to_string((int)player->camera->position.y), 50, 30); + font->draw(batch, std::to_string((int)player->camera->position.z), 90, 30); + font->draw(batch, "fps:", 16, 42); + font->draw(batch, std::to_string(fps), 40, 42); + } + batch->render(); + + // choosen block preview + Texture* blocks = assets->getTexture("block_select"); + batch->texture(blocks); + float u = (player->choosenBlock % 16) / 16.0f; + float v = 1.0f - ((player->choosenBlock / 16) / 16.0f) - 1.0f/16.0f; + batch->rect(16, 280, 64, 64, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1); + batch->render(); +} + +void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, bool occlusion){ glClearColor(0.7f,0.81f,1.0f,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); _chunks = chunks; - // Draw VAO Texture* texture = assets->getTexture("block"); - Texture* blocks = assets->getTexture("block_select"); Shader* shader = assets->getShader("main"); Shader* crosshairShader = assets->getShader("crosshair"); Shader* linesShader = assets->getShader("lines"); @@ -134,7 +160,6 @@ void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, shader->uniform3f("u_fogColor", 0.7f,0.71f,0.73f); shader->uniform3f("u_cameraPos", camera->position.x,camera->position.y,camera->position.z); texture->bind(); - // blocks->bind(); std::vector indices; @@ -171,35 +196,6 @@ void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f, camera->position.z+0.01f, 0, 0, 1, 1); lineBatch->line(camera->position.x, camera->position.y-0.1f, camera->position.z, camera->position.x, camera->position.y-0.1f+0.01f, camera->position.z, 0, 1, 0, 1); lineBatch->render(); - - - glDisable(GL_DEPTH_TEST); - Shader* uishader = assets->getShader("ui"); - uishader->use(); - uishader->uniformMatrix("u_projview", uicamera->getProjection()); - - Font* font = assets->getFont("normal"); - // Texture* blocks = assets->getTexture("blocks") - batch->begin(); - batch->texture(font->texture); - // font->draw(batch, "void Font::draw(Batch2D* batch, std::string text, int x, int y) {", 10, 10); - if (devdata){ - font->draw(batch, "devdata does not exist", 16, 16); - font->draw(batch, std::to_string((int)player->camera->position.x), 10, 30); - font->draw(batch, std::to_string((int)player->camera->position.y), 50, 30); - font->draw(batch, std::to_string((int)player->camera->position.z), 90, 30); - font->draw(batch, "fps:", 16, 42); - font->draw(batch, std::to_string(fps), 40, 42); - } - //batch->rect(0, 0, 256, 256); - batch->render(); - - activeBlockBatch->begin(); - activeBlockBatch->texture(blocks); - float u = (player->choosenBlock % 16) / 16.0f; - float v = 1.0f - ((player->choosenBlock / 16) / 16.0f) - 1.0f/16.0f; - activeBlockBatch->rect(16, 280, 64, 64, u, v, 1.0f/16.0f, 1.0f/16.0f, 1,1,1,1); - activeBlockBatch->render(); } #endif // WORLD_RENDERER_CPP