]> git.localhorst.tv Git - blank.git/commitdiff
outline pointed-at block
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 23 Feb 2015 21:06:07 +0000 (22:06 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 23 Feb 2015 21:06:07 +0000 (22:06 +0100)
Makefile
src/app.cpp
src/app.hpp
src/model.cpp
src/model.hpp

index 346b8490d16f37e1faa0fb4b1723e9cfe814e430..20b02e9a8f2e025aaacba01869174ad369be1a38 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,12 @@ CXXFLAGS += -Wall
 LDXXFLAGS ?=
 LDXXFLAGS += $(PKGLIBS)
 
+# debug
+CXXFLAGS += -g3 -O0
+
+# release
+#CPPFLAGS += -DNDEBUG
+
 SRC = $(wildcard src/*.cpp)
 OBJ = $(SRC:.cpp=.o)
 DEP = $(SRC:.cpp=.d)
index 70c45d478c7284ffdf42866ed6900049c082771d..c418660ec48d209be4fef8ba2074c3aa1bb192ee 100644 (file)
@@ -22,6 +22,9 @@ Application::Application()
 , blockType()
 , cam()
 , chunk()
+, outline()
+, outline_visible(false)
+, outline_transform(1.0f)
 , light_position(17.0f, 17.0f, 17.0f)
 , light_color(1.0f, 1.0f, 1.0f)
 , light_power(250.0f)
@@ -132,6 +135,23 @@ Application::Application()
        chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
        chunk.Invalidate();
 
+       outline.vertices = std::vector<glm::vec3>({
+               { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f },
+               { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f },
+               { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f },
+               { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f },
+               { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f },
+               { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 1.0f },
+               { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f },
+               { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f },
+               { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f },
+               { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f },
+               { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f },
+               { 0.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f },
+       });
+       outline.colors.resize(24, { -1, -1, -1 });
+       outline.Invalidate();
+
        m_handle = program.UniformLocation("M");
        v_handle = program.UniformLocation("V");
        mv_handle = program.UniformLocation("MV");
@@ -242,10 +262,10 @@ void Application::Update(int dt) {
        float dist;
        if (chunk.Intersection(aim, glm::mat4(1.0f), &blkid, &dist)) {
                glm::vec3 pos = Chunk::ToCoords(blkid);
-               std::cout << "pointing at: <" << pos.x << ", " << pos.y << ", " << pos.z << ">, "
-                       "distance: " << dist << std::endl;
+               outline_visible = true;
+               outline_transform = glm::translate(glm::mat4(1.0f), pos);
        } else {
-               std::cout << "pointing at: nothing" << std::endl;
+               outline_visible = false;
        }
 }
 
@@ -266,6 +286,14 @@ void Application::Render() {
        glUniform1f(light_power_handle, light_power);
 
        chunk.Draw();
+       if (outline_visible) {
+               mv = cam.View() * outline_transform;
+               mvp = cam.MakeMVP(outline_transform);
+               glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
+               glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
+               glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
+               outline.Draw();
+       }
 
        window.Flip();
 }
index e5cddcedadb7a722de3cf1c075cc9ce31f6408be..8c6d792bdd48f63041b9068a44c303737be8fd28 100644 (file)
@@ -7,6 +7,7 @@
 #include "camera.hpp"
 #include "controller.hpp"
 #include "init.hpp"
+#include "model.hpp"
 #include "shader.hpp"
 #include "world.hpp"
 
@@ -46,6 +47,10 @@ private:
 
        Camera cam;
        Chunk chunk;
+       OutlineModel outline;
+
+       bool outline_visible;
+       glm::mat4 outline_transform;
 
        glm::vec3 light_position;
        glm::vec3 light_color;
index 032fc150a9e449c12bc68d62c98609280fee09a8..b50ccf0b168888ce0dff21e7d7423cc643c4cc01 100644 (file)
@@ -1,5 +1,7 @@
 #include "model.hpp"
 
+#include <iostream>
+
 
 namespace blank {
 
@@ -35,9 +37,21 @@ void Model::Update() {
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
 
+#ifndef NDEBUG
+       if (colors.size() < vertices.size()) {
+               std::cerr << "Model: not enough colors!" << std::endl;
+               colors.resize(vertices.size(), { 1, 0, 1 });
+       }
+#endif
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
        glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
 
+#ifndef NDEBUG
+       if (normals.size() < vertices.size()) {
+               std::cerr << "Model: not enough normals!" << std::endl;
+               normals.resize(vertices.size(), { 0, 1, 0 });
+       }
+#endif
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
        glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW);
 
@@ -94,4 +108,86 @@ void Model::Draw() {
        glDisableVertexAttribArray(ATTRIB_VERTEX);
 }
 
+
+OutlineModel::OutlineModel()
+: vertices()
+, colors()
+, handle{}
+, dirty(false) {
+       glGenBuffers(ATTRIB_COUNT, handle);
+}
+
+OutlineModel::~OutlineModel() {
+       glDeleteBuffers(ATTRIB_COUNT, handle);
+}
+
+
+void OutlineModel::Clear() {
+       vertices.clear();
+       colors.clear();
+       Invalidate();
+}
+
+void OutlineModel::Reserve(int s) {
+       vertices.reserve(s);
+       colors.reserve(s);
+}
+
+
+void OutlineModel::Update() {
+       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
+       glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
+
+#ifndef NDEBUG
+       if (colors.size() < vertices.size()) {
+               std::cerr << "OutlineModel: not enough colors!" << std::endl;
+               colors.resize(vertices.size(), { 1, 0, 1 });
+       }
+#endif
+       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
+       glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
+
+       dirty = false;
+}
+
+
+void OutlineModel::Draw() {
+       if (dirty) {
+               Update();
+       }
+
+       glEnableVertexAttribArray(ATTRIB_VERTEX);
+       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
+       glVertexAttribPointer(
+               ATTRIB_VERTEX, // location (for shader)
+               3,             // size
+               GL_FLOAT,      // type
+               GL_FALSE,      // normalized
+               0,             // stride
+               nullptr        // offset
+       );
+
+       glEnableVertexAttribArray(ATTRIB_COLOR);
+       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
+       glVertexAttribPointer(
+               ATTRIB_COLOR,  // location (for shader)
+               3,             // size
+               GL_FLOAT,      // type
+               GL_FALSE,      // normalized
+               0,             // stride
+               nullptr        // offset
+       );
+
+       glLineWidth(2.0f);
+
+       glDrawArrays(
+               GL_LINES,       // how
+               0,              // start
+               vertices.size() // len
+       );
+
+       glDisableVertexAttribArray(ATTRIB_COLOR);
+       glDisableVertexAttribArray(ATTRIB_VERTEX);
+}
+
 }
index 1e9dab9d1cde0132c30c1cdad9d656c3e5a47a7a..4fd4515563022215e0ca8e2f847813b4610b501f 100644 (file)
@@ -45,6 +45,42 @@ private:
 
 };
 
+
+class OutlineModel {
+
+public:
+       std::vector<glm::vec3> vertices;
+       std::vector<glm::vec3> colors;
+
+public:
+       OutlineModel();
+       ~OutlineModel();
+
+       OutlineModel(const OutlineModel &) = delete;
+       OutlineModel &operator =(const OutlineModel &) = delete;
+
+       void Invalidate() { dirty = true; }
+
+       void Clear();
+       void Reserve(int);
+
+       void Draw();
+
+private:
+       void Update();
+
+private:
+       enum Attribute {
+               ATTRIB_VERTEX,
+               ATTRIB_COLOR,
+               ATTRIB_COUNT,
+       };
+
+       GLuint handle[ATTRIB_COUNT];
+       bool dirty;
+
+};
+
 }
 
 #endif