[osg-users] Problem to load images
Rômulo Cerqueira
romulogcerqueira at gmail.com
Tue Aug 15 18:58:16 PDT 2017
Hi Robert,
thanks for your reply. Follows my code:
Code:
#define BOOST_TEST_MODULE "NormalMapping_test"
#include <boost/test/unit_test.hpp>
// OpenSceneGraph includes
#include <osg/Geode>
#include <osg/Group>
#include <osg/Image>
#include <osg/ShapeDrawable>
#include <osg/StateSet>
#include <osg/Texture2D>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
// Rock includes
#include <normal_depth_map/NormalDepthMap.hpp>
#include <normal_depth_map/ImageViewerCaptureTool.hpp>
// OpenCV includes
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/contrib/contrib.hpp>
// C++ includes
#include <iostream>
using namespace normal_depth_map;
BOOST_AUTO_TEST_SUITE(NormalMapping)
// add one object to scene (sphere)
void addSimpleObject(osg::ref_ptr<osg::Group> root) {
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0,0,-14), 5)));
root->addChild(geode);
root->getChild(0)->asGeode()->addDrawable(geode->getDrawable(0));
}
// define texture attributes
osg::ref_ptr<osg::StateSet> insertNormalMapTexture(osg::ref_ptr<osg::Image> diffuseImage, osg::ref_ptr<osg::Image> normalImage) {
osg::ref_ptr<osg::Texture2D> diffuse = new osg::Texture2D();
osg::ref_ptr<osg::Texture2D> normal = new osg::Texture2D();
diffuse->setImage(diffuseImage);
diffuse->setDataVariance(osg::Object::DYNAMIC);
diffuse->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
diffuse->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
diffuse->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
diffuse->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
diffuse->setResizeNonPowerOfTwoHint(false);
diffuse->setMaxAnisotropy(8.0f);
normal->setImage(normalImage);
normal->setDataVariance(osg::Object::DYNAMIC);
normal->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
normal->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
normal->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
normal->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
normal->setResizeNonPowerOfTwoHint(false);
normal->setMaxAnisotropy(8.0f);
osg::ref_ptr<osg::StateSet> normalState = new osg::StateSet();
normalState->setTextureAttributeAndModes(TEXTURE_UNIT_DIFFUSE, diffuse, osg::StateAttribute::ON);
normalState->setTextureAttributeAndModes(TEXTURE_UNIT_NORMAL, normal, osg::StateAttribute::ON);
return normalState;
}
// get texture files
void loadTextures(osg::ref_ptr<osg::Group> root, TextureImages textureId) {
osg::ref_ptr<osg::Image> diffuseImage = osgDB::readImageFile("/home/romulo/dev.bir.master/simulation/normal_depth_map/textures/gray_texture_d.jpg");
osg::ref_ptr<osg::Image> normalImage = osgDB::readImageFile("/home/romulo/dev.bir.master/simulation/normal_depth_map/textures/gray_texture_n.jpg");
// texture properties
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
geode->setStateSet(insertNormalMapTexture(diffuseImage, normalImage));
root->addChild(geode);
}
// create scene with normal mapping and one object
osg::ref_ptr<osg::Group> createNormalMapSimpleScene() {
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
stateset->addUniform(new osg::Uniform("diffuseTexture", TEXTURE_UNIT_DIFFUSE));
stateset->addUniform(new osg::Uniform("normalTexture", TEXTURE_UNIT_NORMAL));
stateset->setDataVariance(osg::Object::STATIC);
root->setStateSet(stateset);
loadTextures(root, TEXTURE_GRAY);
addSimpleObject(root);
return root;
}
// compute the normal depth map for a osg scene
cv::Mat computeNormalDepthMap(osg::ref_ptr<osg::Group> root, float maxRange, float fovX, float fovY) {
uint height = 500;
// normal depth map
NormalDepthMap normalDepthMap(maxRange, fovX * 0.5, fovY * 0.5);
ImageViewerCaptureTool capture(fovY, fovX, height);
capture.setBackgroundColor(osg::Vec4d(0, 0, 0, 0));
normalDepthMap.addNodeChild(root);
// grab scene
osg::ref_ptr<osg::Image> osgImage = capture.grabImage(normalDepthMap.getNormalDepthMapNode());
osg::ref_ptr<osg::Image> osgDepth = capture.getDepthBuffer();
cv::Mat cvImage = cv::Mat(osgImage->t(), osgImage->s(), CV_32FC3, osgImage->data());
cv::Mat cvDepth = cv::Mat(osgDepth->t(), osgDepth->s(), CV_32FC1, osgDepth->data());
cvDepth = cvDepth.mul( cv::Mat1f(cvDepth < 1) / 255);
std::vector<cv::Mat> channels;
cv::split(cvImage, channels);
channels[1] = cvDepth;
cv::merge(channels, cvImage);
cv::cvtColor(cvImage, cvImage, cv::COLOR_RGB2BGR);
cv::flip(cvImage, cvImage, 0);
return cvImage.clone();
}
BOOST_AUTO_TEST_CASE(differentNormalMaps_TestCase) {
float maxRange = 20.0f;
float fovX = M_PI / 3; // 60 degrees
float fovY = M_PI / 3; // 60 degrees
osg::ref_ptr<osg::Group> normalRoot = createNormalMapSimpleScene();
cv::Mat cvNormal = computeNormalDepthMap(normalRoot, maxRange, fovX, fovY);
plotSonarTest(cvNormal, maxRange, fovX * 0.5);
}
BOOST_AUTO_TEST_SUITE_END()
...
Thank you!
Cheers,
Rômulo
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=71422#71422
More information about the osg-users
mailing list