Hi,
Here is the "minimal" code I use that may help somebody in the future:
It lacks some comments but it's because I'm not sure I understand everything (I
have been helped to get that)
Code:
#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>
osg::Texture* createRenderTexture(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* createSquare(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 = createSquare();
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("PathToImage");
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
}
//----------------------------------------------------------------------------------------
osgPPU::Processor* processor = new osgPPU::Processor();
processor->setName("Processor");
osgPPU::UnitInOut* ppuout = new osgPPU::UnitInOut;
ppuout->setName("Output");
ppuout->setInputTextureIndexForViewportReference(-1);
osg::Texture *textureOut = createRenderTexture(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(unitInOut);
unitInOut->addChild(unitInOut2);
unitInOut2->addChild(ppuout);
viewer->setSceneData(node);
return viewer->run();
}
Thanks again Art!
Have a nice day everybody![/code]
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=17677#17677
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org