[vtkusers] Offscreen GPU Volume Rendering Mac OS X generates black images

Chris Hafey chafey at gmail.com
Thu Sep 25 09:39:04 EDT 2014


The follow code will reproduce the problem - change the line commented out
near the top to switch between GPU and RayCast rendering to see it working
and not working.  Let me know if you need a CMakefile and DICOM files (I
assume you have DICOM files and can plug this code into an existing project
if needed )

#include <string>

#include <fstream>

#include <vtkSmartPointer.h>

#include <vtkImageSlabReslice.h>

#include <vtkRenderer.h>

#include <vtkRenderWindow.h>

#include <vtkPiecewiseFunction.h>

#include <vtkColorTransferFunction.h>

#include <vtkImageData.h>

#include <vtkMatrix4x4.h>

#include <vtkWindowToImageFilter.h>

#include <vtkDICOMImageReader.h>

#include <vtkSmartVolumeMapper.h>

#include <vtkJPEGWriter.h>

#include <vtkCamera.h>

#include <vtkVolumeProperty.h>

#include <vtkUnsignedCharArray.h>


int main(int argc, char** argv)

{


    //int renderMode =vtkSmartVolumeMapper::RayCastRenderMode; // this works

    int renderMode =vtkSmartVolumeMapper::GPURenderMode; // this generates
black image



    // Read the DICOM Data

    vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<
vtkDICOMImageReader>::New();

    reader->SetDirectoryName("/Users/chafey/DICOM/Volume3");

    reader->Update();

    vtkSmartPointer<vtkImageData> imageData(reader->GetOutput());



    // Translate origin to center of volume

    double xSpacing, ySpacing, zSpacing;

    imageData->GetSpacing(xSpacing, ySpacing, zSpacing);

    int* dims = imageData->GetDimensions();

    double xCenter = dims[0] * xSpacing / 2.0;

    double yCenter = dims[1] * ySpacing / 2.0;

    double zCenter = dims[2] * zSpacing / 2.0;

    imageData->SetOrigin(-xCenter, -yCenter, -zCenter);


     // Setup the renderer for offscreen rendering

    vtkSmartPointer<vtkRenderer>  renderer = vtkSmartPointer<vtkRenderer>::
New();



    // Setup the render window

    vtkSmartPointer<vtkRenderWindow> render_window = vtkSmartPointer<
vtkRenderWindow>::New();

    render_window->SetOffScreenRendering(1);

    render_window->AddRenderer(renderer);

    render_window->SetSize(512, 512);



    // Setup the volume mapper

    vtkSmartPointer<vtkSmartVolumeMapper> volume_mapper = vtkSmartPointer<
vtkSmartVolumeMapper>::New();

    volume_mapper->SetRequestedRenderMode(renderMode);

    volume_mapper->SetInputData(imageData);




    // Setup the volume

    vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();

    volume->SetMapper(volume_mapper);

    renderer->AddViewProp(volume);



    // Render the image

    render_window->Render();

    vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
vtkSmartPointer<vtkWindowToImageFilter>::New();

    windowToImageFilter->SetInput(render_window);

    windowToImageFilter->Update();



    // Create a JPEG in memory

    vtkSmartPointer<vtkJPEGWriter> writer = vtkSmartPointer<vtkJPEGWriter>::
New();

    writer->WriteToMemoryOn();

    writer->SetInputConnection(windowToImageFilter->GetOutputPort());

    writer->Write();

    vtkSmartPointer<vtkUnsignedCharArray> dataPtr = writer->GetResult();

    dataPtr->Squeeze();



    // write the jpeg to disk

    ofstream myFile ("image.jpg", ios::out | ios::binary);

    myFile.write ((const char*)dataPtr->GetPointer(0), dataPtr->GetSize());



    return 0;

}

On Thu, Sep 25, 2014 at 7:42 AM, Aashish Chaudhary <
aashish.chaudhary at kitware.com> wrote:

> On Thu, Sep 25, 2014 at 8:38 AM, Chris Hafey <chafey at gmail.com> wrote:
>
>> No errors on the console.  I will work on a standalone example.  Can you
>> please clarify whether or not I should be using mesa / osmesa for GPU
>> rendering on Mac OS X?
>>
>
> In theory your regular driver should work with the offscreen rendering
> setting. As per my understanding, that will just not open the window but
> will create a context in which the rendering is performed.  But if the
> image is black and if it is not if you don't set that flag then it means
> the context was not created properly. VTK should have given you a error but
> may be it is fooled by the system itself.
>
> You can try osmesa, make sure that VTK detects it, and see if that works.
>
> - Aashish
>
>>
>> Chris
>>
>> On Thu, Sep 25, 2014 at 7:37 AM, Aashish Chaudhary <
>> aashish.chaudhary at kitware.com> wrote:
>>
>>> This is good to know. Do you get any errors on the console? A standalone
>>> example  will be nice.
>>>
>>> - Aashish
>>>
>>> On Thu, Sep 25, 2014 at 8:32 AM, Chris Hafey <chafey at gmail.com> wrote:
>>>
>>>> I am enabling off screen rendering by calling SetOffScreenRendering(1)
>>>> on the vtkRenderWindow.  Should I be using osmesa?  I can create a
>>>> standalone example to reproduce if needed.
>>>>
>>>> Chris
>>>>
>>>> On Thu, Sep 25, 2014 at 7:21 AM, Aashish Chaudhary <
>>>> aashish.chaudhary at kitware.com> wrote:
>>>>
>>>>> No problem. How did you enable the offscreen?  Can you post your code
>>>>> somewhere?
>>>>>
>>>>> Thanks,
>>>>> Aashish
>>>>>
>>>>> On Thu, Sep 25, 2014 at 8:05 AM, Chris Hafey <chafey at gmail.com> wrote:
>>>>>
>>>>>> Hi Aashish,
>>>>>>   Forgive my ignorance but I don't know much about osmesa or whether
>>>>>> I am using it.  I am using VTK as built by homebrew and don't see any
>>>>>> options related to osmesa.  I have not installed osmesa on the machine.  No
>>>>>> errors or warnings are issued on the console.
>>>>>>
>>>>>> Chris
>>>>>>
>>>>>> On Thu, Sep 25, 2014 at 6:46 AM, Aashish Chaudhary <
>>>>>> aashish.chaudhary at kitware.com> wrote:
>>>>>>
>>>>>>> Hi Chris,
>>>>>>>
>>>>>>> Are you using osmesa for the offscreen rendering? Are you getting
>>>>>>> any error on the console?
>>>>>>>
>>>>>>> Thanks
>>>>>>>
>>>>>>>
>>>>>>> On Thu, Sep 25, 2014 at 7:32 AM, Chris Hafey <chafey at gmail.com>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> VTK 6.1.0
>>>>>>>> Mac OS X Mavericks 10.9.5
>>>>>>>> Macbook Pro 2012 Retina with nVidia GeForce GT 650M 1024 MB
>>>>>>>>
>>>>>>>> My program uses VTK to do off screen volume rendering of medical
>>>>>>>> images.  I suspect this is a bug in VTK and not my software because:
>>>>>>>> 1) GPURenderMode works fine on Windows (both in a VM running on
>>>>>>>> same Mac and on two other Windows 7 boxes with an nVidia GPUs)
>>>>>>>> 2) RayCastRenderMode works fine on Mac OS X
>>>>>>>> 3) TextureRenderMode works fine on Mac OS X
>>>>>>>>
>>>>>>>> I tried searching the mailing list and google and couldn't find
>>>>>>>> anything.  The rendering code is about as basic as it can be - it simply
>>>>>>>> sets the color, opacity, clip planes and matrix.  Any ideas on what I might
>>>>>>>> be doing wrong or how I can debug?
>>>>>>>>
>>>>>>>> Thanks
>>>>>>>>
>>>>>>>> Chris Hafey
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> Powered by www.kitware.com
>>>>>>>>
>>>>>>>> Visit other Kitware open-source projects at
>>>>>>>> http://www.kitware.com/opensource/opensource.html
>>>>>>>>
>>>>>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>>>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>>>>>
>>>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> *| Aashish Chaudhary | Technical Leader         | Kitware Inc.
>>>>>>>       *
>>>>>>> *| http://www.kitware.com/company/team/chaudhary.html
>>>>>>> <http://www.kitware.com/company/team/chaudhary.html>*
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>>
>>>>>
>>>>>
>>>>> *| Aashish Chaudhary | Technical Leader         | Kitware Inc.
>>>>>     *
>>>>> *| http://www.kitware.com/company/team/chaudhary.html
>>>>> <http://www.kitware.com/company/team/chaudhary.html>*
>>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>>
>>>
>>>
>>> *| Aashish Chaudhary | Technical Leader         | Kitware Inc.
>>>   *
>>> *| http://www.kitware.com/company/team/chaudhary.html
>>> <http://www.kitware.com/company/team/chaudhary.html>*
>>>
>>
>>
>
>
> --
>
>
>
> *| Aashish Chaudhary | Technical Leader         | Kitware Inc.            *
> *| http://www.kitware.com/company/team/chaudhary.html
> <http://www.kitware.com/company/team/chaudhary.html>*
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20140925/59ce764f/attachment.html>


More information about the vtkusers mailing list