[osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass

Daniel Holz danielh at cm-labs.com
Sun Mar 8 11:58:59 PDT 2009


Hi everyone,

I have trouble with a render to texture pass, where I try to write to 
the RGB and ALPHA components of an attached RGBA texture in a custom 
fragment shader.
Writing to the RGB components works, but gl_FragColor.a = ... does not 
have any effect at all. The ALPHA component will always result in the 
clear color's alpha component.
Also trying to do gl_FragColor = vec4(...,...,...,...) sets the RGB 
values correctly but the ALPHA stays the same.

I am setting up my texture to render to as follows:

   osg::Texture2D texture2D = new osg::Texture2D;
   texture2D->setTextureSize(screen_width, screen_height);
   texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
   texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
   texture2D->setBorderColor(osg::Vec4(1.0,1.0,1.0,1.0));
   texture2D->setWrap(osg::Texture2D::WRAP_S, 
osg::Texture2D::CLAMP_TO_BORDER);
   texture2D->setWrap(osg::Texture2D::WRAP_T, 
osg::Texture2D::CLAMP_TO_BORDER);
   texture2D->setInternalFormat(GL_RGBA32F_ARB);

I am rendering to a screen aligned quad (osg::Geode* quadGeode) where I 
activate GL_BLEND (I thought this might be necessary to actually be able 
to write to gl_FragColor.a in the fragment shader):
        osg::Geode* quadGeode = ... create screen aligned quad here ...
   osg::StateSet* stateset = quadGeode->getOrCreateStateSet();
   // deactivate lighting for faster processing
   stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
   // activate blending to be able to write to gl_FragColor.a
   stateset->setMode(GL_BLEND, osg::StateAttribute::ON);

And the camera is set up as follows:

   osg::Camera* pCamPass = new osg::Camera;
   pCamPass->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   pCamPass->setColorMask(new osg::ColorMask(1,1,1,1));
   pCamPass->setClearColor(osg::Vec4(1.0,1.0,1.0,1.0));
   pCamPass->setViewport(0, 0, screen_width, screen_height);
   pCamPass->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
   pCamPass->setProjectionMatrixAsOrtho2D(0,1,0,1);
   pCamPass->setViewMatrix(osg::Matrix::identity());
   // tell the camera to use OpenGL frame buffer object where supported.
   
pCamPass->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
   // attach the texture to render to
   pCamPass->attach(osg::Camera::COLOR_BUFFER, texture2D);
   // add screen aligned quad for rendering
   pCamPass->addChild(quadGeode);

Does anybody have any clue what could be going wrong here?
Any help is much appreciated.

Cheers
Daniel



More information about the osg-users mailing list