]> git.localhorst.tv Git - blank.git/commitdiff
renamed OutlineMesh -> PrimitiveMesh
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 20 Oct 2015 09:40:07 +0000 (11:40 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 20 Oct 2015 10:05:48 +0000 (12:05 +0200)
now it can be used for lines and surfaces

and with alpha blending

14 files changed:
src/graphics/OutlineMesh.hpp [deleted file]
src/graphics/PrimitiveMesh.hpp [new file with mode: 0644]
src/graphics/Viewport.hpp
src/graphics/mesh.cpp
src/graphics/shader.cpp
src/graphics/viewport.cpp
src/model/CollisionBounds.hpp
src/model/Shape.hpp
src/model/bounds.cpp
src/model/shape.cpp
src/ui/HUD.hpp
src/ui/ui.cpp
src/world/BlockType.hpp
src/world/block.cpp

diff --git a/src/graphics/OutlineMesh.hpp b/src/graphics/OutlineMesh.hpp
deleted file mode 100644 (file)
index 77e0436..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-#ifndef BLANK_GRAPHICS_OUTLINEMESH_HPP_
-#define BLANK_GRAPHICS_OUTLINEMESH_HPP_
-
-#include "VertexArray.hpp"
-
-#include <vector>
-#include <GL/glew.h>
-#include <glm/glm.hpp>
-
-
-namespace blank {
-
-class OutlineMesh {
-
-public:
-       using Position = glm::vec3;
-       using Color = glm::vec3;
-       using Index = unsigned short;
-
-       using Positions = std::vector<Position>;
-       using Colors = std::vector<Color>;
-       using Indices = std::vector<Index>;
-
-       enum Attribute {
-               ATTRIB_VERTEX,
-               ATTRIB_COLOR,
-               ATTRIB_INDEX,
-               ATTRIB_COUNT,
-       };
-
-       struct Buffer {
-
-               Positions vertices;
-               Colors colors;
-               Indices indices;
-
-               void Clear() noexcept {
-                       vertices.clear();
-                       colors.clear();
-                       indices.clear();
-               }
-
-               void Reserve(size_t p, size_t i) {
-                       vertices.reserve(p);
-                       colors.reserve(p);
-                       indices.reserve(i);
-               }
-
-       };
-
-       using VAO = VertexArray<ATTRIB_COUNT>;
-
-public:
-       void Update(const Buffer &) noexcept;
-
-       void Draw() noexcept;
-
-private:
-       VAO vao;
-
-};
-
-}
-
-#endif
diff --git a/src/graphics/PrimitiveMesh.hpp b/src/graphics/PrimitiveMesh.hpp
new file mode 100644 (file)
index 0000000..1aa3cce
--- /dev/null
@@ -0,0 +1,72 @@
+#ifndef BLANK_GRAPHICS_PRIMITIVEMESH_HPP_
+#define BLANK_GRAPHICS_PRIMITIVEMESH_HPP_
+
+#include "VertexArray.hpp"
+
+#include <vector>
+#include <GL/glew.h>
+#include <glm/glm.hpp>
+
+
+namespace blank {
+
+class PrimitiveMesh {
+
+public:
+       using Position = glm::vec3;
+       using Color = glm::vec4;
+       using Index = unsigned short;
+
+       using Positions = std::vector<Position>;
+       using Colors = std::vector<Color>;
+       using Indices = std::vector<Index>;
+
+       enum Attribute {
+               ATTRIB_VERTEX,
+               ATTRIB_COLOR,
+               ATTRIB_INDEX,
+               ATTRIB_COUNT,
+       };
+
+       struct Buffer {
+
+               Positions vertices;
+               Colors colors;
+               Indices indices;
+
+               void Clear() noexcept {
+                       vertices.clear();
+                       colors.clear();
+                       indices.clear();
+               }
+
+               void Reserve(size_t p, size_t i) {
+                       vertices.reserve(p);
+                       colors.reserve(p);
+                       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<ATTRIB_COUNT>;
+
+public:
+       void Update(const Buffer &) noexcept;
+
+       void DrawLines() noexcept;
+       void DrawTriangles() noexcept;
+
+private:
+       VAO vao;
+
+};
+
+}
+
+#endif
index 03e0e0d05c247420387bdc5fc1c851a71b3f992b..a8cb69f0947f70cee14b6fc20df6f544b5114b1b 100644 (file)
@@ -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;
index 4b0f5829d73ed552ff9fc8c4998a77b785425379..3aa631eb77e5e266f156d9516e75760ed92f5c28 100644 (file)
@@ -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;
index d09d9c94ac5d10b97b4e2d3acf7b54caeaca0aeb..0c161f17d7b1308aca1c3ab15c5380b420c1711c 100644 (file)
@@ -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"
index e1719668eab185b4bf27b200de5d8d45d5a99f08..f9558d036865de4e3b388e5e22a96b1d1a8536d8 100644 (file)
@@ -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 {
index 78eb0dda77b6a8a3b55536f2e68de6eedce0e2e3..e43b0d82e66e69ae624cbfbfbbba9e540ea8f770 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef BLANK_MODEL_COLLISIONBOUNDS_HPP_
 #define BLANK_MODEL_COLLISIONBOUNDS_HPP_
 
-#include "../graphics/OutlineMesh.hpp"
+#include "../graphics/PrimitiveMesh.hpp"
 
 #include <glm/glm.hpp>
 
@@ -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;
 
 };
 
index 2f79f82b941459592cb0affbb44734191055f2e9..9170475b2f6dfc5cd670bec8327368a2a521c1f2 100644 (file)
@@ -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 &,
index c6cb3869f46b7cf97416937e723530fd4768a64d..0c91af32021f39d918099e5862c01080d544bace 100644 (file)
@@ -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;
index 4943e6377c3525d0c21b0144f7a3384f74dd8f76..a5e728a398777d744935ee5a6c2f3e2863c12fff 100644 (file)
@@ -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);
        }
index 32a167b865d2979209f763a1e12341280f3b2f28..cab8f93e0bf13c26cccb69a6a3a4d48b6ac28fa9 100644 (file)
@@ -4,7 +4,7 @@
 #include "FixedText.hpp"
 #include "MessageBox.hpp"
 #include "../graphics/EntityMesh.hpp"
-#include "../graphics/OutlineMesh.hpp"
+#include "../graphics/PrimitiveMesh.hpp"
 
 #include <glm/glm.hpp>
 
@@ -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;
 
 };
 
index 43cdb53613ec2660603204128f9fffc91cf24839..e7e6b0c7725a36264222a08633119d4f443ba6d2 100644 (file)
@@ -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<glm::vec3>({
                { -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<OutlineMesh::Index>({
+       buf.indices = std::vector<PrimitiveMesh::Index>({
                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
index b7861e1dd24281088bdab5cbde4d594dff05b489..b50169c53ee72ac033e5c1eac09b1eabd551a9cd 100644 (file)
@@ -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 <glm/glm.hpp>
@@ -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;
 
 };
 
index 5059a999ca85d5e0010dd2f3b1a3ebe4830c8170..7050da4cc115532ad643b6d9d40a6a01edbdf5ef 100644 (file)
@@ -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));
 }