From 2e5a8d9ed00d6cae3334aaf99012752e9a30fbb5 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Fri, 28 Aug 2020 18:46:41 +0200 Subject: [PATCH] ovis: streamline updating texture contents this hides textures as named top-level objects. They are now only referenced as properties of materials. --- modules/ovis/include/opencv2/ovis.hpp | 20 ++++++++--------- modules/ovis/src/ovis.cpp | 32 +++++++++++++++++++++------ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/modules/ovis/include/opencv2/ovis.hpp b/modules/ovis/include/opencv2/ovis.hpp index 599528202..aeff53fae 100644 --- a/modules/ovis/include/opencv2/ovis.hpp +++ b/modules/ovis/include/opencv2/ovis.hpp @@ -86,8 +86,6 @@ public: /** * set window background to custom image - * - * creates a texture named "_Background" * @param image */ CV_WRAP virtual void setBackground(InputArray image) = 0; @@ -354,6 +352,14 @@ CV_EXPORTS_W void setMaterialProperty(const String& name, int prop, const Scalar /// @overload CV_EXPORTS_W void setMaterialProperty(const String& name, int prop, const String& value); +/** + * set the texture of a material to the given value + * @param name material name + * @param prop @ref MaterialProperty + * @param value the texture data + */ +CV_EXPORTS_AS(setMaterialTexture) void setMaterialProperty(const String& name, int prop, InputArray value); + /** * set the shader property of a material to the given value * @param name material name @@ -365,7 +371,7 @@ CV_EXPORTS_W void setMaterialProperty(const String& name, const String& prop, co /** * create a 2D plane, X right, Y down, Z up * - * creates a material and a texture with the same name + * creates a material with the same name * @param name name of the mesh * @param size size in world units * @param image optional texture @@ -403,13 +409,7 @@ CV_EXPORTS_W void createGridMesh(const String& name, const Size2f& size, const S */ CV_EXPORTS_W void createTriangleMesh(const String& name, InputArray vertices, InputArray normals = noArray(), InputArray indices = noArray()); -/** - * updates an existing texture - * - * A new texture can be created with @ref createPlaneMesh - * @param name name of the texture - * @param image the image data - */ +/// @deprecated use setMaterialProperty CV_EXPORTS_W void updateTexture(const String& name, InputArray image); //! @} } diff --git a/modules/ovis/src/ovis.cpp b/modules/ovis/src/ovis.cpp index 6bc2f2191..b5fb01ec4 100644 --- a/modules/ovis/src/ovis.cpp +++ b/modules/ovis/src/ovis.cpp @@ -1016,11 +1016,12 @@ void setMaterialProperty(const String& name, int prop, const Scalar& val) RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME, name, RESOURCEGROUP_NAME); } -void setMaterialProperty(const String& name, int prop, const String& value) +static TextureUnitState* _getTextureUnitForUpdate(const String& material, int prop) { CV_Assert_N(prop >= MATERIAL_TEXTURE0, prop <= MATERIAL_TEXTURE3, _app); - MaterialPtr mat = MaterialManager::getSingleton().getByName(name, RESOURCEGROUP_NAME); + CV_Assert(_app); + auto mat = MaterialManager::getSingleton().getByName(material, RESOURCEGROUP_NAME); CV_Assert(mat); Pass* rpass = mat->getTechniques()[0]->getPasses()[0]; @@ -1028,15 +1029,32 @@ void setMaterialProperty(const String& name, int prop, const String& value) size_t texUnit = prop - MATERIAL_TEXTURE0; CV_Assert(texUnit <= rpass->getTextureUnitStates().size()); + RTShader::ShaderGenerator::getSingleton().invalidateMaterial( + RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME, material, RESOURCEGROUP_NAME); + if (rpass->getTextureUnitStates().size() <= texUnit) { - rpass->createTextureUnitState(value); - return; + return rpass->createTextureUnitState(); } - rpass->getTextureUnitStates()[texUnit]->setTextureName(value); - RTShader::ShaderGenerator::getSingleton().invalidateMaterial( - RTShader::ShaderGenerator::DEFAULT_SCHEME_NAME, name, RESOURCEGROUP_NAME); + return rpass->getTextureUnitStates()[texUnit]; +} + +void setMaterialProperty(const String& name, int prop, const String& value) +{ + auto tu = _getTextureUnitForUpdate(name, prop); + tu->setTextureName(value); +} + +void setMaterialProperty(const String& name, int prop, InputArray value) +{ + auto tu = _getTextureUnitForUpdate(name, prop); + + auto texName = tu->getTextureName(); + if(texName.empty()) texName = name; + + _createTexture(texName, value.getMat()); + tu->setTextureName(texName); } static bool setShaderProperty(const GpuProgramParametersSharedPtr& params, const String& prop,