Just updating the status.. I finally figured this out, about 2am after several more attempts.

Basically, I learned that OpenSceneGraph isn't "magic" and simply traverses the graph in order, so I had to be a bit more careful about the order that I did things like constructing GC's, assigning viewports, and attaching to the main graph.

Also, I finally learned how to use osgViewer::Viewer andm osgViewer::CompositeViewer effectively.. Tu rns out I didn't need a Composite Viewer, I could get my same functionality with multiple contexts/viewports in a single Viewer.. and it's simpler, faster, and less error-prone.

Liking OSG so far.. Bit of a learning curve from basic OpenGL, but not as bad as I thought once I quit expected it to do magic things for me :)

September 11, 2012 4:36 PM
I've done this in raw OpenGL before, but I can't for the life of me get
it working in OpenSceneGraph. I need to render a scene and then display
the result in 2 separate windows: One showing the Color Buffer, one
SHowing the Depth Buffer (and eventually a 3rd showing the STencil
Buffer). I've tried all day and got varying results, but nothing
approaching success. CAn someone tell me what I'm doing wrong?

Relevant code below.. Don't laugh too hard, I've been googling and
reading the examples and cut-n-pasting code snippets all day, and I'm
relatively sure that at least 50% of this is unnecessary.. Right now
it's in a state of opening my windows & showing _a_ texture, but it's
all black (except for the red border I allowed just to see if this code
is doing anything at all). The closest I got was using about 4 cameras
(a Main camera, a Depth FBO camera, then 2 separate cameras for the 2
final screen-aligned quads), which would render my geometry on screen
but nothing into the Depth or Color buffer textures.

..... Initialization stuff up here, nothing to do with OpenSceneGraph ...
there's a global global osgViewer::CompositeViewer "compViewer"

viewerColor = new osgViewer::View();
viewerDepth = new osgViewer::View();
{
std::cout << "** Initializing depth & color texture " << std::endl;
// Now setup the Depth Texture
__depthTexture = new osg::Texture2D();
__depthTexture->setTextureSize(width, height);
__depthTexture->setInternalFormat(GL_DEPTH_COMPONENT);

__depthTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);


__depthTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);


__colorTexture = new osg::Texture2D();
__colorTexture->setTextureSize(width, height);
__colorTexture->setInternalFormat(GL_RGBA);

__colorTexture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);


__colorTexture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);


osg::ref_ptr<osg::GraphicsContext::Traits> traits = new
osg::GraphicsContext::Traits;
traits->x = xPos + 0;
traits->y = yPos + 0;
traits->width = width;
traits->height = height;
traits->windowDecoration = true;
traits->doubleBuffer = true;
traits->sharedContext = 0;
traits->vsync = false;
osg::ref_ptr<osg::GraphicsContext> gc =
osg::GraphicsContext::createGraphicsContext(traits.get());

std::cout << "** Initializing prime camera" << std::endl;
__camera = new osg::Camera();
__camera->setGraphicsContext(gc.get());
__camera->setViewport(new osg::Viewport(0,0, traits->width,
traits->height));

__camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR);
__camera->setProjectionMatrixAsPerspective(70,1.0, 1,200);
__camera->setClearDepth(1.0);
__camera->setViewMatrixAsLookAt( osg::Vec3(0,0,0),
osg::Vec3(0,100,0), osg::Vec3(0,0,1) );
GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
__camera->setDrawBuffer(buffer);
__camera->setReadBuffer(buffer);

__camera->getOrCreateStateSet()->setRenderBinDetails(50,"RenderBin");
//__camera->setRenderOrder(osg::Camera::NESTED_RENDER);
__camera->setRenderOrder(osg::Camera::PRE_RENDER);

__camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
__camera->attach(osg::Camera::DEPTH_BUFFER, __depthTexture);
__camera->attach(osg::Camera::COLOR_BUFFER, __colorTexture);

std::cout << "** Attaching cmaeras to viewer " << std::endl;
// add this slave camera to the viewer, with a shift left of the
projection matrix
if (__Side == "leftview") {
//viewerColor->addSlave(__depthCamera,
osg::Matrixd::translate(-0.8,0,0), osg::Matrixd());
viewerColor->addSlave(__camera.get(),
osg::Matrixd::translate(-0.8,0,0), osg::Matrixd());
} else if (__Side == "rightview") {
//viewerColor->addSlave(__depthCamera,
osg::Matrixd::translate(+0.8,0,0), osg::Matrixd());
viewerColor->addSlave(__camera.get(),
osg::Matrixd::translate(0.8,0,0), osg::Matrixd());
} else {
//viewerColor->addSlave(__depthCamera, osg::Matrixd(),
osg::Matrixd(),false);
viewerColor->addSlave(__camera.get(), osg::Matrixd(),
osg::Matrixd());
}
std::cout << " Planar Depth " << std::endl;
osg::Camera* camPtr = PlanarQuad(xPos, yPos, width,height, gc,
true, buffer,__depthTexture);
viewerDepth->addSlave(camPtr, osg::Matrixd(), osg::Matrixd(),
false);

std::cout << " Planar color " << std::endl;
camPtr = PlanarQuad(xPos, yPos, width,height, gc,
false,buffer,__colorTexture);
viewerColor->addSlave(camPtr, osg::Matrixd(), osg::Matrixd(),
false);
}
std::cout << "** Initializing viewerColor " << std::endl;
// Initialize the variables we'll need for this

compViewer->addView(viewerColor);
compViewer->addView(viewerDepth);
viewerColor->setCameraManipulator(new osgGA::TrackballManipulator());
viewerColor->home();

std::cout << "** Initializing viewerDepth " << std::endl;
viewerDepth->setCameraManipulator(new osgGA::TrackballManipulator());
viewerDepth->assignSceneDataToCameras();
viewerDepth->home();

// This is required or the viewer.frame() doesn't work for some reason
// TODO: Write our own "Null" manipulator

_frameCount = 0;
return 1;
}

osg::Camera* osgRenderer::PlanarQuad( int xPos, int yPos, int width, int
height, osg::GraphicsContext *gc,
bool newGC, GLenum buffer, osg::Texture2D *tex) {
// Build a quad to simply display the Depth Buffer texture
// Construct a Geode, and assign the Quad to it
// and assign the texture
// Also set it to ignore Depth Test, since it will be background
// Debug: Leave a 10px border to see the scene behind

osg::ref_ptr<osg::Drawable> quad = osg::createTexturedQuadGeometry(
osg::Vec3(10,10.0,0),
osg::Vec3(width-20,0,0),osg::Vec3(0,height-20,0),
0, 1, 0, 1);
quad->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex);

quad->setDataVariance( osg::Object::STATIC );
quad->setUseDisplayList( true );

quad->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);


osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable( quad.get() );
geode->getOrCreateStateSet()->setTextureAttributeAndModes(0, tex,
osg::StateAttribute::ON);
geode->getOrCreateStateSet()->setTextureMode(0,
GL_TEXTURE_2D,osg::StateAttribute::ON);

geode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);


osg::ref_ptr<osg::Camera> depCam = new osg::Camera();
// set the projection matrix
depCam->setProjectionMatrix(osg::Matrix::ortho2D(0,width,0,height));
// set the view matrix
depCam->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
depCam->setViewMatrix(osg::Matrix::identity());
// No need to clear anything honestly
depCam->setClearMask(NULL);
//depCam->setRenderOrder(osg::Camera::POST_RENDER);
//depCam->getOrCreateStateSet()->setMode(GL_LIGHTING,
osg::StateAttribute::OFF);
depCam->addChild(geode.get());

std::cout << "** Initializing viewerDepth camera" << std::endl;
osg::Camera* depCamSlave = new osg::Camera();
depCamSlave->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

if (newGC) {
std::cout << "** Initializing depth traits" << std::endl;
osg::ref_ptr<osg::GraphicsContext::Traits> depTraits = new
osg::GraphicsContext::Traits;
depTraits->x = xPos + 0;
depTraits->y = yPos + height;
depTraits->width = width;
depTraits->height = height;
depTraits->windowDecoration = true;
depTraits->doubleBuffer = true;
depTraits->sharedContext = gc;
depTraits->vsync = false;
osg::ref_ptr<osg::GraphicsContext> dep_gc =
osg::GraphicsContext::createGraphicsContext(depTraits.get());
depCamSlave->setGraphicsContext(dep_gc.get());
depCamSlave->setDrawBuffer(buffer);
depCamSlave->setReadBuffer(buffer);
} else {
depCamSlave->setGraphicsContext(gc);
}

depCamSlave->setViewport(0,0,width,height);
depCamSlave->setAllowEventFocus(false);
depCamSlave->setRenderOrder(osg::Camera::POST_RENDER);
depCamSlave->setClearColor(osg::Vec4(1.0f,0.0f,0.0f,0.0f));
depCamSlave->setClearDepth(1.0);

depCamSlave->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);


depCamSlave->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::OFF);


depCamSlave->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);

osg::TexEnv* pTexEnv = new osg::TexEnv();
pTexEnv->setMode(osg::TexEnv::REPLACE);
depCamSlave->getOrCreateStateSet()->setTextureAttributeAndModes(0,
pTexEnv, osg::StateAttribute::ON);
depCamSlave->addChild(depCam.get());
std::cout << "returning" << std::endl;
return depCamSlave;
}

and later in the code I call :
void osgRenderer::RegisterWithScene(osg::Group* _root) {
viewerColor->setSceneData(_root);
viewerColor->assignSceneDataToCameras();
}


--
Randall Hand
http://www.yeraze.com

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to