env is osg3.6.4&win10
//
#include <osgViewer/Viewer>
#include <osgDB/FileNameUtils>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/PositionAttitudeTransform>
#include <osg/MatrixTransform>
#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgGA/Device>
using namespace osg;
void setGaussianBlurShader(osg::ref_ptr<osg::StateSet> outStateSet,
osg::ref_ptr<osg::Image> hotspot)
{
osg::Texture2D * texture1 = new osg::Texture2D;
texture1->setDataVariance(osg::Object::DYNAMIC); // protect from
being optimized away as static state.
texture1->setTextureSize(hotspot->s(), hotspot->t());
texture1->setFilter(osg::Texture2D::MIN_FILTER,
osg::Texture::LINEAR_MIPMAP_LINEAR);
texture1->setFilter(osg::Texture2D::MAG_FILTER,
osg::Texture::LINEAR);
texture1->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
texture1->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
texture1->setResizeNonPowerOfTwoHint(true);
texture1->setImage(hotspot);
outStateSet->setTextureAttributeAndModes(0, texture1,
osg::StateAttribute::ON);
}
void createHudGeo(osg::ref_ptr<osg::Geometry> geom, const Vec3& corner,
const Vec3& widthVec, const Vec3& heightVec, float l = 0, float b = 0,
float r = 1, float t = 1)
{
if (geom.valid())
{
Vec3Array* coords = new Vec3Array(4);
(*coords)[0] = corner + heightVec;
(*coords)[1] = corner;
(*coords)[2] = corner + widthVec;
(*coords)[3] = corner + widthVec + heightVec;
geom->setVertexArray(coords);
Vec2Array* tcoords = new Vec2Array(4);
(*tcoords)[0].set(l, t);
(*tcoords)[1].set(l, b);
(*tcoords)[2].set(r, b);
(*tcoords)[3].set(r, t);
geom->setTexCoordArray(0, tcoords);
osg::Vec3Array* normals = new osg::Vec3Array(1);
(*normals)[0] = widthVec^heightVec;
(*normals)[0].normalize();
geom->setNormalArray(normals, osg::Array::BIND_OVERALL);
DrawElementsUByte* elems = new
DrawElementsUByte(PrimitiveSet::TRIANGLES);
elems->push_back(0);
elems->push_back(1);
elems->push_back(2);
elems->push_back(2);
elems->push_back(3);
elems->push_back(0);
geom->addPrimitiveSet(elems);
}
}
osg::ref_ptr<osg::Geode> genGaussianBlur(const std::vector<osg::Vec3d>&
drawPts, osg::ref_ptr<osg::Image> pSrcImage)
{
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
if (drawPts.size() == 4)
{
int imageWidth = osg::absolute(drawPts.at(2).x() -
drawPts.at(0).x());
int imageHeight = osg::absolute(drawPts.at(2).y() -
drawPts.at(0).y());
osg::Vec3 center(-imageWidth, -imageHeight, 0),
widthVec(imageWidth * 2, 0.0f, 0.0f), heightVec(0.0f, imageHeight * 2,
0.0f);
float l = 0, b = 0, r = 1, t = 1;
//
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
createHudGeo(geom, center, widthVec, heightVec);
quad_geode->addDrawable(geom);
//
osg::StateSet *stateSet = quad_geode->getOrCreateStateSet();
stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
//
setGaussianBlurShader(stateSet, pSrcImage);
}
return quad_geode;
}
osg::ref_ptr<osg::Geode> genGaussianBlur2(const std::vector<osg::Vec3d>&
drawPts, osg::ref_ptr<osg::Image> pSrcImage)
{
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
if (drawPts.size() == 4)
{
int imageWidth = osg::absolute(drawPts.at(2).x() -
drawPts.at(0).x());
int imageHeight = osg::absolute(drawPts.at(2).y() -
drawPts.at(0).y());
osg::Vec3 center(-imageWidth, -imageHeight, 50),
widthVec(imageWidth * 2, 0.0f, 0.0f), heightVec(0.0f, imageHeight * 2,
0.0f);
float l = 0, b = 0, r = 1, t = 1;
//
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
createHudGeo(geom, center, widthVec, heightVec);
quad_geode->addDrawable(geom);
//
osg::StateSet *stateSet = quad_geode->getOrCreateStateSet();
stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
//
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
//
setGaussianBlurShader(stateSet, pSrcImage);
}
return quad_geode;
}
//
int main(int argc, char** argv)
{
// create a viewer:
osg::ref_ptr<osgViewer::Viewer> pView = new osgViewer::Viewer;
if (pView.valid())
{
// Tell the database pager to not modify the unref settings
pView->getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true, false);
// install our default manipulator (do this before calling
load)
pView->setCameraManipulator(new
osgGA::TrackballManipulator);
//
//pView->getCamera()->setNearFarRatio(0.00002);
pView->getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
//
osg::Group* root = new osg::Group();
{
osg::ref_ptr<osg::MatrixTransform> nodeMt = new
osg::MatrixTransform;
//
std::vector<osg::Vec3d> drawPts;
drawPts.push_back(osg::Vec3d(0, 0, 0));
drawPts.push_back(osg::Vec3d(78.020536770112813, 0,
0));
drawPts.push_back(osg::Vec3d(78.020536770112813,
64.098223568871617, 0));
drawPts.push_back(osg::Vec3d(0, 64.098223568871617,
0));
//
osg::ref_ptr<osg::Image> pSrcImage =
osgDB::readRefImageFile("E:/right.jpg");
osg::ref_ptr<osg::Geode> pGeode =
genGaussianBlur(drawPts, pSrcImage);
//
int imageWidth = osg::absolute(drawPts.at(2).x() -
drawPts.at(0).x());
int imageHeight = osg::absolute(drawPts.at(2).y() -
drawPts.at(0).y());
//
osg::ref_ptr<osg::Texture2D> texture2D = new
osg::Texture2D;
texture2D->setTextureSize(imageWidth, imageHeight);
texture2D->setInternalFormat(GL_RGBA32F_ARB);
texture2D->setSourceFormat(GL_RGBA);
texture2D->setSourceType(GL_FLOAT);
texture2D->setFilter(osg::Texture2D::MIN_FILTER,
osg::Texture2D::LINEAR);
texture2D->setFilter(osg::Texture2D::MAG_FILTER,
osg::Texture2D::LINEAR);
//
osg::Camera* camera = new osg::Camera;
// set up the background color and clear mask.
{
camera->setClearColor(osg::Vec4(0.f, 0.f,
1.f, 1.f));
camera->setClearMask(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
//
const osg::BoundingSphere& bs =
pGeode->getBound();
//
float znear = 1.0f*bs.radius();
float zfar = 3.0f*bs.radius();
// 2:1 aspect ratio as per flag geometry
below.
float projTop = 0.25f*znear;
float projRight = projTop *
((double)imageWidth / (double)imageHeight);
znear *= 0.9f;
zfar *= 1.1f;
// set up projection.
camera->setProjectionMatrixAsFrustum(-projRight, projRight, -projTop,
projTop, znear, zfar);
// setup view
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrixAsLookAt(bs.center() -
osg::Vec3(0.0f, 2.0f, 0.0f)*bs.radius(),
bs.center(),
osg::Vec3(0.0f, 0.0f, 1.0f));
// set viewport
camera->setViewport(0, 0, imageWidth,
imageHeight);
// set the camera to render before the main
camera.
camera->setRenderOrder(osg::Camera::POST_RENDER);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
camera->attach(osg::Camera::COLOR_BUFFER,
texture2D.get());
camera->setClearColor(osg::Vec4(1.f, 1.f,
1.f, 1.f));
camera->addChild(pGeode);
//
}
root->addChild(camera);
//
nodeMt->addChild(pGeode);
root->addChild(nodeMt);
//general Image
{
osg::ref_ptr<osg::MatrixTransform> nodeMt =
new osg::MatrixTransform;
//
std::vector<osg::Vec3d> drawPts;
drawPts.push_back(osg::Vec3d(0, 0, 0));
drawPts.push_back(osg::Vec3d(78.020536770112813, 0, 0));
drawPts.push_back(osg::Vec3d(78.020536770112813, 64.098223568871617, 0));
drawPts.push_back(osg::Vec3d(0,
64.098223568871617, 0));
//
//osg::ref_ptr<osg::Image> pSrcImage =
osgDB::readRefImageFile("E:/colorMap.jpg");
osg::ref_ptr<osg::Geode> pGeode =
genGaussianBlur2(drawPts, texture2D->getImage());
nodeMt->addChild(pGeode);
//
root->addChild(nodeMt);
}
//
pView->setSceneData(root);
}
return pView->run();
}
}
--
You received this message because you are subscribed to the Google Groups
"OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/osg-users/e94ff4a9-b66e-4813-8229-85b5739ea0ccn%40googlegroups.com.