Fraxinus  18.10
An IGT application
cxRenderWindowFactory.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxRenderWindowFactory.h"
13 
14 #include <QString>
15 #include <vtkRenderWindow.h>
16 #include <vtkOpenGLRenderWindow.h>
17 #include "QVTKWidget.h"
18 #include "cxSharedOpenGLContext.h"
19 #include "cxLogger.h"
20 
21 namespace cx
22 {
23 
25 {
26  //Note: Setting offScreenRendering to true gives crash in render
27  vtkRenderWindowPtr renderWindow = createRenderWindow("cx_shared_context", false);
28  this->preventSharedContextRenderWindowFromBeingShownOnScreen(renderWindow);
29  renderWindow->Render();
30 }
31 
32 void RenderWindowFactory::preventSharedContextRenderWindowFromBeingShownOnScreen(vtkRenderWindowPtr renderWindow)
33 {
34  mQvtkWidgetForHidingSharedContextRenderWindow = new QVTKWidget();
35  mQvtkWidgetForHidingSharedContextRenderWindow->SetRenderWindow(renderWindow);
36 }
37 
38 vtkRenderWindowPtr RenderWindowFactory::getRenderWindow(QString uid, bool offScreenRendering)
39 {
40  if(this->renderWindowExists(uid))
41  {
42  return mRenderWindows[uid];
43  }
44 
45  vtkRenderWindowPtr renderWindow = this->createRenderWindow(uid, offScreenRendering);
46  return renderWindow;
47 }
48 
50 {
51  return (mRenderWindows.count(uid) > 0);
52 }
53 
55 {
56  if(!mSharedRenderWindow)
57  {
58  CX_LOG_WARNING() << "RenderWindowFactory::getSharedRenderWindow(): No shared opengl renderwindow available.";
59  }
60  return mSharedRenderWindow;
61 }
62 
63 void RenderWindowFactory::setSharedRenderWindow(vtkRenderWindowPtr sharedRenderWindow)
64 {
65  if(mSharedRenderWindow == sharedRenderWindow)
66  {
67  return;
68  }
69  mSharedRenderWindow = sharedRenderWindow;
70 
71  vtkOpenGLRenderWindowPtr opengl_renderwindow = vtkOpenGLRenderWindow::SafeDownCast(mSharedRenderWindow);
72  if(opengl_renderwindow)
73  {
74  mSharedOpenGLContext = SharedOpenGLContextPtr(new SharedOpenGLContext(opengl_renderwindow));
75  }
76  else
77  {
78  CX_LOG_WARNING() << "RenderWindowFactory::setSharedRenderWindow(): got no opengl render window.";
79  }
80 }
81 
83 {
84  return mSharedOpenGLContext;
85 }
86 
87 vtkRenderWindowPtr RenderWindowFactory::createRenderWindow(QString uid, bool offScreenRendering)
88 {
89  vtkRenderWindowPtr renderWindow = vtkRenderWindowPtr::New();
90  renderWindow->SetOffScreenRendering(offScreenRendering);
91 
92  mSharedContextCreatedCallback = SharedContextCreatedCallbackPtr::New();
93  mSharedContextCreatedCallback->setRenderWindowFactory(this);
94 
95  vtkOpenGLRenderWindowPtr opengl_renderwindow = vtkOpenGLRenderWindow::SafeDownCast(renderWindow);
96  if(!opengl_renderwindow)
97  {
98  CX_LOG_ERROR() << "The created renderwindow is not an opengl renderwindow.";
99  }
100  opengl_renderwindow->AddObserver(vtkCommand::CXSharedContextCreatedEvent, mSharedContextCreatedCallback);
101 
102  mRenderWindows[uid] = renderWindow;
103 
104  return renderWindow;
105 }
106 
107 }//cx
Shared OpenGL context.
vtkRenderWindowPtr getRenderWindow(QString uid, bool offScreenRendering=false)
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
SharedOpenGLContextPtr getSharedOpenGLContext() const
vtkSmartPointer< class vtkOpenGLRenderWindow > vtkOpenGLRenderWindowPtr
boost::shared_ptr< class SharedOpenGLContext > SharedOpenGLContextPtr
#define CX_LOG_ERROR
Definition: cxLogger.h:99
vtkRenderWindowPtr getSharedRenderWindow() const
bool renderWindowExists(QString uid)
#define CX_LOG_WARNING
Definition: cxLogger.h:98
Namespace for all CustusX production code.