X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel.cpp;h=ab6e72f18d6735866682b9aef236bc7ff9959f55;hb=e5b1f5ef948724145ec2ed5777f4da1d6d862afd;hp=bbfc97f4fcfb121f8646feec9d59680213958189;hpb=3baab6cca7423d55ea08288d96570b02380b1fe9;p=blank.git diff --git a/src/model.cpp b/src/model.cpp index bbfc97f..ab6e72f 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1,37 +1,82 @@ #include "model.hpp" +#include + namespace blank { -Model::Model( - std::vector &&vtx, - std::vector &&col, - std::vector &&norm -) -: vertices(vtx) -, colors(col) -, normals(norm) -, handle{} { +Model::Model() +: vertices() +, colors() +, normals() +, indices() +, va(0) +, handle{} +, dirty(false) { + glGenVertexArrays(1, &va); glGenBuffers(ATTRIB_COUNT, handle); +} - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW); +Model::~Model() { + glDeleteBuffers(ATTRIB_COUNT, handle); + glDeleteVertexArrays(1, &va); +} - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]); - glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW); +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; + } +} - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]); - glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW); +Model &Model::operator =(Model &&other) { + 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]); + } + dirty = other.dirty; + return *this; } -Model::~Model() { - glDeleteBuffers(ATTRIB_COUNT, handle); + +void Model::Clear() { + vertices.clear(); + colors.clear(); + normals.clear(); + indices.clear(); + Invalidate(); +} + +void Model::Reserve(int v, int i) { + vertices.reserve(v); + colors.reserve(v); + normals.reserve(v); + indices.reserve(i); } -void Model::Draw() { - glEnableVertexAttribArray(ATTRIB_VERTEX); +void Model::CheckUpdate() { + if (dirty) { + glBindVertexArray(va); + Update(); + } +} + +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 @@ -41,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 @@ -52,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 @@ -63,15 +122,116 @@ 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, 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 + indices.size(), // 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 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); + 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()) { + 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 + GL_FLOAT, // type + GL_FALSE, // normalized + 0, // stride + nullptr // offset ); - glDisableVertexAttribArray(ATTRIB_NORMAL); - glDisableVertexAttribArray(ATTRIB_COLOR); - glDisableVertexAttribArray(ATTRIB_VERTEX); + 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); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); + glDrawElements( + GL_LINES, // how + indices.size(), // count + GL_UNSIGNED_SHORT, // type + nullptr // offset + ); } }