OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Volume Rendering and Depth Buffer


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
AnnieOwl
Newbie


Joined: 12 Apr 2019
Posts: 3

PostPosted: Fri Apr 12, 2019 5:20 pm    Post subject:
Volume Rendering and Depth Buffer
Reply with quote

Hi. In a project that I'm working on I have an osgVolume::VolumeScene containing one volume model and some polygonal models. I need to do some depth buffer checks on that scene, so I've attached depth buffer to viewer camera. When I fetch osg::Image representing depth buffer content, everything is fine, except background values are not calculated properly (they should be 1, but instead it's ~0.15). And when I add osg::Depth( osg::Depth::Function::LESS, zNear, zFar ) attribute to the camera, background values are calculated correctly, but I lose all of the polygonal models data (they are no longer visible, and not presented in depth buffer).

Here is function that does depth buffer checking:
Code:

bool pickZBufferIntersection( osgViewer::View& view, bool perspectiveProjection, const osg::Vec2& point2d, osg::Vec3& pickedPoint )
{
    osg::Camera* camera = view.getCamera();
    if ( camera == nullptr )
    {
        Q_ASSERT_X( false, "bool pickZBufferIntersection( ... )", "View has no camera." );
        return false;
    }

    const osg::Image* zImage = camera->getBufferAttachmentMap()[osg::Camera::BufferComponent::DEPTH_BUFFER]._image;
    if ( zImage == nullptr )
    {
        Q_ASSERT_X( false, "bool pickZBufferIntersection( ... )", "ZBuffer is not attached to view camera." );
        return false;
    }

    osg::Vec3f cameraPos;
    osg::Vec3f center;
    osg::Vec3f up;
    camera->getViewMatrixAsLookAt( cameraPos, center, up );

    osg::Matrixd prInv = osg::Matrixd::inverse( camera->getProjectionMatrix() );
    osg::Matrixd viewInv = osg::Matrixd::inverse( camera->getViewMatrix() );

    const osg::Viewport* viewPort = camera->getViewport();
   
    int x = point2d.x() - viewPort->x();
    int y = point2d.y() - viewPort->y();
   
    osg::Vec3 s(0, 0, -1);
    s[0] = (point2d[0] / viewPort->width()) * 2.0 - 1.0;
    s[1] = (point2d[1] / viewPort->height()) * 2.0 - 1.0;
    s = s * prInv * viewInv;
   
    osg::Vec3d v = s - cameraPos;

    double zNear = 1.0f;
    double zFar = 10000.0f;

    static uint pickNumber = 0;

    double zV, z_n, z;
    osg::Vec3f point;

    zV = ( (float*)zImage->data( x, y ) )[ 0 ];

    if( perspectiveProjection )
    {
        z_n = 2.0*zV - 1.0;
        z = 2.0 * zNear * zFar / ( zFar + zNear - z_n * ( zFar - zNear ) );

        point = cameraPos + v*z;
    }
    else
    {
        z = ( zFar - zNear )*zV;
        point = s + ( center - cameraPos )*z;
    }
   
    if( z > zNear + 1e-8 && z < zFar - 1 - 1e-8 )
    {
        pickedPoint = point;
        return true;
    }
    return false;
}


And here is viewer setup bit:
Code:

QWidget* createViewWidget(osgQt::GraphicsWindowQt* gw, osgVolume::VolumeScene& scene)
{
    osgViewer::View* view = new osgViewer::View;
    /* ... */
    osg::Camera* camera = view->getCamera();
    camera->setGraphicsContext(gw);
    camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);

    const osg::GraphicsContext::Traits* traits = gw->getTraits();
    camera->setClearColor(osgColor( SCENE3D_BACKGROUND_COLOR ));
    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));

    osg::ref_ptr<osg::Image> zImage = new osg::Image();
    zImage->allocateImage(1000, 1000, 1, GL_DEPTH_COMPONENT, GL_FLOAT);
    camera->attach(osg::Camera::DEPTH_BUFFER, zImage);
   
    const int orthoRange = 120;
    const float zNear = 1.0f;
    const float zFar = 10000.0f;
    camera->setProjectionMatrixAsOrtho(-orthoRange, orthoRange, -orthoRange, orthoRange, zNear, zFar);
    //camera->getOrCreateStateSet()->setAttribute( new osg::Depth( osg::Depth::Function::LESS, zNear, zFar ), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );

    /* ... */

    view->setSceneData(&scene);
    view->addEventHandler(new osgViewer::StatsHandler);
   
    gw->setTouchEventsEnabled(true);

    return gw->getGLWidget();
}


...

Thank you!

Cheers,
Annie[/img]
Back to top
View user's profile Send private message
Chris Hanson
Guest





PostPosted: Mon Apr 15, 2019 11:17 am    Post subject:
Volume Rendering and Depth Buffer
Reply with quote

Just out of curiousity, what are you volume rendering and what are your requirements? We did some interesting volume rendering work a few years ago for Iowa State University's Ames Lab.

On Mon, Apr 15, 2019 at 1:08 PM Anna Osvin < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
Hi. In a project that I'm working on I have an osgVolume::VolumeScene containing one volume model and some polygonal models. I need to do some depth buffer checks on that scene, so I've attached depth buffer to viewer camera. When I fetch osg::Image representing depth buffer content, everything is fine, except background values are not calculated properly (they should be 1, but instead it's ~0.15). And when I add osg::Depth( osg::Depth::Function::LESS, zNear, zFar ) attribute to the camera, background values are calculated correctly, but I lose all of the polygonal models data (they are no longer visible, and not presented in depth buffer).

Here is function that does depth buffer checking:

Code:

bool pickZBufferIntersection( osgViewer::View& view, bool perspectiveProjection, const osg::Vec2& point2d, osg::Vec3& pickedPoint )
{
    osg::Camera* camera = view.getCamera();
    if ( camera == nullptr )
    {
        Q_ASSERT_X( false, "bool pickZBufferIntersection( ... )", "View has no camera." );
        return false;
    }

    const osg::Image* zImage = camera->getBufferAttachmentMap()[osg::Camera::BufferComponent::DEPTH_BUFFER]._image;
    if ( zImage == nullptr )
    {
        Q_ASSERT_X( false, "bool pickZBufferIntersection( ... )", "ZBuffer is not attached to view camera." );
        return false;
    }

    osg::Vec3f cameraPos;
    osg::Vec3f center;
    osg::Vec3f up;
    camera->getViewMatrixAsLookAt( cameraPos, center, up );

    osg::Matrixd prInv = osg::Matrixd::inverse( camera->getProjectionMatrix() );
    osg::Matrixd viewInv = osg::Matrixd::inverse( camera->getViewMatrix() );

    const osg::Viewport* viewPort = camera->getViewport();

    int x = point2d.x() - viewPort->x();
    int y = point2d.y() - viewPort->y();

    osg::Vec3 s(0, 0, -1);
    s[0] = (point2d[0] / viewPort->width()) * 2.0 - 1.0;
    s[1] = (point2d[1] / viewPort->height()) * 2.0 - 1.0;
    s = s * prInv * viewInv;

    osg::Vec3d v = s - cameraPos;

    double zNear = 1.0f;
    double zFar = 10000.0f;

    static uint pickNumber = 0;

    double zV, z_n, z;
    osg::Vec3f point;

    zV = ( (float*)zImage->data( x, y ) )[ 0 ];

    if( perspectiveProjection )
    {
        z_n = 2.0*zV - 1.0;
        z = 2.0 * zNear * zFar / ( zFar + zNear - z_n * ( zFar - zNear ) );

        point = cameraPos + v*z;
    }
    else
    {
        z = ( zFar - zNear )*zV;
        point = s + ( center - cameraPos )*z;
    }

    if( z > zNear + 1e-8 && z < zFar - 1 - 1e-8 )
    {
        pickedPoint = point;
        return true;
    }
    return false;
}




And here is viewer setup bit:

Code:

QWidget* createViewWidget(osgQt::GraphicsWindowQt* gw, osgVolume::VolumeScene& scene)
{
    osgViewer::View* view = new osgViewer::View;
    /* ... */
    osg::Camera* camera = view->getCamera();
    camera->setGraphicsContext(gw);
    camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);

    const osg::GraphicsContext::Traits* traits = gw->getTraits();
    camera->setClearColor(osgColor( SCENE3D_BACKGROUND_COLOR ));
    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));

    osg::ref_ptr<osg::Image> zImage = new osg::Image();
    zImage->allocateImage(1000, 1000, 1, GL_DEPTH_COMPONENT, GL_FLOAT);
    camera->attach(osg::Camera::DEPTH_BUFFER, zImage);

    const int orthoRange = 120;
    const float zNear = 1.0f;
    const float zFar = 10000.0f;
    camera->setProjectionMatrixAsOrtho(-orthoRange, orthoRange, -orthoRange, orthoRange, zNear, zFar);
    //camera->getOrCreateStateSet()->setAttribute( new osg::Depth( osg::Depth::Function::LESS, zNear, zFar ), osg::StateAttribute:ShockedN | osg::StateAttribute::OVERRIDE );

    /* ... */

    view->setSceneData(&scene);
    view->addEventHandler(new osgViewer::StatsHandler);

    gw->setTouchEventsEnabled(true);

    return gw->getGLWidget();
}




...

Thank you!

Cheers,
Annie[/img]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75829#75829




Attachments:
http://forum.openscenegraph.org//files/depth_buffer_depthrange_185.png
http://forum.openscenegraph.org//files/depth_buffer_154.png


_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



--
Chris 'Xenon' Hanson, omo sanza lettere. http://www.alphapixel.com/
Training • Consulting • Contracting
3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 • GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL
Legal/IP • Forensics • Imaging • UAVs • GIS • GPS • osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile • iPhone/iPad/iOS • Android
@alphapixel facebook.com/alphapixel (775) 623-PIXL [7495]

------------------
Post generated by Mail2Forum
Back to top
AnnieOwl
Newbie


Joined: 12 Apr 2019
Posts: 3

PostPosted: Mon Apr 15, 2019 12:35 pm    Post subject:
Reply with quote

We are working on medical software for dentists. We need to render CBCT and give user possibility to place some markdown points on it, for future diagnostics. Also sometimes it's required to render polygonal jaw models alongside with CBCT. As I said earlier, we nailed down the rendering and even "hit detection", but there is a nasty bag with Z Buffer values of the background being incorrect, therefore user can place points onto nothing.

...

Thank you!

Cheers,
Anna
Back to top
View user's profile Send private message
Chris Hanson
Guest





PostPosted: Mon Apr 15, 2019 2:27 pm    Post subject:
Volume Rendering and Depth Buffer
Reply with quote

Instead of reading Z depth values, can you simply run an intersection of the click ray-vector against the model data (polygonal and volumetric) when they click to place markdown points?

On Mon, Apr 15, 2019 at 2:34 PM Anna Osvin < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
We are working on medical software for Dentists. We need to render CBCT and give user possibility to place some markdown points on it, for future diagnostics. Also sometimes it's required to render polygonal jaw models alongside with CBCT. As I said earlier, we nailed down the rendering and even "hit detection", but there is a nasty bag with Z Buffer values of the background being incorrect, therefore user can place points onto nothing.

...

Thank you!

Cheers,
Anna

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75839#75839





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



--
Chris 'Xenon' Hanson, omo sanza lettere. http://www.alphapixel.com/
Training • Consulting • Contracting
3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 • GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL
Legal/IP • Forensics • Imaging • UAVs • GIS • GPS • osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile • iPhone/iPad/iOS • Android
@alphapixel facebook.com/alphapixel (775) 623-PIXL [7495]

------------------
Post generated by Mail2Forum
Back to top
AnnieOwl
Newbie


Joined: 12 Apr 2019
Posts: 3

PostPosted: Tue Apr 16, 2019 11:06 am    Post subject:
Re: Volume Rendering and Depth Buffer
Reply with quote

If you mean osgViewer::View::computeIntersections, then we tried it. For some reason it does not check intersections with volume model.

Here is raycast intersection check code:
Code:

bool pickPolygonalSceneIntersection( osgViewer::View& view, const osg::Vec2& point2d, osg::Vec3& pickedPoint )
{
    const osg::Camera* camera = view.getCamera();
    if ( camera == nullptr ) {
        Q_ASSERT_X( false, "bool pickPolygonalSceneIntersection( ... )", "View has no camera." );
        return false;
    }
   
    osgUtil::LineSegmentIntersector::Intersections intersections;

    if ( view.computeIntersections( camera, osgUtil::Intersector::CoordinateFrame::WINDOW, point2d.x(), point2d.y(), intersections ) )
    {
        for (osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
            hitr != intersections.end();
            ++hitr)
        {
            if (hitr->nodePath.size() > 4)
            {
                pickedPoint = hitr->getWorldIntersectPoint();
               
                return true;
            }
        }
    }

    return false;
}


Chris Hanson wrote:
Instead of reading Z depth values, can you simply run an intersection of the click ray-vector against the model data (polygonal and volumetric) when they click to place markdown points?

On Mon, Apr 15, 2019 at 2:34 PM Anna Osvin < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
We are working on medical software for Dentists. We need to render CBCT and give user possibility to place some markdown points on it, for future diagnostics. Also sometimes it's required to render polygonal jaw models alongside with CBCT. As I said earlier, we nailed down the rendering and even "hit detection", but there is a nasty bag with Z Buffer values of the background being incorrect, therefore user can place points onto nothing.

...

Thank you!

Cheers,
Anna

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75839#75839





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



--
Chris 'Xenon' Hanson, omo sanza lettere. http://www.alphapixel.com/
Training • Consulting • Contracting
3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 • GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL
Legal/IP • Forensics • Imaging • UAVs • GIS • GPS • osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile • iPhone/iPad/iOS • Android
@alphapixel facebook.com/alphapixel (775) 623-PIXL [7495]

------------------
Post generated by Mail2Forum
[/code]
Back to top
View user's profile Send private message
Chris Hanson
Guest





PostPosted: Tue Apr 16, 2019 3:33 pm    Post subject:
Volume Rendering and Depth Buffer
Reply with quote

Yeah, I wouldn't totally expect it would, but you can probably do that intersection test yourself if needed. I think it'll be faster and more accurate than the Z-buffer.

I did a tool that relied on the Z-buffer once. It was a bad choice...


On Tue, Apr 16, 2019 at 1:05 PM Anna Osvin < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:

Quote:
If you mean osgViewer::View::computeIntersections, then we tried it. For some reason it does not check intersections with volume model.

Here is raycast intersection check code:

Code:

bool pickPolygonalSceneIntersection( osgViewer::View& view, const osg::Vec2& point2d, osg::Vec3& pickedPoint )
{
    const osg::Camera* camera = view.getCamera();
    if ( camera == nullptr ) {
        Q_ASSERT_X( false, "bool pickPolygonalSceneIntersection( ... )", "View has no camera." );
        return false;
    }

    osgUtil::LineSegmentIntersector::Intersections intersections;

    if ( view.computeIntersections( camera, osgUtil::Intersector::CoordinateFrame::WINDOW, point2d.x(), point2d.y(), intersections ) )
    {
        for (osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
            hitr != intersections.end();
            ++hitr)
        {
            if (hitr->nodePath.size() > 4)
            {
                pickedPoint = hitr->getWorldIntersectPoint();

                return true;
            }
        }
    }

    return false;
}





Chris Hanson wrote:
Quote:
Instead of reading Z depth values, can you simply run an intersection of the click ray-vector against the model data (polygonal and volumetric) when they click to place markdown points?

On Mon, Apr 15, 2019 at 2:34 PM Anna Osvin < ()> wrote:


Quote:
We are working on medical software for Dentists. We need to render CBCT and give user possibility to place some markdown points on it, for future diagnostics. Also sometimes it's required to render polygonal jaw models alongside with CBCT. As I said earlier, we nailed down the rendering and even "hit detection", but there is a nasty bag with Z Buffer values of the background being incorrect, therefore user can place points onto nothing.

...

Thank you!

Cheers,
Anna

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75839#75839 (http://forum.openscenegraph.org/viewtopic.php?p=75839#75839)





_______________________________________________
osg-users mailing list
  ()
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org (http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org)




--
Chris 'Xenon' Hanson, omo sanza lettere.  http://www.alphapixel.com/ (http://www.alphapixel.com/)
Training • Consulting • Contracting
3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 • GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL
Legal/IP • Forensics • Imaging • UAVs • GIS • GPS • osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile • iPhone/iPad/iOS • Android
@alphapixel (https://twitter.com/alphapixel) facebook.com/alphapixel (http://facebook.com/alphapixel) (775) 623-PIXL [7495]

  ------------------
Post generated by Mail2Forum
[/code]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75845#75845





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org



--
Chris 'Xenon' Hanson, omo sanza lettere. http://www.alphapixel.com/
Training • Consulting • Contracting
3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 • GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL
Legal/IP • Forensics • Imaging • UAVs • GIS • GPS • osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile • iPhone/iPad/iOS • Android
@alphapixel facebook.com/alphapixel (775) 623-PIXL [7495]

------------------
Post generated by Mail2Forum
Back to top
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts two pass rendering without enhancing ... Trajce Nikolov NICK General 2 Tue Jul 02, 2019 9:58 am View latest post
No new posts problem about reconstructing world co... swaggydong General 0 Sun Jun 23, 2019 8:26 am View latest post
No new posts Rendering a Depthmap lucasamparo General 2 Tue Jun 04, 2019 6:27 pm View latest post
No new posts Layered rendering with a geometry shader AnyOldName3 General 8 Tue May 14, 2019 11:25 pm View latest post
No new posts off screen rendering with OSG Sorin General 0 Mon May 13, 2019 8:14 am View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP