[osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
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 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi, sorry, I didn't read through all your code, but have a look at the osgmultiplerendertargets example. I don't think data is written specifically to alpha there, but you can modify it to check. Run it with --image and --hdr. I think the --image prints out some values of the texture it reads back. jp Daniel Holz wrote: 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 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org -- This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html. This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks Transtec Computers for their support. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
. // activate blending to be able to write to gl_FragColor.a stateset->setMode(GL_BLEND, osg::StateAttribute::ON); .. No, this comment has a misunderstanding. Writing to gl_FragColor.a in the fragment shader does work of course but it isn't the final RGBA value as the pipeline has a hardware stage AFTER the fragment shader. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi Jim, . // activate blending to be able to write to gl_FragColor.a stateset->setMode(GL_BLEND, osg::StateAttribute::ON); .. No, this comment has a misunderstanding. Writing to gl_FragColor.a in the fragment shader does work of course but it isn't the final RGBA value as the pipeline has a hardware stage AFTER the fragment shader. I agree that whatever is put in gl_FragColor.a will not be the final alpha value in the framebuffer, but at least if we put gl_FragColor.a = 0.0 then the current fragment shader's output color should be transparent, and so anything behind should be completely visible, right? We were working under that assumption. If not, then is there some other way to "kill" a fragment we don't want to show up once we're at the fragment shader stage? (I seem to recall there was a way, but I can't remember the specifics). Thanks, J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
hi Jean-Sebastian, I guess you're looking for the discard statement in the fragment shader? regards Ralph Jean-Sébastien Guay schrieb:If not, then is there some other > way to "kill" a fragment we don't want to show up once we're at the > fragment shader stage? (I seem to recall there was a way, but I can't > remember the specifics). > > Thanks, > > J-S > ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi Ralph, I guess you're looking for the discard statement in the fragment shader? Heh, discard, that's it. Thanks :-) J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi, I had some similar problem sometime ago and i solved it changing the internal format of the texture for the RTT. I also had some issues using FRAME_BUFFERS instead of FBOs, but you are already using them. Regards, J. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=8103#8103 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi Joseba, I use FBOs but the problem is that in the beginning of my application it says "RenderStage::runCameraSetUp(), FBO setup failed, FBO status= 0x8cd6". Could this be the problem? And what is the reason for that? Could you tell me which internal format you where using and which issues you were talking about? Thanks Daniel Joseba wrote: Hi, I had some similar problem sometime ago and i solved it changing the internal format of the texture for the RTT. I also had some issues using FRAME_BUFFERS instead of FBOs, but you are already using them. Regards, J. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=8103#8103 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
>I agree that whatever is put in gl_FragColor.a will not be the final >alpha value in the framebuffer, but at least if we put gl_FragColor.a = >0.0 then the current fragment shader's output color should be >transparent, and so anything behind should be completely visible, right? Not necessarily. If the object-that-is-supposed-to-be-transparent still has depth writing enabled, it will overwrite more distant objects. Then there's which blend functions are enabled which can alter the output of the fragment shader. >If not, then is there some other >way to "kill" a fragment we don't want to show up once we're at the >fragment shader stage? GLSL has "discard". ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi, I just want to make sure that we do not misunderstand each other: The problem is not to make a fragment invisible (the discard statement in fact works for that pretty well) but to simply store normalized data ([0,1]) as the alpha component of my RGBA texture by using gl_FragColor.a = ... . But whatever value I pass in, it is ignored. In fact I found out that I CAN write to the alpha value IF I use texture2D->setInternalFormat(GL_RGBA) but this would give me normalized values not just for the alpha component but also for the RGB components and that's not what I want. I need 32 bit floats PER component and NOT normalized because I want to store world space positions in RGB. That's why I decided to use GL_RGBA32F_ARB as internal format. This decision was motivated by the following behavior of osg::Texture2D::setInternalFormat(): In setInternalFormat() the method computeInternalFormatType() is called which sets the "internal format type" to GL_FLOAT for the given "internal format" GL_RGBA32F_ARB: setInternalFormat(GL_RGBA32F_ARB); ==> computeInternalFormatType() gives GL_FLOAT as a result. If I use the "internal format" GL_RGBA the method computeInternalFormatType() computes the "internal format type" NORMALIZED: setInternalFormat(GL_RGBA); ==> computeInternalFormatType() gives NORMALIZED as a result. So how do I get to write to the alpha if its not normalized? Cheers Daniel Joseba wrote: Hi, I had some similar problem sometime ago and i solved it changing the internal format of the texture for the RTT. I also had some issues using FRAME_BUFFERS instead of FBOs, but you are already using them. Regards, J. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=8103#8103 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi Jim, Not necessarily. If the object-that-is-supposed-to-be-transparent still has depth writing enabled, it will overwrite more distant objects. Then there's which blend functions are enabled which can alter the output of the fragment shader. OK, but I'm talking about a pre-render pass to an FBO though. The FBO is attached to an RGBA texture, and we want to be able to store some data in the alpha channel... So we want what we write to gl_FragColor.a to make it into the texture directly. Is there a way to do that? (in effect, we want to be able to write to 4 floats per fragment) Thanks, J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
>OK, but I'm talking about a pre-render pass to an FBO though. The FBO is >attached to an RGBA texture, and we want to be able to store some data >in the alpha channel... So we want what we write to gl_FragColor.a to >make it into the texture directly. Is there a way to do that? Never used FBO, but guessing it simply switches the destination address, and the pipeline still operates the same way (??). ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi, We write to all the channels and there doesn't seem to be any trouble. But be carefull, if u write to the alpha channel and blending is on, it will use these values. Can't you write your data into other channel or to a separate image (another attachment like COLOR_BUFFER1. in the shader it becomes gl_FragData[i] where 'i' is the index of the color buffer you want to set). Guy. >OK, but I'm talking about a pre-render pass to an FBO though. The FBO is >attached to an RGBA texture, and we want to be able to store some data >in the alpha channel... So we want what we write to gl_FragColor.a to >make it into the texture directly. Is there a way to do that? Never used FBO, but guessing it simply switches the destination address, and the pipeline still operates the same way (??). ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.or g ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Hi, Jean-Sébastien Guay wrote: Hi Jim, Not necessarily. If the object-that-is-supposed-to-be-transparent still has depth writing enabled, it will overwrite more distant objects. Then there's which blend functions are enabled which can alter the output of the fragment shader. OK, but I'm talking about a pre-render pass to an FBO though. The FBO is attached to an RGBA texture, and we want to be able to store some data in the alpha channel... So we want what we write to gl_FragColor.a to make it into the texture directly. Is there a way to do that? > > (in effect, we want to be able to write to 4 floats per fragment) Yes, this is definitely possible, but why stop at 4 :). Have a look at the osgstereomatch example and its shaders. Have a look in the readme at the section "Multiple Pass", there we want to simply store 16 values into 4 textures (MRT) during the subtract pass. In this case the alpha channel behaves exactly as the RGB channels. Also investigate the use of gl_FragData versus gl_FragColor for writing unclipped values. rgds jp Thanks, J-S -- This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html. This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks Transtec Computers for their support. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Can't write to ALPHA component with custom fragment shader in RTT pass
Thanks for all your help. It directed me to the solution! For other users who face the same problem: The setting to obtain a RGBA texture with 32Bit floats per component which one can write unclamped values to is as follows: Code: 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->setInternalFormat(GL_RGBA32F_ARB); texture2D->setSourceFormat(GL_RGBA); // we need to set the source format to GL_RGBA. otherwise the values will be clamped to [0,1]. // Note: use gl_FragData[0..n] to write to texture in fragment shader. otherwise data is clamped. osg::Camera* camera = ... camera->pCamPass->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0), texture2D); Just watch out that you write to the texture using gl_FragData[0] (in this example) instead of gl_FragColor if you want the values to be passed in the texture in an unclamped fashion. Have fun! Daniel -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=8263#8263 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org