7 #include "fwRenderVTK/vtk/Helpers.hpp" 9 #include <fwCore/base.hpp> 11 #include <vtkAssemblyNode.h> 12 #include <vtkAssemblyPath.h> 13 #include <vtkCamera.h> 15 #include <vtkOpenGLRenderWindow.h> 16 #include <vtkPicker.h> 17 #include <vtkPoints.h> 18 #include <vtkProp3DCollection.h> 19 #include <vtkRenderer.h> 29 vtkIdType getNearestPointId(vtkPoints* pts, vtkRenderer* renderer)
32 double camPosition[3];
33 double distance = VTK_DOUBLE_MAX;
34 renderer->GetActiveCamera()->GetPosition(camPosition);
36 for(vtkIdType i = 0; i < pts->GetNumberOfPoints(); i++)
38 double*
point = pts->GetPoint(i);
39 double distancePtCam = vtkMath::Distance2BetweenPoints(point, camPosition);
41 if(distancePtCam < distance)
44 distance = distancePtCam;
53 vtkProp* getNearestPickedProp(vtkAbstractPropPicker* picker, vtkRenderer* renderer)
56 vtkPicker* vtkpicker = vtkPicker::SafeDownCast(picker);
58 SLM_ASSERT(
"getNearestPickedProp *need* a picker.", picker);
59 SLM_ASSERT(
"getNearestPickedProp *need* a renderer.", renderer);
63 vtkIdType
id = getNearestPointId(vtkpicker->GetPickedPositions(), renderer);
65 if (
id > -1 && vtkpicker->GetProp3Ds()->GetNumberOfItems() > id)
67 res = vtkProp::SafeDownCast(vtkpicker->GetProp3Ds()->GetItemAsObject(static_cast<int>(
id)));
72 res = picker->GetProp3D();
79 bool getNearestPickedPosition(vtkAbstractPropPicker* picker, vtkRenderer* renderer,
double position[3])
82 vtkPicker* vtkpicker = vtkPicker::SafeDownCast(picker);
84 SLM_ASSERT(
"getNearestPickedProp *need* a picker.", picker);
85 SLM_ASSERT(
"getNearestPickedProp *need* a renderer.", renderer);
90 vtkPoints* pts = vtkpicker->GetPickedPositions();
91 vtkIdType
id = getNearestPointId(pts, renderer);
95 point = pts->GetPoint(
id);
99 point = vtkpicker->GetPickPosition();
104 point = picker->GetPickPosition();
109 std::copy(point, point + 3, position);
119 #if VTK_MAJOR_VERSION >= 7 120 vtkSmartPointer<vtkShaderProgram>
122 vtkSmartPointer<vtkShaderProgram2>
126 buildShader( vtkOpenGLRenderWindow* pWindow,
127 const char* pcVertexShader,
128 const char* pcFragmentShader )
130 SLM_ASSERT(
"NULL parameter", pWindow && pcVertexShader && pcFragmentShader );
132 vtkOpenGLRenderWindow* pOpenGLWindow = vtkOpenGLRenderWindow::SafeDownCast(pWindow);
135 SLM_ERROR(
"Shader only supported using OpenGL.");
139 #if VTK_MAJOR_VERSION >= 7 140 vtkSmartPointer<vtkShaderProgram> pProgram = vtkSmartPointer<vtkShaderProgram>::New();
143 vtkShader* shader = vtkShader::New();
144 shader->SetType(vtkShader::Vertex);
145 shader->SetSource(pcVertexShader);
146 pProgram->SetVertexShader(shader);
151 vtkShader* shader = vtkShader::New();
152 shader->SetType(vtkShader::Fragment);
153 shader->SetSource(pcFragmentShader);
154 pProgram->SetFragmentShader(shader);
158 vtkSmartPointer<vtkShaderProgram2> pProgram = vtkSmartPointer<vtkShaderProgram2>::New();
160 vtkShader2* shader = vtkShader2::New();
161 shader->SetType(VTK_SHADER_TYPE_VERTEX);
162 shader->SetSourceCode(pcVertexShader);
163 shader->SetContext(pProgram->GetContext());
164 pProgram->GetShaders()->AddItem(shader);
169 vtkShader2* shader = vtkShader2::New();
170 shader->SetType(VTK_SHADER_TYPE_FRAGMENT);
171 shader->SetSourceCode(pcFragmentShader);
172 shader->SetContext(pProgram->GetContext());
173 pProgram->GetShaders()->AddItem(shader);
176 #endif // VTK_MAJOR_VERSION >= 7 183 void openShader(
const char* _pcName, std::string& _strShader)
187 std::ifstream myfile(_pcName);
188 if ( myfile.is_open() )
190 while ( myfile.good() )
192 getline( myfile, line );
193 _strShader += line +
"\n";
199 SLM_ERROR(
"Unable to open file : " + std::string(_pcName));
205 #if VTK_MAJOR_VERSION >= 7 206 vtkSmartPointer<vtkShaderProgram>
208 vtkSmartPointer<vtkShaderProgram2>
212 buildShaderFromFile( vtkRenderWindow* pWindow,
213 const char* pcVertexName,
214 const char* pcFragmentName )
216 SLM_ASSERT(
"NULL parameter", pWindow && pcVertexName && pcFragmentName );
218 vtkOpenGLRenderWindow* pOpenGLWindow = vtkOpenGLRenderWindow::SafeDownCast(pWindow);
221 SLM_ERROR(
"Shader only supported using OpenGL.");
225 #if VTK_MAJOR_VERSION >= 7 226 vtkSmartPointer<vtkShaderProgram> pProgram = vtkSmartPointer<vtkShaderProgram>::New();
229 std::string strShader(
"");
230 openShader(pcVertexName, strShader);
232 vtkShader* shader = vtkShader::New();
233 shader->SetType(vtkShader::Vertex);
234 shader->SetSource(strShader.c_str());
235 pProgram->SetVertexShader(shader);
240 std::string strShader(
"");
241 openShader(pcFragmentName, strShader);
243 vtkShader* shader = vtkShader::New();
244 shader->SetType(vtkShader::Fragment);
245 shader->SetSource(strShader.c_str());
246 pProgram->SetFragmentShader(shader);
250 vtkSmartPointer<vtkShaderProgram2> pProgram = vtkSmartPointer<vtkShaderProgram2>::New();
253 std::string strShader(
"");
254 openShader(pcVertexName, strShader);
256 vtkShader2* shader = vtkShader2::New();
257 shader->SetType(VTK_SHADER_TYPE_VERTEX);
258 shader->SetSourceCode(strShader.c_str());
259 shader->SetContext(pProgram->GetContext());
260 pProgram->GetShaders()->AddItem(shader);
265 std::string strShader(
"");
266 openShader(pcFragmentName, strShader);
268 vtkShader2* shader = vtkShader2::New();
269 shader->SetType(VTK_SHADER_TYPE_FRAGMENT);
270 shader->SetSourceCode(strShader.c_str());
271 shader->SetContext(pProgram->GetContext());
272 pProgram->GetShaders()->AddItem(shader);
#define SLM_ERROR(message)
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
The namespace fwRenderVTK contains classes for rendering with VTK.