[osg-users] Using a PRE_RENDER stage texture as uniform to a later render stage

Ross Bohner rgbohner at iastate.edu
Sat Nov 21 19:16:21 PST 2009


I am using a pre_render stage camera to generate a texture for later use.  However the texture is not accessable to a later stage shader when it is set as uniform,  Since the texture functions fine when attached to geometry and the shaders handle non generated textures without problem, I am wondering whether is even possible to send an earlier render stage output to a later stage shader.  Conceptually it makes sense that this is possible.  If it is, I would appreciate possible examples of this working.   The code below is a snippet of how I am currently try to implement this functionality.

thank you for any help offered


//constructor for the render camera class, which prerenders to a texture
RenderCamera::RenderCamera(int height, int width){
_texture = new osg::Texture2D();
_height = height;
_width = width;

//Will need to set the texture size to the size of the viewport, it is not right now
_texture->setTextureSize(width, height);

// set up the render to camera attributes.

// set viewport
setViewport(0,0, _width, _height);

//set the render target, the frame buffer object is required else the textures seg fault for some reason
setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT, osg::Camera::FRAME_BUFFER);

// attach the texture and use it as the color buffer. this will render the depth buffer to the texture.
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(_width, _height, 1, GL_RGBA, GL_FLOAT);
_texture->setImage(0, image.get());

//render the color buffer to texture
attach(osg::Camera::COLOR_BUFFER0, _texture.get(), 0, 0, false, 8, 8);

//send the texture generated from the prerender camera
osg::Texture2D* RenderCamera::getTexture(){
return _texture.get();

//initialize the prerender camera class
osg::ref_ptr<RenderCamera> smokeCamera = new RenderCamera(1024, 1024);
smokeCamera->setShader("shaders/depthPosition.vs", "shaders/depthPosition.fs");
smokeCamera->setRenderOrder(osg::Camera::PRE_RENDER, 0);

//set the second stage stateset 
osg::ref_ptr<osg::StateSet> stateset = root->getOrCreateStateSet();
osg::ref_ptr<osg::Program> program(new osg::Program);
program->addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, "shaders/volumetricSmoke.vs"));
program->addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, "shaders/volumetricSmoke.fs"));

//the texture does not get passed to the shader
stateset->setTextureAttributeAndModes(0, smokeCamera->getTexture(), osg::StateAttribute::ON);
stateset->addUniform(new osg::Uniform("smokeDepthTexture", 0), true);

Thank you!


Read this topic online here:

More information about the osg-users mailing list