]> git.localhorst.tv Git - blank.git/commitdiff
dedicated shader for outlines
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 13 Aug 2015 16:08:22 +0000 (18:08 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 13 Aug 2015 18:21:12 +0000 (20:21 +0200)
TODO
src/graphics/PlainColor.hpp [new file with mode: 0644]
src/graphics/Viewport.hpp
src/graphics/shader.cpp
src/graphics/viewport.cpp
src/ui/ui.cpp

diff --git a/TODO b/TODO
index fffc6dedce77407f8db9a4f82884d05ec82bc8a4..9bd2aaa629f35a60f8122b5b88c218a8bc444876 100644 (file)
--- a/TODO
+++ b/TODO
@@ -8,10 +8,6 @@ composite entity animations
        complex entities are made up of part which have their own local
        transform that can be animated (like e.g. an arm or head)
 
-textures
-
-       okay, now I need a better solution for the crosshair ^^
-
 font rendering
 
        with background now being a thing, a padding might be nice
diff --git a/src/graphics/PlainColor.hpp b/src/graphics/PlainColor.hpp
new file mode 100644 (file)
index 0000000..cfaaebe
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef BLANK_GRAPHICS_PLAINCOLOR_HPP_
+#define BLANK_GRAPHICS_PLAINCOLOR_HPP_
+
+#include "Program.hpp"
+
+#include <GL/glew.h>
+#include <glm/glm.hpp>
+
+
+namespace blank {
+
+class PlainColor {
+
+public:
+       PlainColor();
+
+       void Activate() noexcept;
+
+       void SetM(const glm::mat4 &m) noexcept;
+       void SetProjection(const glm::mat4 &p) noexcept;
+       void SetView(const glm::mat4 &v) noexcept;
+       void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept;
+       void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept;
+
+       const glm::mat4 &Projection() const noexcept { return projection; }
+       const glm::mat4 &View() const noexcept { return view; }
+       const glm::mat4 &GetVP() const noexcept { return vp; }
+
+private:
+       Program program;
+
+       glm::mat4 projection;
+       glm::mat4 view;
+       glm::mat4 vp;
+
+       GLuint mvp_handle;
+
+};
+
+}
+
+#endif
index 1995ce6c3f0342b7319e107164f8f42a07f363fb..4d55d8893dadbe289bd3a9c835170c1709ba4906 100644 (file)
@@ -7,6 +7,7 @@
 #include "Camera.hpp"
 #include "Canvas.hpp"
 #include "DirectionalLighting.hpp"
+#include "PlainColor.hpp"
 
 #include <glm/glm.hpp>
 
@@ -49,6 +50,8 @@ public:
        BlockLighting &ChunkProgram() noexcept;
        DirectionalLighting &EntityProgram() noexcept;
        DirectionalLighting &HUDProgram() noexcept;
+       PlainColor &WorldOutlineProgram() noexcept;
+       PlainColor &HUDOutlineProgram() noexcept;
        BlendedSprite &SpriteProgram() noexcept;
 
        void WorldPosition(const glm::mat4 &) noexcept;
@@ -64,6 +67,7 @@ private:
 
        BlockLighting chunk_prog;
        DirectionalLighting entity_prog;
+       PlainColor outline_prog;
        BlendedSprite sprite_prog;
 
        enum {
@@ -71,6 +75,8 @@ private:
                CHUNK,
                ENTITY,
                HUD,
+               OUTLINE_WORLD,
+               OUTLINE_HUD,
                SPRITE,
        } active_prog;
 
index 938dc9772b1d4a5cd05fe4830846c897a78a77dd..f04b46f5f1eba30ca828d3fc6c92d4dbe5a73d94 100644 (file)
@@ -1,6 +1,7 @@
 #include "BlendedSprite.hpp"
 #include "BlockLighting.hpp"
 #include "DirectionalLighting.hpp"
+#include "PlainColor.hpp"
 #include "Program.hpp"
 #include "Shader.hpp"
 
@@ -492,4 +493,69 @@ void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
        program.Uniform(bg_handle, v);
 }
 
+
+PlainColor::PlainColor()
+: program()
+, vp(1.0f)
+, mvp_handle(0) {
+       program.LoadShader(
+               GL_VERTEX_SHADER,
+               "#version 330 core\n"
+               "layout(location = 0) in vec3 vtx_position;\n"
+               "layout(location = 1) in vec3 vtx_color;\n"
+               "uniform mat4 MVP;\n"
+               "out vec3 frag_color;\n"
+               "void main() {\n"
+                       "gl_Position = MVP * vec4(vtx_position, 1);\n"
+                       "frag_color = vtx_color;\n"
+               "}\n"
+       );
+       program.LoadShader(
+               GL_FRAGMENT_SHADER,
+               "#version 330 core\n"
+               "in vec3 frag_color;\n"
+               "out vec3 color;\n"
+               "void main() {\n"
+                       "color = frag_color;\n"
+               "}\n"
+       );
+       program.Link();
+       if (!program.Linked()) {
+               program.Log(std::cerr);
+               throw std::runtime_error("link program");
+       }
+
+       mvp_handle = program.UniformLocation("MVP");
+}
+
+
+void PlainColor::Activate() noexcept {
+       program.Use();
+}
+
+void PlainColor::SetM(const glm::mat4 &m) noexcept {
+       program.Uniform(mvp_handle, vp * m);
+}
+
+void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
+       projection = p;
+       vp = p * view;
+}
+
+void PlainColor::SetView(const glm::mat4 &v) noexcept {
+       view = v;
+       vp = projection * v;
+}
+
+void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
+       projection = p;
+       view = v;
+       vp = p * v;
+}
+
+void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
+       SetVP(v, p);
+       SetM(m);
+}
+
 }
index e6ac76810b1c39a33c0008e9a2434621a3f93c0d..48bd5ee32ce017751f436a25a8228a51f5703059 100644 (file)
@@ -200,6 +200,24 @@ 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;
+       }
+       return outline_prog;
+}
+
+PlainColor &Viewport::HUDOutlineProgram() noexcept {
+       if (active_prog != OUTLINE_HUD) {
+               outline_prog.Activate();
+               outline_prog.SetVP(canv.View(), canv.Projection());
+               active_prog = OUTLINE_HUD;
+       }
+       return outline_prog;
+}
+
 BlendedSprite &Viewport::SpriteProgram() noexcept {
        if (active_prog != SPRITE) {
                sprite_prog.Activate();
index be03c7e88f22fe7d202440a208b91b3eb2d7d4a9..f9836ba65a6af4035d7b95e8934c27eebcdced2c 100644 (file)
@@ -72,17 +72,18 @@ void HUD::Display(const Block &b) {
 void HUD::Render(Viewport &viewport) noexcept {
        viewport.ClearDepth();
 
-       DirectionalLighting &world_prog = viewport.HUDProgram();
-       world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
-       // disable distance fog
-       world_prog.SetFogDensity(0.0f);
-
+       PlainColor &outline_prog = viewport.HUDOutlineProgram();
        viewport.EnableInvertBlending();
        viewport.SetCursor(glm::vec3(0.0f), Gravity::CENTER);
-       world_prog.SetM(viewport.Cursor());
+       outline_prog.SetM(viewport.Cursor());
        crosshair.Draw();
 
        if (block_visible) {
+               DirectionalLighting &world_prog = viewport.HUDProgram();
+               world_prog.SetLightDirection({ 1.0f, 3.0f, 5.0f });
+               // disable distance fog
+               world_prog.SetFogDensity(0.0f);
+
                viewport.DisableBlending();
                world_prog.SetM(block_transform);
                block.Draw();
@@ -511,9 +512,9 @@ void Interface::CheckAim() {
                outl_buf.Clear();
                aim_chunk->Type(aim_chunk->BlockAt(aim_block)).FillOutlineModel(outl_buf);
                outline.Update(outl_buf);
-               outline_transform = glm::scale(glm::vec3(1.0002f));
-               outline_transform *= aim_chunk->Transform(world.Player().ChunkCoords());
+               outline_transform = aim_chunk->Transform(world.Player().ChunkCoords());
                outline_transform *= aim_chunk->ToTransform(Chunk::ToPos(aim_block), aim_block);
+               outline_transform *= glm::scale(glm::vec3(1.005f));
        } else {
                aim_chunk = nullptr;
        }
@@ -524,8 +525,8 @@ void Interface::Render(Viewport &viewport) noexcept {
        if (config.visual_disabled) return;
 
        if (aim_chunk) {
-               DirectionalLighting &world_prog = viewport.EntityProgram();
-               world_prog.SetM(outline_transform);
+               PlainColor &outline_prog = viewport.WorldOutlineProgram();
+               outline_prog.SetM(outline_transform);
                outline.Draw();
        }