]> git.localhorst.tv Git - blank.git/commitdiff
don't push block normals to GPU
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 26 May 2015 16:44:24 +0000 (18:44 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 26 May 2015 16:44:24 +0000 (18:44 +0200)
src/block.cpp
src/chunk.cpp
src/chunk.hpp
src/model.cpp
src/model.hpp
src/shader.cpp
src/shader.hpp
src/shape.cpp
src/shape.hpp

index 6e6ac18533d7cf10173a121b4f79839b4e8c3d21..dea52672359cc61c4756fbf826b90196320b8a2f 100644 (file)
@@ -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] = {
 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
        { 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 {
        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);
 }
 
        buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
 }
 
index 2a764f31c1c637435feb7738161a42f576f997b9..74ca1bcf96933274f9560cbb151e8629177eeb0c 100644 (file)
@@ -322,7 +322,7 @@ int Chunk::GetLight(int index) const {
        return light[index];
 }
 
        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));
 
        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) {
                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())
+                       ));
                }
        }
 
                }
        }
 
index 4f6732d39d84789a64e46fdb381a574b49316f33..4addb56d7a9bf9b87935664dba54fdfe266756f7 100644 (file)
@@ -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)); }
 
        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,
 
        bool Intersection(
                const Ray &ray,
index 125d57aefcd16d7f7e7291229bf999879610d5d4..62a90e554837ee18cf2fa66552e66020a8e6e7c4 100644 (file)
@@ -166,23 +166,6 @@ void BlockModel::Update(const Buffer &buf) {
                nullptr        // offset
        );
 
                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;
 #ifndef NDEBUG
        if (buf.lights.size() < buf.vertices.size()) {
                std::cerr << "BlockModel: not enough lights!" << std::endl;
index 7b9fc3d1b09cf702109111ae51568e694f45736a..557b4acc6ebe4cb1092a95361cb10d14709703c3 100644 (file)
@@ -80,13 +80,11 @@ class BlockModel {
 public:
        using Position = glm::vec3;
        using Color = glm::vec3;
 public:
        using Position = glm::vec3;
        using Color = glm::vec3;
-       using Normal = glm::vec3;
        using Light = float;
        using Index = unsigned int;
 
        using Positions = std::vector<Position>;
        using Colors = std::vector<Color>;
        using Light = float;
        using Index = unsigned int;
 
        using Positions = std::vector<Position>;
        using Colors = std::vector<Color>;
-       using Normals = std::vector<Normal>;
        using Lights = std::vector<Light>;
        using Indices = std::vector<Index>;
 
        using Lights = std::vector<Light>;
        using Indices = std::vector<Index>;
 
@@ -95,14 +93,12 @@ public:
 
                Positions vertices;
                Colors colors;
 
                Positions vertices;
                Colors colors;
-               Normals normals;
                Lights lights;
                Indices indices;
 
                void Clear() {
                        vertices.clear();
                        colors.clear();
                Lights lights;
                Indices indices;
 
                void Clear() {
                        vertices.clear();
                        colors.clear();
-                       normals.clear();
                        lights.clear();
                        indices.clear();
                }
                        lights.clear();
                        indices.clear();
                }
@@ -110,7 +106,6 @@ public:
                void Reserve(size_t p, size_t i) {
                        vertices.reserve(p);
                        colors.reserve(p);
                void Reserve(size_t p, size_t i) {
                        vertices.reserve(p);
                        colors.reserve(p);
-                       normals.reserve(p);
                        lights.reserve(p);
                        indices.reserve(i);
                }
                        lights.reserve(p);
                        indices.reserve(i);
                }
@@ -135,7 +130,6 @@ private:
        enum Attribute {
                ATTRIB_VERTEX,
                ATTRIB_COLOR,
        enum Attribute {
                ATTRIB_VERTEX,
                ATTRIB_COLOR,
-               ATTRIB_NORMAL,
                ATTRIB_LIGHT,
                ATTRIB_INDEX,
                ATTRIB_COUNT,
                ATTRIB_LIGHT,
                ATTRIB_INDEX,
                ATTRIB_COUNT,
index 134f710c9684b2c690a3b71b58219872a02d419f..01f786117a781b9536be7bb623962dadee2b789d 100644 (file)
@@ -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);
 }
 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)
 BlockLighting::BlockLighting()
 : program()
 , vp(1.0f)
-, m_handle(0)
 , mv_handle(0)
 , mvp_handle(0)
 , fog_density_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"
                "#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"
                "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"
                "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"
        );
                        "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"
                "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"
                        "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");
        }
 
                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");
        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);
 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]);
 }
        glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
        glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
 }
index 873460bf17644947ee61f3e87e8f5be82ccad2d8..bb9ad845445acbacc101313e129d9c0c50f39cdc 100644 (file)
@@ -49,6 +49,7 @@ public:
        bool Linked() const;
        void Log(std::ostream &) const;
 
        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); }
        GLint UniformLocation(const GLchar *name) const;
 
        void Use() const { glUseProgram(handle); }
@@ -130,7 +131,6 @@ private:
        glm::mat4 view;
        glm::mat4 vp;
 
        glm::mat4 view;
        glm::mat4 vp;
 
-       GLuint m_handle;
        GLuint mv_handle;
        GLuint mvp_handle;
        GLuint light_direction_handle;
        GLuint mv_handle;
        GLuint mvp_handle;
        GLuint light_direction_handle;
index 8111258be6f83d5b2f7d24baeca0a96c99757e3d..545a8e4712a57c0b998353eaeeaab111d86063fb 100644 (file)
@@ -6,16 +6,16 @@ namespace blank {
 void Shape::Vertices(
        Model::Positions &vertex,
        Model::Normals &normal,
 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) {
 ) 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) {
        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,
 void Shape::Outline(
        OutlineModel::Positions &vertex,
        OutlineModel::Indices &index,
index f31075b10fb48f8e2103bbad1349af97b9c10dfd..abf87954c557bcfe6d9459492db60fb641cae757 100644 (file)
@@ -17,14 +17,19 @@ struct Shape {
        /// the number of vertex indices this shape has
        size_t VertexIndexCount() const { return vtx_idx.size(); }
 
        /// 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
        /// fill given buffers with this shape's elements with an
-       /// optional offset
+       /// optional transform and offset
        void Vertices(
                Model::Positions &vertex,
                Model::Normals &normal,
        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,
        ) const;
        void Vertices(
                Model::Positions &vertex,
@@ -33,6 +38,12 @@ struct Shape {
                const glm::mat4 &transform,
                Model::Index idx_offset = 0
        ) const;
                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(); }
 
        /// the number of vertices this shape's outline has
        size_t OutlineCount() const { return out_pos.size(); }