X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel.cpp;h=b50ccf0b168888ce0dff21e7d7423cc643c4cc01;hb=2d8c7c015478a4528c0909f11d43998b1393948d;hp=4ee621afbb614605acd7dd3a02674721e6d3bc3c;hpb=d18be10ef3f0a7b61c6f5c4c4096ca2b776c75b3;p=blank.git diff --git a/src/model.cpp b/src/model.cpp index 4ee621a..b50ccf0 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1,26 +1,193 @@ #include "model.hpp" -#include -#include -#include +#include namespace blank { Model::Model() -: position(0, 0, 0) -, pitch(0) -, yaw(0) { - +: vertices() +, colors() +, normals() +, handle{} +, dirty(false) { + glGenBuffers(ATTRIB_COUNT, handle); } Model::~Model() { + glDeleteBuffers(ATTRIB_COUNT, handle); +} + + +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 }); + } +#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(); + } + + 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 + ); + + glEnableVertexAttribArray(ATTRIB_NORMAL); + glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]); + glVertexAttribPointer( + ATTRIB_NORMAL, // location (for shader) + 3, // size + GL_FLOAT, // type + GL_FALSE, // normalized + 0, // stride + nullptr // offset + ); + + glDrawArrays( + GL_TRIANGLES, // how + 0, // start + vertices.size() // len + ); + + glDisableVertexAttribArray(ATTRIB_NORMAL); + glDisableVertexAttribArray(ATTRIB_COLOR); + glDisableVertexAttribArray(ATTRIB_VERTEX); } -glm::mat4 Model::Transform() const { - return glm::translate(position) * glm::eulerAngleYX(yaw, pitch); +OutlineModel::OutlineModel() +: vertices() +, colors() +, handle{} +, dirty(false) { + glGenBuffers(ATTRIB_COUNT, handle); +} + +OutlineModel::~OutlineModel() { + glDeleteBuffers(ATTRIB_COUNT, handle); +} + + +void OutlineModel::Clear() { + vertices.clear(); + colors.clear(); + Invalidate(); +} + +void OutlineModel::Reserve(int s) { + vertices.reserve(s); + colors.reserve(s); +} + + +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 + 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 + ); + + glLineWidth(2.0f); + + glDrawArrays( + GL_LINES, // how + 0, // start + vertices.size() // len + ); + + glDisableVertexAttribArray(ATTRIB_COLOR); + glDisableVertexAttribArray(ATTRIB_VERTEX); } }