From: Daniel Karbach Date: Wed, 11 Mar 2015 19:37:30 +0000 (+0100) Subject: save a little memory X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=f932e8c0273794bcd954c9f5b504bad6140f7cf4;p=blank.git save a little memory --- diff --git a/src/block.cpp b/src/block.cpp index b0de21a..cdfc452 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -6,12 +6,12 @@ namespace blank { const NullShape BlockType::DEFAULT_SHAPE; void BlockType::FillModel( - Model &model, + Model::Buffer &buf, const glm::vec3 &pos_offset, Model::Index idx_offset ) const { - shape->Vertices(model.vertices, model.normals, model.indices, pos_offset, idx_offset); - model.colors.insert(model.colors.end(), shape->VertexCount(), color); + shape->Vertices(buf.vertices, buf.normals, buf.indices, pos_offset, idx_offset); + buf.colors.insert(buf.colors.end(), shape->VertexCount(), color); } void BlockType::FillOutlineModel( diff --git a/src/block.hpp b/src/block.hpp index 5d8e7e7..1663c16 100644 --- a/src/block.hpp +++ b/src/block.hpp @@ -47,7 +47,7 @@ struct BlockType { void FillModel( - Model &m, + Model::Buffer &m, const glm::vec3 &pos_offset = { 0, 0, 0 }, Model::Index idx_offset = 0 ) const; diff --git a/src/chunk.cpp b/src/chunk.cpp index 10031e7..bed947e 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -6,6 +6,12 @@ #include +namespace { + +blank::Model::Buffer buf; + +} + namespace blank { Chunk::Chunk(const BlockTypeRegistry &types) @@ -117,7 +123,6 @@ void Chunk::CheckUpdate() { if (dirty) { Update(); } - model.CheckUpdate(); } void Chunk::Update() { @@ -127,17 +132,17 @@ void Chunk::Update() { vtx_count += shape->VertexCount(); idx_count += shape->VertexIndexCount(); } - model.Clear(); - model.Reserve(vtx_count, idx_count); + buf.Clear(); + buf.Reserve(vtx_count, idx_count); Model::Index vtx_counter = 0; for (size_t i = 0; i < Size(); ++i) { const BlockType &type = Type(blocks[i]); - type.FillModel(model, ToCoords(i), vtx_counter); + type.FillModel(buf, ToCoords(i), vtx_counter); vtx_counter += type.shape->VertexCount(); } - model.Invalidate(); + model.Update(buf); dirty = false; } diff --git a/src/hud.cpp b/src/hud.cpp index d604011..c43d784 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -10,6 +10,7 @@ namespace blank { HUD::HUD() : block() +, block_buf() , block_transform(1.0f) , block_visible(false) , crosshair() @@ -46,8 +47,9 @@ void HUD::Viewport(float x, float y, float width, float height) { void HUD::Display(const BlockType &type) { - block.Clear(); - type.FillModel(block); + block_buf.Clear(); + type.FillModel(block_buf); + block.Update(block_buf); block_visible = type.visible; } diff --git a/src/hud.hpp b/src/hud.hpp index 47fcdb0..a3dc02e 100644 --- a/src/hud.hpp +++ b/src/hud.hpp @@ -27,6 +27,7 @@ public: private: Model block; + Model::Buffer block_buf; glm::mat4 block_transform; bool block_visible; diff --git a/src/model.cpp b/src/model.cpp index ab6e72f..d26968a 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,18 @@ 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::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::CheckUpdate() { - if (dirty) { - glBindVertexArray(va); - Update(); - } -} - -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); + 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 +52,12 @@ void Model::Update() { ); #ifndef NDEBUG - if (colors.size() < vertices.size()) { + if (buf.colors.size() < buf.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); + 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 +69,12 @@ void Model::Update() { ); #ifndef NDEBUG - if (normals.size() < vertices.size()) { + if (buf.normals.size() < buf.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); + 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) @@ -123,25 +86,19 @@ void Model::Update() { ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW); - - dirty = false; + glBufferData(GL_ELEMENT_ARRAY_BUFFER, buf.indices.size() * sizeof(Index), buf.indices.data(), GL_STATIC_DRAW); + count = buf.indices.size(); } -void Model::Draw() { +void Model::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 ); } diff --git a/src/model.hpp b/src/model.hpp index 62f467c..d5c77d1 100644 --- a/src/model.hpp +++ b/src/model.hpp @@ -22,10 +22,28 @@ public: using Indices = std::vector; public: - Positions vertices; - Colors colors; - Normals normals; - Indices indices; + struct Buffer { + + Positions vertices; + Colors colors; + Normals normals; + Indices indices; + + void Clear() { + vertices.clear(); + colors.clear(); + normals.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + colors.reserve(p); + normals.reserve(p); + indices.reserve(i); + } + + }; public: Model(); @@ -37,16 +55,9 @@ public: Model(Model &&); Model &operator =(Model &&); - void Invalidate() { dirty = true; } - - void Clear(); - void Reserve(int vtx_count, int idx_count); + void Update(const Buffer &); - void CheckUpdate(); - void Draw(); - -private: - void Update(); + void Draw() const; private: enum Attribute { @@ -59,7 +70,7 @@ private: GLuint va; GLuint handle[ATTRIB_COUNT]; - bool dirty; + size_t count; };