[osg-users] Multiple cameras with setRenderOrder(PRE_RENDER, i)

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


Hi,

Thank you for the code snippet.  Looking it over I see two differences from my implementation which have been giving me a headache.  These are not necessarily related to camera render orders so it might be appropriate to send these as separate threads.  

First difference from your code and mine is the lack of you needing to allocate an image to the render in order to place the texture within a stateset:


Code:

//... initializing texture, attached to camera'a color buffer, etc

[b]osg::Image* image = new osg::Image;
image->allocateImage(width, height, 1, GL_RGBA, GL_FLOAT);
_texture->setImage(0, image);[/b]

//... initializing geometry and stateset
stateset->setTextureAttributeAndModes(0, _texture, osg::StateAttribute::ON);




In my experience, this is required or else a seg fault occurs when the draw traversal references "_texture".  I am confused on how you are able to pass the texture without allocating an image inside the texture.  I have included a code snippet below to fully clarify what I see occurring.

The second difference is your code uses non power of 2 dimensions for your texture to be rendered by your camera.  Whenever I have tried to do this I have run into the error : 
"Warning: detected OpenGL error 'invalid value' after RenderBin::draw(,)"  during every draw call to the geometry containing the texture

Here is my code snippet with the problematic line in bold

Code:

//Will need to set the texture size to the size of the viewport, it is not right now
_texture = new osg::Texture2D();
{
_texture->setTextureSize(width, height);
_texture->setInternalFormat(GL_RGBA);
_texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::NEAREST); 
_texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::NEAREST); 
}

//set up the camera
{
// set up the render to camera attributes.
setClearMask( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 
setClearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f));

// set viewport
setViewport(0,0, width, height);
setRenderOrder(osg::Camera::PRE_RENDER);
setRenderTargetImplementation(osg::Camera::FRAME_BUFFER, osg::Camera::FRAME_BUFFER);

// attach the texture and use it as the color buffer. this will render the depth buffer to the texture.
//this should not be needed
//############################## Problem 1 ##########################
osg::Image* image = new osg::Image;
image->allocateImage(width, height, 1, GL_RGBA, GL_FLOAT);
_texture->setImage(0, image);
//############################## Problem 1 ##########################

//render the depth buffer to the texture
attach(osg::Camera::COLOR_BUFFER0, _texture, 0, 0, false, 8, 8);
}

osg::Geometry* polyGeom = new osg::Geometry();
polyGeom->setDataVariance( osg::Object::DYNAMIC );
polyGeom->setSupportsDisplayList(false);

float x0 = -5;
float y0 = 0;
float w = 10;
float h = 10;

osg::Vec3Array* verts = new osg::Vec3Array;
verts->push_back(osg::Vec3(x0, 0, y0));
verts->push_back(osg::Vec3(x0 + w, 0, y0));
verts->push_back(osg::Vec3(x0 + w, 0, y0 + h));
verts->push_back(osg::Vec3(x0, 0, y0 + h));
polyGeom->setVertexArray(verts);
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,verts->size()));

osg::Vec2Array* texcoords = new osg::Vec2Array;
texcoords->push_back(osg::Vec2(0, 0));
texcoords->push_back(osg::Vec2(1, 0));
texcoords->push_back(osg::Vec2(1, 1));
texcoords->push_back(osg::Vec2(0, 1));
polyGeom->setTexCoordArray(0,texcoords);

osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
polyGeom->setColorArray(colors);
polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL);

osg::StateSet* stateset = new osg::StateSet;

//########################### problem 2 ############################
//without allocating an image to the texture this line will cause a seg fault
stateset->setTextureAttributeAndModes(0, _texture, osg::StateAttribute::ON);
//###############################################################
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
polyGeom->setStateSet(stateset);

osg::Geode* geode = new osg::Geode();
geode->addDrawable(polyGeom);
return geode;





Thank you for taking time to go over the code and looking at this post

Cheers,
Ross

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=20071#20071







More information about the osg-users mailing list