From 706ff69c48aabc29556f5cdfc43a755b5895901b Mon Sep 17 00:00:00 2001 From: Ricardo Ortiz <ricardo.ortiz@kitware.com> Date: Sat, 26 Sep 2015 10:16:42 -0400 Subject: [PATCH] Minor changes to vtk rendering pipeline. --- Core/Module.cpp | 6 ++ Core/Module.h | 5 ++ Core/SDK.cpp | 11 +-- Core/ViewerBase.cpp | 3 - VTKRendering/VTKViewer.cpp | 146 ++++++++++++++++++++----------------- 5 files changed, 96 insertions(+), 75 deletions(-) diff --git a/Core/Module.cpp b/Core/Module.cpp index 6fe8eb9d1..a1ebee393 100644 --- a/Core/Module.cpp +++ b/Core/Module.cpp @@ -48,6 +48,12 @@ bool Module::isTerminationDone() return this->terminationCompleted; } +//--------------------------------------------------------------------------- +bool Module::isTerminated() +{ + return this->terminateExecution; +} + //--------------------------------------------------------------------------- void Module::waitTermination() { diff --git a/Core/Module.h b/Core/Module.h index 5d0118387..9e989cfd9 100644 --- a/Core/Module.h +++ b/Core/Module.h @@ -54,6 +54,11 @@ public: /// bool isTerminationDone(); + /// + /// \brief Check if the termination signal has being sent. + /// + bool isTerminated(); + /// /// \brief Wait for termination. /// diff --git a/Core/SDK.cpp b/Core/SDK.cpp index a7d56b319..7f297eec6 100644 --- a/Core/SDK.cpp +++ b/Core/SDK.cpp @@ -121,10 +121,7 @@ void SDK::initRegisteredModules() { for(auto &module : this->moduleList) { - if(module->getType() != core::ClassType::Viewer) - { - module->init(); - } + module->init(); } } @@ -179,14 +176,14 @@ void SDK::run() this->viewer->exec(); } - // Tell framework threads to shutdown + // Tell framework threads to shutdown if the viewer returs this->shutDown(); this->terminateAll(); // Wait for all threads to finish processing - for (auto &module : this->modules) + for (auto &moduleThread : this->modules) { - module.join(); + moduleThread.join(); } } diff --git a/Core/ViewerBase.cpp b/Core/ViewerBase.cpp index b2a6ffecc..b0351895b 100644 --- a/Core/ViewerBase.cpp +++ b/Core/ViewerBase.cpp @@ -256,9 +256,6 @@ void ViewerBase::setWindowTitle(const std::string &str) void ViewerBase::exec() { - // Init the viewer - this->init(); - while (!terminateExecution) { this->processWindowEvents(); diff --git a/VTKRendering/VTKViewer.cpp b/VTKRendering/VTKViewer.cpp index f25e5220e..a84a1d236 100644 --- a/VTKRendering/VTKViewer.cpp +++ b/VTKRendering/VTKViewer.cpp @@ -55,15 +55,15 @@ public: } /// - /// \brief Callback method + /// \brief Callback method executed by the render window interactor. /// - void Execute(vtkObject *caller, unsigned long eventId, void *callData) + void Execute(vtkObject *vtkNotUsed(caller), unsigned long eventId, void *callData) { switch(eventId) { case vtkCommand::TimerEvent: { - if(timerId == *static_cast<int*>(callData)) + if(timerId == *static_cast<int*>(callData) && !this->viewer->isTerminated()) { this->renderWindow->Render(); } @@ -71,14 +71,13 @@ public: } case vtkCommand::ExitEvent: { - vtkRenderWindowInteractor *iren = - static_cast<vtkRenderWindowInteractor*>(caller); // Close the window - iren->GetRenderWindow()->Finalize(); + this->renderWindow->Finalize(); // Stop the interactor - iren->TerminateApp(); - viewer->terminate(); + this->renderWindowInteractor->TerminateApp(); + this->renderWindowInteractor->DestroyTimer(this->timerId); + this->viewer->terminate(); break; } } @@ -97,15 +96,8 @@ public: /// void render() { - this->viewer->adjustFPS(); this->renderWindow->Render(); - this->renderWindowInteractor->AddObserver(vtkCommand::TimerEvent,this); - - this->renderWindowInteractor->AddObserver(vtkCommand::ExitEvent,this); - - this->timerId = renderWindowInteractor->CreateRepeatingTimer(1000.0/60.0); this->renderWindowInteractor->Start(); - this->renderWindowInteractor->DestroyTimer(this->timerId); } /// @@ -116,6 +108,58 @@ public: this->renderWindow->RemoveRenderer(renderer); } + /// + /// \brief Add light to vtk renderer + /// + void addLight(vtkRenderer *renderer, Light *light) + { + auto position = light->lightPos.getPosition(); + auto colorDiffuse = light->lightColorDiffuse.getValue(); + auto colorAmbient = light->lightColorDiffuse.getValue(); + auto colorSpecular = light->lightColorDiffuse.getValue(); + auto coneAngle = light->spotCutOffAngle; + auto focalPoint = light->focusPosition; + auto constAttenuation = light->attn_constant; + auto linearAttenuation = light->attn_linear; + auto quadAttenuation = light->attn_quadratic; + + vtkNew<vtkLight> l; + l->SetLightTypeToSceneLight(); + l->SetPosition(position[0],position[1],position[2]); + l->SetDiffuseColor(colorDiffuse[0],colorDiffuse[1],colorDiffuse[2]); + l->SetAmbientColor(colorAmbient[0],colorAmbient[1],colorAmbient[2]); + l->SetSpecularColor(colorSpecular[0],colorSpecular[1],colorSpecular[2]); + l->SetConeAngle(coneAngle); + l->SetFocalPoint(focalPoint[0],focalPoint[1],focalPoint[2]); + l->SetAttenuationValues(constAttenuation,linearAttenuation,quadAttenuation); + renderer->AddLight(l.GetPointer()); + } + + /// + /// \brief Add camera to vtk renderer + /// + void addCamera(vtkRenderer *renderer, Camera *camera) + { + auto position = camera->getPos(); + auto focus = camera->getFocus(); + auto upView = camera->getOrientation() * core::Vec3f::UnitZ(); + auto viewangle = camera->getViewAngleDeg(); + auto nearClippingRange = camera->getNearClipDist(); + auto farClippingRange = camera->getFarClipDist(); + auto zoom = camera->getZoom(); + + vtkNew<vtkCamera> c; + c->SetPosition(position[0],position[1],position[2]); + c->SetFocalPoint(focus[0],focus[1],focus[2]); + c->SetViewAngle(viewangle); + c->SetClippingRange(nearClippingRange,farClippingRange); + c->Zoom(zoom); + c->SetViewUp(upView[0],upView[1],upView[2]); + + renderer->SetActiveCamera(c.GetPointer()); + renderer->ResetCamera(); + } + /// /// \brief Add renderer to the render window /// @@ -123,61 +167,26 @@ public: { // Create a new renderer and add actors to it. vtkNew<vtkRenderer> renderer; + // The actors are obtained from VTKRenderDelegates std::shared_ptr<VTKRenderDelegate> delegate; for(auto &ro : this->viewer->renderOperations) { - - // Set up lighting + // Set up lights auto lights = ro.scene->getLights(); for(auto &light : lights) { - auto position = light->lightPos.getPosition(); - auto colorDiffuse = light->lightColorDiffuse.getValue(); - auto colorAmbient = light->lightColorDiffuse.getValue(); - auto colorSpecular = light->lightColorDiffuse.getValue(); - auto coneAngle = light->spotCutOffAngle; - auto focalPoint = light->focusPosition; - auto constAttenuation = light->attn_constant; - auto linearAttenuation = light->attn_linear; - auto quadAttenuation = light->attn_quadratic; - - vtkNew<vtkLight> l; - l->SetLightTypeToSceneLight(); - l->SetPosition(position[0],position[1],position[2]); - l->SetDiffuseColor(colorDiffuse[0],colorDiffuse[1],colorDiffuse[2]); - l->SetAmbientColor(colorAmbient[0],colorAmbient[1],colorAmbient[2]); - l->SetSpecularColor(colorSpecular[0],colorSpecular[1],colorSpecular[2]); - l->SetConeAngle(coneAngle); - l->SetFocalPoint(focalPoint[0],focalPoint[1],focalPoint[2]); - l->SetAttenuationValues(constAttenuation,linearAttenuation,quadAttenuation); - renderer->AddLight(l.GetPointer()); + this->addLight(renderer.GetPointer(),light.get()); } // Set up camera auto camera = ro.scene->getCamera(); if(camera) { - auto position = camera->getPos(); - auto focus = camera->getFocus(); - auto upView = camera->getOrientation() * core::Vec3f::UnitZ(); - auto viewangle = camera->getViewAngleDeg(); - auto nearClippingRange = camera->getNearClipDist(); - auto farClippingRange = camera->getFarClipDist(); - auto zoom = camera->getZoom(); - - vtkNew<vtkCamera> c; - c->SetPosition(position[0],position[1],position[2]); - c->SetFocalPoint(focus[0],focus[1],focus[2]); - c->SetViewAngle(viewangle); - c->SetClippingRange(nearClippingRange,farClippingRange); - c->Zoom(zoom); - c->SetViewUp(upView[0],upView[1],upView[2]); - - renderer->SetActiveCamera(c.GetPointer()); - renderer->ResetCamera(); + this->addCamera(renderer.GetPointer(),camera.get()); } + // Set up actors for(const auto &object : ro.scene->getSceneObject()) { delegate = std::dynamic_pointer_cast<VTKRenderDelegate>(object->getRenderDelegate()); @@ -187,6 +196,8 @@ public: } } } + + // Add actors from objects directly attached to the viewer. for(const auto &object : this->viewer->objectList) { delegate = std::dynamic_pointer_cast<VTKRenderDelegate>(object->getRenderDelegate()); @@ -212,20 +223,26 @@ public: this->renderWindow->SetWindowName(this->viewer->windowTitle.data()); this->renderWindowInteractor->SetRenderWindow(this->renderWindow.GetPointer()); + this->renderWindowInteractor->AddObserver(vtkCommand::TimerEvent,this); + this->renderWindowInteractor->AddObserver(vtkCommand::ExitEvent,this); + + // Initialize must be called prior to creating timer events. + this->renderWindowInteractor->Initialize(); + this->timerId = renderWindowInteractor->CreateRepeatingTimer(1000.0/60.0); + vtkNew<vtkInteractorStyleSwitch> style; style->SetCurrentStyleToTrackballCamera(); renderWindowInteractor->SetInteractorStyle(style.GetPointer()); - if(viewer->viewerRenderDetail & SIMMEDTK_VIEWERRENDER_GLOBAL_AXIS) + if(0)// viewer->viewerRenderDetail & SIMMEDTK_VIEWERRENDER_GLOBAL_AXIS) { - std::cerr << "Axis display does not work properly." << std::endl; -// vtkNew<vtkAxesActor> axesActor; -// -// vtkNew<vtkOrientationMarkerWidget> orientationWidget; -// orientationWidget->SetOrientationMarker( axesActor.GetPointer() ); -// orientationWidget->SetInteractor( renderWindowInteractor.GetPointer() ); -// orientationWidget->SetEnabled( 1 ); -// orientationWidget->InteractiveOff(); + // FIXME: This causes the renderer to crash. + vtkNew<vtkAxesActor> axesActor; + vtkNew<vtkOrientationMarkerWidget> orientationWidget; + orientationWidget->SetOrientationMarker( axesActor.GetPointer() ); + orientationWidget->SetInteractor( renderWindowInteractor.GetPointer() ); + orientationWidget->SetEnabled( 1 ); + orientationWidget->InteractiveOff(); } // Set up background @@ -251,7 +268,6 @@ VTKViewer::~VTKViewer() void VTKViewer::exec() { - this->init(); this->render(); this->terminationCompleted = true; } -- GitLab