From f7e522a1cb57011af794ad0217ae68f07198932c Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Wed, 8 Aug 2018 15:51:54 +0200 Subject: [PATCH 1/2] ovis: add updateTexture function and allow grayscale texture format --- modules/ovis/include/opencv2/ovis.hpp | 9 +++++++++ modules/ovis/src/ovis.cpp | 29 +++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/modules/ovis/include/opencv2/ovis.hpp b/modules/ovis/include/opencv2/ovis.hpp index cd171a28e..afe2db967 100644 --- a/modules/ovis/include/opencv2/ovis.hpp +++ b/modules/ovis/include/opencv2/ovis.hpp @@ -281,6 +281,15 @@ CV_EXPORTS_W void createPointCloudMesh(const String& name, InputArray vertices, * @param segments number of segments per side */ CV_EXPORTS_W void createGridMesh(const String& name, const Size2f& size, const Size& segments = Size(1, 1)); + +/** + * updates an existing texture + * + * A new texture can be created with @ref createPlaneMesh + * @param name name of the texture + * @param image the image data + */ +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 bb798011f..3bccc25f5 100644 --- a/modules/ovis/src/ovis.cpp +++ b/modules/ovis/src/ovis.cpp @@ -31,11 +31,28 @@ WindowScene::~WindowScene() {} void _createTexture(const String& name, Mat image) { + PixelFormat format; + switch(image.type()) + { + case CV_8UC4: + format = PF_BYTE_BGRA; + break; + case CV_8UC3: + format = PF_BYTE_BGR; + break; + case CV_8UC1: + format = PF_BYTE_L; + break; + default: + CV_Error(Error::StsBadArg, "currently only CV_8UC1, CV_8UC3, CV_8UC4 textures are supported"); + break; + } + TextureManager& texMgr = TextureManager::getSingleton(); TexturePtr tex = texMgr.getByName(name, RESOURCEGROUP_NAME); Image im; - im.loadDynamicImage(image.ptr(), image.cols, image.rows, 1, PF_BYTE_BGR); + im.loadDynamicImage(image.ptr(), image.cols, image.rows, 1, format); if (tex) { @@ -323,7 +340,7 @@ public: void setBackground(InputArray image) { - CV_Assert(image.type() == CV_8UC3, bgplane); + CV_Assert(bgplane); String name = sceneMgr->getName() + "_Background"; @@ -770,5 +787,13 @@ void setMaterialProperty(const String& name, const String& prop, const Scalar& v if(!set) CV_Error_(Error::StsBadArg, ("shader parameter named '%s' not found", prop.c_str())); } + +void updateTexture(const String& name, InputArray image) +{ + CV_Assert(_app); + TexturePtr tex = TextureManager::getSingleton().getByName(name, RESOURCEGROUP_NAME); + CV_Assert(tex); + _createTexture(name, image.getMat()); +} } } From c655c31054a66c7bc1f3fac649066e52f224a17f Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Wed, 8 Aug 2018 17:05:46 +0200 Subject: [PATCH 2/2] ovis: allow up to 4 active texture units --- modules/ovis/include/opencv2/ovis.hpp | 6 +++++- modules/ovis/src/ovis.cpp | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/modules/ovis/include/opencv2/ovis.hpp b/modules/ovis/include/opencv2/ovis.hpp index afe2db967..d9f684c07 100644 --- a/modules/ovis/include/opencv2/ovis.hpp +++ b/modules/ovis/include/opencv2/ovis.hpp @@ -35,7 +35,11 @@ enum MaterialProperty MATERIAL_POINT_SIZE, MATERIAL_OPACITY, MATERIAL_EMISSIVE, - MATERIAL_TEXTURE + MATERIAL_TEXTURE0, + MATERIAL_TEXTURE = MATERIAL_TEXTURE0, + MATERIAL_TEXTURE1, + MATERIAL_TEXTURE2, + MATERIAL_TEXTURE3, }; enum EntityProperty diff --git a/modules/ovis/src/ovis.cpp b/modules/ovis/src/ovis.cpp index 3bccc25f5..44723c2cc 100644 --- a/modules/ovis/src/ovis.cpp +++ b/modules/ovis/src/ovis.cpp @@ -717,20 +717,23 @@ void setMaterialProperty(const String& name, int prop, const Scalar& val) void setMaterialProperty(const String& name, int prop, const String& value) { - CV_Assert(prop == MATERIAL_TEXTURE, _app); + CV_Assert(prop >= MATERIAL_TEXTURE0, prop <= MATERIAL_TEXTURE3, _app); MaterialPtr mat = MaterialManager::getSingleton().getByName(name, RESOURCEGROUP_NAME); CV_Assert(mat); Pass* rpass = mat->getTechniques()[0]->getPasses()[0]; - if (rpass->getTextureUnitStates().empty()) + size_t texUnit = prop - MATERIAL_TEXTURE0; + CV_Assert(texUnit <= rpass->getTextureUnitStates().size()); + + if (rpass->getTextureUnitStates().size() <= texUnit) { rpass->createTextureUnitState(value); return; } - rpass->getTextureUnitStates()[0]->setTextureName(value); + rpass->getTextureUnitStates()[texUnit]->setTextureName(value); } static bool setShaderProperty(const GpuProgramParametersSharedPtr& params, const String& prop,