overriding draw function in OSG

56 views
Skip to first unread message

sylvi

unread,
Apr 12, 2007, 8:59:51 AM4/12/07
to NAVER based on OSG

Prof. Park,

Now I am working in adding occlusion in AR application. For that, I
need to override draw function.
I found some example using Producer::Camera::SceneHandler, using this
class we can write down our own draw function. I put openGL draw
function primitives inside draw function. It works fine. However,
other model in the scenegraph seems not be drawn. I dont know how to
call draw function for other models.


Below I put my codes:
When I run this codes, only cubes are drawn in the screen. The other
model (domino) is not drawn.
I think I need to call function to draw domino inside MySceneHandler2,
I tried several ways but it is not success.
If you have any suggestion, please let me know.

class MySceneHandler2 : public Producer::Camera::SceneHandler
{
public:
MySceneHandler2(osgProducer::OsgSceneHandler *sh) :
Producer::Camera::SceneHandler(),
_sh(sh)
{}

virtual void draw( Producer::Camera & camera )
{

glEnable( GL_DEPTH_TEST );
glClearColor( 0.5, 0.5, 0.5, 1.0 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Lazy initialize
if( !_initialized ) init();

glPushMatrix();
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glTranslatef( _x, _y, _z);
glRotatef( _angle, 1, 0, 0 );
glRotatef( _angle, 0, 1, 0 );

// Draw the OpenGL cube
glCallList( _cube );

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glPopMatrix();

glFlush();
}

osgProducer::OsgSceneHandler *_sh;
}

int main(int argc, char **argv)
{
osgProducer::Viewer *viewer = new osgProducer::Viewer();

viewer->setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
osg::Node* modelNode = osgDB::readNodeFile("I:/Research/OSG-based-
NAVER/NAVER/bin/models/wood_domino.pfb");

osg::MatrixTransform* modelTransform = new osg::MatrixTransform;

osg::Matrix modelScale;
osg::Matrix modelTranslate;
osg::Matrix modelRot;

modelScale.makeScale(1.0, 1.0, 1.0);
modelRot.makeRotate(0.,osg::Vec3f(1.,0.,0.));
modelTranslate.makeTranslate(osg::Vec3(0, 0, 0));

modelTransform->postMult(modelTranslate);
modelTransform->postMult(modelRot);
modelTransform->postMult(modelScale);

modelTransform->addChild(modelNode);

osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(modelTransform);

osgProducer::OsgSceneHandler *sh =
dynamic_cast<osgProducer::OsgSceneHandler *>(
viewer->getCamera(0)->getSceneHandler() );

osgUtil::SceneView *sv = NULL;
if( sh != 0L )
{
sh->getSceneView()->getRenderStage()->setClearMask(0);
sv= sh->getSceneView();
}

viewer->getCamera(0)->getRenderSurface()->fullScreen(true);
viewer->realize();
viewer->getCamera(0)->setSceneHandler( new MySceneHandler2(sh) );
viewer->setSceneData(root.get());

while (!viewer->done())
{
viewer->sync();
viewer->update();
viewer->frame();
}

viewer->sync();
viewer->cleanup_frame();
viewer->sync();

return 0;
}

Changhoon Park

unread,
Apr 12, 2007, 9:10:35 AM4/12/07
to naver-bas...@googlegroups.com

virtual void 
setDrawCallback (DrawCallback *dc)
 Set the DrawCallback which allows users to attach customize the drawing of existing Drawable object.


Hello sylvi

What about using drawcallback?

You can define a different  way of drawing for a specific node without disturbing other nodes.

C. Park

sylvi

unread,
Apr 12, 2007, 9:23:14 AM4/12/07
to NAVER based on OSG

Prof. Park,

I have tried using setDrawCallback, but this function is member of
Drawable class so I can not attach it to osg::Node or
osg::ModelTransform.

On Apr 12, 10:10 pm, "Changhoon Park" <changhoonp...@gmail.com> wrote:
> virtual void setDrawCallback<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>(
> DrawCallback<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>*dc)
> Set the DrawCallback<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>which


> allows users to attach customize the drawing of existing

> Drawable<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>object.


>
> Hello sylvi
>
> What about using drawcallback?
>
> You can define a different way of drawing for a specific node without
> disturbing other nodes.
>
> C. Park
>

> --
> Changhoon PARK- Hide quoted text -
>
> - Show quoted text -

Changhoon Park

unread,
Apr 12, 2007, 9:48:57 AM4/12/07
to naver-bas...@googlegroups.com
const DrawablegetDrawable (unsigned int i) const
 Return the Drawable at position i.


osg::Geode ...



On 4/12/07, sylvi <syl...@gmail.com > wrote:

sylvi

unread,
Apr 12, 2007, 10:42:20 PM4/12/07
to NAVER based on OSG

Prof. Park,

I found the way how to overide draw function using
osg::Drawable::DrawCallback and osg::NodeVisitor to traverse the node.
I took example from osgcallback.
It seems work in my simple application. Now I am adding it to
nvmOSGART.
I will give you an update later.

Thank you.

sylvi

On Apr 12, 10:48 pm, "Changhoon Park" <changhoonp...@gmail.com> wrote:
> const Drawable<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>*
> getDrawable<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>(unsigned
> int i) const
> Return the Drawable<http://www.openscenegraph.org/documentation/OpenSceneGraphReferenceDo...>at


> position
> i.
>
> osg::Geode ...
>

Changhoon Park

unread,
Apr 12, 2007, 11:02:07 PM4/12/07
to naver-bas...@googlegroups.com
Good job !

Maybe you can find difference between callback of module and that of scene graph node.

In my thinking, you case is first one to use node callbacks.

You can extend your modules to define draw callback. 
If there will be more cases like you, we will extend our kernel to support node callback at the module

C. Park.

> > > >     modelRot.makeRotate (0.,osg::Vec3f(1.,0.,0.));



--
Changhoon PARK

sylvi

unread,
Apr 14, 2007, 8:19:18 AM4/14/07
to NAVER based on OSG

I have added the occlusion into nvmOSGART module.
The result can be seen in
http://groups.google.com/group/naver-based-on-osg/web/occlusion-result-in-nvmosgart?hl=en

It is still not perfect but it seems better than no occlusion at all.

How does it work?

I create 3D model for floor, wall, stair and attach those model to the
marker.

Inside OSGART, I create the DrawCallback and NodeVisitor class. (code
is attached below).
(I dont use PreDraw or PostDraw since this process has to be done in
the first stage of draw)
In the XML file, we specify which models are the occlusion model.
The occlusion models need to be draw first before the other models,
therefore
the occlusion models have to be attach first before the other models
are attached to the marker.
In draw callback function, by disabling the color buffer, the
occlusion model will be drawn in Depth Buffer only.
Next, the other models will be drawn in color and depth buffer.
As the result, when the occlusion model is closer to the camera, the
image capture by camera will appear occludes the 3D model.


sylvi

=================================================
codes:

class DrawableOcclusionDrawCallback : public
osg::Drawable::DrawCallback
{
virtual void drawImplementation(osg::State& state,const
osg::Drawable* drawable) const
{
glPushAttrib(GL_COLOR_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); //disable color
buffer
drawable->drawImplementation(state);
glPopAttrib(); //restore the values
}
};

class InsertOcclusionCallbacksVisitor : public osg::NodeVisitor
{
public:

InsertOcclusionCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
}

virtual void apply(osg::Node& node)
{
traverse(node);
}

virtual void apply(osg::Geode& geode)
{
for(unsigned int i=0;i<geode.getNumDrawables();++i)
{
geode.getDrawable(i)->setDrawCallback(new
DrawableOcclusionDrawCallback());
}
}

virtual void apply(osg::Transform& node)
{
apply((osg::Node&)node);
}
};

On Apr 13, 12:02 pm, "Changhoon Park" <changhoonp...@gmail.com> wrote:
> Good job !
>
> Maybe you can find difference between callback of module and that of scene
> graph node.
>
> In my thinking, you case is first one to use node callbacks.
>
> You can extend your modules to define draw callback.
> If there will be more cases like you, we will extend our kernel to support
> node callback at the module
>
> C. Park.
>

> > > > > > modelRot.makeRotate(0.,osg::Vec3f(1.,0.,0.));

Changhoon Park

unread,
Apr 14, 2007, 9:46:49 AM4/14/07
to naver-bas...@googlegroups.com
Hello sylvi

I wonder that what if there are one more than occlusion object.
like the oder of rendering ?

C. Park

sylvi

unread,
Apr 15, 2007, 9:49:33 AM4/15/07
to NAVER based on OSG

Hi,
In the current application, I have 2 occlusion models, floor with 4
walls and stair.
The order of rendering is determined by the order of specification in
the XML file.
for example:
<OcclusionModel>
<node nodeID= "vp_stair"/>
<node nodeID= "vp_floor"/>
</OcclusionModel>
As the result, stair will be rendered first.
I havenot tested to change the order but I think it should not be
matter.
If it is matter then we can easily change the order of rendering by
modifying XML file.

Do I answer your question?

sylvi


On Apr 14, 10:46 pm, "Changhoon Park" <changhoonp...@gmail.com> wrote:
> Hello sylvi
>
> I wonder that what if there are one more than occlusion object.
> like the oder of rendering ?
>
> C. Park
>

> On 4/14/07, sylvi <sylv...@gmail.com> wrote:
>
>
>
>
>
> > I have added the occlusion into nvmOSGART module.
> > The result can be seen in
>

> >http://groups.google.com/group/naver-based-on-osg/web/occlusion-resul...

> > InsertOcclusionCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVER­SE_ALL_CHILDREN)

> ...
>
> read more »- Hide quoted text -

Reply all
Reply to author
Forward
0 new messages