]> git.localhorst.tv Git - blank.git/blobdiff - src/model.cpp
added Galois LFSR PRNG
[blank.git] / src / model.cpp
index b50ccf0b168888ce0dff21e7d7423cc643c4cc01..d26968a0e6282e51cdcb9dda431eb90a751d4c33 100644 (file)
@@ -6,66 +6,42 @@
 namespace blank {
 
 Model::Model()
-: vertices()
-, colors()
-, normals()
+:  va(0)
 , handle{}
-, dirty(false) {
+, count(0) {
+       glGenVertexArrays(1, &va);
        glGenBuffers(ATTRIB_COUNT, handle);
 }
 
 Model::~Model() {
        glDeleteBuffers(ATTRIB_COUNT, handle);
+       glDeleteVertexArrays(1, &va);
 }
 
-
-void Model::Clear() {
-       vertices.clear();
-       colors.clear();
-       normals.clear();
-       Invalidate();
-}
-
-void Model::Reserve(int s) {
-       vertices.reserve(s);
-       colors.reserve(s);
-       normals.reserve(s);
-}
-
-
-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 });
+Model::Model(Model &&other)
+: va(other.va)
+, count(other.count) {
+       other.va = 0;
+       for (int i = 0; i < ATTRIB_COUNT; ++i) {
+               handle[i] = other.handle[i];
+               other.handle[i] = 0;
        }
-#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);
-
-       dirty = false;
 }
 
-
-void Model::Draw() {
-       if (dirty) {
-               Update();
+Model &Model::operator =(Model &&other) {
+       std::swap(va, other.va);
+       for (int i = 0; i < ATTRIB_COUNT; ++i) {
+               std::swap(handle[i], other.handle[i]);
        }
+       count = other.count;
+       return *this;
+}
 
-       glEnableVertexAttribArray(ATTRIB_VERTEX);
+void Model::Update(const Buffer &buf) {
+       glBindVertexArray(va);
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
+       glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW);
+       glEnableVertexAttribArray(ATTRIB_VERTEX);
        glVertexAttribPointer(
                ATTRIB_VERTEX, // location (for shader)
                3,             // size
@@ -75,8 +51,14 @@ void Model::Draw() {
                nullptr        // offset
        );
 
-       glEnableVertexAttribArray(ATTRIB_COLOR);
+#ifndef NDEBUG
+       if (buf.colors.size() < buf.vertices.size()) {
+               std::cerr << "Model: not enough colors!" << std::endl;
+       }
+#endif
        glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
+       glBufferData(GL_ARRAY_BUFFER, buf.colors.size() * sizeof(glm::vec3), buf.colors.data(), GL_STATIC_DRAW);
+       glEnableVertexAttribArray(ATTRIB_COLOR);
        glVertexAttribPointer(
                ATTRIB_COLOR,  // location (for shader)
                3,             // size
@@ -86,8 +68,14 @@ void Model::Draw() {
                nullptr        // offset
        );
 
-       glEnableVertexAttribArray(ATTRIB_NORMAL);
+#ifndef NDEBUG
+       if (buf.normals.size() < buf.vertices.size()) {
+               std::cerr << "Model: 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
@@ -97,67 +85,59 @@ void Model::Draw() {
                nullptr        // offset
        );
 
-       glDrawArrays(
-               GL_TRIANGLES,   // how
-               0,              // start
-               vertices.size() // len
-       );
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
+       glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW);
+       count = buf.indices.size();
+}
 
-       glDisableVertexAttribArray(ATTRIB_NORMAL);
-       glDisableVertexAttribArray(ATTRIB_COLOR);
-       glDisableVertexAttribArray(ATTRIB_VERTEX);
+
+void Model::Draw() const {
+       glBindVertexArray(va);
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
+       glDrawElements(
+               GL_TRIANGLES,    // how
+               count,           // count
+               GL_UNSIGNED_INT, // type
+               nullptr          // offset
+       );
 }
 
 
 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);
 }
 
 
 void OutlineModel::Clear() {
        vertices.clear();
        colors.clear();
+       indices.clear();
        Invalidate();
 }
 
-void OutlineModel::Reserve(int s) {
-       vertices.reserve(s);
-       colors.reserve(s);
+void OutlineModel::Reserve(int v, int i) {
+       vertices.reserve(v);
+       colors.reserve(v);
+       indices.reserve(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);
-
-#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
@@ -167,8 +147,15 @@ void OutlineModel::Draw() {
                nullptr        // offset
        );
 
-       glEnableVertexAttribArray(ATTRIB_COLOR);
+#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);
+       glEnableVertexAttribArray(ATTRIB_COLOR);
        glVertexAttribPointer(
                ATTRIB_COLOR,  // location (for shader)
                3,             // size
@@ -178,16 +165,30 @@ void OutlineModel::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 OutlineModel::Draw() {
+       glBindVertexArray(va);
+
+       if (dirty) {
+               Update();
+       }
+
+       glEnable(GL_LINE_SMOOTH);
        glLineWidth(2.0f);
 
-       glDrawArrays(
-               GL_LINES,       // how
-               0,              // start
-               vertices.size() // len
+       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
+       glDrawElements(
+               GL_LINES,          // how
+               indices.size(),    // count
+               GL_UNSIGNED_SHORT, // type
+               nullptr            // offset
        );
-
-       glDisableVertexAttribArray(ATTRIB_COLOR);
-       glDisableVertexAttribArray(ATTRIB_VERTEX);
 }
 
 }