From cbba8c40a62c334aba03e5ba957b85d699767418 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 26 Nov 2016 01:50:36 -0400 Subject: [PATCH] Split Mesh:attachAttribute(name, nil) out into Mesh:detachAttribute. Fix attachAttribute. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t let me push commits on a friday night… --HG-- branch : minor --- src/modules/graphics/opengl/Mesh.cpp | 43 +++++++++++++---------- src/modules/graphics/opengl/Mesh.h | 1 + src/modules/graphics/opengl/Shader.cpp | 2 +- src/modules/graphics/opengl/wrap_Mesh.cpp | 20 +++++++---- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/modules/graphics/opengl/Mesh.cpp b/src/modules/graphics/opengl/Mesh.cpp index 7d7c011aa..23eb8411d 100644 --- a/src/modules/graphics/opengl/Mesh.cpp +++ b/src/modules/graphics/opengl/Mesh.cpp @@ -311,24 +311,7 @@ bool Mesh::isAttributeEnabled(const std::string &name) const void Mesh::attachAttribute(const std::string &name, Mesh *mesh, const std::string &attachname) { - AttachedAttribute oldattrib = {}; - - auto it = attachedAttributes.find(name); - if (it != attachedAttributes.end()) - oldattrib = it->second; - - if (mesh == nullptr) - { - if (it != attachedAttributes.end() && oldattrib.mesh != this) - { - oldattrib.mesh->release(); - attachedAttributes.erase(it); - if (getAttributeIndex(name) != -1) - attachAttribute(name, this, name); - return; - } - } - else if (mesh != this) + if (mesh != this) { for (const auto &it : mesh->attachedAttributes) { @@ -339,7 +322,13 @@ void Mesh::attachAttribute(const std::string &name, Mesh *mesh, const std::strin } } + AttachedAttribute oldattrib = {}; AttachedAttribute newattrib = {}; + + auto it = attachedAttributes.find(name); + if (it != attachedAttributes.end()) + oldattrib = it->second; + newattrib.mesh = mesh; newattrib.enabled = oldattrib.mesh ? oldattrib.enabled : true; newattrib.index = mesh->getAttributeIndex(attachname); @@ -356,6 +345,24 @@ void Mesh::attachAttribute(const std::string &name, Mesh *mesh, const std::strin oldattrib.mesh->release(); } +bool Mesh::detachAttribute(const std::string &name) +{ + auto it = attachedAttributes.find(name); + + if (it != attachedAttributes.end() && it->second.mesh != this) + { + it->second.mesh->release(); + attachedAttributes.erase(it); + + if (getAttributeIndex(name) != -1) + attachAttribute(name, this, name); + + return true; + } + + return false; +} + void *Mesh::mapVertexData() { return vbo->map(); diff --git a/src/modules/graphics/opengl/Mesh.h b/src/modules/graphics/opengl/Mesh.h index c34032d3b..701feb89a 100644 --- a/src/modules/graphics/opengl/Mesh.h +++ b/src/modules/graphics/opengl/Mesh.h @@ -139,6 +139,7 @@ public: * will be used when drawing this Mesh. **/ void attachAttribute(const std::string &name, Mesh *mesh, const std::string &attachname); + bool detachAttribute(const std::string &name); void *mapVertexData(); void unmapVertexData(size_t modifiedoffset = 0, size_t modifiedsize = -1); diff --git a/src/modules/graphics/opengl/Shader.cpp b/src/modules/graphics/opengl/Shader.cpp index 0eb8ce9ae..df6e848a5 100644 --- a/src/modules/graphics/opengl/Shader.cpp +++ b/src/modules/graphics/opengl/Shader.cpp @@ -679,7 +679,7 @@ void Shader::sendTextures(const UniformInfo *info, Texture **textures, int count return; count = std::min(count, info->count); - bool updateuniform = internalUpdate; + bool updateuniform = false; // Make sure the shader's samplers are associated with texture units. for (int i = 0; i < count; i++) diff --git a/src/modules/graphics/opengl/wrap_Mesh.cpp b/src/modules/graphics/opengl/wrap_Mesh.cpp index f0bde6fa0..004340ef7 100644 --- a/src/modules/graphics/opengl/wrap_Mesh.cpp +++ b/src/modules/graphics/opengl/wrap_Mesh.cpp @@ -337,17 +337,22 @@ int w_Mesh_attachAttribute(lua_State *L) { Mesh *t = luax_checkmesh(L, 1); const char *name = luaL_checkstring(L, 2); - const char *attachname = name; - Mesh *mesh = nullptr; - if (lua_isnoneornil(L, 3)) - { - mesh = luax_checkmesh(L, 3); - attachname = luaL_optstring(L, 4, attachname); - } + Mesh *mesh = luax_checkmesh(L, 3); + const char *attachname = luaL_optstring(L, 4, name); luax_catchexcept(L, [&](){ t->attachAttribute(name, mesh, attachname); }); return 0; } +int w_Mesh_detachAttribute(lua_State *L) +{ + Mesh *t = luax_checkmesh(L, 1); + const char *name = luaL_checkstring(L, 2); + bool success = false; + luax_catchexcept(L, [&](){ success = t->detachAttribute(name); }); + luax_pushboolean(L, success); + return 1; +} + int w_Mesh_flush(lua_State *L) { Mesh *t = luax_checkmesh(L, 1); @@ -520,6 +525,7 @@ static const luaL_Reg w_Mesh_functions[] = { "setAttributeEnabled", w_Mesh_setAttributeEnabled }, { "isAttributeEnabled", w_Mesh_isAttributeEnabled }, { "attachAttribute", w_Mesh_attachAttribute }, + { "detachAttribute", w_Mesh_detachAttribute }, { "flush", w_Mesh_flush }, { "setVertexMap", w_Mesh_setVertexMap }, { "getVertexMap", w_Mesh_getVertexMap },