]> git.localhorst.tv Git - blank.git/blobdiff - src/model.cpp
added ability to get seperate information about block face obstruction
[blank.git] / src / model.cpp
index d26968a0e6282e51cdcb9dda431eb90a751d4c33..1d4edef0b17fea6ea45845c67a9b2333535f858e 100644 (file)
@@ -93,7 +93,6 @@ void Model::Update(const Buffer &buf) {
 
 void Model::Draw() const {
        glBindVertexArray(va);
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
        glDrawElements(
                GL_TRIANGLES,    // how
                count,           // count
@@ -103,6 +102,102 @@ void Model::Draw() const {
 }
 
 
+BlockModel::BlockModel()
+: va(0)
+, handle{}
+, count(0) {
+       glGenVertexArrays(1, &va);
+       glGenBuffers(ATTRIB_COUNT, handle);
+}
+
+BlockModel::~BlockModel() {
+       glDeleteBuffers(ATTRIB_COUNT, handle);
+       glDeleteVertexArrays(1, &va);
+}
+
+BlockModel::BlockModel(BlockModel &&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;
+       }
+}
+
+BlockModel &BlockModel::operator =(BlockModel &&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;
+}
+
+void BlockModel::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
+               GL_FLOAT,      // type
+               GL_FALSE,      // normalized
+               0,             // stride
+               nullptr        // offset
+       );
+
+#ifndef NDEBUG
+       if (buf.colors.size() < buf.vertices.size()) {
+               std::cerr << "BlockModel: 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
+               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;
+       }
+#endif
+       glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_LIGHT]);
+       glBufferData(GL_ARRAY_BUFFER, buf.lights.size() * sizeof(float), buf.lights.data(), GL_STATIC_DRAW);
+       glEnableVertexAttribArray(ATTRIB_LIGHT);
+       glVertexAttribPointer(
+               ATTRIB_LIGHT, // location (for shader)
+               1,            // size
+               GL_FLOAT,     // type
+               GL_FALSE,     // normalized
+               0,            // stride
+               nullptr       // offset
+       );
+
+       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();
+}
+
+
+void BlockModel::Draw() const {
+       glBindVertexArray(va);
+       glDrawElements(
+               GL_TRIANGLES,    // how
+               count,           // count
+               GL_UNSIGNED_INT, // type
+               nullptr          // offset
+       );
+}
+
 OutlineModel::OutlineModel()
 : vertices()
 , colors()
@@ -182,7 +277,6 @@ void OutlineModel::Draw() {
        glEnable(GL_LINE_SMOOTH);
        glLineWidth(2.0f);
 
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
        glDrawElements(
                GL_LINES,          // how
                indices.size(),    // count