1 // Copyright (C) 2007-2012 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 )
100 bool InitializeBufferExtensions()
102 vglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)GL_GetProcAddress( "glGenBuffersARB" );
103 if( !vglGenBuffersARB )
106 vglBindBufferARB = (PFNGLBINDBUFFERARBPROC)GL_GetProcAddress( "glBindBufferARB" );
107 if( !vglBindBufferARB )
110 vglBufferDataARB = (PFNGLBUFFERDATAARBPROC)GL_GetProcAddress( "glBufferDataARB" );
111 if( !vglBufferDataARB )
114 vglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)GL_GetProcAddress( "glDeleteBuffersARB" );
115 if( !vglDeleteBuffersARB )
121 static bool IsBufferExtensionsInitialized = InitializeBufferExtensions();
123 //-----------------------------------------------------------------------------
124 VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
126 Q_INIT_RESOURCE( VTKViewer );
128 this->ExtensionsInitialized = ES_None;
130 this->PointSpriteTexture = 0;
132 this->MarkerEnabled = false;
133 this->MarkerType = VTK::MT_NONE;
134 this->MarkerScale = VTK::MS_NONE;
138 //-----------------------------------------------------------------------------
139 VTKViewer_PolyDataMapper::~VTKViewer_PolyDataMapper()
141 if( PointSpriteTexture > 0 )
142 glDeleteTextures( 1, &PointSpriteTexture );
145 //-----------------------------------------------------------------------------
146 void VTKViewer_PolyDataMapper::SetMarkerEnabled( bool theMarkerEnabled )
148 if( this->MarkerEnabled == theMarkerEnabled )
151 this->MarkerEnabled = theMarkerEnabled;
155 //-----------------------------------------------------------------------------
156 void VTKViewer_PolyDataMapper::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
158 if( this->MarkerType == theMarkerType && this->MarkerScale == theMarkerScale )
161 this->MarkerType = theMarkerType;
162 this->MarkerScale = theMarkerScale;
164 if( this->MarkerType == VTK::MT_NONE || this->MarkerType == VTK::MT_USER ) {
165 this->ImageData = NULL;
170 int aMarkerType = (int)this->MarkerType;
171 int aMarkerScale = (int)this->MarkerScale;
173 int anId = (int)VTK::MS_70 * aMarkerType + aMarkerScale;
175 if( this->StandardTextures.find( anId ) == this->StandardTextures.end() )
177 QString aFileName = QString( ":/textures/texture%1.dat" ).arg( aMarkerType );
178 VTK::MarkerTexture aMarkerTexture;
179 if( VTK::LoadTextureData( aFileName, theMarkerScale, aMarkerTexture ) )
180 this->StandardTextures[ anId ] = VTK::MakeVTKImage( aMarkerTexture );
183 this->ImageData = this->StandardTextures[ anId ];
187 //-----------------------------------------------------------------------------
188 void VTKViewer_PolyDataMapper::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
190 if( this->MarkerType == VTK::MT_USER && this->MarkerId == theMarkerId )
193 this->MarkerType = VTK::MT_USER;
194 this->MarkerId = theMarkerId;
196 if( this->CustomTextures.find( theMarkerId ) == this->CustomTextures.end() )
197 this->CustomTextures[ theMarkerId ] = VTK::MakeVTKImage( theMarkerTexture );
199 this->ImageData = this->CustomTextures[ theMarkerId ];
203 //-----------------------------------------------------------------------------
204 VTK::MarkerType VTKViewer_PolyDataMapper::GetMarkerType()
206 return this->MarkerType;
209 //-----------------------------------------------------------------------------
210 VTK::MarkerScale VTKViewer_PolyDataMapper::GetMarkerScale()
212 return this->MarkerScale;
215 //-----------------------------------------------------------------------------
216 int VTKViewer_PolyDataMapper::GetMarkerTexture()
218 return this->MarkerId;
221 //-----------------------------------------------------------------------------
222 int VTKViewer_PolyDataMapper::InitExtensions()
224 char* ext = (char*)glGetString( GL_EXTENSIONS );
225 if( !IsBufferExtensionsInitialized ||
226 strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
227 strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL )
229 MESSAGE("Initializing ARB extensions failed");
236 //-----------------------------------------------------------------------------
237 void VTKViewer_PolyDataMapper::InitPointSprites()
239 glEnable( GL_POINT_SPRITE_ARB );
240 glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
242 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT );
244 glDepthFunc( GL_LEQUAL );
245 glEnable( GL_DEPTH_TEST );
247 glEnable( GL_ALPHA_TEST );
248 glAlphaFunc( GL_GREATER, 0.0 );
250 glDisable( GL_LIGHTING );
252 glDisable( GL_COLOR_MATERIAL );
255 //-----------------------------------------------------------------------------
256 void VTKViewer_PolyDataMapper::CleanupPointSprites()
260 glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
261 glDisable( GL_POINT_SPRITE_ARB );
264 //-----------------------------------------------------------------------------
265 void VTKViewer_PolyDataMapper::InitTextures()
267 if( !this->ImageData.GetPointer() )
270 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
271 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
272 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
273 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
275 int* aSize = this->ImageData->GetDimensions();
276 unsigned char* dataPtr = (unsigned char*)this->ImageData->GetScalarPointer();
277 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
278 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
280 //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
281 glEnable( GL_TEXTURE_2D );
282 glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
283 glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
286 //-----------------------------------------------------------------------------
287 void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
289 bool isUsePointSprites = this->MarkerEnabled && this->MarkerType != VTK::MT_NONE;
290 if( isUsePointSprites )
292 if( this->ExtensionsInitialized == ES_None )
293 this->ExtensionsInitialized = this->InitExtensions();
294 this->InitPointSprites();
295 this->InitTextures();
298 MAPPER_SUPERCLASS::RenderPiece( ren, act );
300 if( isUsePointSprites )
301 this->CleanupPointSprites();
304 //-----------------------------------------------------------------------------
305 // Definition of structures and fuctions used in Draw() method
308 //-----------------------------------------------------------------------------
315 //-----------------------------------------------------------------------------
316 struct TColorFunctorBase
320 TColorFunctorBase( vtkProperty* theProperty )
322 myAlpha = theProperty->GetOpacity();
327 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
330 //-----------------------------------------------------------------------------
331 struct TPropertyColor : TColorFunctorBase
335 TPropertyColor( vtkProperty* theProperty ):
336 TColorFunctorBase( theProperty )
338 theProperty->GetColor( myColor );
343 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
345 theVertex.r = myColor[0];
346 theVertex.g = myColor[1];
347 theVertex.b = myColor[2];
348 theVertex.a = myAlpha;
352 //-----------------------------------------------------------------------------
353 struct TColors2Color : TColorFunctorBase
355 vtkUnsignedCharArray* myColors;
357 TColors2Color( vtkProperty* theProperty,
358 vtkUnsignedCharArray* theColors ):
359 TColorFunctorBase( theProperty ),
360 myColors( theColors )
365 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
367 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
368 unsigned char* aColor = myColors->GetPointer( aTupleId << 2 );
370 theVertex.r = int( aColor[0] ) / 255.0;
371 theVertex.g = int( aColor[1] ) / 255.0;
372 theVertex.b = int( aColor[2] ) / 255.0;
373 theVertex.a = myAlpha;
378 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
381 //-----------------------------------------------------------------------------
382 struct TPointColors2Color : TColors2Color
384 TPointColors2Color( vtkProperty* theProperty,
385 vtkUnsignedCharArray* theColors ):
386 TColors2Color( theProperty, theColors )
391 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
397 //-----------------------------------------------------------------------------
398 struct TCellColors2Color : TColors2Color
400 TCellColors2Color( vtkProperty* theProperty,
401 vtkUnsignedCharArray* theColors ):
402 TColors2Color( theProperty, theColors )
407 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
413 //-----------------------------------------------------------------------------
414 template < class TCoordinates >
415 void DrawPoints( TCoordinates* theStartPoints,
416 vtkCellArray* theCells,
417 TColorFunctorBase* theColorFunctor,
418 TVertex* theVertexArr,
419 vtkIdType &theCellId,
420 vtkIdType &theVertexId )
422 vtkIdType* ptIds = theCells->GetPointer();
423 vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
425 while ( ptIds < endPtIds ) {
426 vtkIdType nPts = *ptIds;
430 TVertex& aVertex = theVertexArr[ theVertexId ];
431 vtkIdType aPointId = *ptIds;
433 TCoordinates* anOffsetPoints = theStartPoints + 3 * aPointId;
434 aVertex.vx = anOffsetPoints[0];
435 aVertex.vy = anOffsetPoints[1];
436 aVertex.vz = anOffsetPoints[2];
438 theColorFunctor->get( aVertex, aPointId, theCellId );
449 //-----------------------------------------------------------------------------
450 template < class TCoordinates >
451 void DrawCellsPoints( vtkPolyData* theInput,
452 vtkPoints* thePoints,
453 TColorFunctorBase* theColorFunctor,
454 TVertex* theVertexArr )
456 vtkIdType aCellId = 0, aVertexId = 0;
458 TCoordinates* aStartPoints = (TCoordinates*)thePoints->GetVoidPointer(0);
460 if ( vtkCellArray* aCellArray = theInput->GetVerts() )
461 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
463 if ( vtkCellArray* aCellArray = theInput->GetLines() )
464 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
466 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
467 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
469 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
470 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
474 //-----------------------------------------------------------------------------
475 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
477 if( !this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer() )
478 return MAPPER_SUPERCLASS::Draw( ren, act );
480 vtkUnsignedCharArray* colors = NULL;
481 vtkPolyData* input = this->GetInput();
485 vtkProperty* prop = act->GetProperty();
487 points = input->GetPoints();
491 colors = this->Colors;
492 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
493 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
494 !input->GetPointData()->GetScalars() )
495 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
500 vtkIdType aTotalConnectivitySize = 0;
502 if ( vtkCellArray* aCellArray = input->GetVerts() )
503 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
505 if ( vtkCellArray* aCellArray = input->GetLines() )
506 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
508 if ( vtkCellArray* aCellArray = input->GetPolys() )
509 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
511 if ( vtkCellArray* aCellArray = input->GetStrips() )
512 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
514 if ( aTotalConnectivitySize > 0 ) {
515 VTK::TVertex* aVertexArr = new VTK::TVertex[ aTotalConnectivitySize ];
517 int* aSize = this->ImageData->GetDimensions();
518 glPointSize( std::max( aSize[0], aSize[1] ) );
520 int aMode = 0; // to remove
522 VTK::TColorFunctorBase* aColorFunctor = NULL;
523 if( colors && aMode != 1 ) {
525 aColorFunctor = new VTK::TCellColors2Color( prop, colors );
527 aColorFunctor = new VTK::TPointColors2Color( prop, colors );
529 aColorFunctor = new VTK::TPropertyColor( prop );
531 if ( points->GetDataType() == VTK_FLOAT )
532 VTK::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr );
534 VTK::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr );
536 delete aColorFunctor;
539 if( this->ExtensionsInitialized == ES_Ok ) {
540 GLuint aBufferObjectID = 0;
541 vglGenBuffersARB( 1, &aBufferObjectID );
542 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
544 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
545 vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
547 delete [] aVertexArr;
549 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
550 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
552 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), (void*)0 );
553 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), (void*)(4*sizeof(GLfloat)) );
555 glEnableClientState( GL_VERTEX_ARRAY );
556 glEnableClientState( GL_COLOR_ARRAY );
558 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
560 glDisableClientState( GL_COLOR_ARRAY );
561 glDisableClientState( GL_VERTEX_ARRAY );
563 vglDeleteBuffersARB( 1, &aBufferObjectID );
564 } else { // there are no extensions
565 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), aVertexArr );
566 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex),
567 (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
569 glEnableClientState( GL_VERTEX_ARRAY );
570 glEnableClientState( GL_COLOR_ARRAY );
572 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
574 glDisableClientState( GL_COLOR_ARRAY );
575 glDisableClientState( GL_VERTEX_ARRAY );
577 delete [] aVertexArr;
582 this->UpdateProgress(1.0);