Added osgSim::OverlayNode setup in osgsimulation, and added preliminary test

code to the osgSim::OverlayNode to test osgsimulation/the architecture.
This commit is contained in:
Robert Osfield
2005-08-31 15:04:27 +00:00
parent af7eba1a7f
commit a420782199
3 changed files with 124 additions and 13 deletions

View File

@@ -28,6 +28,7 @@ public:
virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
{
osg::notify(osg::NOTICE)<<"Oddddd stuff"<<std::endl;
_node->accept(*nv);
}
@@ -35,7 +36,7 @@ public:
};
OverlayNode::OverlayNode():
_textureUnit(0)
_textureUnit(1)
{
init();
}
@@ -50,15 +51,46 @@ OverlayNode::OverlayNode(const OverlayNode& copy, const osg::CopyOp& copyop):
void OverlayNode::init()
{
_camera = new osg::CameraNode;
unsigned int tex_width = 1024;
unsigned int tex_height = 1024;
osg::Texture2D* texture = new osg::Texture2D;
texture->setTextureSize(tex_width, tex_height);
texture->setInternalFormat(GL_RGBA);
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_BORDER);
texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_BORDER);
texture->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
_texture = texture;
// set up the render to texture camera.
{
// create the camera
_camera = new osg::CameraNode;
_camera->setClearColor(osg::Vec4(1.0f,0.5f,0.5f,1.0f));
// set viewport
_camera->setViewport(0,0,tex_width,tex_height);
// set the camera to render before the main camera.
_camera->setRenderOrder(osg::CameraNode::PRE_RENDER);
// tell the camera to use OpenGL frame buffer object where supported.
_camera->setRenderTargetImplmentation(osg::CameraNode::FRAME_BUFFER_OBJECT);
// attach the texture and use it as the color buffer.
_camera->attach(osg::CameraNode::COLOR_BUFFER, _texture.get());
}
_texgenNode = new osg::TexGenNode;
_texgenNode->setTextureUnit(_textureUnit);
_texture = new osg::Texture2D;
_mainSubgraphStateSet = new osg::StateSet;
_mainSubgraphStateSet->setTextureAttributeAndModes(_textureUnit, _texture.get(), osg::StateAttribute::ON);
setOverlayTextureUnit(1);
}
@@ -77,14 +109,55 @@ void OverlayNode::traverse(osg::NodeVisitor& nv)
return;
}
unsigned int contextID = cv->getState()!=0 ? cv->getState()->getContextID() : 0;
// if we need to redraw then do cull traversal on camera.
if (!_textureObjectValidList[contextID])
{
osg::Vec3 _position(0,0,0);
// now compute the camera's view and projection matrix to point at the shadower (the camera's children)
osg::BoundingSphere bs;
for(unsigned int i=0; i<_camera->getNumChildren(); ++i)
{
bs.expandBy(_camera->getChild(i)->getBound());
}
if (!bs.valid())
{
osg::notify(osg::WARN) << "bb invalid"<<_camera.get()<<std::endl;
return;
}
float centerDistance = (_position-bs.center()).length();
float znear = centerDistance-bs.radius();
float zfar = centerDistance+bs.radius();
float zNearRatio = 0.001f;
if (znear<zfar*zNearRatio) znear = zfar*zNearRatio;
float top = (bs.radius()/centerDistance)*znear;
float right = top;
_camera->setReferenceFrame(osg::CameraNode::ABSOLUTE_RF);
_camera->setProjectionMatrixAsFrustum(-right,right,-top,top,znear,zfar);
_camera->setViewMatrixAsLookAt(_position,bs.center(),osg::Vec3(0.0f,1.0f,0.0f));
// compute the matrix which takes a vertex from local coords into tex coords
// will use this later to specify osg::TexGen..
osg::Matrix MVPT = _camera->getViewMatrix() *
_camera->getProjectionMatrix() *
osg::Matrix::translate(1.0,1.0,1.0) *
osg::Matrix::scale(0.5f,0.5f,0.5f);
_texgenNode->getTexGen()->setMode(osg::TexGen::EYE_LINEAR);
_texgenNode->getTexGen()->setPlanesFromMatrix(MVPT);
_camera->accept(*cv);
_textureObjectValidList[contextID] = 1;
// _textureObjectValidList[contextID] = 1;
}
@@ -103,7 +176,13 @@ void OverlayNode::traverse(osg::NodeVisitor& nv)
void OverlayNode::setOverlaySubgraph(osg::Node* node)
{
if (_overlaySubgraph == node) return;
_overlaySubgraph = node;
_camera->removeChild(0, _camera->getNumChildren());
_camera->addChild(node);
dirtyOverlayTexture();
}
@@ -114,10 +193,14 @@ void OverlayNode::dirtyOverlayTexture()
void OverlayNode::setOverlayTextureUnit(unsigned int unit)
{
if (_textureUnit==unit) return;
_texgenNode->setTextureUnit(unit);
_textureUnit = unit;
_texgenNode->setTextureUnit(_textureUnit);
_mainSubgraphStateSet->clear();
_mainSubgraphStateSet->setTextureAttributeAndModes(_textureUnit, _texture.get(), osg::StateAttribute::ON);
_mainSubgraphStateSet->setTextureMode(_textureUnit, GL_TEXTURE_GEN_S, osg::StateAttribute::ON);
_mainSubgraphStateSet->setTextureMode(_textureUnit, GL_TEXTURE_GEN_T, osg::StateAttribute::ON);
_mainSubgraphStateSet->setTextureMode(_textureUnit, GL_TEXTURE_GEN_R, osg::StateAttribute::ON);
_mainSubgraphStateSet->setTextureMode(_textureUnit, GL_TEXTURE_GEN_Q, osg::StateAttribute::ON);
}