From 15ff5ed48855c6bd09bc8f5152f46065484c1e94 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 26 May 2015 18:44:24 +0200 Subject: [PATCH] don't push block normals to GPU --- src/block.cpp | 4 ++-- src/chunk.cpp | 8 ++++++-- src/chunk.hpp | 2 +- src/model.cpp | 17 ----------------- src/model.hpp | 6 ------ src/shader.cpp | 15 +++++---------- src/shader.hpp | 2 +- src/shape.cpp | 26 ++++++++++++++++++++------ src/shape.hpp | 19 +++++++++++++++---- 9 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/block.cpp b/src/block.cpp index 6e6ac18..dea5267 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -61,7 +61,7 @@ BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s) namespace { const Block::Face face_map[Block::FACE_COUNT * Block::TURN_COUNT][Block::FACE_COUNT] = { - { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_FRONT, Block::FACE_BACK, }, // face: up, turn: none x + { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_RIGHT, Block::FACE_LEFT, Block::FACE_FRONT, Block::FACE_BACK, }, // face: up, turn: none { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_FRONT, Block::FACE_BACK, Block::FACE_LEFT, Block::FACE_RIGHT, }, // face: up, turn: left { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_LEFT, Block::FACE_RIGHT, Block::FACE_BACK, Block::FACE_FRONT, }, // face: up, turn: around { Block::FACE_UP, Block::FACE_DOWN, Block::FACE_BACK, Block::FACE_FRONT, Block::FACE_RIGHT, Block::FACE_LEFT, }, // face: up, turn: right @@ -107,7 +107,7 @@ void BlockType::FillBlockModel( const glm::mat4 &transform, BlockModel::Index idx_offset ) const { - shape->Vertices(buf.vertices, buf.normals, buf.indices, transform, idx_offset); + shape->Vertices(buf.vertices, buf.indices, transform, idx_offset); buf.colors.insert(buf.colors.end(), shape->VertexCount(), color); } diff --git a/src/chunk.cpp b/src/chunk.cpp index 2a764f3..74ca1bc 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -322,7 +322,7 @@ int Chunk::GetLight(int index) const { return light[index]; } -float Chunk::GetVertexLight(int index, const BlockModel::Position &vtx, const BlockModel::Normal &norm) const { +float Chunk::GetVertexLight(int index, const BlockModel::Position &vtx, const Model::Normal &norm) const { float light = GetLight(index); Chunk::Pos pos(ToPos(index)); @@ -452,7 +452,11 @@ void Chunk::Update() { vtx_counter += type.shape->VertexCount(); for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) { - buf.lights.emplace_back(GetVertexLight(i, buf.vertices[vtx], buf.normals[vtx])); + buf.lights.emplace_back(GetVertexLight( + i, + buf.vertices[vtx], + type.shape->VertexNormal(vtx - vtx_begin, blocks[i].Transform()) + )); } } diff --git a/src/chunk.hpp b/src/chunk.hpp index 4f6732d..4addb56 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -111,7 +111,7 @@ public: int GetLight(const Pos &pos) const { return GetLight(ToIndex(pos)); } int GetLight(const Block::Pos &pos) const { return GetLight(ToIndex(pos)); } - float GetVertexLight(int index, const BlockModel::Position &, const BlockModel::Normal &) const; + float GetVertexLight(int index, const BlockModel::Position &, const Model::Normal &) const; bool Intersection( const Ray &ray, diff --git a/src/model.cpp b/src/model.cpp index 125d57a..62a90e5 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -166,23 +166,6 @@ void BlockModel::Update(const Buffer &buf) { nullptr // offset ); -#ifndef NDEBUG - if (buf.normals.size() < buf.vertices.size()) { - std::cerr << "BlockModel: not enough normals!" << std::endl; - } -#endif - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]); - glBufferData(GL_ARRAY_BUFFER, buf.normals.size() * sizeof(glm::vec3), buf.normals.data(), GL_STATIC_DRAW); - glEnableVertexAttribArray(ATTRIB_NORMAL); - glVertexAttribPointer( - ATTRIB_NORMAL, // location (for shader) - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized - 0, // stride - nullptr // offset - ); - #ifndef NDEBUG if (buf.lights.size() < buf.vertices.size()) { std::cerr << "BlockModel: not enough lights!" << std::endl; diff --git a/src/model.hpp b/src/model.hpp index 7b9fc3d..557b4ac 100644 --- a/src/model.hpp +++ b/src/model.hpp @@ -80,13 +80,11 @@ class BlockModel { public: using Position = glm::vec3; using Color = glm::vec3; - using Normal = glm::vec3; using Light = float; using Index = unsigned int; using Positions = std::vector; using Colors = std::vector; - using Normals = std::vector; using Lights = std::vector; using Indices = std::vector; @@ -95,14 +93,12 @@ public: Positions vertices; Colors colors; - Normals normals; Lights lights; Indices indices; void Clear() { vertices.clear(); colors.clear(); - normals.clear(); lights.clear(); indices.clear(); } @@ -110,7 +106,6 @@ public: void Reserve(size_t p, size_t i) { vertices.reserve(p); colors.reserve(p); - normals.reserve(p); lights.reserve(p); indices.reserve(i); } @@ -135,7 +130,6 @@ private: enum Attribute { ATTRIB_VERTEX, ATTRIB_COLOR, - ATTRIB_NORMAL, ATTRIB_LIGHT, ATTRIB_INDEX, ATTRIB_COUNT, diff --git a/src/shader.cpp b/src/shader.cpp index 134f710..01f7861 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -132,6 +132,10 @@ void Program::Log(std::ostream &out) const { } +GLint Program::AttributeLocation(const GLchar *name) const { + return glGetAttribLocation(handle, name); +} + GLint Program::UniformLocation(const GLchar *name) const { return glGetUniformLocation(handle, name); } @@ -257,7 +261,6 @@ void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const g BlockLighting::BlockLighting() : program() , vp(1.0f) -, m_handle(0) , mv_handle(0) , mvp_handle(0) , fog_density_handle(0) { @@ -266,20 +269,16 @@ BlockLighting::BlockLighting() "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\n" "layout(location = 1) in vec3 vtx_color;\n" - "layout(location = 2) in vec3 vtx_normal;\n" - "layout(location = 3) in float vtx_light;\n" - "uniform mat4 M;\n" + "layout(location = 2) in float vtx_light;\n" "uniform mat4 MV;\n" "uniform mat4 MVP;\n" "out vec3 frag_color;\n" "out vec3 vtx_viewspace;\n" - "out vec3 normal;\n" "out float frag_light;\n" "void main() {\n" "gl_Position = MVP * vec4(vtx_position, 1);\n" "frag_color = vtx_color;\n" "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" - "normal = (M * vec4(vtx_normal, 0)).xyz;\n" "frag_light = vtx_light;\n" "}\n" ); @@ -294,8 +293,6 @@ BlockLighting::BlockLighting() "void main() {\n" "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n" "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n" - //"float light_power = clamp(frag_light / 15, 0, 1);\n" - // this should be the same as the clear color, otherwise looks really weird "vec3 fog_color = vec3(0, 0, 0);\n" "float e = 2.718281828;\n" //"vec3 reflect_color = ambient + frag_color * light_power;\n" @@ -310,7 +307,6 @@ BlockLighting::BlockLighting() throw std::runtime_error("link program"); } - m_handle = program.UniformLocation("M"); mv_handle = program.UniformLocation("MV"); mvp_handle = program.UniformLocation("MVP"); fog_density_handle = program.UniformLocation("fog_density"); @@ -326,7 +322,6 @@ void BlockLighting::Activate() { void BlockLighting::SetM(const glm::mat4 &m) { glm::mat4 mv(view * m); glm::mat4 mvp(vp * m); - 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]); } diff --git a/src/shader.hpp b/src/shader.hpp index 873460b..bb9ad84 100644 --- a/src/shader.hpp +++ b/src/shader.hpp @@ -49,6 +49,7 @@ public: bool Linked() const; void Log(std::ostream &) const; + GLint AttributeLocation(const GLchar *name) const; GLint UniformLocation(const GLchar *name) const; void Use() const { glUseProgram(handle); } @@ -130,7 +131,6 @@ private: glm::mat4 view; glm::mat4 vp; - GLuint m_handle; GLuint mv_handle; GLuint mvp_handle; GLuint light_direction_handle; diff --git a/src/shape.cpp b/src/shape.cpp index 8111258..545a8e4 100644 --- a/src/shape.cpp +++ b/src/shape.cpp @@ -6,16 +6,16 @@ namespace blank { void Shape::Vertices( Model::Positions &vertex, Model::Normals &normal, - Model::Indices &index, - const Model::Position &elem_offset, - Model::Index idx_offset + Model::Indices &index ) const { for (const auto &pos : vtx_pos) { - vertex.emplace_back(elem_offset + pos); + vertex.emplace_back(pos); + } + for (const auto &nrm : vtx_nrm) { + normal.emplace_back(nrm); } - normal.insert(normal.end(), vtx_nrm.begin(), vtx_nrm.end()); for (auto idx : vtx_idx) { - index.emplace_back(idx_offset + idx); + index.emplace_back(idx); } } @@ -37,6 +37,20 @@ void Shape::Vertices( } } +void Shape::Vertices( + BlockModel::Positions &vertex, + BlockModel::Indices &index, + const glm::mat4 &transform, + BlockModel::Index idx_offset +) const { + for (const auto &pos : vtx_pos) { + vertex.emplace_back(transform * glm::vec4(pos, 1.0f)); + } + for (auto idx : vtx_idx) { + index.emplace_back(idx_offset + idx); + } +} + void Shape::Outline( OutlineModel::Positions &vertex, OutlineModel::Indices &index, diff --git a/src/shape.hpp b/src/shape.hpp index f31075b..abf8795 100644 --- a/src/shape.hpp +++ b/src/shape.hpp @@ -17,14 +17,19 @@ struct Shape { /// the number of vertex indices this shape has size_t VertexIndexCount() const { return vtx_idx.size(); } + const Model::Normal &VertexNormal(size_t idx) const { return vtx_nrm[idx]; } + Model::Normal VertexNormal( + size_t idx, const glm::mat4 &transform + ) const { + return Model::Normal(transform * glm::vec4(vtx_nrm[idx], 0.0f)); + } + /// fill given buffers with this shape's elements with an - /// optional offset + /// optional transform and offset void Vertices( Model::Positions &vertex, Model::Normals &normal, - Model::Indices &index, - const Model::Position &elem_offset = { 0.0f, 0.0f, 0.0f }, - Model::Index idx_offset = 0 + Model::Indices &index ) const; void Vertices( Model::Positions &vertex, @@ -33,6 +38,12 @@ struct Shape { const glm::mat4 &transform, Model::Index idx_offset = 0 ) const; + void Vertices( + BlockModel::Positions &vertex, + BlockModel::Indices &index, + const glm::mat4 &transform, + BlockModel::Index idx_offset = 0 + ) const; /// the number of vertices this shape's outline has size_t OutlineCount() const { return out_pos.size(); } -- 2.39.2