Harash, now I understand what you were talking about. I am attempting to use
the hdr example from osgPPU to perform the following procedure you outlined.
However, I am having difficulty in breaking out the blur section of the hdr
example provided with osgPPU.
Could you possibly tell me what I am missing from my code here?
Code:
#include <stdafx.h>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/ShapeDrawable>
#include <iostream>
#include <osg/Texture2D>
#include <osg/Camera>
#include <osgPPU/Processor.h>
#include <osgPPU/UnitInOut.h>
#include <osgPPU/UnitOut.h>
#include <osgPPU/UnitTexture.h>
#include <osgPPU/ShaderAttribute.h>
#include <osgPPU/Unit.h>
#include <osgPPU/UnitInOut.h>
#include <osgPPU/UnitText.h>
#include <osgPPU/UnitInResampleOut.h>
#include <osgPPU/UnitInMipmapOut.h>
#include <osgPPU/UnitOutCapture.h>
#include <osgPPU/UnitBypass.h>
#include <osgDB/ReaderWriter>
#include <osgDB/ReadFile>
osg::Texture* createRenderTexture3(int tex_width, int tex_height, bool depth)
{
// create simple 2D texture
osg::Texture2D* texture2D = new osg::Texture2D;
texture2D->setTextureSize(tex_width, tex_height);
texture2D->setResizeNonPowerOfTwoHint(false);
texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
texture2D->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
texture2D->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
texture2D->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
osg::Image *image = new osg::Image();
image->allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_FLOAT);
memset(image->data(), 0, image->getTotalSizeInBytes());
texture2D->setImage(image);
texture2D->setUnRefImageDataAfterApply(true);
// setup float format
if (!depth)
{
texture2D->setInternalFormat(GL_RGBA16F_ARB);
texture2D->setSourceFormat(GL_RGBA);
texture2D->setSourceType(GL_FLOAT);
}else{
texture2D->setInternalFormat(GL_DEPTH_COMPONENT);
}
return texture2D;
}
//--------------------------------------------------------------------------
// Quad
//--------------------------------------------------------------------------
osg::Drawable* createSquare2(float textureCoordMax=1.0f)
{
// set up the Geometry.
osg::Geometry* geom = new osg::Geometry;
osg::Vec3Array* coords = new osg::Vec3Array(4);
(*coords)[0].set(-1.0f,0.0f,1.0f);
(*coords)[1].set(-1.0f,0.0f,-1.0f);
(*coords)[2].set(1.0f,0.0f,-1.0f);
(*coords)[3].set(1.0f,0.0f,1.0f);
geom->setVertexArray(coords);
osg::Vec3Array* norms = new osg::Vec3Array(1);
(*norms)[0].set(0.0f,-1.0f,0.0f);
geom->setNormalArray(norms);
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
osg::Vec2Array* tcoords = new osg::Vec2Array(4);
(*tcoords)[0].set(0.0f,0.0f);
(*tcoords)[1].set(0.0f,textureCoordMax);
(*tcoords)[2].set(textureCoordMax,textureCoordMax);
(*tcoords)[3].set(textureCoordMax,0.0f);
geom->setTexCoordArray(0,tcoords);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
return geom;
}
//--------------------------------------------------------------------------
int main(int argc, char **argv)
{
// construct the scene
osg::Group* node = new osg::Group();
osg::Drawable* quad = createSquare2();
osg::Geode* geode = new osg::Geode();
geode->addDrawable(quad);
node->addChild(geode);
// construct the viewer.
osgViewer::Viewer* viewer = new osgViewer::Viewer();
unsigned int screenWidth;
unsigned int screenHeight;
osg::GraphicsContext::getWindowingSystemInterface()->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0),
screenWidth, screenHeight);
unsigned int windowWidth = 640;
unsigned int windowHeight = 480;
viewer->setUpViewInWindow((screenWidth-windowWidth)/2,
(screenHeight-windowHeight)/2, windowWidth, windowHeight);
viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
// stateset
osg::StateSet *stateSet = quad->getOrCreateStateSet();
// texture
osg::Texture2D* texture = new osg::Texture2D;
osg::Image* image =
osgDB::readImageFile("C:\\projects\\exampleOsg1\\Data\\Images\\lz.rgb");
if (!image)
{
std::cout << " couldn't find texture, quiting." << std::endl;
return 1;
}
texture->setImage(image);
osgPPU::UnitTexture* unitTexture= new osgPPU::UnitTexture(texture);
//----------------------------------------------------------------------------------------
// First
osgPPU::UnitInOut* unitInOut= new osgPPU::UnitInOut();
unitInOut->setInputToUniform(unitTexture,"textureNameInShader",true);
osgPPU::ShaderAttribute* shaderAttribute= new osgPPU::ShaderAttribute();
{
osg::Shader* shader= new osg::Shader(osg::Shader::FRAGMENT);
const char* shaderSource=
"uniform sampler2D textureNameInShader;\n"
"void main()\n"
"{\n"
" gl_FragColor=texture2D(textureNameInShader,gl_TexCoord[0].st);\n"
"}";
shader->setShaderSource(shaderSource);
shaderAttribute->addShader(shader);
shaderAttribute->setName("nomShaderAttribute");
shaderAttribute->add("textureNameInShader", osg::Uniform::SAMPLER_2D);
shaderAttribute->set("textureNameInShader", 0);
unitInOut->setName("unitName");
unitInOut->getOrCreateStateSet()->setAttributeAndModes(shaderAttribute);
unitInOut->setInputTextureIndexForViewportReference(-1);//first UnitInOut
you set -1
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
// Second
osgPPU::UnitInOut* unitInOut2= new osgPPU::UnitInOut();
unitInOut2->setInputToUniform(unitInOut,"textureNameInShader2",true);
osgPPU::ShaderAttribute* shaderAttribute2= new osgPPU::ShaderAttribute();
{
osg::Shader* shader2= new osg::Shader(osg::Shader::FRAGMENT);
const char* shaderSource2=
"uniform sampler2D textureNameInShader2;\n"
"void main()\n"
"{\n"
"
gl_FragColor=texture2D(textureNameInShader2,gl_TexCoord[0].st)+vec4(0.0,0.0,0.5,1.0);\n"
"}";
shader2->setShaderSource(shaderSource2);
shaderAttribute2->addShader(shader2);
shaderAttribute2->setName("nomShaderAttribute2");
shaderAttribute2->add("textureNameInShader2", osg::Uniform::SAMPLER_2D);
shaderAttribute2->set("textureNameInShader2", 0);
unitInOut2->setName("unitName2");
unitInOut2->getOrCreateStateSet()->setAttributeAndModes(shaderAttribute2);
unitInOut2->setInputTextureIndexForViewportReference(0);//all UnitInOut
following you set 0
}
//--------------------------------------------------------------------------
float mHDRBlurSigma = 4.0;
float mHDRBlurRadius = 7.0;
//read writers
osg::ref_ptr<osgDB::ReaderWriter::Options> fragmentOptions = new
osgDB::ReaderWriter::Options("fragment");
osg::ref_ptr<osgDB::ReaderWriter::Options> vertexOptions = new
osgDB::ReaderWriter::Options("vertex");
// read shaders from file
osg::Shader* vshader =
osgDB::readShaderFile("Data/glsl/gauss_convolution_vp.glsl",
vertexOptions.get());
osg::Shader* fhshader =
osgDB::readShaderFile("Data/glsl/gauss_convolution_1Dx_fp.glsl",
fragmentOptions.get());
osg::Shader* fvshader =
osgDB::readShaderFile("Data/glsl/gauss_convolution_1Dy_fp.glsl",
fragmentOptions.get());
// now we perform a gauss blur on the downsampled data
osgPPU::UnitInOut* blurx = new osgPPU::UnitInOut();
osgPPU::UnitInOut* blury = new osgPPU::UnitInOut();
// set name and indicies
blurx->setName("BlurHorizontal");
blury->setName("BlurVertical");
{
// setup horizontal blur shaders
osgPPU::ShaderAttribute* gaussx = new osgPPU::ShaderAttribute();
gaussx->addShader(vshader);
gaussx->addShader(fhshader);
gaussx->setName("BlurHorizontalShader");
gaussx->add("sigma", osg::Uniform::FLOAT);
gaussx->add("radius", osg::Uniform::FLOAT);
gaussx->add("texUnit0", osg::Uniform::SAMPLER_2D);
gaussx->set("sigma", mHDRBlurSigma);
gaussx->set("radius", mHDRBlurRadius);
gaussx->set("texUnit0", 0);
blurx->getOrCreateStateSet()->setAttributeAndModes(gaussx);
// setup vertical blur shaders
osgPPU::ShaderAttribute* gaussy = new osgPPU::ShaderAttribute();
gaussy->addShader(vshader);
gaussy->addShader(fvshader);
gaussy->setName("BlurVerticalShader");
gaussy->add("sigma", osg::Uniform::FLOAT);
gaussy->add("radius", osg::Uniform::FLOAT);
gaussy->add("texUnit0", osg::Uniform::SAMPLER_2D);
gaussy->set("sigma", mHDRBlurSigma);
gaussy->set("radius", mHDRBlurRadius);
gaussy->set("texUnit0", 0);
//blury->setShader(gaussy);
blury->getOrCreateStateSet()->setAttributeAndModes(gaussy);
}
//brightpass->addChild(blurx);
//blurx->addChild(blury);
//--------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
osgPPU::Processor* processor = new osgPPU::Processor();
processor->setName("Processor");
osgPPU::UnitInOut* ppuout = new osgPPU::UnitInOut;
ppuout->setName("Output");
ppuout->setInputTextureIndexForViewportReference(-1);
osg::Texture *textureOut = createRenderTexture3(image->s(), image->t(),
false);
ppuout->setOutputTexture(textureOut, 0);
stateSet->setTextureAttributeAndModes(0, textureOut ,
osg::StateAttribute::ON);
osg::Camera* camera= new osg::Camera();
camera->setViewport(new osg::Viewport(0,0, image->s(), image->t()));
processor->setCamera(camera);
node->addChild(processor);
processor->addChild(unitTexture);
unitTexture->addChild(blurx);
blurx->addChild(blury);
blury->addChild(unitInOut);
unitInOut->addChild(ppuout);
//unitTexture->addChild(unitInOut);
//unitInOut->addChild(unitInOut2);
//unitInOut2->addChild(ppuout);
viewer->setSceneData(node);
return viewer->run();
}
Harash Sharma wrote:
> Hi Julia, Allen,
>
>
> I have one solution that most probably seems to be wht you are looking for. I
> have been using OSG for doing some image processing. Thanks to Mr. Art Tevs'
> osgPPU.
>
> Render the scene to a texture T1, High pass filter this image using a shader
> to extract the edges (store to T2), followed by a Gaussian filtering of T2.
> Overlay this processed image over the original with a weight factor.
>
> Scene ------> T1 --> [HighPass] --> T2 --> [Gaussian(LowPass)] --> T3-----|
> | +--> T4 --> Output to Framebuffer
> |----------------------------------------------------------------|
>
>
> Regards
>
> Harash
>
> From: Allen Saucier <>
> To:
> Sent: Tue, March 16, 2010 8:27:51 PM
> Subject: Re: bluring model edges
>
> Hi,
>
> I am in need of this type of feature, too. I need to be able to take a model
> & "fuzz" it's edges, though I must admit that I don't fully understand the
> code given, I'll try this.
>
> Thanks Guy for the ideas.
>
> Cheers,
> Allen
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=25726#25726
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> ()
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
> ------------------
> Post generated by Mail2Forum
[/code]
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=26159#26159
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org