X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmodel.cpp;h=125d57aefcd16d7f7e7291229bf999879610d5d4;hb=e74f1ad236429f05db90c0ace825277e2a3fbc05;hp=ab6e72f18d6735866682b9aef236bc7ff9959f55;hpb=28fda9f7f55a9e806ade3f49f4e94f0242ec2c3c;p=blank.git diff --git a/src/model.cpp b/src/model.cpp index ab6e72f..125d57a 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -6,13 +6,9 @@ namespace blank { Model::Model() -: vertices() -, colors() -, normals() -, indices() -, va(0) +: va(0) , handle{} -, dirty(false) { +, count(0) { glGenVertexArrays(1, &va); glGenBuffers(ATTRIB_COUNT, handle); } @@ -23,12 +19,8 @@ Model::~Model() { } 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) { +: va(other.va) +, count(other.count) { other.va = 0; for (int i = 0; i < ATTRIB_COUNT; ++i) { handle[i] = other.handle[i]; @@ -37,45 +29,116 @@ Model::Model(Model &&other) } 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; + count = other.count; return *this; } +void Model::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 + ); -void Model::Clear() { - vertices.clear(); - colors.clear(); - normals.clear(); - indices.clear(); - Invalidate(); +#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 + 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, 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 + ); + + 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::Reserve(int v, int i) { - vertices.reserve(v); - colors.reserve(v); - normals.reserve(v); - indices.reserve(i); + +void Model::Draw() const { + glBindVertexArray(va); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); + glDrawElements( + GL_TRIANGLES, // how + count, // count + GL_UNSIGNED_INT, // type + nullptr // offset + ); } -void Model::CheckUpdate() { - if (dirty) { - glBindVertexArray(va); - Update(); +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; } } -void Model::Update() { +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, 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) @@ -87,13 +150,12 @@ void Model::Update() { ); #ifndef NDEBUG - if (colors.size() < vertices.size()) { - std::cerr << "Model: not enough colors!" << std::endl; - colors.resize(vertices.size(), { 1, 0, 1 }); + 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, 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) @@ -105,13 +167,12 @@ void Model::Update() { ); #ifndef NDEBUG - if (normals.size() < vertices.size()) { - std::cerr << "Model: not enough normals!" << std::endl; - normals.resize(vertices.size(), { 0, 1, 0 }); + if (buf.normals.size() < buf.vertices.size()) { + std::cerr << "BlockModel: 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) @@ -122,30 +183,40 @@ void Model::Update() { nullptr // offset ); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW); +#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 + ); - 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() { +void BlockModel::Draw() const { glBindVertexArray(va); - - if (dirty) { - Update(); - } - 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 ); } - OutlineModel::OutlineModel() : vertices() , colors()