1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "VTKViewer_PolyDataMapper.h"
21 #include "VTKViewer_MarkerUtils.h"
23 #include <utilities.h>
27 #include <vtkCellArray.h>
28 #include <vtkImageData.h>
29 #include <vtkObjectFactory.h>
30 #include <vtkPointData.h>
31 #include <vtkPolyData.h>
32 #include <vtkProperty.h>
33 #include <vtkRenderer.h>
34 #include <vtkSmartPointer.h>
35 #include <vtkTimerLog.h>
36 #include <vtkWindow.h>
39 # ifndef GLX_GLXEXT_LEGACY
40 # define GLX_GLXEXT_LEGACY
48 #ifndef VTK_IMPLEMENT_MESA_CXX
49 vtkStandardNewMacro(VTKViewer_PolyDataMapper);
52 // some definitions for what the polydata has in it
53 #define VTK_PDPSM_COLORS 0x0001
54 #define VTK_PDPSM_CELL_COLORS 0x0002
55 #define VTK_PDPSM_POINT_TYPE_FLOAT 0x0004
56 #define VTK_PDPSM_POINT_TYPE_DOUBLE 0x0008
57 #define VTK_PDPSM_NORMAL_TYPE_FLOAT 0x0010
58 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
59 #define VTK_PDPSM_OPAQUE_COLORS 0x0040
65 #define APIENTRYP APIENTRY *
68 #ifndef GL_VERTEX_PROGRAM_POINT_SIZE_ARB
69 #define GL_VERTEX_PROGRAM_POINT_SIZE_ARB 0x8642
72 #ifndef GL_ARB_point_sprite
73 #define GL_POINT_SPRITE_ARB 0x8861
74 #define GL_COORD_REPLACE_ARB 0x8862
77 #ifndef GL_ARB_vertex_buffer_object
78 typedef ptrdiff_t GLsizeiptrARB;
80 #define GL_ARRAY_BUFFER_ARB 0x8892
81 #define GL_STATIC_DRAW_ARB 0x88E4
84 typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
85 typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
86 typedef void (APIENTRYP PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
87 typedef void (APIENTRYP PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);
89 static PFNGLGENBUFFERSARBPROC vglGenBuffersARB = NULL;
90 static PFNGLBINDBUFFERARBPROC vglBindBufferARB = NULL;
91 static PFNGLBUFFERDATAARBPROC vglBufferDataARB = NULL;
92 static PFNGLDELETEBUFFERSARBPROC vglDeleteBuffersARB = NULL;
95 #define GL_GetProcAddress( x ) glXGetProcAddressARB( (const GLubyte*)x )
97 #define GL_GetProcAddress( x ) wglGetProcAddress( (const LPCSTR)x )
107 bool InitializeBufferExtensions()
109 vglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)GL_GetProcAddress( "glGenBuffersARB" );
110 if( !vglGenBuffersARB )
113 vglBindBufferARB = (PFNGLBINDBUFFERARBPROC)GL_GetProcAddress( "glBindBufferARB" );
114 if( !vglBindBufferARB )
117 vglBufferDataARB = (PFNGLBUFFERDATAARBPROC)GL_GetProcAddress( "glBufferDataARB" );
118 if( !vglBufferDataARB )
121 vglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)GL_GetProcAddress( "glDeleteBuffersARB" );
122 if( !vglDeleteBuffersARB )
128 static bool IsBufferExtensionsInitialized = InitializeBufferExtensions();
130 //-----------------------------------------------------------------------------
131 VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
133 Q_INIT_RESOURCE( VTKViewer );
135 this->ExtensionsInitialized = ES_None;
137 this->PointSpriteTexture = 0;
139 this->MarkerEnabled = false;
140 this->MarkerType = VTK::MT_NONE;
141 this->MarkerScale = VTK::MS_NONE;
145 //-----------------------------------------------------------------------------
146 VTKViewer_PolyDataMapper::~VTKViewer_PolyDataMapper()
148 if( PointSpriteTexture > 0 )
149 glDeleteTextures( 1, &PointSpriteTexture );
152 //-----------------------------------------------------------------------------
153 void VTKViewer_PolyDataMapper::SetMarkerEnabled( bool theMarkerEnabled )
155 if( this->MarkerEnabled == theMarkerEnabled )
158 this->MarkerEnabled = theMarkerEnabled;
162 //-----------------------------------------------------------------------------
163 void VTKViewer_PolyDataMapper::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
165 if( this->MarkerType == theMarkerType && this->MarkerScale == theMarkerScale )
168 this->MarkerType = theMarkerType;
169 this->MarkerScale = theMarkerScale;
171 if( this->MarkerType == VTK::MT_NONE || this->MarkerType == VTK::MT_USER ) {
172 this->ImageData = NULL;
177 int aMarkerType = (int)this->MarkerType;
178 int aMarkerScale = (int)this->MarkerScale;
180 int anId = (int)VTK::MS_70 * aMarkerType + aMarkerScale;
182 if( this->StandardTextures.find( anId ) == this->StandardTextures.end() )
184 QString aFileName = QString( ":/textures/texture%1.dat" ).arg( aMarkerType );
185 VTK::MarkerTexture aMarkerTexture;
186 if( VTK::LoadTextureData( aFileName, theMarkerScale, aMarkerTexture ) )
187 this->StandardTextures[ anId ] = VTK::MakeVTKImage( aMarkerTexture );
190 this->ImageData = this->StandardTextures[ anId ];
194 //-----------------------------------------------------------------------------
195 void VTKViewer_PolyDataMapper::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
197 if( this->MarkerType == VTK::MT_USER && this->MarkerId == theMarkerId )
200 this->MarkerType = VTK::MT_USER;
201 this->MarkerId = theMarkerId;
203 if( this->CustomTextures.find( theMarkerId ) == this->CustomTextures.end() )
204 this->CustomTextures[ theMarkerId ] = VTK::MakeVTKImage( theMarkerTexture );
206 this->ImageData = this->CustomTextures[ theMarkerId ];
210 //-----------------------------------------------------------------------------
211 VTK::MarkerType VTKViewer_PolyDataMapper::GetMarkerType()
213 return this->MarkerType;
216 //-----------------------------------------------------------------------------
217 VTK::MarkerScale VTKViewer_PolyDataMapper::GetMarkerScale()
219 return this->MarkerScale;
222 //-----------------------------------------------------------------------------
223 int VTKViewer_PolyDataMapper::GetMarkerTexture()
225 return this->MarkerId;
228 //-----------------------------------------------------------------------------
229 int VTKViewer_PolyDataMapper::InitExtensions()
231 char* ext = (char*)glGetString( GL_EXTENSIONS );
232 if( !IsBufferExtensionsInitialized ||
233 strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
234 strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL )
236 MESSAGE("Initializing ARB extensions failed");
243 //-----------------------------------------------------------------------------
244 void VTKViewer_PolyDataMapper::InitPointSprites()
246 glEnable( GL_POINT_SPRITE_ARB );
247 glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
249 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT );
251 glDepthFunc( GL_LEQUAL );
252 glEnable( GL_DEPTH_TEST );
254 glEnable( GL_ALPHA_TEST );
255 glAlphaFunc( GL_GREATER, 0.0 );
257 glDisable( GL_LIGHTING );
259 glDisable( GL_COLOR_MATERIAL );
262 //-----------------------------------------------------------------------------
263 void VTKViewer_PolyDataMapper::CleanupPointSprites()
267 glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
268 glDisable( GL_POINT_SPRITE_ARB );
271 //-----------------------------------------------------------------------------
272 void VTKViewer_PolyDataMapper::InitTextures()
274 if( !this->ImageData.GetPointer() )
277 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
278 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
279 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
280 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
282 int* aSize = this->ImageData->GetDimensions();
283 unsigned char* dataPtr = (unsigned char*)this->ImageData->GetScalarPointer();
284 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
285 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
287 //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
288 glEnable( GL_TEXTURE_2D );
289 glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
290 glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
293 //-----------------------------------------------------------------------------
294 void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
296 bool isUsePointSprites = this->MarkerEnabled && this->MarkerType != VTK::MT_NONE;
297 if( isUsePointSprites )
299 if( this->ExtensionsInitialized == ES_None )
300 this->ExtensionsInitialized = this->InitExtensions();
301 this->InitPointSprites();
302 this->InitTextures();
305 MAPPER_SUPERCLASS::RenderPiece( ren, act );
307 if( isUsePointSprites )
308 this->CleanupPointSprites();
311 //-----------------------------------------------------------------------------
312 // Definition of structures and fuctions used in Draw() method
315 //-----------------------------------------------------------------------------
322 //-----------------------------------------------------------------------------
323 struct TColorFunctorBase
327 TColorFunctorBase( vtkProperty* theProperty )
329 myAlpha = theProperty->GetOpacity();
334 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
337 //-----------------------------------------------------------------------------
338 struct TPropertyColor : TColorFunctorBase
342 TPropertyColor( vtkProperty* theProperty ):
343 TColorFunctorBase( theProperty )
345 theProperty->GetColor( myColor );
350 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
352 theVertex.r = myColor[0];
353 theVertex.g = myColor[1];
354 theVertex.b = myColor[2];
355 theVertex.a = myAlpha;
359 //-----------------------------------------------------------------------------
360 struct TColors2Color : TColorFunctorBase
362 vtkUnsignedCharArray* myColors;
364 TColors2Color( vtkProperty* theProperty,
365 vtkUnsignedCharArray* theColors ):
366 TColorFunctorBase( theProperty ),
367 myColors( theColors )
372 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
374 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
375 unsigned char* aColor = myColors->GetPointer( aTupleId << 2 );
377 theVertex.r = int( aColor[0] ) / 255.0;
378 theVertex.g = int( aColor[1] ) / 255.0;
379 theVertex.b = int( aColor[2] ) / 255.0;
380 theVertex.a = myAlpha;
385 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
388 //-----------------------------------------------------------------------------
389 struct TPointColors2Color : TColors2Color
391 TPointColors2Color( vtkProperty* theProperty,
392 vtkUnsignedCharArray* theColors ):
393 TColors2Color( theProperty, theColors )
398 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
404 //-----------------------------------------------------------------------------
405 struct TCellColors2Color : TColors2Color
407 TCellColors2Color( vtkProperty* theProperty,
408 vtkUnsignedCharArray* theColors ):
409 TColors2Color( theProperty, theColors )
414 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
420 //-----------------------------------------------------------------------------
421 template < class TCoordinates >
422 void DrawPoints( TCoordinates* theStartPoints,
423 vtkCellArray* theCells,
424 TColorFunctorBase* theColorFunctor,
425 TVertex* theVertexArr,
426 vtkIdType &theCellId,
427 vtkIdType &theVertexId )
429 vtkIdType* ptIds = theCells->GetPointer();
430 vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
432 while ( ptIds < endPtIds ) {
433 vtkIdType nPts = *ptIds;
437 TVertex& aVertex = theVertexArr[ theVertexId ];
438 vtkIdType aPointId = *ptIds;
440 TCoordinates* anOffsetPoints = theStartPoints + 3 * aPointId;
441 aVertex.vx = anOffsetPoints[0];
442 aVertex.vy = anOffsetPoints[1];
443 aVertex.vz = anOffsetPoints[2];
445 theColorFunctor->get( aVertex, aPointId, theCellId );
456 //-----------------------------------------------------------------------------
457 template < class TCoordinates >
458 void DrawCellsPoints( vtkPolyData* theInput,
459 vtkPoints* thePoints,
460 TColorFunctorBase* theColorFunctor,
461 TVertex* theVertexArr )
463 vtkIdType aCellId = 0, aVertexId = 0;
465 TCoordinates* aStartPoints = (TCoordinates*)thePoints->GetVoidPointer(0);
467 if ( vtkCellArray* aCellArray = theInput->GetVerts() )
468 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
470 if ( vtkCellArray* aCellArray = theInput->GetLines() )
471 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
473 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
474 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
476 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
477 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
481 //-----------------------------------------------------------------------------
482 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
484 if( !this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer() )
485 return MAPPER_SUPERCLASS::Draw( ren, act );
487 vtkUnsignedCharArray* colors = NULL;
488 vtkPolyData* input = this->GetInput();
492 vtkProperty* prop = act->GetProperty();
494 points = input->GetPoints();
498 colors = this->Colors;
499 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
500 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
501 !input->GetPointData()->GetScalars() )
502 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
507 vtkIdType aTotalConnectivitySize = 0;
509 if ( vtkCellArray* aCellArray = input->GetVerts() )
510 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
512 if ( vtkCellArray* aCellArray = input->GetLines() )
513 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
515 if ( vtkCellArray* aCellArray = input->GetPolys() )
516 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
518 if ( vtkCellArray* aCellArray = input->GetStrips() )
519 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
521 if ( aTotalConnectivitySize > 0 ) {
522 VTK::TVertex* aVertexArr = new VTK::TVertex[ aTotalConnectivitySize ];
524 int* aSize = this->ImageData->GetDimensions();
525 glPointSize( std::max( aSize[0], aSize[1] ) );
527 int aMode = 0; // to remove
529 VTK::TColorFunctorBase* aColorFunctor = NULL;
530 if( colors && aMode != 1 ) {
532 aColorFunctor = new VTK::TCellColors2Color( prop, colors );
534 aColorFunctor = new VTK::TPointColors2Color( prop, colors );
536 aColorFunctor = new VTK::TPropertyColor( prop );
538 if ( points->GetDataType() == VTK_FLOAT )
539 VTK::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr );
541 VTK::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr );
543 delete aColorFunctor;
546 if( this->ExtensionsInitialized == ES_Ok ) {
547 GLuint aBufferObjectID = 0;
548 vglGenBuffersARB( 1, &aBufferObjectID );
549 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
551 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
552 vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
554 delete [] aVertexArr;
556 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
557 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
559 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), (void*)0 );
560 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), (void*)(4*sizeof(GLfloat)) );
562 glEnableClientState( GL_VERTEX_ARRAY );
563 glEnableClientState( GL_COLOR_ARRAY );
565 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
567 glDisableClientState( GL_COLOR_ARRAY );
568 glDisableClientState( GL_VERTEX_ARRAY );
570 vglDeleteBuffersARB( 1, &aBufferObjectID );
571 } else { // there are no extensions
572 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), aVertexArr );
573 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex),
574 (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
576 glEnableClientState( GL_VERTEX_ARRAY );
577 glEnableClientState( GL_COLOR_ARRAY );
579 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
581 glDisableClientState( GL_COLOR_ARRAY );
582 glDisableClientState( GL_VERTEX_ARRAY );
584 delete [] aVertexArr;
589 this->UpdateProgress(1.0);