X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel.cpp;h=4e75674256d59a25e05fbbe042b89dee5b0ef86b;hb=83ed3de28841d1eecfca39ff540e804cf6809b32;hp=032fc150a9e449c12bc68d62c98609280fee09a8;hpb=482114e156e91729f2529ea6bb1fe98dacdee97f;p=blank.git diff --git a/src/model.cpp b/src/model.cpp index 032fc15..4e75674 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1,57 +1,144 @@ #include "model.hpp" +#include + namespace blank { -Model::Model() -: vertices() -, colors() -, normals() +Model::Model() noexcept +: va(0) , handle{} -, dirty(false) { +, count(0) { + glGenVertexArrays(1, &va); glGenBuffers(ATTRIB_COUNT, handle); } -Model::~Model() { +Model::~Model() noexcept { glDeleteBuffers(ATTRIB_COUNT, handle); + glDeleteVertexArrays(1, &va); } - -void Model::Clear() { - vertices.clear(); - colors.clear(); - normals.clear(); - Invalidate(); +Model::Model(Model &&other) noexcept +: 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; + } } -void Model::Reserve(int s) { - vertices.reserve(s); - colors.reserve(s); - normals.reserve(s); +Model &Model::operator =(Model &&other) noexcept { + 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 Model::Update() { +void Model::Update(const Buffer &buf) noexcept { + glBindVertexArray(va); glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW); + 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 << "Model: not enough colors!" << std::endl; + } +#endif glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]); - glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW); + 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.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, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW); + 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 + ); - dirty = false; + 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 Model::Draw() { - if (dirty) { - Update(); +void Model::Draw() const noexcept { + glBindVertexArray(va); + glDrawElements( + GL_TRIANGLES, // how + count, // count + GL_UNSIGNED_INT, // type + nullptr // offset + ); +} + + +BlockModel::BlockModel() noexcept +: va(0) +, handle{} +, count(0) { + glGenVertexArrays(1, &va); + glGenBuffers(ATTRIB_COUNT, handle); +} + +BlockModel::~BlockModel() noexcept { + glDeleteBuffers(ATTRIB_COUNT, handle); + glDeleteVertexArrays(1, &va); +} + +BlockModel::BlockModel(BlockModel &&other) noexcept +: 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; } +} - glEnableVertexAttribArray(ATTRIB_VERTEX); +BlockModel &BlockModel::operator =(BlockModel &&other) noexcept { + 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) noexcept { + 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 @@ -61,8 +148,14 @@ void Model::Draw() { nullptr // offset ); - glEnableVertexAttribArray(ATTRIB_COLOR); +#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 @@ -72,10 +165,76 @@ void Model::Draw() { nullptr // offset ); - glEnableVertexAttribArray(ATTRIB_NORMAL); - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]); +#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_NORMAL, // location (for shader) + 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 noexcept { + glBindVertexArray(va); + glDrawElements( + GL_TRIANGLES, // how + count, // count + GL_UNSIGNED_INT, // type + nullptr // offset + ); +} + +OutlineModel::OutlineModel() noexcept +: vertices() +, colors() +, indices() +, va(0) +, handle{} +, dirty(false) { + glGenVertexArrays(1, &va); + glGenBuffers(ATTRIB_COUNT, handle); +} + +OutlineModel::~OutlineModel() noexcept { + glDeleteBuffers(ATTRIB_COUNT, handle); + glDeleteVertexArrays(1, &va); +} + + +void OutlineModel::Clear() noexcept { + 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() noexcept { + 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 @@ -83,15 +242,47 @@ void Model::Draw() { nullptr // offset ); - glDrawArrays( - GL_TRIANGLES, // how - 0, // start - vertices.size() // len +#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() noexcept { + glBindVertexArray(va); + + if (dirty) { + Update(); + } + + glEnable(GL_LINE_SMOOTH); + glLineWidth(2.0f); + + glDrawElements( + GL_LINES, // how + indices.size(), // count + GL_UNSIGNED_SHORT, // type + nullptr // offset + ); } }