Discussion:
Both orthographic and perspective projections for the same camera
guher b
2008-12-27 18:17:42 UTC
Permalink
Hi,

while trying to implement rtt textures,
I came a cross a situation that I can not solve in anyway.
A camera (this camera is a post render camera (which was added to scenegraph) which draws to frame buffer.and meanwhile, the main camera (of the viewer) produces a texture (via fbo) that this camera uses to draw to frame buffer)

This camera(post render camera) should draw two different geometries; a simple quad, which is used to render a textured quad with orthographic projection (to draw the main camera's fbo's generated texture in to the frame buffer), and secondly a geometry(a sphere) with perspective projection to be drawn to the frame buffer afterwards.

To handle different projections, I reimplemented the drawcallback of the first geometry (which needed orthographic projection) and did nothing for the sphere which used the camera's original projection (perspective projection). However, when the sphere is visible, sphere is not rendered as it should be, the whole scene becomes the sphere's color, when the sphere is visible, and nothing is drawn when the sphere is not visible. There is no sphere shape at all. I belive that the drawcallback of the first geometry does something wrong which produces this behavior, cause when the drawcallback is not done, the first geom is not drawn but the sphere is drawn as it should be. The following code shows my drawcallback of the first geometry,
class TmpDrawCallback : public osg::Drawable::DrawCallback
{
public:
TmpDrawCallback() : osg::Drawable::DrawCallback(){
projectionMatrix=new osg::RefMatrix(osg::Matrix::ortho(0,1,0,1,0,1));
osg::Matrix m;
m.getLookAt(osg::Vec3d(0,0,0),osg::Vec3d(0,0,-2),osg::Vec3d(0,1,0));
modelViewMatrix=new osg::RefMatrix(m);
}
~TmpDrawCallback() {}

void drawImplementation (osg::RenderInfo& ri, const osg::Drawable* dr) const;
osg::RefMatrix * projectionMatrix;
osg::RefMatrix * modelViewMatrix;




};
void TmpDrawCallback::drawImplementation (osg::RenderInfo& ri, const osg::Drawable* dr) const
{
osg::RefMatrix * originalPm=new osg::RefMatrix(ri.getState()->getProjectionMatrix());
osg::RefMatrix * originalMvm=new osg::RefMatrix(ri.getState()->getModelViewMatrix());

ri.getState()->applyProjectionMatrix(projectionMatrix);
ri.getState()->applyModelViewMatrix(modelViewMatrix);

dr->drawImplementation(ri);

ri.getState()->applyProjectionMatrix(originalPm);
ri.getState()->applyModelViewMatrix(originalMvm);
}


Does anyone see the flaw here? Or, is there any other way of implementing 2 different projections for the samecamera(without reimplementing the drawcallback)?
Thanks in advance
guher b
2008-12-28 11:29:30 UTC
Permalink
Hi,
I have resolved the problem, I found out that there is a class which helps changing projection, that is osg::Projection which provides different projections for the same camera.
Subject: [osg-users] Both orthographic and perspective projections for the same camera
Date: Saturday, December 27, 2008, 8:17 PM
Hi,
while trying to implement rtt textures,
I came a cross a situation that I can not solve in anyway.
A camera (this camera is a post render camera (which was
added to scenegraph) which draws to frame buffer.and
meanwhile, the main camera (of the viewer) produces a
texture (via fbo) that this camera uses to draw to frame
buffer)
This camera(post render camera) should draw two different
geometries; a simple quad, which is used to render a
textured quad with orthographic projection (to draw the main
camera's fbo's generated texture in to the frame
buffer), and secondly a geometry(a sphere) with perspective
projection to be drawn to the frame buffer afterwards.
To handle different projections, I reimplemented the
drawcallback of the first geometry (which needed
orthographic projection) and did nothing for the sphere
which used the camera's original projection (perspective
projection). However, when the sphere is visible, sphere is
not rendered as it should be, the whole scene becomes the
sphere's color, when the sphere is visible, and nothing
is drawn when the sphere is not visible. There is no sphere
shape at all. I belive that the drawcallback of the first
geometry does something wrong which produces this behavior,
cause when the drawcallback is not done, the first geom is
not drawn but the sphere is drawn as it should be. The
following code shows my drawcallback of the first geometry,
class TmpDrawCallback : public osg::Drawable::DrawCallback
{
osg::Drawable::DrawCallback(){
projectionMatrix=new
osg::RefMatrix(osg::Matrix::ortho(0,1,0,1,0,1));
osg::Matrix m;
m.getLookAt(osg::Vec3d(0,0,0),osg::Vec3d(0,0,-2),osg::Vec3d(0,1,0));
modelViewMatrix=new osg::RefMatrix(m);
}
~TmpDrawCallback() {}
void drawImplementation
(osg::RenderInfo& ri, const osg::Drawable* dr) const;
osg::RefMatrix * projectionMatrix;
osg::RefMatrix * modelViewMatrix;
};
void TmpDrawCallback::drawImplementation
(osg::RenderInfo& ri, const osg::Drawable* dr) const
{
osg::RefMatrix * originalPm=new
osg::RefMatrix(ri.getState()->getProjectionMatrix());
osg::RefMatrix * originalMvm=new
osg::RefMatrix(ri.getState()->getModelViewMatrix());
ri.getState()->applyProjectionMatrix(projectionMatrix);
ri.getState()->applyModelViewMatrix(modelViewMatrix);
dr->drawImplementation(ri);
ri.getState()->applyProjectionMatrix(originalPm);
ri.getState()->applyModelViewMatrix(originalMvm);
}
Does anyone see the flaw here? Or, is there any other way
of implementing 2 different projections for the
samecamera(without reimplementing the drawcallback)?
Thanks in advance
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Robert Osfield
2009-01-05 09:28:59 UTC
Permalink
Hi Guher(?),

You can nest Camera's to override the project and modelview matrices,
or as your follow up post mentionds you can use osg::Projection that
just overrides the projection matrix.
Post by guher b
Hi,
while trying to implement rtt textures,
I came a cross a situation that I can not solve in anyway.
A camera (this camera is a post render camera (which was added to scenegraph) which draws to frame buffer.and meanwhile, the main camera (of the viewer) produces a texture (via fbo) that this camera uses to draw to frame buffer)
This camera(post render camera) should draw two different geometries; a simple quad, which is used to render a textured quad with orthographic projection (to draw the main camera's fbo's generated texture in to the frame buffer), and secondly a geometry(a sphere) with perspective projection to be drawn to the frame buffer afterwards.
To handle different projections, I reimplemented the drawcallback of the first geometry (which needed orthographic projection) and did nothing for the sphere which used the camera's original projection (perspective projection). However, when the sphere is visible, sphere is not rendered as it should be, the whole scene becomes the sphere's color, when the sphere is visible, and nothing is drawn when the sphere is not visible. There is no sphere shape at all. I belive that the drawcallback of the first geometry does something wrong which produces this behavior, cause when the drawcallback is not done, the first geom is not drawn but the sphere is drawn as it should be. The following code shows my drawcallback of the first geometry,
class TmpDrawCallback : public osg::Drawable::DrawCallback
{
TmpDrawCallback() : osg::Drawable::DrawCallback(){
projectionMatrix=new osg::RefMatrix(osg::Matrix::ortho(0,1,0,1,0,1));
osg::Matrix m;
m.getLookAt(osg::Vec3d(0,0,0),osg::Vec3d(0,0,-2),osg::Vec3d(0,1,0));
modelViewMatrix=new osg::RefMatrix(m);
}
~TmpDrawCallback() {}
void drawImplementation (osg::RenderInfo& ri, const osg::Drawable* dr) const;
osg::RefMatrix * projectionMatrix;
osg::RefMatrix * modelViewMatrix;
};
void TmpDrawCallback::drawImplementation (osg::RenderInfo& ri, const osg::Drawable* dr) const
{
osg::RefMatrix * originalPm=new osg::RefMatrix(ri.getState()->getProjectionMatrix());
osg::RefMatrix * originalMvm=new osg::RefMatrix(ri.getState()->getModelViewMatrix());
ri.getState()->applyProjectionMatrix(projectionMatrix);
ri.getState()->applyModelViewMatrix(modelViewMatrix);
dr->drawImplementation(ri);
ri.getState()->applyProjectionMatrix(originalPm);
ri.getState()->applyModelViewMatrix(originalMvm);
}
Does anyone see the flaw here? Or, is there any other way of implementing 2 different projections for the samecamera(without reimplementing the drawcallback)?
Thanks in advance
_______________________________________________
osg-users mailing list
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Loading...