From 75172fd735e34082c34b47ae7c194445b53038d9 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 20 Oct 2015 11:40:07 +0200 Subject: [PATCH] renamed OutlineMesh -> PrimitiveMesh now it can be used for lines and surfaces and with alpha blending --- .../{OutlineMesh.hpp => PrimitiveMesh.hpp} | 17 +++++++--- src/graphics/Viewport.hpp | 10 +++--- src/graphics/mesh.cpp | 31 ++++++++++++++++--- src/graphics/shader.cpp | 8 ++--- src/graphics/viewport.cpp | 24 +++++++------- src/model/CollisionBounds.hpp | 12 +++---- src/model/Shape.hpp | 2 +- src/model/bounds.cpp | 6 ++-- src/model/shape.cpp | 2 +- src/ui/HUD.hpp | 6 ++-- src/ui/ui.cpp | 18 +++++------ src/world/BlockType.hpp | 4 +-- src/world/block.cpp | 4 +-- 13 files changed, 87 insertions(+), 57 deletions(-) rename src/graphics/{OutlineMesh.hpp => PrimitiveMesh.hpp} (70%) diff --git a/src/graphics/OutlineMesh.hpp b/src/graphics/PrimitiveMesh.hpp similarity index 70% rename from src/graphics/OutlineMesh.hpp rename to src/graphics/PrimitiveMesh.hpp index 77e0436..1aa3cce 100644 --- a/src/graphics/OutlineMesh.hpp +++ b/src/graphics/PrimitiveMesh.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_GRAPHICS_OUTLINEMESH_HPP_ -#define BLANK_GRAPHICS_OUTLINEMESH_HPP_ +#ifndef BLANK_GRAPHICS_PRIMITIVEMESH_HPP_ +#define BLANK_GRAPHICS_PRIMITIVEMESH_HPP_ #include "VertexArray.hpp" @@ -10,11 +10,11 @@ namespace blank { -class OutlineMesh { +class PrimitiveMesh { public: using Position = glm::vec3; - using Color = glm::vec3; + using Color = glm::vec4; using Index = unsigned short; using Positions = std::vector; @@ -46,6 +46,12 @@ public: indices.reserve(i); } + void FillRect( + float w, float h, + const glm::vec4 &color = glm::vec4(0.0f), + const glm::vec2 &pivot = glm::vec2(0.0f) + ); + }; using VAO = VertexArray; @@ -53,7 +59,8 @@ public: public: void Update(const Buffer &) noexcept; - void Draw() noexcept; + void DrawLines() noexcept; + void DrawTriangles() noexcept; private: VAO vao; diff --git a/src/graphics/Viewport.hpp b/src/graphics/Viewport.hpp index 03e0e0d..a8cb69f 100644 --- a/src/graphics/Viewport.hpp +++ b/src/graphics/Viewport.hpp @@ -52,8 +52,8 @@ public: BlockLighting &ChunkProgram() noexcept; DirectionalLighting &EntityProgram() noexcept; DirectionalLighting &HUDProgram() noexcept; - PlainColor &WorldOutlineProgram() noexcept; - PlainColor &HUDOutlineProgram() noexcept; + PlainColor &WorldColorProgram() noexcept; + PlainColor &HUDColorProgram() noexcept; SkyBoxShader &SkyBoxProgram() noexcept; BlendedSprite &SpriteProgram() noexcept; @@ -70,7 +70,7 @@ private: BlockLighting chunk_prog; DirectionalLighting entity_prog; - PlainColor outline_prog; + PlainColor color_prog; SkyBoxShader sky_prog; BlendedSprite sprite_prog; @@ -79,8 +79,8 @@ private: CHUNK, ENTITY, HUD, - OUTLINE_WORLD, - OUTLINE_HUD, + COLOR_WORLD, + COLOR_HUD, SKY_BOX, SPRITE, } active_prog; diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp index 4b0f582..3aa631e 100644 --- a/src/graphics/mesh.cpp +++ b/src/graphics/mesh.cpp @@ -1,6 +1,6 @@ #include "BlockMesh.hpp" #include "EntityMesh.hpp" -#include "OutlineMesh.hpp" +#include "PrimitiveMesh.hpp" #include "SkyBoxMesh.hpp" #include "SpriteMesh.hpp" @@ -72,10 +72,29 @@ void BlockMesh::Draw() const noexcept { } -void OutlineMesh::Update(const Buffer &buf) noexcept { +void PrimitiveMesh::Buffer::FillRect( + float w, float h, + const glm::vec4 &color, + const glm::vec2 &pivot +) { + Clear(); + Reserve(4, 6); + + vertices.emplace_back( -pivot.x, -pivot.y, 0.0f); + vertices.emplace_back(w-pivot.x, -pivot.y, 0.0f); + vertices.emplace_back( -pivot.x, h-pivot.y, 0.0f); + vertices.emplace_back(w-pivot.x, h-pivot.y, 0.0f); + + colors.resize(4, color); + + indices.assign({ 0, 2, 1, 1, 2, 3 }); +} + + +void PrimitiveMesh::Update(const Buffer &buf) noexcept { #ifndef NDEBUG if (buf.colors.size() < buf.vertices.size()) { - std::cerr << "OutlineMesh: not enough colors!" << std::endl; + std::cerr << "PrimitiveMesh: not enough colors!" << std::endl; } #endif @@ -86,12 +105,16 @@ void OutlineMesh::Update(const Buffer &buf) noexcept { } -void OutlineMesh::Draw() noexcept { +void PrimitiveMesh::DrawLines() noexcept { glEnable(GL_LINE_SMOOTH); glLineWidth(2.0f); vao.DrawLineElements(); } +void PrimitiveMesh::DrawTriangles() noexcept { + vao.DrawTriangleElements(); +} + void SkyBoxMesh::LoadUnitBox() { Buffer buffer; diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index d09d9c9..0c161f1 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -617,9 +617,9 @@ PlainColor::PlainColor() GL_VERTEX_SHADER, "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\n" - "layout(location = 1) in vec3 vtx_color;\n" + "layout(location = 1) in vec4 vtx_color;\n" "uniform mat4 MVP;\n" - "out vec3 frag_color;\n" + "out vec4 frag_color;\n" "void main() {\n" "gl_Position = MVP * vec4(vtx_position, 1);\n" "frag_color = vtx_color;\n" @@ -628,8 +628,8 @@ PlainColor::PlainColor() program.LoadShader( GL_FRAGMENT_SHADER, "#version 330 core\n" - "in vec3 frag_color;\n" - "out vec3 color;\n" + "in vec4 frag_color;\n" + "out vec4 color;\n" "void main() {\n" "color = frag_color;\n" "}\n" diff --git a/src/graphics/viewport.cpp b/src/graphics/viewport.cpp index e171966..f9558d0 100644 --- a/src/graphics/viewport.cpp +++ b/src/graphics/viewport.cpp @@ -219,22 +219,22 @@ DirectionalLighting &Viewport::HUDProgram() noexcept { return entity_prog; } -PlainColor &Viewport::WorldOutlineProgram() noexcept { - if (active_prog != OUTLINE_WORLD) { - outline_prog.Activate(); - outline_prog.SetVP(cam.View(), cam.Projection()); - active_prog = OUTLINE_WORLD; +PlainColor &Viewport::WorldColorProgram() noexcept { + if (active_prog != COLOR_WORLD) { + color_prog.Activate(); + color_prog.SetVP(cam.View(), cam.Projection()); + active_prog = COLOR_WORLD; } - return outline_prog; + return color_prog; } -PlainColor &Viewport::HUDOutlineProgram() noexcept { - if (active_prog != OUTLINE_HUD) { - outline_prog.Activate(); - outline_prog.SetVP(canv.View(), canv.Projection()); - active_prog = OUTLINE_HUD; +PlainColor &Viewport::HUDColorProgram() noexcept { + if (active_prog != COLOR_HUD) { + color_prog.Activate(); + color_prog.SetVP(canv.View(), canv.Projection()); + active_prog = COLOR_HUD; } - return outline_prog; + return color_prog; } SkyBoxShader &Viewport::SkyBoxProgram() noexcept { diff --git a/src/model/CollisionBounds.hpp b/src/model/CollisionBounds.hpp index 78eb0dd..e43b0d8 100644 --- a/src/model/CollisionBounds.hpp +++ b/src/model/CollisionBounds.hpp @@ -1,7 +1,7 @@ #ifndef BLANK_MODEL_COLLISIONBOUNDS_HPP_ #define BLANK_MODEL_COLLISIONBOUNDS_HPP_ -#include "../graphics/OutlineMesh.hpp" +#include "../graphics/PrimitiveMesh.hpp" #include @@ -19,7 +19,7 @@ struct CollisionBounds { std::size_t OutlineIndexCount() const { return out_idx.size(); } /// fill given buffers with these bounds' outline's elements - void Outline(OutlineMesh::Buffer &out) const; + void Outline(PrimitiveMesh::Buffer &out) const; /// Check if given ray would pass though this shape if it were /// transformed with given matrix. @@ -44,12 +44,12 @@ struct CollisionBounds { protected: void SetOutline( - const OutlineMesh::Positions &pos, - const OutlineMesh::Indices &idx); + const PrimitiveMesh::Positions &pos, + const PrimitiveMesh::Indices &idx); private: - OutlineMesh::Positions out_pos; - OutlineMesh::Indices out_idx; + PrimitiveMesh::Positions out_pos; + PrimitiveMesh::Indices out_idx; }; diff --git a/src/model/Shape.hpp b/src/model/Shape.hpp index 2f79f82..9170475 100644 --- a/src/model/Shape.hpp +++ b/src/model/Shape.hpp @@ -70,7 +70,7 @@ public: size_t OutlineCount() const noexcept; size_t OutlineIndexCount() const noexcept; - void Outline(OutlineMesh::Buffer &out) const; + void Outline(PrimitiveMesh::Buffer &out) const; bool Intersects( const Ray &, diff --git a/src/model/bounds.cpp b/src/model/bounds.cpp index c6cb386..0c91af3 100644 --- a/src/model/bounds.cpp +++ b/src/model/bounds.cpp @@ -4,14 +4,14 @@ namespace blank { -void CollisionBounds::Outline(OutlineMesh::Buffer &out) const { +void CollisionBounds::Outline(PrimitiveMesh::Buffer &out) const { out.vertices.insert(out.vertices.end(), out_pos.begin(), out_pos.end()); out.indices.insert(out.indices.end(), out_idx.begin(), out_idx.end()); } void CollisionBounds::SetOutline( - const OutlineMesh::Positions &pos, - const OutlineMesh::Indices &idx + const PrimitiveMesh::Positions &pos, + const PrimitiveMesh::Indices &idx ) { out_pos = pos; out_idx = idx; diff --git a/src/model/shape.cpp b/src/model/shape.cpp index 4943e63..a5e728a 100644 --- a/src/model/shape.cpp +++ b/src/model/shape.cpp @@ -187,7 +187,7 @@ size_t Shape::OutlineIndexCount() const noexcept { } } -void Shape::Outline(OutlineMesh::Buffer &out) const { +void Shape::Outline(PrimitiveMesh::Buffer &out) const { if (bounds) { bounds->Outline(out); } diff --git a/src/ui/HUD.hpp b/src/ui/HUD.hpp index 32a167b..cab8f93 100644 --- a/src/ui/HUD.hpp +++ b/src/ui/HUD.hpp @@ -4,7 +4,7 @@ #include "FixedText.hpp" #include "MessageBox.hpp" #include "../graphics/EntityMesh.hpp" -#include "../graphics/OutlineMesh.hpp" +#include "../graphics/PrimitiveMesh.hpp" #include @@ -57,7 +57,7 @@ private: const Player &player; // block focus - OutlineMesh outline; + PrimitiveMesh outline; glm::mat4 outline_transform; bool outline_visible; @@ -82,7 +82,7 @@ private: IntervalTimer msg_timer; // crosshair - OutlineMesh crosshair; + PrimitiveMesh crosshair; }; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 43cdb53..e7e6b0c 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -252,21 +252,21 @@ HUD::HUD(Environment &env, Config &config, const Player &player) messages.Background(glm::vec4(0.5f)); // crosshair - OutlineMesh::Buffer buf; + PrimitiveMesh::Buffer buf; buf.vertices = std::vector({ { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f }, { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f }, }); - buf.indices = std::vector({ + buf.indices = std::vector({ 0, 1, 2, 3 }); - buf.colors.resize(4, { 10.0f, 10.0f, 10.0f }); + buf.colors.resize(4, { 10.0f, 10.0f, 10.0f, 1.0f }); crosshair.Update(buf); } namespace { -OutlineMesh::Buffer outl_buf; +PrimitiveMesh::Buffer outl_buf; } @@ -274,7 +274,7 @@ void HUD::FocusBlock(const Chunk &chunk, int index) { const Block &block = chunk.BlockAt(index); const BlockType &type = chunk.Type(index); outl_buf.Clear(); - type.FillOutlineMesh(outl_buf); + type.OutlinePrimitiveMesh(outl_buf); outline.Update(outl_buf); outline_transform = chunk.Transform(player.GetEntity().ChunkCoords()); outline_transform *= chunk.ToTransform(Chunk::ToPos(index), index); @@ -377,9 +377,9 @@ void HUD::Update(int dt) { void HUD::Render(Viewport &viewport) noexcept { // block focus if (outline_visible && config.video.world) { - PlainColor &outline_prog = viewport.WorldOutlineProgram(); + PlainColor &outline_prog = viewport.WorldColorProgram(); outline_prog.SetM(outline_transform); - outline.Draw(); + outline.DrawLines(); } // clear depth buffer so everything renders above the world @@ -405,11 +405,11 @@ void HUD::Render(Viewport &viewport) noexcept { } // crosshair - PlainColor &outline_prog = viewport.HUDOutlineProgram(); + PlainColor &outline_prog = viewport.HUDColorProgram(); viewport.EnableInvertBlending(); viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER); outline_prog.SetM(viewport.Cursor()); - crosshair.Draw(); + crosshair.DrawLines(); } // debug overlay diff --git a/src/world/BlockType.hpp b/src/world/BlockType.hpp index b7861e1..b50169c 100644 --- a/src/world/BlockType.hpp +++ b/src/world/BlockType.hpp @@ -4,7 +4,7 @@ #include "Block.hpp" #include "../graphics/BlockMesh.hpp" #include "../graphics/EntityMesh.hpp" -#include "../graphics/OutlineMesh.hpp" +#include "../graphics/PrimitiveMesh.hpp" #include "../model/Shape.hpp" #include @@ -79,7 +79,7 @@ struct BlockType { const glm::mat4 &transform = glm::mat4(1.0f), BlockMesh::Index idx_offset = 0 ) const noexcept; - void FillOutlineMesh(OutlineMesh::Buffer &m) const noexcept; + void OutlinePrimitiveMesh(PrimitiveMesh::Buffer &) const noexcept; }; diff --git a/src/world/block.cpp b/src/world/block.cpp index 5059a99..7050da4 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -119,10 +119,10 @@ void BlockType::FillBlockMesh( buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod); } -void BlockType::FillOutlineMesh(OutlineMesh::Buffer &buf) const noexcept { +void BlockType::OutlinePrimitiveMesh(PrimitiveMesh::Buffer &buf) const noexcept { if (!shape) return; shape->Outline(buf); - buf.colors.insert(buf.colors.end(), shape->OutlineCount(), outline_color); + buf.colors.insert(buf.colors.end(), shape->OutlineCount(), glm::vec4(outline_color, 1.0f)); } -- 2.39.2