<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>Hi,</div>

<div> </div>

<div>thanks for the quick help. I think the non-linear z-Buffer is the issue that I am struggling with. I already use the camera clipping range to map the buffer values to distances. However the resulting distances do not match my expectations.</div>

<div> </div>

<div>I created an example with a sphere source (radius = 20) centered in the origin. A light is positioned 50 units from the origin and is focused towards the origin. Thus I expect the minimum depth in the shadow map to be around 30. However it is 42.</div>

<div> </div>

<div>To compute the depths I first download the texture to an unsigned int array. As the mapping is usually down on floats I convert my values to floats by dividing through the maximum value of unsigned int. After that my values are in the range from 0 to 1 and I can use conversion formula found on the web.</div>

<div> </div>

<div>In my understanding if I position the camera on one of the coordinate system axis, thus looking on the bounding box of the cube in a perpendicular direction, the minimum value in the depth map should be 0 as the near plane of the clipping range is exactly as far away as the closest point of the sphere. However the minimum value is not 0 and consequently after the conversion the minimum distance does not match the expected value of 30.</div>

<div> </div>

<div>I hope anyone have an idea what I am doing wrong. I attach the sample code at the end if anyone wants to have a look at it.</div>

<div> </div>

<div>Thanks</div>

<div>Max</div>

<div> </div>

<div>
<div>#include "vtkActor.h"<br/>
#include "vtkCamera.h"<br/>
#include "vtkNew.h"<br/>
#include "vtkOpenGLRenderer.h"<br/>
#include "vtkPolyDataMapper.h"<br/>
#include "vtkRenderWindow.h"<br/>
#include "vtkRenderer.h"<br/>
#include "vtkShadowMapBakerPass.h"<br/>
#include "vtkTextureObject.h"<br/>
#include "vtkSphereSource.h"<br/>
#include "vtkPixelBufferObject.h"<br/>
#include "vtkLight.h"</div>

<div>void createLight(vtkLight* light, float radius, float phi, float theta);</div>

<div>int main(int argc, char *argv[])<br/>
{<br/>
    const int resolution = 1024;</div>

<div>    //Create Renderer<br/>
    vtkNew<vtkRenderer> renderer;<br/>
    renderer->SetBackground(0.3, 0.4, 0.6);</div>

<div>    //create light and add it to renderer<br/>
    vtkNew<vtkLight> light;<br/>
    createLight(light.Get(), 50.0, 0.0, 0.0);<br/>
    renderer->RemoveAllLights();<br/>
    renderer->AddLight(light.Get());</div>

<div>    //create source<br/>
    vtkNew<vtkSphereSource> sphereSource;<br/>
    sphereSource->SetRadius(20.0);<br/>
    sphereSource->SetPhiResolution(100.0);<br/>
    sphereSource->SetThetaResolution(100.0);</div>

<div>    //configure mapper<br/>
    vtkNew<vtkPolyDataMapper> mapper;<br/>
    mapper->SetInputConnection(sphereSource->GetOutputPort());</div>

<div>    //Configure actor<br/>
    vtkNew<vtkActor> actor;<br/>
    actor->SetMapper(mapper.Get());<br/>
    renderer->AddActor(actor.Get());</div>

<div><br/>
    //Configure Render Window<br/>
    vtkNew<vtkRenderWindow> renderWindow;<br/>
    renderWindow->SetSize(600, 600);<br/>
    renderWindow->AddRenderer(renderer.Get());<br/>
    renderWindow->SetMultiSamples(0);<br/>
    renderWindow->SetOffScreenRendering(1);</div>

<div>    //create baker pass<br/>
    vtkNew<vtkShadowMapBakerPass> bakerPass;<br/>
    bakerPass->SetResolution(resolution);</div>

<div>    // tell the renderer to use the baker pass<br/>
    vtkOpenGLRenderer *glrenderer =<br/>
        vtkOpenGLRenderer::SafeDownCast(renderer.GetPointer());<br/>
    glrenderer->SetPass(bakerPass.Get());</div>

<div>    //render, i.e. create shadow map<br/>
    renderWindow->Render();</div>

<div>    // get the shadow map<br/>
    vtkTextureObject *to = (*bakerPass->GetShadowMaps())[0];<br/>
    // by default the textures have depth comparison on<br/>
    // but for simple display we need to turn it off<br/>
    to->SetDepthTextureCompare(false);</div>

<div>    //download texture into array<br/>
    vtkPixelBufferObject* pixelBuffer = to->Download();<br/>
    int type = 7;<br/>
    unsigned int* bufferArray = new unsigned int[resolution*resolution];<br/>
    unsigned int dims[2] = {resolution, resolution};<br/>
    int numcomps = 1;<br/>
    vtkIdType increments[2] = {0, 0};<br/>
    pixelBuffer->Download2D(type, bufferArray, dims, numcomps, increments);</div>

<div>    //Get Light Camera and clipping range<br/>
    auto cameraVector = bakerPass->GetLightCameras();<br/>
    vtkCamera* cam = (*cameraVector)[0];<br/>
    double clippingRange[2];<br/>
    cam->GetClippingRange(clippingRange);<br/>
    float zNear = clippingRange[0];<br/>
    float zFar = clippingRange[1];</div>

<div>    std::cout << "zNear = " << zNear << ", zFar = " << zFar <<  std::endl;</div>

<div>    //normalize depth values<br/>
    float* floatBuffer = new float[resolution * resolution]; <br/>
    for(unsigned int i = 0; i < resolution * resolution; ++i)<br/>
    {<br/>
        unsigned int& value = bufferArray[i];</div>

<div>        //convert to float [0, 1]<br/>
        float floatValue = ((float) value)/ULONG_MAX;    </div>

<div>        //convert to distance<br/>
        floatValue = 2.0 * zNear * zFar / (zFar + zNear - floatValue * (zFar - zNear)); <br/>
        floatBuffer[i] = floatValue;<br/>
    }</div>
</div>

<div> 
<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Gesendet:</b> Donnerstag, 09. Juni 2016 um 15:35 Uhr<br/>
<b>Von:</b> "Ken Martin" <ken.martin@kitware.com><br/>
<b>An:</b> "Maximilian Dankbar" <indienator@gmx.de><br/>
<b>Cc:</b> vtk <vtkusers@vtk.org><br/>
<b>Betreff:</b> Re: [vtkusers] Use depth map from vtkShadowMapBakerPass</div>

<div name="quoted-content">
<div>Not sure if this helps (or is your issue) but the depth values are post light-camera perspective transform. To get them back into world coordinates you have to apply the inverse of that transform (or something simpler if you just want the world coordinate distance). It is a non-linear mapping. If you google zbuffer to world there are usually a number of hits that talk about this process. To do it you need the  light-camera clipping range from the original matrix (around line 619 in the shadow  map baker pass)

<div> </div>

<div>Thanks</div>

<div>Ken

<div> </div>

<div> </div>
</div>
</div>

<div class="gmail_extra"> 
<div class="gmail_quote">On Thu, Jun 9, 2016 at 7:05 AM, Maximilian Dankbar <span><<a href="indienator@gmx.de" target="_parent">indienator@gmx.de</a>></span> wrote:

<blockquote class="gmail_quote" style="margin: 0 0 0 0.8ex;border-left: 1.0px rgb(204,204,204) solid;padding-left: 1.0ex;">
<div>
<div style="font-family: Verdana;font-size: 12.0px;">
<div style="font-family: Verdana;font-size: 12.0px;">
<div>Hi everyone,</div>

<div> </div>

<div>I try to use the depth maps generated by vtkShadowMapBakerPass to determine which objects are occluded by other objects.  To do that I download the texture to an unsigned int array.</div>

<div> </div>

<div>However I can not figure out how to interpret the values within the map. Normalizing with the clipping range values of the corresponding light camera only works for one camera position. In that case the direction of projection of the camera is (0, 1, 0). In all other cases that I tested the near clipping range is far too low and the far clipping range probably too high.</div>

<div> </div>

<div>Is there any other way to normalize the depth values?</div>

<div> </div>

<div>Thank you in advance for your help!</div>

<div>
<div>Max</div>
</div>
</div>
</div>
</div>
<br/>
_______________________________________________<br/>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br/>
<br/>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br/>
<br/>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br/>
<br/>
Search the list archives at: <a href="http://markmail.org/search/?q=vtkusers" target="_blank">http://markmail.org/search/?q=vtkusers</a><br/>
<br/>
Follow this link to subscribe/unsubscribe:<br/>
<a href="http://public.kitware.com/mailman/listinfo/vtkusers" target="_blank">http://public.kitware.com/mailman/listinfo/vtkusers</a><br/>
 </blockquote>
</div>
 

<div> </div>
--

<div class="gmail_signature">Ken Martin PhD

<div>Chairman & CFO<br/>
Kitware Inc.<br/>
28 Corporate Drive<br/>
Clifton Park NY 12065<br/>
518 371 3971
<div> </div>

<div><span style="font-size: 10.0pt;font-family: Tahoma , sans-serif;">This communication, including all attachments, contains confidential and legally privileged information, and it is intended only for the use of the addressee.  Access to this email by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution or any action taken in reliance on it is prohibited and may be unlawful. If you received this communication in error please notify us immediately and destroy the original message.  Thank you.</span></div>
</div>
</div>
</div>
</div>
</div>
</div></div></body></html>