X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel.cpp;h=d26968a0e6282e51cdcb9dda431eb90a751d4c33;hb=a58c4558e7d4934f4d0ee621520acfe1c8258c93;hp=0e358c5585c62300d9a7aae3abe0e52da8a18863;hpb=82426ae2997d2b21703d2d5afb631a84736e975f;p=blank.git diff --git a/src/model.cpp b/src/model.cpp index 0e358c5..d26968a 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -6,23 +6,22 @@ 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); } Model::Model(Model &&other) -: vertices(std::move(other.vertices)) -, colors(std::move(other.colors)) -, normals(std::move(other.normals)) -, dirty(other.dirty) { +: 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; @@ -30,70 +29,19 @@ Model::Model(Model &&other) } Model &Model::operator =(Model &&other) { - vertices = std::move(other.vertices); - colors = std::move(other.colors); - normals = std::move(other.normals); - indices = std::move(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; + count = other.count; return *this; } - -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::Update() { +void Model::Update(const Buffer &buf) { + glBindVertexArray(va); 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); - - 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() { - if (dirty) { - Update(); - } - + glBufferData(GL_ARRAY_BUFFER, buf.vertices.size() * sizeof(glm::vec3), buf.vertices.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(ATTRIB_VERTEX); - glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]); glVertexAttribPointer( ATTRIB_VERTEX, // location (for shader) 3, // size @@ -103,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 @@ -114,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 @@ -125,17 +85,21 @@ void Model::Draw() { 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 Model::Draw() const { + glBindVertexArray(va); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); glDrawElements( - GL_TRIANGLES, // how - indices.size(), // count + GL_TRIANGLES, // how + count, // count GL_UNSIGNED_INT, // type - nullptr // offset + nullptr // offset ); - - glDisableVertexAttribArray(ATTRIB_NORMAL); - glDisableVertexAttribArray(ATTRIB_COLOR); - glDisableVertexAttribArray(ATTRIB_VERTEX); } @@ -143,13 +107,16 @@ 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); } @@ -170,6 +137,15 @@ void OutlineModel::Reserve(int v, int 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()) { @@ -179,6 +155,15 @@ void OutlineModel::Update() { #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 + ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW); @@ -188,32 +173,12 @@ void OutlineModel::Update() { void OutlineModel::Draw() { + glBindVertexArray(va); + 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 - ); - glEnable(GL_LINE_SMOOTH); glLineWidth(2.0f); @@ -224,9 +189,6 @@ void OutlineModel::Draw() { GL_UNSIGNED_SHORT, // type nullptr // offset ); - - glDisableVertexAttribArray(ATTRIB_COLOR); - glDisableVertexAttribArray(ATTRIB_VERTEX); } }