From 2d8c7c015478a4528c0909f11d43998b1393948d Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 23 Feb 2015 22:06:07 +0100 Subject: [PATCH] outline pointed-at block --- Makefile | 6 ++++ src/app.cpp | 34 ++++++++++++++++-- src/app.hpp | 5 +++ src/model.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/model.hpp | 36 +++++++++++++++++++ 5 files changed, 174 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 346b849..20b02e9 100644 --- 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) diff --git a/src/app.cpp b/src/app.cpp index 70c45d4..c418660 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -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({ + { 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(); } diff --git a/src/app.hpp b/src/app.hpp index e5cddce..8c6d792 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -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; diff --git a/src/model.cpp b/src/model.cpp index 032fc15..b50ccf0 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1,5 +1,7 @@ #include "model.hpp" +#include + 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); +} + } diff --git a/src/model.hpp b/src/model.hpp index 1e9dab9..4fd4515 100644 --- a/src/model.hpp +++ b/src/model.hpp @@ -45,6 +45,42 @@ private: }; + +class OutlineModel { + +public: + std::vector vertices; + std::vector 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 -- 2.39.2