Hi,
I try to do a depth of field effect in OpenSG using a two-pass
Post-processing Shader.
On the first pass the scene is rendered, outputting the color, depth and
a blurriness factor (depending on the depth).
On the second pass the image from the first pass is filtered with a
variable-sized filter kernel to simulate the circle of confusion. The
blurriness factor from the first pass controls the size of the filter
kernel.
For being able to render in the scene to multiple buffers (color in
RGBA8 and blurfactor in RGBA16) at one time I need to use multiple
Render Targets. Unfortunately I'm very new to OpenSG and using 1.8 I
couldn't find any descriptions or examples how to do that.
Up to now I create two diffrent textures and attach them to an
FBOViewport-Object:
int x = 1600;
int y = 1200;
TextureChunkPtr fboLeftTexture = TextureChunk::create();
ImagePtr leftimg = Image::create();
beginEditCP(leftimg);
leftimg->set(Image::OSG_RGBA_PF, x, y);
endEditCP(leftimg);
beginEditCP(fboLeftTexture);
{
fboLeftTexture->setEnvMode(GL_REPLACE);
fboLeftTexture->setImage(leftimg);
fboLeftTexture->setTarget(GL_TEXTURE_2D);
fboLeftTexture->setInternalFormat(GL_RGBA8);
fboLeftTexture->setMagFilter( GL_LINEAR );
fboLeftTexture->setMinFilter( GL_LINEAR); }
endEditCP(fboLeftTexture);
TextureChunkPtr fboLeftTexture2 = TextureChunk::create(); ImagePtr
leftimg2 = Image::create();
beginEditCP(leftimg2);
leftimg2->set(Image::OSG_RGBA_PF, x, y);
endEditCP(leftimg2);
beginEditCP(fboLeftTexture2);
{
fboLeftTexture2->setEnvMode(GL_REPLACE);
fboLeftTexture2->setImage(leftimg2);
fboLeftTexture2->setTarget(GL_TEXTURE_2D);
fboLeftTexture2->setInternalFormat(GL_RGBA16);
fboLeftTexture2->setMagFilter( GL_LINEAR );
fboLeftTexture2->setMinFilter( GL_LINEAR); }
endEditCP(fboLeftTexture2);
m_fboLeft = FBOViewport::create();
beginEditCP(m_fboLeft);
{
m_fboLeft->setCamera(_leftViewport->getCamera());
m_fboLeft->setBackground(_leftViewport->getBackground());
m_fboLeft->setRoot(_leftViewport->getRoot());
m_fboLeft->setSize(0, 0,1,1);
m_fboLeft->setStorageWidth(x);
m_fboLeft->setStorageHeight(y);
m_fboLeft->setParent(_window);
m_fboLeft->getTextures().push_back(fboLeftTexture);
m_fboLeft->getTextures().push_back(fboLeftTexture2);
m_fboLeft->setIgnoreCameraDecorators(false);
}
endEditCP(m_fboLeft);
After that I create two materialchunks, one for each pass, and add each one
to a polygonForeground, which are attached to the FBOviewport at the end.
The materialchunks keep the textures and the shaderprogramms:
// create the shader material
ChunkMaterialPtr chunkMaterialLeft = ChunkMaterial::create();
//first pass: scene rendering
std::string _vp_program = readFile("dof.vert");
std::string _fp_program = readFile("dof.frag");
SHLChunkPtr _shl = SHLChunk::create();
beginEditCP(_shl);
_shl->setVertexProgram(_vp_program);
_shl->setFragmentProgram(_fp_program);
_shl->setUniformParameter("Tex0", 0);
_shl->setUniformParameter("focalLen", 7.0f);
_shl->setUniformParameter("Zfocus", 10.0f);
_shl->setUniformParameter("maxCoC", 5.0f);
endEditCP(_shl);
beginEditCP(chunkMaterialLeft);
{
chunkMaterialLeft->addChunk( fboLeftTexture );
chunkMaterialLeft->addChunk(_shl);
}
endEditCP(chunkMaterialLeft);
//The Polygon foreground
PolygonForegroundPtr polyForegroundLeft = PolygonForeground::create();
beginEditCP(polyForegroundLeft);
{
polyForegroundLeft->setTile(false);
polyForegroundLeft->setMaterial(chunkMaterialLeft);
polyForegroundLeft->getMFPositions()->push_back(Pnt2f(0.0, 0.0));
polyForegroundLeft->getMFPositions()->push_back(Pnt2f(0.0+x, 0.0));
polyForegroundLeft->getMFPositions()->push_back(Pnt2f(0.0+x, 0.0+y));
polyForegroundLeft->getMFPositions()->push_back(Pnt2f(0.0, 0.0+y));
polyForegroundLeft->getMFTexCoords()->push_back(Vec3f(0.0, 0.0, 0.0));
polyForegroundLeft->getMFTexCoords()->push_back(Vec3f(1.0, 0.0, 0.0));
polyForegroundLeft->getMFTexCoords()->push_back(Vec3f(1.0, 1.0, 0.0));
polyForegroundLeft->getMFTexCoords()->push_back(Vec3f(0.0, 1.0, 0.0));
polyForegroundLeft->setNormalizedX(false);
polyForegroundLeft->setNormalizedY(false);
}
endEditCP(polyForegroundLeft);
// second pass: Post-processing (simulate Circle of Confusion)
beginEditCP(_shl2);
_shl2->setVertexProgram(_vp_program2);
_shl2->setFragmentProgram(_fp_program2);
_shl2->setUniformParameter("Width", x);
_shl2->setUniformParameter("Height", y);
_shl2->setUniformParameter("Tex0", 0);
_shl2->setUniformParameter("Tex1", 1);
// _shl2->setUniformParameter("maxCoC", 5.0);
endEditCP(_shl2);
ChunkMaterialPtr chunkMaterialLeft2 = ChunkMaterial::create();
beginEditCP(chunkMaterialLeft2);
{
chunkMaterialLeft2->addChunk( fboLeftTexture2);
chunkMaterialLeft2->addChunk(_shl2);
}
endEditCP(chunkMaterialLeft2);
endEditCP(chunkMaterialRight2);
PolygonForegroundPtr polyForegroundLeft2 = PolygonForeground::create();
beginEditCP(polyForegroundLeft2);
{
polyForegroundLeft2->setTile(false);
polyForegroundLeft2->setMaterial(chunkMaterialLeft2);
polyForegroundLeft2->getMFPositions()->push_back(Pnt2f(0.0, 0.0));
polyForegroundLeft2->getMFPositions()->push_back(Pnt2f(0.0+x, 0.0));
polyForegroundLeft2->getMFPositions()->push_back(Pnt2f(0.0+x, 0.0+y));
polyForegroundLeft2->getMFPositions()->push_back(Pnt2f(0.0, 0.0+y));
polyForegroundLeft2->getMFTexCoords()->push_back(Vec3f(0.0, 0.0, 0.0));
polyForegroundLeft2->getMFTexCoords()->push_back(Vec3f(1.0, 0.0, 0.0));
polyForegroundLeft2->getMFTexCoords()->push_back(Vec3f(1.0, 1.0, 0.0));
polyForegroundLeft2->getMFTexCoords()->push_back(Vec3f(0.0, 1.0, 0.0));
polyForegroundLeft2->setNormalizedX(false);
polyForegroundLeft2->setNormalizedY(false);
}
endEditCP(polyForegroundLeft2);
beginEditCP(m_leftViewport);
m_leftViewport->getForegrounds().push_back(polyForegroundLeft);
m_leftViewport->getForegrounds().push_back(polyForegroundLeft2);
endEditCP(m_leftViewport);
beginEditCP(m_window);
{
m_window->addPort(m_fboLeft);
}
endEditCP(m_window);
m_foregrounds["FBO_left"] = polyForegroundLeft;
m_foregrounds["FBO_left2"] = polyForegroundLeft2;
It compiles without any mistakes, but I get a warning: "FBO initialize
pre OpenGL error: invalid operation!" Do anybody know what it means?
I don't know what I'm doing wrong. Do I attach the texture to the
fboviewport in the right way?
I would be very thankful for all help and hints.
Thank you very much,
Greetings,
Ria
------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the
lucky parental unit. See the prize list and enter to win:
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users