<div dir="ltr">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.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Apr 15, 2019 at 1:08 PM Anna Osvin <<a href="mailto:annieloveowls@gmail.com">annieloveowls@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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).<br>
<br>
Here is function that does depth buffer checking:<br>
<br>
Code:<br>
<br>
bool pickZBufferIntersection( osgViewer::View& view, bool perspectiveProjection, const osg::Vec2& point2d, osg::Vec3& pickedPoint )<br>
{<br>
osg::Camera* camera = view.getCamera();<br>
if ( camera == nullptr )<br>
{<br>
Q_ASSERT_X( false, "bool pickZBufferIntersection( ... )", "View has no camera." );<br>
return false;<br>
}<br>
<br>
const osg::Image* zImage = camera->getBufferAttachmentMap()[osg::Camera::BufferComponent::DEPTH_BUFFER]._image;<br>
if ( zImage == nullptr )<br>
{<br>
Q_ASSERT_X( false, "bool pickZBufferIntersection( ... )", "ZBuffer is not attached to view camera." );<br>
return false;<br>
}<br>
<br>
osg::Vec3f cameraPos;<br>
osg::Vec3f center;<br>
osg::Vec3f up;<br>
camera->getViewMatrixAsLookAt( cameraPos, center, up );<br>
<br>
osg::Matrixd prInv = osg::Matrixd::inverse( camera->getProjectionMatrix() );<br>
osg::Matrixd viewInv = osg::Matrixd::inverse( camera->getViewMatrix() );<br>
<br>
const osg::Viewport* viewPort = camera->getViewport();<br>
<br>
int x = point2d.x() - viewPort->x();<br>
int y = point2d.y() - viewPort->y();<br>
<br>
osg::Vec3 s(0, 0, -1);<br>
s[0] = (point2d[0] / viewPort->width()) * 2.0 - 1.0;<br>
s[1] = (point2d[1] / viewPort->height()) * 2.0 - 1.0;<br>
s = s * prInv * viewInv;<br>
<br>
osg::Vec3d v = s - cameraPos;<br>
<br>
double zNear = 1.0f;<br>
double zFar = 10000.0f;<br>
<br>
static uint pickNumber = 0;<br>
<br>
double zV, z_n, z;<br>
osg::Vec3f point;<br>
<br>
zV = ( (float*)zImage->data( x, y ) )[ 0 ];<br>
<br>
if( perspectiveProjection )<br>
{<br>
z_n = 2.0*zV - 1.0;<br>
z = 2.0 * zNear * zFar / ( zFar + zNear - z_n * ( zFar - zNear ) );<br>
<br>
point = cameraPos + v*z;<br>
}<br>
else<br>
{<br>
z = ( zFar - zNear )*zV;<br>
point = s + ( center - cameraPos )*z;<br>
}<br>
<br>
if( z > zNear + 1e-8 && z < zFar - 1 - 1e-8 )<br>
{<br>
pickedPoint = point;<br>
return true;<br>
}<br>
return false;<br>
}<br>
<br>
<br>
<br>
<br>
And here is viewer setup bit:<br>
<br>
Code:<br>
<br>
QWidget* createViewWidget(osgQt::GraphicsWindowQt* gw, osgVolume::VolumeScene& scene)<br>
{<br>
osgViewer::View* view = new osgViewer::View;<br>
/* ... */<br>
osg::Camera* camera = view->getCamera();<br>
camera->setGraphicsContext(gw);<br>
camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);<br>
<br>
const osg::GraphicsContext::Traits* traits = gw->getTraits();<br>
camera->setClearColor(osgColor( SCENE3D_BACKGROUND_COLOR ));<br>
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));<br>
<br>
osg::ref_ptr<osg::Image> zImage = new osg::Image();<br>
zImage->allocateImage(1000, 1000, 1, GL_DEPTH_COMPONENT, GL_FLOAT);<br>
camera->attach(osg::Camera::DEPTH_BUFFER, zImage);<br>
<br>
const int orthoRange = 120;<br>
const float zNear = 1.0f;<br>
const float zFar = 10000.0f;<br>
camera->setProjectionMatrixAsOrtho(-orthoRange, orthoRange, -orthoRange, orthoRange, zNear, zFar);<br>
//camera->getOrCreateStateSet()->setAttribute( new osg::Depth( osg::Depth::Function::LESS, zNear, zFar ), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );<br>
<br>
/* ... */<br>
<br>
view->setSceneData(&scene);<br>
view->addEventHandler(new osgViewer::StatsHandler);<br>
<br>
gw->setTouchEventsEnabled(true);<br>
<br>
return gw->getGLWidget();<br>
}<br>
<br>
<br>
<br>
<br>
... <br>
<br>
Thank you!<br>
<br>
Cheers,<br>
Annie[/img]<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=75829#75829" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=75829#75829</a><br>
<br>
<br>
<br>
<br>
Attachments: <br>
<a href="http://forum.openscenegraph.org//files/depth_buffer_depthrange_185.png" rel="noreferrer" target="_blank">http://forum.openscenegraph.org//files/depth_buffer_depthrange_185.png</a><br>
<a href="http://forum.openscenegraph.org//files/depth_buffer_154.png" rel="noreferrer" target="_blank">http://forum.openscenegraph.org//files/depth_buffer_154.png</a><br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div style="text-align:center">Chris 'Xenon' Hanson, omo sanza lettere. Xenon@AlphaPixel.com <a href="http://www.alphapixel.com/" target="_blank">http://www.alphapixel.com/</a></div><div style="text-align:center">Training • Consulting • Contracting</div><div style="text-align:center">3D • Scene Graphs (Open Scene Graph/OSG) • OpenGL 2 • OpenGL 3 • OpenGL 4 • GLSL • OpenGL ES 1 • OpenGL ES 2 • OpenCL</div><div style="text-align:center"><span style="font-size:12.8px">Legal/IP •</span><span style="font-size:12.8px"> </span><span style="font-size:12.8px">Forensics •</span><span style="font-size:12.8px"> </span>Imaging <span style="font-size:12.8px">•</span><span style="font-size:12.8px"> </span><span style="font-size:12.8px">UAVs </span><span style="font-size:12.8px">• GIS • GPS • osgEarth • Terrain • Telemetry • Cryptography • LIDAR • Embedded • Mobile • iPhone/iPad/iOS • Android</span></div><div style="text-align:center"><a href="https://twitter.com/alphapixel" target="_blank">@alphapixel</a> <a href="http://facebook.com/alphapixel" target="_blank">facebook.com/alphapixel</a> (775) 623-PIXL [7495]<br></div></div></div></div></div></div>