]> git.localhorst.tv Git - blank.git/commitdiff
use one vao per model
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 10 Mar 2015 19:46:12 +0000 (20:46 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 10 Mar 2015 20:51:09 +0000 (21:51 +0100)
src/app.cpp
src/chunk.cpp
src/chunk.hpp
src/generator.cpp
src/model.cpp
src/model.hpp

index 9580d33560024171ab9db84e957777b697f2c970..d8f1e2147ba2589df979be0c7bbdd3402fc4eb57 100644 (file)
@@ -31,10 +31,6 @@ Application::Application()
 , place_id(1) {
        GLContext::EnableVSync();
 
-       GLuint VertexArrayID;
-       glGenVertexArrays(1, &VertexArrayID);
-       glBindVertexArray(VertexArrayID);
-
        world.Generate({ -4, -4, -4 }, { 5, 5, 5});
 
        hud.Viewport(960, 600);
index 52d17c055319464479f8d1d7b248044de024cf52..fbfc8578ac3a93da8eab354ad1e37fa56ee07c5f 100644 (file)
@@ -111,6 +111,13 @@ glm::mat4 Chunk::Transform(const Pos &offset) const {
 }
 
 
+void Chunk::CheckUpdate() {
+       if (dirty) {
+               Update();
+       }
+       model.CheckUpdate();
+}
+
 void Chunk::Update() {
        int vtx_count = 0, idx_count = 0;
        for (const auto &block : blocks) {
index 5c1cadb38d50f1222253d26b123b916a28170b3e..d996deefc23088876598b6fb486c5a43e91591e5 100644 (file)
@@ -72,6 +72,7 @@ public:
        const Pos &Position() const { return position; }
        glm::mat4 Transform(const Pos &offset) const;
 
+       void CheckUpdate();
        void Draw();
 
 private:
index 10cb668c9cfb491681987501fe8a0f68199f6683..60708e021a01c93751b1aa767963cc3add0c4d57 100644 (file)
@@ -33,6 +33,7 @@ void Generator::operator ()(Chunk &chunk) const {
                }
        }
        chunk.Invalidate();
+       chunk.CheckUpdate();
 }
 
 }
index 0e358c5585c62300d9a7aae3abe0e52da8a18863..ab6e72f18d6735866682b9aef236bc7ff9959f55 100644 (file)
@@ -9,20 +9,27 @@ Model::Model()
 : vertices()
 , colors()
 , normals()
+, indices()
+, va(0)
 , handle{}
 , dirty(false) {
+       glGenVertexArrays(1, &va);
        glGenBuffers(ATTRIB_COUNT, handle);
 }
 
 Model::~Model() {
        glDeleteBuffers(ATTRIB_COUNT, handle);
+       glDeleteVertexArrays(1, &va);
 }
 
 Model::Model(Model &&other)
 : vertices(std::move(other.vertices))
 , colors(std::move(other.colors))
 , normals(std::move(other.normals))
+, indices(std::move(other.indices))
+, va(other.va)
 , dirty(other.dirty) {
+       other.va = 0;
        for (int i = 0; i < ATTRIB_COUNT; ++i) {
                handle[i] = other.handle[i];
                other.handle[i] = 0;
@@ -30,10 +37,11 @@ Model::Model(Model &&other)
 }
 
 Model &Model::operator =(Model &&other) {
-       vertices = std::move(other.vertices);
-       colors = std::move(other.colors);
-       normals = std::move(other.normals);
-       indices = std::move(other.indices);
+       std::swap(vertices, other.vertices);
+       std::swap(colors, other.colors);
+       std::swap(normals, other.normals);
+       std::swap(indices, other.indices);
+       std::swap(va, other.va);
        for (int i = 0; i < ATTRIB_COUNT; ++i) {
                std::swap(handle[i], other.handle[i]);
        }
@@ -58,42 +66,17 @@ void Model::Reserve(int v, int i) {
 }
 
 
-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);
-
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
-       glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
-
-       dirty = false;
-}
-
-
-void Model::Draw() {
+void Model::CheckUpdate() {
        if (dirty) {
+               glBindVertexArray(va);
                Update();
        }
+}
 
-       glEnableVertexAttribArray(ATTRIB_VERTEX);
+void Model::Update() {
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
+       glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
+       glEnableVertexAttribArray(ATTRIB_VERTEX);
        glVertexAttribPointer(
                ATTRIB_VERTEX, // location (for shader)
                3,             // size
@@ -103,8 +86,15 @@ void Model::Draw() {
                nullptr        // offset
        );
 
-       glEnableVertexAttribArray(ATTRIB_COLOR);
+#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);
+       glEnableVertexAttribArray(ATTRIB_COLOR);
        glVertexAttribPointer(
                ATTRIB_COLOR,  // location (for shader)
                3,             // size
@@ -114,8 +104,15 @@ void Model::Draw() {
                nullptr        // offset
        );
 
-       glEnableVertexAttribArray(ATTRIB_NORMAL);
+#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);
+       glEnableVertexAttribArray(ATTRIB_NORMAL);
        glVertexAttribPointer(
                ATTRIB_NORMAL, // location (for shader)
                3,             // size
@@ -125,6 +122,20 @@ void Model::Draw() {
                nullptr        // offset
        );
 
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
+       glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
+
+       dirty = false;
+}
+
+
+void Model::Draw() {
+       glBindVertexArray(va);
+
+       if (dirty) {
+               Update();
+       }
+
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
        glDrawElements(
                GL_TRIANGLES,      // how
@@ -132,10 +143,6 @@ void Model::Draw() {
                GL_UNSIGNED_INT, // type
                nullptr            // offset
        );
-
-       glDisableVertexAttribArray(ATTRIB_NORMAL);
-       glDisableVertexAttribArray(ATTRIB_COLOR);
-       glDisableVertexAttribArray(ATTRIB_VERTEX);
 }
 
 
@@ -143,13 +150,16 @@ OutlineModel::OutlineModel()
 : vertices()
 , colors()
 , indices()
+, va(0)
 , handle{}
 , dirty(false) {
+       glGenVertexArrays(1, &va);
        glGenBuffers(ATTRIB_COUNT, handle);
 }
 
 OutlineModel::~OutlineModel() {
        glDeleteBuffers(ATTRIB_COUNT, handle);
+       glDeleteVertexArrays(1, &va);
 }
 
 
@@ -170,6 +180,15 @@ void OutlineModel::Reserve(int v, int i) {
 void OutlineModel::Update() {
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
+       glEnableVertexAttribArray(ATTRIB_VERTEX);
+       glVertexAttribPointer(
+               ATTRIB_VERTEX, // location (for shader)
+               3,             // size
+               GL_FLOAT,      // type
+               GL_FALSE,      // normalized
+               0,             // stride
+               nullptr        // offset
+       );
 
 #ifndef NDEBUG
        if (colors.size() < vertices.size()) {
@@ -179,6 +198,15 @@ void OutlineModel::Update() {
 #endif
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
        glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
+       glEnableVertexAttribArray(ATTRIB_COLOR);
+       glVertexAttribPointer(
+               ATTRIB_COLOR,  // location (for shader)
+               3,             // size
+               GL_FLOAT,      // type
+               GL_FALSE,      // normalized
+               0,             // stride
+               nullptr        // offset
+       );
 
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
@@ -188,32 +216,12 @@ void OutlineModel::Update() {
 
 
 void OutlineModel::Draw() {
+       glBindVertexArray(va);
+
        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
-       );
-
        glEnable(GL_LINE_SMOOTH);
        glLineWidth(2.0f);
 
@@ -224,9 +232,6 @@ void OutlineModel::Draw() {
                GL_UNSIGNED_SHORT, // type
                nullptr            // offset
        );
-
-       glDisableVertexAttribArray(ATTRIB_COLOR);
-       glDisableVertexAttribArray(ATTRIB_VERTEX);
 }
 
 }
index 6e9583b3ec84f6ae05c7f1beaeeaa05b7e36e75b..ea9bf119e80e6061084117edb0e1c11549a8b1a0 100644 (file)
@@ -34,6 +34,7 @@ public:
        void Clear();
        void Reserve(int vtx_count, int idx_count);
 
+       void CheckUpdate();
        void Draw();
 
 private:
@@ -48,6 +49,7 @@ private:
                ATTRIB_COUNT,
        };
 
+       GLuint va;
        GLuint handle[ATTRIB_COUNT];
        bool dirty;
 
@@ -89,6 +91,7 @@ private:
                ATTRIB_COUNT,
        };
 
+       GLuint va;
        GLuint handle[ATTRIB_COUNT];
        bool dirty;