Hi,
Thanks chris.
the needed lines was:
Code:
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
The problem for some reason is I cant move the camera.
which I can't understand.
Heres a class to create my portal, (quite a handy class if you need one?)
Code:
class Portal
{
public:
Portal()
{
// create objects
subgraph = new osg::Group();
trans = new osg::MatrixTransform();
node = new osg::Group;
}
~Portal() {}
void SetSize(float width, float height)
{
// set bounding box size
bb.xMin() = -width / 2;
bb.xMax() = width / 2;
bb.yMin() = 0;
bb.yMax() = 0;
bb.zMin() = -height / 2;
bb.zMax() = height / 2;
}
void SetMatrix(osg::Matrix mat)
{
// set matrix
trans->setMatrix(mat);
}
void SetSubGraph(osg::Group* subgraph)
{
// setsubgraph
this->subgraph = subgraph;
}
osg::MatrixTransform* create(const osg::Vec4& clearColour)
{
trans->addChild(node);
// create texture
unsigned int tex_width = 800;
unsigned int tex_height = 600;
float portalWidth = bb.xMax()- bb.xMin();
float portalHeight = bb.zMax()- bb.zMin();
ratio = (600. / 800.) / (portalHeight / portalWidth);
texture = new osg::Texture2D;
texture->setTextureSize(tex_width, tex_height);
texture->setInternalFormat(GL_RGB);
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
// set up the render to texture camera.
{
camera = new osg::Camera;
// set clear the color and depth buffer
camera->setClearColor(clearColour);
camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setProjectionMatrix(osg::Matrixd::identity());
camera->setViewMatrix(osg::Matrixd::identity());
// set viewport
camera->setViewport(0,0,tex_width,tex_height);
camera->setAllowEventFocus(false);
// set camera lookat (NOT WORKING)
camera->setViewMatrixAsLookAt( osg::Vec3( 0., -200., 0.
), osg::Vec3( 0., 0., 0. ), osg::Vec3( 0., 0., 1. ));
// set the camera to render before the main camera.
camera->setRenderOrder(osg::Camera::PRE_RENDER);
// tell the camera to use OpenGL frame buffer object
where supported.
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
// attach the texture and use it as the color buffer.
camera->attach(osg::Camera::COLOR_BUFFER, texture);
// add subgraph to render
camera->addChild(subgraph);
// add camera
node->addChild(camera);
}
{
// left hand side of bounding box.
osg::Vec3 top_left(bb.xMin(),bb.yMin(),bb.zMax());
osg::Vec3 bottom_left(bb.xMin(),bb.yMin(),bb.zMin());
osg::Vec3 bottom_right(bb.xMax(),bb.yMax(),bb.zMin());
osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMax());
// create the geometry for the wall.
geom = new osg::Geometry;
// create vertices
osg::Vec3Array* vertices = new osg::Vec3Array(4);
(*vertices)[0] = top_left;
(*vertices)[1] = bottom_left;
(*vertices)[2] = bottom_right;
(*vertices)[3] = top_right;
geom->setVertexArray(vertices);
// create texchoords
osg::Vec2Array* texcoords = new osg::Vec2Array(4);
(*texcoords)[0].set((1 - ratio) / 2,1.0f);
(*texcoords)[1].set((1 - ratio) / 2,0.0f);
(*texcoords)[2].set(1 - ((1 - ratio) / 2),0.0f);
(*texcoords)[3].set(1 - ((1 - ratio) / 2),1.0f);
geom->setTexCoordArray(0,texcoords);
// create normals
osg::Vec3Array* normals = new osg::Vec3Array(1);
(*normals)[0].set(0.0f,0.0f,1.0f);
geom->setNormalArray(normals);
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
// create colors
osg::Vec4Array* colors = new osg::Vec4Array(1);
(*colors)[0].set(1.0f,1.0f,1.0f,.5f);
geom->setColorArray(colors);
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
geom->addPrimitiveSet(new
osg::DrawArrays(GL_QUADS,0,4));
// new we need to add the texture to the Drawable, we
do so by creating a
// StateSet to contain the Texture StateAttribute.
osg::StateSet* stateset = geom->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0,
texture,osg::StateAttribute::ON);
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
// create geode
geode = new osg::Geode();
geode->addDrawable(geom);
{
// create material
osg::Material * material = new osg::Material();
material->setTransparency(osg::Material::FRONT_AND_BACK, 0.3);
geode->getOrCreateStateSet()->setRenderBinDetails(0, "transparent");
geode->getOrCreateStateSet()->setMode(
GL_BLEND, osg::StateAttribute::ON );
geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
geode->getOrCreateStateSet()->setAttributeAndModes(material,
osg::StateAttribute::OVERRIDE);
osg::BlendFunc *func = new osg::BlendFunc();
func->setFunction(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA);
geode->getOrCreateStateSet()->setAttributeAndModes(func);
}
node->addChild(geode);
}
return trans;
}
// portal objects
osg::Camera* camera;
osg::BoundingBox bb;
osg::Group* subgraph;
osg::Vec4 clearColor;
osg::Group* node;
osg::Texture2D* texture;
osg::Geometry* geom;
osg::Geode* geode;
osg::MatrixTransform* trans;
};
And this is how to create a portal:
Code:
{
// create portal
Portal *portal = new Portal();
// set portal size
portal->SetSize(4 * 12, 3 * 12);
//set portal matrix
osg::Matrix portalMat;
portalMat.setTrans(0, 0, 0);
portal->SetMatrix(portalMat);
// create portal's subGraph
{
osg::MatrixTransform* box = createOSGBox(
osg::Vec3(5,5, 5 ) );
osg::Matrix mat;
mat.setTrans(0, 10, 0);
box->setMatrix(mat);
portal->SetSubGraph(box);
}
// add portal to main graph
group->addChild(portal->create(osg::Vec4(.3, 0, 0, 1)));
}
As I said it cont let me set the camera's position.
Even if I try to set it within the main render loop the camera wont change
position.
while( !viewer.done() )
{
...
portal->camera->setViewMatrixAsLookAt( osg::Vec3( 0., -400., 0.
), osg::Vec3( 0., 0., 0. ), osg::Vec3( 0., 0., 1. ));
...
}
Any ideas?
Thank you!
Cheers,
Paul
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=46567#46567
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org