[vtkusers] Using vtkExternalOpenGLCamera

Sankhesh Jhaveri sankhesh.jhaveri at kitware.com
Thu Jul 20 15:28:31 EDT 2017


Hi Elaine,

I see. In that case, you can remove all the code where you’re explicitly
setting the projection and view matrices on the external camera. The
external renderer handles that internally (automatically). The external
renderer is responsible for updating the VTK camera matrices based on the
OpenGL state.

Thanks,
Sankhesh
​

On Thu, Jul 20, 2017 at 2:13 PM Elaine Jiang <elainejiang8 at gmail.com> wrote:

> Hi Sankhesh,
>
> That makes sense! However, my end goal is indeed to integrate with a
> virtual reality system that is controlling the OpenGL camera -  I was just
> trying to get the external camera to work before integrating with a VR
> program. I can attach the render function that I'm working on that is part
> of a VR program. Here, I'm using SetViewTransformMatrix() and
> SetProjectionTransformMatrix(). Sorry for the confusion before, and thanks
> for your quick replies!
>
>
>     /// This is called each time through the main graphics loop, and
>     /// re-draws the scene according to whatever has changed since the
>     /// last time it was drawn.
>     *void onVRRenderGraphics(const MinVR::VRGraphicsState &renderState) {*
>         // Only draw if the application is still running.
>         *if (isRunning()) {*
>             // Enable depth testing. Demonstrates OpenGL context being
> managed by external
>             // application i.e. GLUT in this case.
>             *glEnable(GL_DEPTH_TEST);*
>
>             // Buffers being managed by external application i.e. GLUT in
> this case.
>             *glClearColor(1.0f, 1.0f, 1.0f, 0.0f);*
>             *glClearDepth(1.0f);*
>             *glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);* //
> Clear the color buffer
>
>             *glFlush(); * // Render now
>
>             *glEnable(GL_LIGHTING);*
> *            glEnable(GL_LIGHT0);*
>
>            // color
>             *GLfloat diffuse[] = {1.0f, 0.8f, 1.0f, 1.0f};*
> *            glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);*
> *            GLfloat specular[] = {0.5f, 0.0f, 0.0f, 1.0f};*
> *            glLightfv(GL_LIGHT0, GL_SPECULAR, specular);*
> *            GLfloat ambient[] = {1.0f, 1.0f, 0.2f,  1.0f};*
> *            glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);*
>
>
>             // Second the load() step.  We let MinVR give us the projection
>             // matrix from the render state argument to this method.
>             *const float* pm = renderState.getProjectionMatrix();*
> *            glm::mat4 projMatrix = glm::mat4( pm[0],  pm[1], pm[2],
> pm[3],*
> *                                            pm[4],  pm[5], pm[6], pm[7],*
> *                                            pm[8],  pm[9],pm[10],pm[11],*
> *                                            pm[12],pm[13],pm[14],pm[15]);*
>
>             // The draw step.  We let MinVR give us the view matrix.
>             *const float* vm = renderState.getViewMatrix();*
> *            glm::mat4 viewMatrix = glm::mat4( vm[0],  vm[1], vm[2],
> vm[3],*
> *                                            vm[4],  vm[5], vm[6], vm[7],*
> *                                            vm[8],  vm[9],vm[10],vm[11],*
> *                                            vm[12],vm[13],vm[14],vm[15]);*
>
>
> *            camera = (vtkExternalOpenGLCamera *)ren->GetActiveCamera();*
>
>             *double view[16];*
> *            for(int i = 0; i < 16; i++) {*
> *                view[i] = glm::value_ptr(viewMatrix)[i];*
> *            }*
>
>             *camera->SetViewTransformMatrix(view);*
>
> *            double proj[16];*
> *            for(int i = 0; i < 16; i++) {*
> *                proj[i] = glm::value_ptr(projMatrix)[i];*
> *            }*
>
> *            camera->SetProjectionTransformMatrix(proj);*
>
>
> *            for(int i = 0; i < 7; i++) {*
> *                // transpose - vtk*
> *                //actors[i]->SetOrientation(0,0,0);*
> *                actors[i]->RotateX(y_angle);*
> *                actors[i]->RotateY(x_angle);*
> *                actors[i]->SetScale(scale_size);*
> *            }*
>
> *            externalVTKWidget->GetRenderWindow()->Render();*
>
>             // We let MinVR swap the graphics buffers.
>             // glutSwapBuffers();
>        * }*
> *    }*
> *    };*
>
>
> Thanks,
> Elaine
>
> On Thu, Jul 20, 2017 at 2:00 PM, Sankhesh Jhaveri <
> sankhesh.jhaveri at kitware.com> wrote:
>
>> Hi Elaine,
>>
>> I didn’t see anything obvious in your sample code so decided to look more
>> closely at the external renderer implementation. Its been a while since I
>> looked at that code and my memory was rusty.
>>
>> You don’t need the external renderer if you are driving the OpenGL scene
>> via VTK. Think of the external renderer as glue code that synchronizes the
>> external OpenGL scene with VTK. It makes sure that the camera used is an
>> external camera and overrides the camera parameters with what it finds in
>> the external OpenGL scene. You need it in cases where something else is
>> controlling the OpenGL camera and you’d like the VTK scene to be up-to-date
>> as well; e.g. virtual reality systems where the main application updates
>> OpenGL camera parameters.
>>
>> In your case, you can simply instantiate a vtkRenderer and set it on the
>> vtkExternalOpenGLRenderWindow via vtkRenderWindow::AddRenderer(vtkRenderer*
>> ren). The rest of the code should work as expected - except that the
>> active camera would be of type vtkOpenGLCamera instead of
>> vtkExternalOpenGLCamera.
>>
>> Hope that helps.
>>
>> Sankhesh
>>>>
>> On Thu, Jul 20, 2017 at 1:31 PM Elaine Jiang <elainejiang8 at gmail.com>
>> wrote:
>>
>>> Hi Sankhesh,
>>>
>>> Here's my initialization function and my render function. The external
>>> camera set up is highlighted.
>>>
>>> *vtkNew<ExternalVTKWidget> externalVTKWidget;*
>>> *vtkSmartPointer<vtkRenderer> ren = externalVTKWidget->AddRenderer();*
>>> *static int windowId = -1;*
>>> *static int windowH = 800;*
>>> *static int windowW = 800;*
>>> *vtkSmartPointer<vtkActor> actors[7];*
>>>
>>> *vtkNew<vtkTransform> transform;*
>>> *int press_x, press_y;*
>>> *int release_x, release_y;*
>>> *float x_angle = 0.0;*
>>> *float y_angle = 0.0;*
>>> *float scale_size = 1;*
>>> *int xform_mode = 0;*
>>> *#define XFORM_NONE    0*
>>> *#define XFORM_ROTATE  1*
>>> *#define XFORM_SCALE 2*
>>>
>>>
>>> *void initialize() {*
>>>     *vtkNew<vtkExternalOpenGLRenderWindow> renWin;*
>>> *    externalVTKWidget->SetRenderWindow(renWin.GetPointer());*
>>>
>>>     *std::string files[7] = {"../data/newsi-ascii.vtk",
>>> "../data/newjets-ascii.vtk", "../data/fekcorr-ascii.vtk",
>>> "../data/newar-ascii.vtk", "../data/newhetg-ascii.vtk",
>>> "../data/newopt-ascii.vtk", "../data/newsi-ascii.vtk"};*
>>>
>>> *    for(int i = 0; i < 7; i++) {*
>>>        * vtkSmartPointer<vtkPolyDataReader> reader =*
>>> *        vtkSmartPointer<vtkPolyDataReader>::New();*
>>> *        reader->SetFileName(files[i].c_str());*
>>> *        reader->Update();*
>>> *        vtkSmartPointer<vtkPolyData> inputPolyData =
>>> reader->GetOutput();*
>>>
>>> *        vtkSmartPointer<vtkCleanPolyData> clean =*
>>> *        vtkSmartPointer<vtkCleanPolyData>::New();*
>>> *        clean->SetInputData(inputPolyData);*
>>>
>>>         // Generate normals
>>>         *vtkSmartPointer<vtkPolyDataNormals> normals =*
>>> *        vtkSmartPointer<vtkPolyDataNormals>::New();*
>>> *        normals->SetInputConnection(clean->GetOutputPort());*
>>> *        normals->SplittingOff();*
>>>
>>>
>>>         *vtkSmartPointer<vtkPolyDataMapper> mapper =*
>>> *        vtkSmartPointer<vtkPolyDataMapper>::New();*
>>> *        mapper->SetInputConnection(normals->GetOutputPort());*
>>>
>>>         *vtkSmartPointer<vtkActor> actor =*
>>> *        vtkSmartPointer<vtkActor>::New();*
>>> *        actor->SetMapper(mapper);*
>>> *        actor->GetProperty()->SetInterpolationToFlat();*
>>>
>>> *        ren->AddActor(actor);*
>>> *        actors[i] = actor;*
>>> *    }*
>>>
>>>     *ren->SetBackground(0, 0, 0);*
>>> *    ren->MakeCamera();*
>>> *    ren->ResetCamera();*
>>> *    renWin->AddRenderer(ren);*
>>> *}*
>>>
>>> // Render function
>>> *void display() {*
>>>     // Enable depth testing. Demonstrates OpenGL context being managed
>>> by external
>>>     // application i.e. GLUT in this case.
>>>     *glEnable(GL_DEPTH_TEST);*
>>>
>>>     // Buffers being managed by external application i.e. GLUT in this
>>> case.
>>>     *glClearColor(0.0f, 0.0f, 0.0f, 1.0f);* // Set background color to
>>> black and opaque
>>>     *glClearDepth(1.0f);*
>>>     *glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);* // Clear the
>>> color buffer
>>>
>>>     *glFlush();*  // Render now
>>>
>>>     *glEnable(GL_LIGHTING);*
>>> *    glEnable(GL_LIGHT0);*
>>>
>>>     *GLfloat diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};*
>>> *    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);*
>>> *    GLfloat specular[] = {0.5f, 0.0f, 0.0f, 1.0f};*
>>> *    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);*
>>> *    GLfloat ambient[] = {1.0f, 1.0f, 0.2f,  1.0f};*
>>> *    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);*
>>>
>>>
>>>     *vtkExternalOpenGLCamera *camera = (vtkExternalOpenGLCamera
>>> *)ren->GetActiveCamera();*
>>> *    camera->SetPosition(0,0,-100);*
>>> *    camera->SetFocalPoint(0,0,0); *
>>> *    camera->SetViewUp(0,1,0); *
>>>
>>>
>>>     *for(int i = 0; i < 7; i++) {*
>>> *        // transpose - vtk*
>>> *        actors[i]->SetOrientation(0,0,0);*
>>> *        actors[i]->RotateX(y_angle);*
>>> *        actors[i]->RotateY(x_angle);*
>>> *        actors[i]->SetScale(scale_size);*
>>>
>>> *        // transpose - opengl*
>>> *        double f[16];*
>>> *        actors[i]->GetMatrix(f);*
>>>
>>> *        // transpose*
>>> *        double g[16];*
>>> *        g[0] = f[0]; g[1] = f[4]; g[2] = f[8]; g[3] = f[12];*
>>> *        g[4] = f[1]; g[5] = f[5]; g[6] = f[9]; g[7] = f[13];*
>>> *        g[8] = f[2]; g[9] = f[6]; g[10]= f[10];g[11]= f[14];*
>>> *        g[12]= f[3]; g[13]= f[7]; g[14]= f[11];g[15]= f[15];*
>>> *        glMultMatrixd(g); // multiply current matrix with specified
>>> matrix*
>>> *    }*
>>>
>>>     *externalVTKWidget->GetRenderWindow()->Render();*
>>> *    glutSwapBuffers();*
>>> *}*
>>>
>>> Thanks,
>>> Elaine
>>>
>>> On Thu, Jul 20, 2017 at 1:11 PM, Sankhesh Jhaveri <
>>> sankhesh.jhaveri at kitware.com> wrote:
>>>
>>>> Hi Elaine,
>>>>
>>>> You shouldn’t have to set that flag for using the matrices set on the
>>>> external camera. Could you share some sample code that doesn’t work?
>>>>
>>>> Thanks,
>>>> Sankhesh
>>>>>>>>
>>>> On Thu, Jul 20, 2017 at 11:38 AM Elaine Jiang <elainejiang8 at gmail.com>
>>>> wrote:
>>>>
>>>>> Hi Sankhesh,
>>>>>
>>>>> I've tried setting the camera and focal point parameters without using
>>>>> SetProjectionMatrix() and SetModelViewTransformMatrix(), and I've also
>>>>> tried just calling SetProjectionMatrix() and SetModelViewTransformMatrix()
>>>>> by themselves. Neither approach seemed to have an effect on the scene. I
>>>>> was looking through the documentation and was wondering if I need to use
>>>>> the function vtkCamera::vtkSetUseExplicitProjectionTransformMatrix(bool) to
>>>>> solve the problem?
>>>>>
>>>>> Thanks,
>>>>> Elaine
>>>>>
>>>>> On Thu, Jul 20, 2017 at 11:12 AM, Sankhesh Jhaveri <
>>>>> sankhesh.jhaveri at kitware.com> wrote:
>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> Could you please provide more details on how you’re trying to set the
>>>>>> camera parameters?
>>>>>> The way the external camera works is that it ignores the camera
>>>>>> position and focal point parameters if the projection and model view
>>>>>> matrices are set explicitly using
>>>>>> vtkExternalOpenGLCamera::SetProjectionMatrix() and
>>>>>> vtkExternalOpenGLCamera::SetModelViewTransformMatrix()
>>>>>>
>>>>>> Thanks,
>>>>>> Sankhesh
>>>>>>>>>>>>
>>>>>> On Wed, Jul 19, 2017 at 9:59 AM elainejiang8 <elainejiang8 at gmail.com>
>>>>>> wrote:
>>>>>>
>>>>>>> Hi everyone!
>>>>>>>
>>>>>>> I'm building a program that uses the externalVTKWidget to volume
>>>>>>> render an
>>>>>>> object in an OpenGL context. Right now, I'm calling
>>>>>>>
>>>>>>> *ren->GetActiveCamera();
>>>>>>> *
>>>>>>>
>>>>>>> (where ren is a vtkExternalOpenGLRenderWindow instance) in my render
>>>>>>> function so I can control the position and focal point of the camera.
>>>>>>> However, I'm unable to get the camera to actually have any effect on
>>>>>>> the
>>>>>>> scene. The documentation for vtkExternalOpenGLCamera is rather slim,
>>>>>>> and I
>>>>>>> can't find any similar examples, so I am a bit stuck on this issue.
>>>>>>>
>>>>>>> Does anyone have any insight into how I could approach this problem?
>>>>>>> Thanks
>>>>>>> in advance!
>>>>>>>
>>>>>>> Elaine
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> View this message in context:
>>>>>>> http://vtk.1045678.n5.nabble.com/Using-vtkExternalOpenGLCamera-tp5744015.html
>>>>>>> Sent from the VTK - Users mailing list archive at Nabble.com.
>>>>>>> _______________________________________________
>>>>>>> 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
>>>>>>>
>>>>>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>>>>>
>>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>>>>>
>>>>>> --
>>>>>> Sankhesh Jhaveri *Sr. Research & Development Engineer* | Kitware
>>>>>> <http://www.kitware.com/> | (518) 881-4417
>>>>>>>>>>>>
>>>>>
>>>>> --
>>>> Sankhesh Jhaveri *Sr. Research & Development Engineer* | Kitware
>>>> <http://www.kitware.com/> | (518) 881-4417
>>>>>>>>
>>>
>>> --
>> Sankhesh Jhaveri *Sr. Research & Development Engineer* | Kitware
>> <http://www.kitware.com/> | (518) 881-4417
>>>>
>
> --
Sankhesh Jhaveri *Sr. Research & Development Engineer* | Kitware
<http://www.kitware.com/> | (518) 881-4417
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170720/b5bf0f7d/attachment.html>


More information about the vtkusers mailing list