From 806f9e849704d2c1439b9ff9d2c673dac8cda8e9 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 30 Sep 2021 19:20:35 -0700 Subject: [PATCH 01/22] fix selection buffer crash due to resize and incorrect selections Signed-off-by: Ian Chen --- ogre2/src/Ogre2RayQuery.cc | 4 ++ ogre2/src/Ogre2SelectionBuffer.cc | 76 +++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/ogre2/src/Ogre2RayQuery.cc b/ogre2/src/Ogre2RayQuery.cc index ea2468004..f46c82ede 100644 --- a/ogre2/src/Ogre2RayQuery.cc +++ b/ogre2/src/Ogre2RayQuery.cc @@ -123,6 +123,10 @@ RayQueryResult Ogre2RayQuery::ClosestPoint() ////////////////////////////////////////////////// RayQueryResult Ogre2RayQuery::ClosestPointBySelectionBuffer() { + // update selection buffer dimension in case window is resized + this->dataPtr->camera->SelectionBuffer()->SetDimensions( + this->dataPtr->camera->ImageWidth(), this->dataPtr->camera->ImageHeight()); + RayQueryResult result; Ogre::Item *ogreItem = nullptr; math::Vector3d point; diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 62686ccb8..0082f0ad3 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -88,6 +88,9 @@ class ignition::rendering::Ogre2SelectionBufferPrivate /// into a render target or render texture. public: Ogre::CompositorWorkspace *ogreCompositorWorkspace = nullptr; + /// \brief Name of the compositor workspace definition + public: std::string ogreCompWorkspaceDefName; + /// \brief The selection buffer material public: Ogre::MaterialPtr selectionMaterial; }; @@ -181,10 +184,27 @@ void Ogre2SelectionBuffer::Update() ///////////////////////////////////////////////// void Ogre2SelectionBuffer::DeleteRTTBuffer() { - if (this->dataPtr->selectionMaterial) + // \todo Delete material? + // This causes an ogre assertion error about unlinking renderables + // when the RTT buffer is created again + // if (this->dataPtr->selectionMaterial) + // { + // Ogre::MaterialManager::getSingleton().remove( + // this->dataPtr->selectionMaterial->getName()); + // this->dataPtr->selectionMaterial.setNull(); + // } + + if (this->dataPtr->ogreCompositorWorkspace) { - Ogre::MaterialManager::getSingleton().remove( - this->dataPtr->selectionMaterial->getName()); + // TODO(ahcorde): Remove the workspace. Potential leak here + this->dataPtr->ogreCompMgr->removeWorkspace( + this->dataPtr->ogreCompositorWorkspace); + + this->dataPtr->ogreCompMgr->removeWorkspaceDefinition( + this->dataPtr->ogreCompWorkspaceDefName); + this->dataPtr->ogreCompMgr->removeNodeDefinition( + this->dataPtr->ogreCompWorkspaceDefName + "/Node"); + this->dataPtr->ogreCompositorWorkspace = nullptr; } auto engine = Ogre2RenderEngine::Instance(); @@ -222,11 +242,18 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() // The SelectionBuffer material is defined in script // (selection_buffer.material). std::string matSelectionName = "SelectionBuffer"; - Ogre::MaterialPtr matSelection = + std::string matSelectionCloneName = + this->dataPtr->camera->getName() + "_" + matSelectionName; + this->dataPtr->selectionMaterial = Ogre::MaterialManager::getSingleton().getByName(matSelectionName); - this->dataPtr->selectionMaterial = matSelection->clone( - this->dataPtr->camera->getName() + "_" + matSelectionName); - this->dataPtr->selectionMaterial->load(); + if (this->dataPtr->selectionMaterial.isNull()) + { + Ogre::MaterialPtr matSelection = + Ogre::MaterialManager::getSingleton().getByName(matSelectionName); + this->dataPtr->selectionMaterial = matSelection->clone( + matSelectionCloneName); + this->dataPtr->selectionMaterial->load(); + } Ogre::Pass *p = this->dataPtr->selectionMaterial->getTechnique(0)->getPass(0); Ogre::GpuProgramParametersSharedPtr psParams = p->getFragmentProgramParameters(); @@ -252,13 +279,15 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() // create compositor workspace for rendering // Setup the selection buffer compositor. - const Ogre::String workspaceName = "SelectionBufferWorkspace" + + this->dataPtr->ogreCompWorkspaceDefName = "SelectionBufferWorkspace" + this->dataPtr->camera->getName(); + std::string nodeSpaceDefName = + this->dataPtr->ogreCompWorkspaceDefName + "/Node"; + Ogre::CompositorNodeDef *nodeDef = this->dataPtr->ogreCompMgr->addNodeDefinition( - "AutoGen " + Ogre::IdString(workspaceName + - "/Node").getReleaseText()); + nodeSpaceDefName); Ogre::TextureDefinitionBase::TextureDefinition *depthTexDef = nodeDef->addTextureDefinition("depthTexture"); depthTexDef->textureType = Ogre::TextureTypes::Type2D; @@ -346,7 +375,8 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() } Ogre::CompositorWorkspaceDef *workDef = - this->dataPtr->ogreCompMgr->addWorkspaceDefinition(workspaceName); + this->dataPtr->ogreCompMgr->addWorkspaceDefinition( + this->dataPtr->ogreCompWorkspaceDefName); workDef->connectExternal(0, nodeDef->getName(), 0); this->dataPtr->ogreCompositorWorkspace = @@ -354,7 +384,7 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->scene->OgreSceneManager(), this->dataPtr->renderTexture, this->dataPtr->selectionCamera, - workspaceName, + this->dataPtr->ogreCompWorkspaceDefName, false); } @@ -369,14 +399,6 @@ void Ogre2SelectionBuffer::SetDimensions( this->dataPtr->height = _height; this->DeleteRTTBuffer(); - - if (this->dataPtr->ogreCompositorWorkspace) - { - // TODO(ahcorde): Remove the workspace. Potential leak here - this->dataPtr->ogreCompMgr->removeWorkspace( - this->dataPtr->ogreCompositorWorkspace); - } - this->CreateRTTBuffer(); } ///////////////////////////////////////////////// @@ -398,6 +420,14 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, if (!this->dataPtr->camera) return false; + // check camera has valid projection matrix + // There could be nan values if camera was resized + Ogre::Matrix4 projectionMatrix = + this->dataPtr->camera->getProjectionMatrix(); + if (projectionMatrix.getTrans().isNaN() || + projectionMatrix.extractQuaternion().isNaN()) + return false; + const unsigned int targetWidth = this->dataPtr->width; const unsigned int targetHeight = this->dataPtr->height; @@ -426,9 +456,8 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, transMatrix[1][3] += y1+y2; Ogre::Matrix4 customProjectionMatrix = scaleMatrix * transMatrix * this->dataPtr->camera->getProjectionMatrix(); - - this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, - customProjectionMatrix); + this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, + customProjectionMatrix); this->dataPtr->selectionCamera->setPosition( this->dataPtr->camera->getDerivedPosition()); @@ -442,7 +471,6 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, image.convertFromTexture(this->dataPtr->renderTexture, 0, 0); Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); - // Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); float color = pixel[3]; uint32_t *rgba = reinterpret_cast(&color); unsigned int r = *rgba >> 24 & 0xFF; From 8350c99ec926134292ce70046d4cea92b30f5ab2 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 1 Oct 2021 22:26:08 -0700 Subject: [PATCH 02/22] test updating full selection buffer texture Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 53 ++++++++++++++++--------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 0082f0ad3..e24948a6d 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -228,7 +228,9 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() Ogre::GpuPageOutStrategy::SaveToSystemRam, Ogre::TextureFlags::RenderToTexture, Ogre::TextureTypes::Type2D); - this->dataPtr->renderTexture->setResolution(1, 1); + // this->dataPtr->renderTexture->setResolution(1, 1); + this->dataPtr->renderTexture->setResolution(this->dataPtr->width, + this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); @@ -435,29 +437,29 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, || _y >= static_cast(targetHeight)) return false; - // // 1x1 selection buffer, adapted from rviz - // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html - unsigned int width = 1; - unsigned int height = 1; - float x1 = static_cast(_x) / - static_cast(targetWidth - 1) - 0.5f; - float y1 = static_cast(_y) / - static_cast(targetHeight - 1) - 0.5f; - float x2 = static_cast(_x+width) / - static_cast(targetWidth - 1) - 0.5f; - float y2 = static_cast(_y+height) / - static_cast(targetHeight - 1) - 0.5f; - - Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; - Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; - scaleMatrix[0][0] = 1.0 / (x2-x1); - scaleMatrix[1][1] = 1.0 / (y2-y1); - transMatrix[0][3] -= x1+x2; - transMatrix[1][3] += y1+y2; - Ogre::Matrix4 customProjectionMatrix = - scaleMatrix * transMatrix * this->dataPtr->camera->getProjectionMatrix(); - this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, - customProjectionMatrix); + // // // 1x1 selection buffer, adapted from rviz + // // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html + // unsigned int width = 1; + // unsigned int height = 1; + // float x1 = static_cast(_x) / + // static_cast(targetWidth - 1) - 0.5f; + // float y1 = static_cast(_y) / + // static_cast(targetHeight - 1) - 0.5f; + // float x2 = static_cast(_x+width) / + // static_cast(targetWidth - 1) - 0.5f; + // float y2 = static_cast(_y+height) / + // static_cast(targetHeight - 1) - 0.5f; + + // Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; + // Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; + // scaleMatrix[0][0] = 1.0 / (x2-x1); + // scaleMatrix[1][1] = 1.0 / (y2-y1); + // transMatrix[0][3] -= x1+x2; + // transMatrix[1][3] += y1+y2; + // Ogre::Matrix4 customProjectionMatrix = + // scaleMatrix * transMatrix * this->dataPtr->camera->getProjectionMatrix(); + // this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, + // customProjectionMatrix); this->dataPtr->selectionCamera->setPosition( this->dataPtr->camera->getDerivedPosition()); @@ -470,7 +472,8 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, Ogre::Image2 image; image.convertFromTexture(this->dataPtr->renderTexture, 0, 0); - Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); + // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); + Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); float color = pixel[3]; uint32_t *rgba = reinterpret_cast(&color); unsigned int r = *rgba >> 24 & 0xFF; From 361f8a7f5a3ab100cc854e2827c187f73f28e370 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 1 Oct 2021 22:28:12 -0700 Subject: [PATCH 03/22] reenable visual at test Signed-off-by: Ian Chen --- test/integration/camera.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 4457e3b36..3ea305f28 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -187,14 +187,14 @@ void CameraTest::VisualAt(const std::string &_renderEngine) << _renderEngine << std::endl; return; } - else if (_renderEngine == "ogre2") - { - // VisualAt tests fail on CI, see issue #170 - // https://github.com/ignitionrobotics/ign-rendering/issues/170 - igndbg << "VisualAt test is disabled in " << _renderEngine << "." - << std::endl; - return; - } + // else if (_renderEngine == "ogre2") + // { + // // VisualAt tests fail on CI, see issue #170 + // // https://github.com/ignitionrobotics/ign-rendering/issues/170 + // igndbg << "VisualAt test is disabled in " << _renderEngine << "." + // << std::endl; + // return; + // } // create and populate scene RenderEngine *engine = rendering::engine(_renderEngine); From ff4ddb6b138520bf1a4275a2aebb6360269e93d7 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 1 Oct 2021 22:44:23 -0700 Subject: [PATCH 04/22] fix codecheck Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index e24948a6d..da988761e 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -457,7 +457,8 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, // transMatrix[0][3] -= x1+x2; // transMatrix[1][3] += y1+y2; // Ogre::Matrix4 customProjectionMatrix = - // scaleMatrix * transMatrix * this->dataPtr->camera->getProjectionMatrix(); + // scaleMatrix * transMatrix * + // this->dataPtr->camera->getProjectionMatrix(); // this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, // customProjectionMatrix); From 8e13cc637c16835a817d4ae8d4e3809ce91b3c56 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 09:46:18 -0700 Subject: [PATCH 05/22] testing rgb no depth, full buffer Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 66 +++++++++++++++++++++---------- test/integration/camera.cc | 1 + 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index da988761e..42c4532a0 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -232,7 +232,8 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->renderTexture->setResolution(this->dataPtr->width, this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); - this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); + // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); + this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); this->dataPtr->renderTexture->scheduleTransitionTo( Ogre::GpuResidency::Resident); @@ -240,6 +241,7 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->selectionCamera->addListener( this->dataPtr->materialSwitcher.get()); +/* // Load selection material // The SelectionBuffer material is defined in script // (selection_buffer.material). @@ -380,6 +382,13 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->ogreCompMgr->addWorkspaceDefinition( this->dataPtr->ogreCompWorkspaceDefName); workDef->connectExternal(0, nodeDef->getName(), 0); +*/ + +/////////// + this->dataPtr->ogreCompMgr->createBasicWorkspaceDef( + this->dataPtr->ogreCompWorkspaceDefName, + Ogre::ColourValue(0.1, 0.2, 0.3)); +/////////// this->dataPtr->ogreCompositorWorkspace = this->dataPtr->ogreCompMgr->addWorkspace( @@ -474,27 +483,42 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, image.convertFromTexture(this->dataPtr->renderTexture, 0, 0); // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); - Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); - float color = pixel[3]; - uint32_t *rgba = reinterpret_cast(&color); - unsigned int r = *rgba >> 24 & 0xFF; - unsigned int g = *rgba >> 16 & 0xFF; - unsigned int b = *rgba >> 8 & 0xFF; - - math::Vector3d point(pixel[0], pixel[1], pixel[2]); - - auto rot = Ogre2Conversions::Convert( - this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); - auto pos = Ogre2Conversions::Convert( - this->dataPtr->camera->getParentSceneNode()->_getDerivedPosition()); - math::Pose3d p(pos, rot); - point = rot * point + pos; - - ignition::math::Color cv; + +//////////////// + math::Vector3d point; + Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); + std::cerr << "got color value: " << colorValue.r << " " << colorValue.g + << " " << colorValue.b << std::endl; + + ignition::math::Color cv( + colorValue.r, + colorValue.g, + colorValue.b); + cv.A(1.0); - cv.R(r / 255.0); - cv.G(g / 255.0); - cv.B(b / 255.0); +///////////// + +// Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); +// float color = pixel[3]; +// uint32_t *rgba = reinterpret_cast(&color); +// unsigned int r = *rgba >> 24 & 0xFF; +// unsigned int g = *rgba >> 16 & 0xFF; +// unsigned int b = *rgba >> 8 & 0xFF; +// +// math::Vector3d point(pixel[0], pixel[1], pixel[2]); +// +// auto rot = Ogre2Conversions::Convert( +// this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); +// auto pos = Ogre2Conversions::Convert( +// this->dataPtr->camera->getParentSceneNode()->_getDerivedPosition()); +// math::Pose3d p(pos, rot); +// point = rot * point + pos; +// +// ignition::math::Color cv; +// cv.A(1.0); +// cv.R(r / 255.0); +// cv.G(g / 255.0); +// cv.B(b / 255.0); const std::string &entName = this->dataPtr->materialSwitcher->EntityName(cv); diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 3ea305f28..633421a4e 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -250,6 +250,7 @@ void CameraTest::VisualAt(const std::string &_renderEngine) for (auto x = 0u; x < camera->ImageWidth(); x = x + 100) { auto vis = camera->VisualAt(math::Vector2i(x, camera->ImageHeight() / 2)); + std::cerr << "x==== " << x << std::endl; if (x <= 100) { From ba2bd43ed1fd7390d5e9b842b3347e69feb065e0 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 12:28:32 -0700 Subject: [PATCH 06/22] use 1x1 buffer, still no depth data Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 58 +++++++++++++++---------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 42c4532a0..a465c6cc0 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -228,9 +228,9 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() Ogre::GpuPageOutStrategy::SaveToSystemRam, Ogre::TextureFlags::RenderToTexture, Ogre::TextureTypes::Type2D); - // this->dataPtr->renderTexture->setResolution(1, 1); - this->dataPtr->renderTexture->setResolution(this->dataPtr->width, - this->dataPtr->height); + this->dataPtr->renderTexture->setResolution(1, 1); + // this->dataPtr->renderTexture->setResolution(this->dataPtr->width, + // this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); @@ -446,30 +446,30 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, || _y >= static_cast(targetHeight)) return false; - // // // 1x1 selection buffer, adapted from rviz - // // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html - // unsigned int width = 1; - // unsigned int height = 1; - // float x1 = static_cast(_x) / - // static_cast(targetWidth - 1) - 0.5f; - // float y1 = static_cast(_y) / - // static_cast(targetHeight - 1) - 0.5f; - // float x2 = static_cast(_x+width) / - // static_cast(targetWidth - 1) - 0.5f; - // float y2 = static_cast(_y+height) / - // static_cast(targetHeight - 1) - 0.5f; - - // Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; - // Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; - // scaleMatrix[0][0] = 1.0 / (x2-x1); - // scaleMatrix[1][1] = 1.0 / (y2-y1); - // transMatrix[0][3] -= x1+x2; - // transMatrix[1][3] += y1+y2; - // Ogre::Matrix4 customProjectionMatrix = - // scaleMatrix * transMatrix * - // this->dataPtr->camera->getProjectionMatrix(); - // this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, - // customProjectionMatrix); + // // 1x1 selection buffer, adapted from rviz + // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html + unsigned int width = 1; + unsigned int height = 1; + float x1 = static_cast(_x) / + static_cast(targetWidth - 1) - 0.5f; + float y1 = static_cast(_y) / + static_cast(targetHeight - 1) - 0.5f; + float x2 = static_cast(_x+width) / + static_cast(targetWidth - 1) - 0.5f; + float y2 = static_cast(_y+height) / + static_cast(targetHeight - 1) - 0.5f; + + Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; + Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; + scaleMatrix[0][0] = 1.0 / (x2-x1); + scaleMatrix[1][1] = 1.0 / (y2-y1); + transMatrix[0][3] -= x1+x2; + transMatrix[1][3] += y1+y2; + Ogre::Matrix4 customProjectionMatrix = + scaleMatrix * transMatrix * + this->dataPtr->camera->getProjectionMatrix(); + this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, + customProjectionMatrix); this->dataPtr->selectionCamera->setPosition( this->dataPtr->camera->getDerivedPosition()); @@ -481,12 +481,12 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, Ogre::Image2 image; image.convertFromTexture(this->dataPtr->renderTexture, 0, 0); - // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); //////////////// math::Vector3d point; - Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); + // Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); + Ogre::ColourValue colorValue = image.getColourAt(0, 0, 0, 0); std::cerr << "got color value: " << colorValue.r << " " << colorValue.g << " " << colorValue.b << std::endl; From a1d1726d85a07e14c8f00a9763aa44832546d5f8 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 13:57:19 -0700 Subject: [PATCH 07/22] prnt scaling factor Signed-off-by: Ian Chen --- ogre2/src/Ogre2Camera.cc | 2 ++ ogre2/src/Ogre2SelectionBuffer.cc | 3 ++- test/integration/camera.cc | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index 86d1d7b96..f0e70647e 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -264,6 +264,8 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); + std::cerr << "scaling factor " << ratio << ", " << mousePos << std::endl; + Ogre::Item *ogreItem = this->selectionBuffer->OnSelectionClick( mousePos.X(), mousePos.Y()); diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index a465c6cc0..179488793 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -487,7 +487,8 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point; // Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); Ogre::ColourValue colorValue = image.getColourAt(0, 0, 0, 0); - std::cerr << "got color value: " << colorValue.r << " " << colorValue.g + std::cerr << "x: " << _x << "got color value: " << colorValue.r << " " + << colorValue.g << " " << colorValue.b << std::endl; ignition::math::Color cv( diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 633421a4e..3ea305f28 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -250,7 +250,6 @@ void CameraTest::VisualAt(const std::string &_renderEngine) for (auto x = 0u; x < camera->ImageWidth(); x = x + 100) { auto vis = camera->VisualAt(math::Vector2i(x, camera->ImageHeight() / 2)); - std::cerr << "x==== " << x << std::endl; if (x <= 100) { From 251c21c1f2b238832ab46eddb555db7f42c0b586 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 14:23:16 -0700 Subject: [PATCH 08/22] disable device ratio Signed-off-by: Ian Chen --- ogre2/src/Ogre2Camera.cc | 15 ++++++++------- ogre2/src/Ogre2SelectionBuffer.cc | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index f0e70647e..524968717 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -259,15 +259,16 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) this->ImageWidth(), this->ImageHeight()); } - float ratio = screenScalingFactor(); - ignition::math::Vector2i mousePos( - static_cast(std::rint(ratio * _mousePos.X())), - static_cast(std::rint(ratio * _mousePos.Y()))); - - std::cerr << "scaling factor " << ratio << ", " << mousePos << std::endl; + // float ratio = screenScalingFactor(); + // ignition::math::Vector2i mousePos( + // static_cast(std::rint(ratio * _mousePos.X())), + // static_cast(std::rint(ratio * _mousePos.Y()))); + // Ogre::Item *ogreItem = this->selectionBuffer->OnSelectionClick( + // mousePos.X(), mousePos.Y()); Ogre::Item *ogreItem = this->selectionBuffer->OnSelectionClick( - mousePos.X(), mousePos.Y()); + _mousePos.X(), _mousePos.Y()); + if (ogreItem) { diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 179488793..e1a0470c7 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -487,7 +487,7 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point; // Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); Ogre::ColourValue colorValue = image.getColourAt(0, 0, 0, 0); - std::cerr << "x: " << _x << "got color value: " << colorValue.r << " " + std::cerr << "x: " << _x << " got color value: " << colorValue.r << " " << colorValue.g << " " << colorValue.b << std::endl; From 3867a0c26592ca243e9eb0ca2165679b9eee20b4 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 15:17:06 -0700 Subject: [PATCH 09/22] reenable depth and utils test Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 80 +++++++++++++++---------------- src/Utils_TEST.cc | 16 +++---- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index e1a0470c7..e6b462c1b 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -230,10 +230,10 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() Ogre::TextureTypes::Type2D); this->dataPtr->renderTexture->setResolution(1, 1); // this->dataPtr->renderTexture->setResolution(this->dataPtr->width, - // this->dataPtr->height); + // this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); - // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); - this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); + this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); + // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); this->dataPtr->renderTexture->scheduleTransitionTo( Ogre::GpuResidency::Resident); @@ -241,7 +241,6 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->selectionCamera->addListener( this->dataPtr->materialSwitcher.get()); -/* // Load selection material // The SelectionBuffer material is defined in script // (selection_buffer.material). @@ -382,12 +381,11 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->ogreCompMgr->addWorkspaceDefinition( this->dataPtr->ogreCompWorkspaceDefName); workDef->connectExternal(0, nodeDef->getName(), 0); -*/ /////////// - this->dataPtr->ogreCompMgr->createBasicWorkspaceDef( - this->dataPtr->ogreCompWorkspaceDefName, - Ogre::ColourValue(0.1, 0.2, 0.3)); +// this->dataPtr->ogreCompMgr->createBasicWorkspaceDef( +// this->dataPtr->ogreCompWorkspaceDefName, +// Ogre::ColourValue(0.1, 0.2, 0.3)); /////////// this->dataPtr->ogreCompositorWorkspace = @@ -484,42 +482,42 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); //////////////// - math::Vector3d point; - // Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); - Ogre::ColourValue colorValue = image.getColourAt(0, 0, 0, 0); - std::cerr << "x: " << _x << " got color value: " << colorValue.r << " " - << colorValue.g - << " " << colorValue.b << std::endl; +// math::Vector3d point; +// // Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); +// Ogre::ColourValue colorValue = image.getColourAt(0, 0, 0, 0); +// std::cerr << "x: " << _x << " got color value: " << colorValue.r << " " +// << colorValue.g +// << " " << colorValue.b << std::endl; +// +// ignition::math::Color cv( +// colorValue.r, +// colorValue.g, +// colorValue.b); +// +// cv.A(1.0); +///////////// - ignition::math::Color cv( - colorValue.r, - colorValue.g, - colorValue.b); + Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); + float color = pixel[3]; + uint32_t *rgba = reinterpret_cast(&color); + unsigned int r = *rgba >> 24 & 0xFF; + unsigned int g = *rgba >> 16 & 0xFF; + unsigned int b = *rgba >> 8 & 0xFF; - cv.A(1.0); -///////////// + math::Vector3d point(pixel[0], pixel[1], pixel[2]); -// Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); -// float color = pixel[3]; -// uint32_t *rgba = reinterpret_cast(&color); -// unsigned int r = *rgba >> 24 & 0xFF; -// unsigned int g = *rgba >> 16 & 0xFF; -// unsigned int b = *rgba >> 8 & 0xFF; -// -// math::Vector3d point(pixel[0], pixel[1], pixel[2]); -// -// auto rot = Ogre2Conversions::Convert( -// this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); -// auto pos = Ogre2Conversions::Convert( -// this->dataPtr->camera->getParentSceneNode()->_getDerivedPosition()); -// math::Pose3d p(pos, rot); -// point = rot * point + pos; -// -// ignition::math::Color cv; -// cv.A(1.0); -// cv.R(r / 255.0); -// cv.G(g / 255.0); -// cv.B(b / 255.0); + auto rot = Ogre2Conversions::Convert( + this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); + auto pos = Ogre2Conversions::Convert( + this->dataPtr->camera->getParentSceneNode()->_getDerivedPosition()); + math::Pose3d p(pos, rot); + point = rot * point + pos; + + ignition::math::Color cv; + cv.A(1.0); + cv.R(r / 255.0); + cv.G(g / 255.0); + cv.B(b / 255.0); const std::string &entName = this->dataPtr->materialSwitcher->EntityName(cv); diff --git a/src/Utils_TEST.cc b/src/Utils_TEST.cc index 06b86b99a..fd53b854a 100644 --- a/src/Utils_TEST.cc +++ b/src/Utils_TEST.cc @@ -121,14 +121,14 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) root->AddChild(camera); camera->Update(); - if (_renderEngine == "ogre2") - { - // tests using selection buffer fail on CI, see issue #170 - // https://github.com/ignitionrobotics/ign-rendering/issues/170 - igndbg << "Selection buffer based screenToScene test is disabled in " - << _renderEngine << "." << std::endl; - return; - } + // if (_renderEngine == "ogre2") + // { + // // tests using selection buffer fail on CI, see issue #170 + // // https://github.com/ignitionrobotics/ign-rendering/issues/170 + // igndbg << "Selection buffer based screenToScene test is disabled in " + // << _renderEngine << "." << std::endl; + // return; + // } // \todo(anyone) // the centerClick var above is set to a screen pos of (width/2, height/2). From e31b06c3f2e10e53cabbb557bb5aef152a4aacfd Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 16:14:26 -0700 Subject: [PATCH 10/22] disable utils test, add visual at test after resize Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 4 ++++ src/Utils_TEST.cc | 28 ++++++++++++++-------------- test/integration/camera.cc | 23 +++++++++++++++++++++++ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index e6b462c1b..430e51283 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -506,6 +506,10 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point(pixel[0], pixel[1], pixel[2]); + std::cerr << "x: " << _x << " got color value: " << r << " " + << g << " " << b << ", point: " << point << std::endl; + + auto rot = Ogre2Conversions::Convert( this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); auto pos = Ogre2Conversions::Convert( diff --git a/src/Utils_TEST.cc b/src/Utils_TEST.cc index fd53b854a..b3f9786ef 100644 --- a/src/Utils_TEST.cc +++ b/src/Utils_TEST.cc @@ -121,14 +121,14 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) root->AddChild(camera); camera->Update(); - // if (_renderEngine == "ogre2") - // { - // // tests using selection buffer fail on CI, see issue #170 - // // https://github.com/ignitionrobotics/ign-rendering/issues/170 - // igndbg << "Selection buffer based screenToScene test is disabled in " - // << _renderEngine << "." << std::endl; - // return; - // } + if (_renderEngine == "ogre2") + { + // tests using selection buffer fail on CI, see issue #170 + // https://github.com/ignitionrobotics/ign-rendering/issues/170 + igndbg << "Selection buffer based screenToScene test is disabled in " + << _renderEngine << "." << std::endl; + return; + } // \todo(anyone) // the centerClick var above is set to a screen pos of (width/2, height/2). @@ -143,20 +143,20 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) // API without RayQueryResult and default max distance result = screenToScene(centerClick, camera, rayQuery, rayResult); - EXPECT_NEAR(0.5, result.Z(), 3e-6); + EXPECT_NEAR(0.5, result.Z(), 5e-4); EXPECT_NEAR(0.0, result.X(), 2e-6); EXPECT_NEAR(0.0, result.Y(), 2e-6); EXPECT_TRUE(rayResult); - EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); + EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 5e-4); EXPECT_EQ(box->Id(), rayResult.objectId); result = screenToScene(centerClick, camera, rayQuery, rayResult, 20.0); - EXPECT_NEAR(0.5, result.Z(), 3e-6); + EXPECT_NEAR(0.5, result.Z(), 5e-4); EXPECT_NEAR(0.0, result.X(), 2e-6); EXPECT_NEAR(0.0, result.Y(), 2e-6); EXPECT_TRUE(rayResult); - EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); + EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 5e-4); EXPECT_EQ(box->Id(), rayResult.objectId); // Move camera closer to box @@ -165,11 +165,11 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) result = screenToScene(centerClick, camera, rayQuery, rayResult); - EXPECT_NEAR(0.5, result.Z(), 3e-6); + EXPECT_NEAR(0.5, result.Z(), 1e-4); EXPECT_NEAR(0.0, result.X(), 2e-6); EXPECT_NEAR(0.0, result.Y(), 2e-6); EXPECT_TRUE(rayResult); - EXPECT_NEAR(6.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); + EXPECT_NEAR(6.5 - camera->NearClipPlane(), rayResult.distance, 1e-4); EXPECT_EQ(box->Id(), rayResult.objectId); } diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 3ea305f28..3e933584a 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -285,6 +285,29 @@ void CameraTest::VisualAt(const std::string &_renderEngine) } } + // change camera size + camera->SetImageWidth(1200); + camera->SetImageHeight(800); + + // render a few frames + for (auto i = 0; i < 30; ++i) + { + camera->Update(); + } + + // test that VisualAt still works after resize + { + double x = 300; + auto vis = camera->VisualAt(math::Vector2i(x, camera->ImageHeight() / 2)); + EXPECT_NE(nullptr, vis) << "X: " << x; + if (vis) + { + EXPECT_EQ("sphere", vis->Name()); + } + } + + + // Clean up engine->DestroyScene(scene); rendering::unloadEngine(engine->Name()); From adf0a3e8fb486545df325c8f5312c62f2c1dc98a Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sat, 2 Oct 2021 23:42:34 -0700 Subject: [PATCH 11/22] test texelfetch Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 1 - .../src/media/materials/programs/selection_buffer_fs.glsl | 7 +++++-- .../src/media/materials/scripts/selection_buffer.material | 2 ++ test/integration/camera.cc | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 430e51283..07554900d 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -509,7 +509,6 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, std::cerr << "x: " << _x << " got color value: " << r << " " << g << " " << b << ", point: " << point << std::endl; - auto rot = Ogre2Conversions::Convert( this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); auto pos = Ogre2Conversions::Convert( diff --git a/ogre2/src/media/materials/programs/selection_buffer_fs.glsl b/ogre2/src/media/materials/programs/selection_buffer_fs.glsl index d9acd041e..0fd84bc22 100644 --- a/ogre2/src/media/materials/programs/selection_buffer_fs.glsl +++ b/ogre2/src/media/materials/programs/selection_buffer_fs.glsl @@ -25,6 +25,7 @@ in block uniform sampler2D colorTexture; uniform sampler2D depthTexture; +uniform vec4 colorTexResolution; out vec4 fragColor; @@ -58,9 +59,11 @@ void main() point = vec3(inf); // color - vec4 color = texture(colorTexture, inPs.uv0); + vec4 color = texelFetch(colorTexture, + ivec2(inPs.uv0 * colorTexResolution.xy), 0); float rgba = packFloat(color); - fragColor = vec4(point.xyz, rgba); + // fragColor = vec4(point.xyz, rgba); + fragColor = vec4(color.rgb, rgba); } diff --git a/ogre2/src/media/materials/scripts/selection_buffer.material b/ogre2/src/media/materials/scripts/selection_buffer.material index a23bcf287..8b00054c0 100644 --- a/ogre2/src/media/materials/scripts/selection_buffer.material +++ b/ogre2/src/media/materials/scripts/selection_buffer.material @@ -22,6 +22,8 @@ fragment_program selection_buffer_fs glsl { param_named colorTexture int 0 param_named depthTexture int 1 + + param_named_auto colorTexResolution texture_size 0 } } diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 3e933584a..f2ffdfe74 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -297,7 +297,7 @@ void CameraTest::VisualAt(const std::string &_renderEngine) // test that VisualAt still works after resize { - double x = 300; + unsigned int x = 300u; auto vis = camera->VisualAt(math::Vector2i(x, camera->ImageHeight() / 2)); EXPECT_NE(nullptr, vis) << "X: " << x; if (vis) From 2a1d370c0d6976550ffe42be5b56f38f9bffe22a Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Sun, 3 Oct 2021 11:33:19 -0700 Subject: [PATCH 12/22] back to full buffer Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 58 ++++++++++++++++--------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 07554900d..58c5125bc 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -228,9 +228,9 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() Ogre::GpuPageOutStrategy::SaveToSystemRam, Ogre::TextureFlags::RenderToTexture, Ogre::TextureTypes::Type2D); - this->dataPtr->renderTexture->setResolution(1, 1); - // this->dataPtr->renderTexture->setResolution(this->dataPtr->width, - // this->dataPtr->height); + // this->dataPtr->renderTexture->setResolution(1, 1); + this->dataPtr->renderTexture->setResolution(this->dataPtr->width, + this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); @@ -444,30 +444,30 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, || _y >= static_cast(targetHeight)) return false; - // // 1x1 selection buffer, adapted from rviz - // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html - unsigned int width = 1; - unsigned int height = 1; - float x1 = static_cast(_x) / - static_cast(targetWidth - 1) - 0.5f; - float y1 = static_cast(_y) / - static_cast(targetHeight - 1) - 0.5f; - float x2 = static_cast(_x+width) / - static_cast(targetWidth - 1) - 0.5f; - float y2 = static_cast(_y+height) / - static_cast(targetHeight - 1) - 0.5f; - - Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; - Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; - scaleMatrix[0][0] = 1.0 / (x2-x1); - scaleMatrix[1][1] = 1.0 / (y2-y1); - transMatrix[0][3] -= x1+x2; - transMatrix[1][3] += y1+y2; - Ogre::Matrix4 customProjectionMatrix = - scaleMatrix * transMatrix * - this->dataPtr->camera->getProjectionMatrix(); - this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, - customProjectionMatrix); + // // // 1x1 selection buffer, adapted from rviz + // // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html + // unsigned int width = 1; + // unsigned int height = 1; + // float x1 = static_cast(_x) / + // static_cast(targetWidth - 1) - 0.5f; + // float y1 = static_cast(_y) / + // static_cast(targetHeight - 1) - 0.5f; + // float x2 = static_cast(_x+width) / + // static_cast(targetWidth - 1) - 0.5f; + // float y2 = static_cast(_y+height) / + // static_cast(targetHeight - 1) - 0.5f; + + // Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; + // Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; + // scaleMatrix[0][0] = 1.0 / (x2-x1); + // scaleMatrix[1][1] = 1.0 / (y2-y1); + // transMatrix[0][3] -= x1+x2; + // transMatrix[1][3] += y1+y2; + // Ogre::Matrix4 customProjectionMatrix = + // scaleMatrix * transMatrix * + // this->dataPtr->camera->getProjectionMatrix(); + // this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, + // customProjectionMatrix); this->dataPtr->selectionCamera->setPosition( this->dataPtr->camera->getDerivedPosition()); @@ -497,7 +497,9 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, // cv.A(1.0); ///////////// - Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); + // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); + Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); + float color = pixel[3]; uint32_t *rgba = reinterpret_cast(&color); unsigned int r = *rgba >> 24 & 0xFF; From 5010e1a4f8554c02245763f24ce2f3f84eec0828 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 6 Oct 2021 22:58:23 -0700 Subject: [PATCH 13/22] print ogre log Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 2 +- .../materials/programs/selection_buffer_fs.glsl | 2 +- test/integration/camera.cc | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 58c5125bc..e6a7fe67c 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -509,7 +509,7 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point(pixel[0], pixel[1], pixel[2]); std::cerr << "x: " << _x << " got color value: " << r << " " - << g << " " << b << ", point: " << point << std::endl; + << g << " " << b << ", point: " << point << " | " << color << std::endl; auto rot = Ogre2Conversions::Convert( this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); diff --git a/ogre2/src/media/materials/programs/selection_buffer_fs.glsl b/ogre2/src/media/materials/programs/selection_buffer_fs.glsl index 0fd84bc22..002290a6d 100644 --- a/ogre2/src/media/materials/programs/selection_buffer_fs.glsl +++ b/ogre2/src/media/materials/programs/selection_buffer_fs.glsl @@ -65,5 +65,5 @@ void main() float rgba = packFloat(color); // fragColor = vec4(point.xyz, rgba); - fragColor = vec4(color.rgb, rgba); + fragColor = vec4(0.1, 0.2, 0.3, 0.5); } diff --git a/test/integration/camera.cc b/test/integration/camera.cc index f2ffdfe74..5849b44ee 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -244,6 +244,20 @@ void CameraTest::VisualAt(const std::string &_renderEngine) camera->Update(); } + std::string home; + ignition::common::env(IGN_HOMEDIR, home); + std::ifstream file(home + "/.ignition/rendering/ogre2.log"); + if (!file.is_open()) + { + std::cerr << "Failed to read file ogre2.log" << std::endl; + return; + } + std::string str((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + std::cerr << "========================\n" << str << std::endl; + + + EXPECT_EQ(800u, camera->ImageWidth()); EXPECT_EQ(600u, camera->ImageHeight()); From 9809ed56647bf95256939277b1779bd3f623db07 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 6 Oct 2021 23:03:15 -0700 Subject: [PATCH 14/22] codecheck Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index e6a7fe67c..1b7c8340e 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -509,7 +509,8 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point(pixel[0], pixel[1], pixel[2]); std::cerr << "x: " << _x << " got color value: " << r << " " - << g << " " << b << ", point: " << point << " | " << color << std::endl; + << g << " " << b << ", point: " << point << " | " << color + << std::endl; auto rot = Ogre2Conversions::Convert( this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); From 36fd8b828ce748f9bc8bff31f7a4acc1ea9c89f5 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 6 Oct 2021 23:45:15 -0700 Subject: [PATCH 15/22] fixing selection buffer mat script Signed-off-by: Ian Chen --- .../materials/scripts/selection_buffer.material | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ogre2/src/media/materials/scripts/selection_buffer.material b/ogre2/src/media/materials/scripts/selection_buffer.material index 8b00054c0..a64d57878 100644 --- a/ogre2/src/media/materials/scripts/selection_buffer.material +++ b/ogre2/src/media/materials/scripts/selection_buffer.material @@ -15,6 +15,16 @@ * */ +vertex_program selection_buffer_vs glsl +{ + // reuse depth camera vertex shader + source depth_camera_vs.glsl + default_params + { + param_named_auto worldViewProj worldviewproj_matrix + } +} + fragment_program selection_buffer_fs glsl { source selection_buffer_fs.glsl @@ -36,9 +46,7 @@ material SelectionBuffer pass { // Make this pass use the vertex shader defined above - vertex_program_ref DepthCameraVS - { - } + vertex_program_ref selection_buffer_vs { } // Make this pass use the pixel shader defined above fragment_program_ref selection_buffer_fs { } From bbbd0b67bf61fb8bdfdb473d7828fe52ba459d34 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 7 Oct 2021 00:21:19 -0700 Subject: [PATCH 16/22] try 1x1 buffer again Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 59 +++++++++---------- .../programs/selection_buffer_fs.glsl | 3 +- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 1b7c8340e..a70d94aad 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -228,9 +228,9 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() Ogre::GpuPageOutStrategy::SaveToSystemRam, Ogre::TextureFlags::RenderToTexture, Ogre::TextureTypes::Type2D); - // this->dataPtr->renderTexture->setResolution(1, 1); - this->dataPtr->renderTexture->setResolution(this->dataPtr->width, - this->dataPtr->height); + this->dataPtr->renderTexture->setResolution(1, 1); + // this->dataPtr->renderTexture->setResolution(this->dataPtr->width, + // this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); @@ -444,30 +444,30 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, || _y >= static_cast(targetHeight)) return false; - // // // 1x1 selection buffer, adapted from rviz - // // // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html - // unsigned int width = 1; - // unsigned int height = 1; - // float x1 = static_cast(_x) / - // static_cast(targetWidth - 1) - 0.5f; - // float y1 = static_cast(_y) / - // static_cast(targetHeight - 1) - 0.5f; - // float x2 = static_cast(_x+width) / - // static_cast(targetWidth - 1) - 0.5f; - // float y2 = static_cast(_y+height) / - // static_cast(targetHeight - 1) - 0.5f; - - // Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; - // Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; - // scaleMatrix[0][0] = 1.0 / (x2-x1); - // scaleMatrix[1][1] = 1.0 / (y2-y1); - // transMatrix[0][3] -= x1+x2; - // transMatrix[1][3] += y1+y2; - // Ogre::Matrix4 customProjectionMatrix = - // scaleMatrix * transMatrix * - // this->dataPtr->camera->getProjectionMatrix(); - // this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, - // customProjectionMatrix); + // 1x1 selection buffer, adapted from rviz + // http://docs.ros.org/indigo/api/rviz/html/c++/selection__manager_8cpp.html + unsigned int width = 1; + unsigned int height = 1; + float x1 = static_cast(_x) / + static_cast(targetWidth - 1) - 0.5f; + float y1 = static_cast(_y) / + static_cast(targetHeight - 1) - 0.5f; + float x2 = static_cast(_x+width) / + static_cast(targetWidth - 1) - 0.5f; + float y2 = static_cast(_y+height) / + static_cast(targetHeight - 1) - 0.5f; + + Ogre::Matrix4 scaleMatrix = Ogre::Matrix4::IDENTITY; + Ogre::Matrix4 transMatrix = Ogre::Matrix4::IDENTITY; + scaleMatrix[0][0] = 1.0 / (x2-x1); + scaleMatrix[1][1] = 1.0 / (y2-y1); + transMatrix[0][3] -= x1+x2; + transMatrix[1][3] += y1+y2; + Ogre::Matrix4 customProjectionMatrix = + scaleMatrix * transMatrix * + this->dataPtr->camera->getProjectionMatrix(); + this->dataPtr->selectionCamera->setCustomProjectionMatrix(true, + customProjectionMatrix); this->dataPtr->selectionCamera->setPosition( this->dataPtr->camera->getDerivedPosition()); @@ -479,7 +479,7 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, Ogre::Image2 image; image.convertFromTexture(this->dataPtr->renderTexture, 0, 0); - // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); + Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); //////////////// // math::Vector3d point; @@ -497,8 +497,7 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, // cv.A(1.0); ///////////// - // Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); - Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); + // Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); float color = pixel[3]; uint32_t *rgba = reinterpret_cast(&color); diff --git a/ogre2/src/media/materials/programs/selection_buffer_fs.glsl b/ogre2/src/media/materials/programs/selection_buffer_fs.glsl index 002290a6d..a80bcf4ad 100644 --- a/ogre2/src/media/materials/programs/selection_buffer_fs.glsl +++ b/ogre2/src/media/materials/programs/selection_buffer_fs.glsl @@ -64,6 +64,5 @@ void main() float rgba = packFloat(color); - // fragColor = vec4(point.xyz, rgba); - fragColor = vec4(0.1, 0.2, 0.3, 0.5); + fragColor = vec4(point.xyz, rgba); } From a1edad4ccbbcd3f94bb9f414fdb9351991cccef0 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 7 Oct 2021 01:36:30 -0700 Subject: [PATCH 17/22] revert some test changes Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 6 +++--- src/Utils_TEST.cc | 28 ++++++++++++++-------------- test/integration/camera.cc | 24 +++++++++++------------- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index a70d94aad..6ead15020 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -507,9 +507,9 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point(pixel[0], pixel[1], pixel[2]); - std::cerr << "x: " << _x << " got color value: " << r << " " - << g << " " << b << ", point: " << point << " | " << color - << std::endl; + // std::cerr << "x: " << _x << " got color value: " << r << " " + // << g << " " << b << ", point: " << point << " | " << color + // << std::endl; auto rot = Ogre2Conversions::Convert( this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); diff --git a/src/Utils_TEST.cc b/src/Utils_TEST.cc index b3f9786ef..e1f927a80 100644 --- a/src/Utils_TEST.cc +++ b/src/Utils_TEST.cc @@ -121,14 +121,14 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) root->AddChild(camera); camera->Update(); - if (_renderEngine == "ogre2") - { - // tests using selection buffer fail on CI, see issue #170 - // https://github.com/ignitionrobotics/ign-rendering/issues/170 - igndbg << "Selection buffer based screenToScene test is disabled in " - << _renderEngine << "." << std::endl; - return; - } +// if (_renderEngine == "ogre2") +// { +// // tests using selection buffer fail on CI, see issue #170 +// // https://github.com/ignitionrobotics/ign-rendering/issues/170 +// igndbg << "Selection buffer based screenToScene test is disabled in " +// << _renderEngine << "." << std::endl; +// return; +// } // \todo(anyone) // the centerClick var above is set to a screen pos of (width/2, height/2). @@ -143,20 +143,20 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) // API without RayQueryResult and default max distance result = screenToScene(centerClick, camera, rayQuery, rayResult); - EXPECT_NEAR(0.5, result.Z(), 5e-4); + EXPECT_NEAR(0.5, result.Z(), 3e-6); EXPECT_NEAR(0.0, result.X(), 2e-6); EXPECT_NEAR(0.0, result.Y(), 2e-6); EXPECT_TRUE(rayResult); - EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 5e-4); + EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); EXPECT_EQ(box->Id(), rayResult.objectId); result = screenToScene(centerClick, camera, rayQuery, rayResult, 20.0); - EXPECT_NEAR(0.5, result.Z(), 5e-4); + EXPECT_NEAR(0.5, result.Z(), 3e-6); EXPECT_NEAR(0.0, result.X(), 2e-6); EXPECT_NEAR(0.0, result.Y(), 2e-6); EXPECT_TRUE(rayResult); - EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 5e-4); + EXPECT_NEAR(14.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); EXPECT_EQ(box->Id(), rayResult.objectId); // Move camera closer to box @@ -165,11 +165,11 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) result = screenToScene(centerClick, camera, rayQuery, rayResult); - EXPECT_NEAR(0.5, result.Z(), 1e-4); + EXPECT_NEAR(0.5, result.Z(), 3e-6); EXPECT_NEAR(0.0, result.X(), 2e-6); EXPECT_NEAR(0.0, result.Y(), 2e-6); EXPECT_TRUE(rayResult); - EXPECT_NEAR(6.5 - camera->NearClipPlane(), rayResult.distance, 1e-4); + EXPECT_NEAR(6.5 - camera->NearClipPlane(), rayResult.distance, 4e-6); EXPECT_EQ(box->Id(), rayResult.objectId); } diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 5849b44ee..9cbbeda54 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -244,19 +244,17 @@ void CameraTest::VisualAt(const std::string &_renderEngine) camera->Update(); } - std::string home; - ignition::common::env(IGN_HOMEDIR, home); - std::ifstream file(home + "/.ignition/rendering/ogre2.log"); - if (!file.is_open()) - { - std::cerr << "Failed to read file ogre2.log" << std::endl; - return; - } - std::string str((std::istreambuf_iterator(file)), - std::istreambuf_iterator()); - std::cerr << "========================\n" << str << std::endl; - - + // std::string home; + // ignition::common::env(IGN_HOMEDIR, home); + // std::ifstream file(home + "/.ignition/rendering/ogre2.log"); + // if (!file.is_open()) + // { + // std::cerr << "Failed to read file ogre2.log" << std::endl; + // return; + // } + // std::string str((std::istreambuf_iterator(file)), + // std::istreambuf_iterator()); + // std::cerr << "========================\n" << str << std::endl; EXPECT_EQ(800u, camera->ImageWidth()); EXPECT_EQ(600u, camera->ImageHeight()); From bfacea067d2357f49d5cd2e30871e9f2ae0765fa Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 19 Oct 2021 16:20:35 -0700 Subject: [PATCH 18/22] uncomment tests Signed-off-by: Ian Chen --- ogre2/src/Ogre2Camera.cc | 14 +++++--------- ogre2/src/Ogre2SelectionBuffer.cc | 28 ---------------------------- test/integration/camera.cc | 20 -------------------- test/integration/scene.cc | 8 -------- 4 files changed, 5 insertions(+), 65 deletions(-) diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index 524968717..c259fff64 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -259,16 +259,12 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) this->ImageWidth(), this->ImageHeight()); } - // float ratio = screenScalingFactor(); - // ignition::math::Vector2i mousePos( - // static_cast(std::rint(ratio * _mousePos.X())), - // static_cast(std::rint(ratio * _mousePos.Y()))); - // Ogre::Item *ogreItem = this->selectionBuffer->OnSelectionClick( - // mousePos.X(), mousePos.Y()); - + float ratio = screenScalingFactor(); + ignition::math::Vector2i mousePos( + static_cast(std::rint(ratio * _mousePos.X())), + static_cast(std::rint(ratio * _mousePos.Y()))); Ogre::Item *ogreItem = this->selectionBuffer->OnSelectionClick( - _mousePos.X(), _mousePos.Y()); - + mousePos.X(), mousePos.Y()); if (ogreItem) { diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 6ead15020..79d4e7882 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -382,12 +382,6 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() this->dataPtr->ogreCompWorkspaceDefName); workDef->connectExternal(0, nodeDef->getName(), 0); -/////////// -// this->dataPtr->ogreCompMgr->createBasicWorkspaceDef( -// this->dataPtr->ogreCompWorkspaceDefName, -// Ogre::ColourValue(0.1, 0.2, 0.3)); -/////////// - this->dataPtr->ogreCompositorWorkspace = this->dataPtr->ogreCompMgr->addWorkspace( this->dataPtr->scene->OgreSceneManager(), @@ -481,24 +475,6 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, image.convertFromTexture(this->dataPtr->renderTexture, 0, 0); Ogre::ColourValue pixel = image.getColourAt(0, 0, 0, 0); -//////////////// -// math::Vector3d point; -// // Ogre::ColourValue colorValue = image.getColourAt(_x, _y, 0, 0); -// Ogre::ColourValue colorValue = image.getColourAt(0, 0, 0, 0); -// std::cerr << "x: " << _x << " got color value: " << colorValue.r << " " -// << colorValue.g -// << " " << colorValue.b << std::endl; -// -// ignition::math::Color cv( -// colorValue.r, -// colorValue.g, -// colorValue.b); -// -// cv.A(1.0); -///////////// - - // Ogre::ColourValue pixel = image.getColourAt(_x, _y, 0, 0); - float color = pixel[3]; uint32_t *rgba = reinterpret_cast(&color); unsigned int r = *rgba >> 24 & 0xFF; @@ -507,10 +483,6 @@ bool Ogre2SelectionBuffer::ExecuteQuery(const int _x, const int _y, math::Vector3d point(pixel[0], pixel[1], pixel[2]); - // std::cerr << "x: " << _x << " got color value: " << r << " " - // << g << " " << b << ", point: " << point << " | " << color - // << std::endl; - auto rot = Ogre2Conversions::Convert( this->dataPtr->camera->getParentSceneNode()->_getDerivedOrientation()); auto pos = Ogre2Conversions::Convert( diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 9cbbeda54..74e72532c 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -187,14 +187,6 @@ void CameraTest::VisualAt(const std::string &_renderEngine) << _renderEngine << std::endl; return; } - // else if (_renderEngine == "ogre2") - // { - // // VisualAt tests fail on CI, see issue #170 - // // https://github.com/ignitionrobotics/ign-rendering/issues/170 - // igndbg << "VisualAt test is disabled in " << _renderEngine << "." - // << std::endl; - // return; - // } // create and populate scene RenderEngine *engine = rendering::engine(_renderEngine); @@ -244,18 +236,6 @@ void CameraTest::VisualAt(const std::string &_renderEngine) camera->Update(); } - // std::string home; - // ignition::common::env(IGN_HOMEDIR, home); - // std::ifstream file(home + "/.ignition/rendering/ogre2.log"); - // if (!file.is_open()) - // { - // std::cerr << "Failed to read file ogre2.log" << std::endl; - // return; - // } - // std::string str((std::istreambuf_iterator(file)), - // std::istreambuf_iterator()); - // std::cerr << "========================\n" << str << std::endl; - EXPECT_EQ(800u, camera->ImageWidth()); EXPECT_EQ(600u, camera->ImageHeight()); diff --git a/test/integration/scene.cc b/test/integration/scene.cc index ebca2cd74..79e657e3c 100644 --- a/test/integration/scene.cc +++ b/test/integration/scene.cc @@ -154,14 +154,6 @@ void SceneTest::VisualAt(const std::string &_renderEngine) << _renderEngine << std::endl; return; } - else if (_renderEngine == "ogre2") - { - // VisualAt tests fail on CI, see issue #170 - // https://github.com/ignitionrobotics/ign-rendering/issues/170 - igndbg << "VisualAt test is disabled in " << _renderEngine << "." - << std::endl; - return; - } // create and populate scene RenderEngine *engine = rendering::engine(_renderEngine); From ce66356d50701af658b12bf51293489343c4283b Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Tue, 19 Oct 2021 17:36:53 -0700 Subject: [PATCH 19/22] update scaling factor Signed-off-by: Ian Chen --- ogre/src/OgreCamera.cc | 3 ++- ogre2/src/Ogre2Camera.cc | 3 ++- src/Utils_TEST.cc | 11 +---------- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/ogre/src/OgreCamera.cc b/ogre/src/OgreCamera.cc index 39a99c3f5..f1a19c018 100644 --- a/ogre/src/OgreCamera.cc +++ b/ogre/src/OgreCamera.cc @@ -222,7 +222,8 @@ VisualPtr OgreCamera::VisualAt(const ignition::math::Vector2i } } - float ratio = screenScalingFactor(); + // float ratio = screenScalingFactor(); + float ratio = 1.0f; ignition::math::Vector2i mousePos( static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index c259fff64..c08a63a84 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -259,7 +259,8 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) this->ImageWidth(), this->ImageHeight()); } - float ratio = screenScalingFactor(); + // float ratio = screenScalingFactor(); + float ratio = 1.0f; ignition::math::Vector2i mousePos( static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); diff --git a/src/Utils_TEST.cc b/src/Utils_TEST.cc index 97f2ce825..5440c11f3 100644 --- a/src/Utils_TEST.cc +++ b/src/Utils_TEST.cc @@ -121,15 +121,6 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) root->AddChild(camera); camera->Update(); -// if (_renderEngine == "ogre2") -// { -// // tests using selection buffer fail on CI, see issue #170 -// // https://github.com/ignitionrobotics/ign-rendering/issues/170 -// igndbg << "Selection buffer based screenToScene test is disabled in " -// << _renderEngine << "." << std::endl; -// return; -// } - // \todo(anyone) // the centerClick var above is set to a screen pos of (width/2, height/2). // This is off-by-1. The actual center pos should be at @@ -137,7 +128,7 @@ void UtilTest::ClickToScene(const std::string &_renderEngine) // from the expected position. However, fixing the centerClick above caused // the screenToPlane tests to fail so only modifying the pos here, and the // cause of test failure need to be investigated. - centerClick = ignition::math::Vector2i(halfWidth-1, halfHeight-1); + // centerClick = ignition::math::Vector2i(halfWidth-1, halfHeight-1); // API without RayQueryResult and default max distance result = screenToScene(centerClick, camera, rayQuery, rayResult); From ab5b3971ab4f40545b42763472134bccfb179e49 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 11 Nov 2021 11:20:52 -0800 Subject: [PATCH 20/22] fix removing selection mat Signed-off-by: Ian Chen --- ogre2/src/Ogre2SelectionBuffer.cc | 25 ++++++++++--------------- test/integration/camera.cc | 2 -- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/ogre2/src/Ogre2SelectionBuffer.cc b/ogre2/src/Ogre2SelectionBuffer.cc index 79d4e7882..72194730c 100644 --- a/ogre2/src/Ogre2SelectionBuffer.cc +++ b/ogre2/src/Ogre2SelectionBuffer.cc @@ -145,6 +145,16 @@ Ogre2SelectionBuffer::~Ogre2SelectionBuffer() { this->DeleteRTTBuffer(); + // remove selectionMaterial in destructor + // this does not need to be done in DeleteRTTBuffer as we do not need to + // reload the same material every time + if (!this->dataPtr->selectionMaterial.isNull()) + { + Ogre::MaterialManager::getSingleton().remove( + this->dataPtr->selectionMaterial->getName()); + this->dataPtr->selectionMaterial.setNull(); + } + // remove selection buffer camera this->dataPtr->sceneMgr->destroyCamera(this->dataPtr->selectionCamera); } @@ -184,16 +194,6 @@ void Ogre2SelectionBuffer::Update() ///////////////////////////////////////////////// void Ogre2SelectionBuffer::DeleteRTTBuffer() { - // \todo Delete material? - // This causes an ogre assertion error about unlinking renderables - // when the RTT buffer is created again - // if (this->dataPtr->selectionMaterial) - // { - // Ogre::MaterialManager::getSingleton().remove( - // this->dataPtr->selectionMaterial->getName()); - // this->dataPtr->selectionMaterial.setNull(); - // } - if (this->dataPtr->ogreCompositorWorkspace) { // TODO(ahcorde): Remove the workspace. Potential leak here @@ -229,11 +229,8 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() Ogre::TextureFlags::RenderToTexture, Ogre::TextureTypes::Type2D); this->dataPtr->renderTexture->setResolution(1, 1); - // this->dataPtr->renderTexture->setResolution(this->dataPtr->width, - // this->dataPtr->height); this->dataPtr->renderTexture->setNumMipmaps(1u); this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA32_FLOAT); - // this->dataPtr->renderTexture->setPixelFormat(Ogre::PFG_RGBA8_UNORM); this->dataPtr->renderTexture->scheduleTransitionTo( Ogre::GpuResidency::Resident); @@ -247,8 +244,6 @@ void Ogre2SelectionBuffer::CreateRTTBuffer() std::string matSelectionName = "SelectionBuffer"; std::string matSelectionCloneName = this->dataPtr->camera->getName() + "_" + matSelectionName; - this->dataPtr->selectionMaterial = - Ogre::MaterialManager::getSingleton().getByName(matSelectionName); if (this->dataPtr->selectionMaterial.isNull()) { Ogre::MaterialPtr matSelection = diff --git a/test/integration/camera.cc b/test/integration/camera.cc index 74e72532c..95619639d 100644 --- a/test/integration/camera.cc +++ b/test/integration/camera.cc @@ -298,8 +298,6 @@ void CameraTest::VisualAt(const std::string &_renderEngine) } } - - // Clean up engine->DestroyScene(scene); rendering::unloadEngine(engine->Name()); From b0fd1c7cc59c771d18a0c26e31d61a910c6c7792 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 11 Nov 2021 11:35:40 -0800 Subject: [PATCH 21/22] update screenScalingFactor Signed-off-by: Ian Chen --- ogre/src/OgreCamera.cc | 3 +-- ogre2/src/Ogre2Camera.cc | 3 +-- src/Utils.cc | 5 ++++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ogre/src/OgreCamera.cc b/ogre/src/OgreCamera.cc index f1a19c018..39a99c3f5 100644 --- a/ogre/src/OgreCamera.cc +++ b/ogre/src/OgreCamera.cc @@ -222,8 +222,7 @@ VisualPtr OgreCamera::VisualAt(const ignition::math::Vector2i } } - // float ratio = screenScalingFactor(); - float ratio = 1.0f; + float ratio = screenScalingFactor(); ignition::math::Vector2i mousePos( static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); diff --git a/ogre2/src/Ogre2Camera.cc b/ogre2/src/Ogre2Camera.cc index c08a63a84..c259fff64 100644 --- a/ogre2/src/Ogre2Camera.cc +++ b/ogre2/src/Ogre2Camera.cc @@ -259,8 +259,7 @@ VisualPtr Ogre2Camera::VisualAt(const ignition::math::Vector2i &_mousePos) this->ImageWidth(), this->ImageHeight()); } - // float ratio = screenScalingFactor(); - float ratio = 1.0f; + float ratio = screenScalingFactor(); ignition::math::Vector2i mousePos( static_cast(std::rint(ratio * _mousePos.X())), static_cast(std::rint(ratio * _mousePos.Y()))); diff --git a/src/Utils.cc b/src/Utils.cc index 0ac5902c5..e7d7b6fe4 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -104,7 +104,10 @@ float screenScalingFactor() { // todo(anyone) set device pixel ratio for high dpi displays on Windows float ratio = 1.0; -#ifdef __linux__ + + // the scaling factor seems to cause issues with mouse picking. + // see, https://github.com/ignitionrobotics/ign-gazebo/issues/147 +#if 0 auto closeDisplay = [](Display * display) { if (display) From 4739072e00bd69febb47447a944701a4d5aa567f Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 11 Nov 2021 11:37:22 -0800 Subject: [PATCH 22/22] minor tweak Signed-off-by: Ian Chen --- src/Utils.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils.cc b/src/Utils.cc index e7d7b6fe4..394e7ab71 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -106,7 +106,7 @@ float screenScalingFactor() float ratio = 1.0; // the scaling factor seems to cause issues with mouse picking. - // see, https://github.com/ignitionrobotics/ign-gazebo/issues/147 + // see https://github.com/ignitionrobotics/ign-gazebo/issues/147 #if 0 auto closeDisplay = [](Display * display) {