1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 #define GL_GLEXT_PROTOTYPES
23 #include "VTKViewer_PolyDataMapper.h"
24 #include "VTKViewer_MarkerUtils.h"
26 #include <utilities.h>
30 #include <vtkCellArray.h>
31 #include <vtkXMLImageDataReader.h>
32 #include <vtkImageData.h>
33 #include <vtkObjectFactory.h>
34 #include <vtkPointData.h>
35 #include <vtkPolyData.h>
36 #include <vtkProperty.h>
37 #include <vtkRenderer.h>
38 #include <vtkSmartPointer.h>
39 #include <vtkTimerLog.h>
40 #include <vtkWindow.h>
41 #include <vtkRenderWindow.h>
42 #include <vtkCommand.h>
43 #include <vtkCellData.h>
46 # ifndef GLX_GLXEXT_LEGACY
47 # define GLX_GLXEXT_LEGACY
55 #ifndef VTK_IMPLEMENT_MESA_CXX
56 vtkStandardNewMacro(VTKViewer_PolyDataMapper);
59 // some definitions for what the polydata has in it
60 #define VTK_PDPSM_COLORS 0x0001
61 #define VTK_PDPSM_CELL_COLORS 0x0002
62 #define VTK_PDPSM_POINT_TYPE_FLOAT 0x0004
63 #define VTK_PDPSM_POINT_TYPE_DOUBLE 0x0008
64 #define VTK_PDPSM_NORMAL_TYPE_FLOAT 0x0010
65 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
66 #define VTK_PDPSM_OPAQUE_COLORS 0x0040
68 typedef GLfloat TBall;
76 // ----------------------------------------------- Special Textures -----------------------------------
77 // texture id for balls drawing
78 #define BallTextureId 0
81 //-----------------------------------------------------------------------------
82 VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
84 Q_INIT_RESOURCE( VTKViewer );
86 this->ExtensionsInitialized = ES_None;
88 this->PointSpriteTexture = 0;
90 this->MarkerEnabled = false;
91 this->MarkerType = VTK::MT_NONE;
92 this->MarkerScale = VTK::MS_NONE;
94 this->BallEnabled = false;
95 this->BallScale = 1.0;
96 this->PointProgram = 0;
98 this->VertexShader = 0;
99 this->FragmentShader = 0;
102 this->OpenGLHelper.Init();
105 //-----------------------------------------------------------------------------
106 VTKViewer_PolyDataMapper::~VTKViewer_PolyDataMapper()
108 if( PointSpriteTexture > 0 )
109 glDeleteTextures( 1, &PointSpriteTexture );
112 this->OpenGLHelper.DestroyShaderProgram( this->PointProgram, this->VertexShader, this->FragmentShader);
116 //-----------------------------------------------------------------------------
117 int VTKViewer_PolyDataMapper::InitShader()
120 std::string filePath = std::string( getenv( "GUI_ROOT_DIR") ) + "/share/salome/resources/gui/Point";
121 if( !this->OpenGLHelper.CreateShaderProgram(filePath, this->PointProgram, this->VertexShader, this->FragmentShader) )
124 // Get uniform locations.
125 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
127 this->myLocations.ModelViewProjection = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uModelViewProjectionMatrix" );
128 this->myLocations.Projection = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uProjectionMatrix" );
129 this->myLocations.GeneralPointSize = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uGeneralPointSize" );
130 this->myLocations.PointSprite = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uPointSprite" );
132 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
134 this->OpenGLHelper.vglGenVertexArraysARB(1, &this->VertexArrayObject);
136 std::string fileName = std::string( getenv( "GUI_ROOT_DIR") ) +
137 "/share/salome/resources/gui/Vertex_Program_ARB.txt";
139 char* shader = readFromFile( fileName );
141 GLhandleARB VertexShader = this->OpenGLHelper.vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
142 this->OpenGLHelper.vglShaderSourceARB( VertexShader, 1, (const GLcharARB**)&shader, NULL );
143 this->OpenGLHelper.vglCompileShaderARB( VertexShader );
145 this->PointProgram = this->OpenGLHelper.vglCreateProgramObjectARB();
146 this->OpenGLHelper.vglAttachObjectARB( this->PointProgram, VertexShader );
148 this->OpenGLHelper.vglLinkProgramARB( this->PointProgram );
156 //-----------------------------------------------------------------------------
157 void VTKViewer_PolyDataMapper::SetMarkerEnabled( bool theMarkerEnabled )
159 if( this->MarkerEnabled == theMarkerEnabled )
162 this->MarkerEnabled = theMarkerEnabled;
166 //-----------------------------------------------------------------------------
167 // Definition of structures and fuctions used in SetBallEnabled() method
170 //----------------------------------------------------------------------------
171 vtkSmartPointer<vtkImageData> MakeTexture( const char* theMainTexture, const char* theAlphaTexture ) {
172 if( !theMainTexture || !theAlphaTexture )
175 vtkXMLImageDataReader* aMainReader = vtkXMLImageDataReader::New();
176 vtkXMLImageDataReader* anAlphaReader = vtkXMLImageDataReader::New();
178 aMainReader->SetFileName( theMainTexture );
179 anAlphaReader->SetFileName( theAlphaTexture );
181 aMainReader->Update();
182 anAlphaReader->Update();
184 vtkImageData* aMainImageData = aMainReader->GetOutput();
185 vtkImageData* anAlphaImageData = anAlphaReader->GetOutput();
187 int* aMainImageSize = aMainImageData->GetDimensions();
188 int* anAlphaImageSize = anAlphaImageData->GetDimensions();
189 if(aMainImageSize[0] != anAlphaImageSize[0] || aMainImageSize[1] != anAlphaImageSize[1])
192 vtkSmartPointer<vtkImageData> aCompositeImageData = vtkImageData::New();
193 aCompositeImageData->Delete();
195 int aNbCompositeComponents = 4;
196 aCompositeImageData->SetDimensions(aMainImageSize);
197 aCompositeImageData->AllocateScalars( VTK_UNSIGNED_CHAR, aNbCompositeComponents );
199 unsigned char* aMainDataPtr = (unsigned char*)aMainImageData->GetScalarPointer();
200 unsigned char* anAlphaDataPtr = (unsigned char*)anAlphaImageData->GetScalarPointer();
201 unsigned char *aCompositeDataPtr = (unsigned char * )aCompositeImageData->GetScalarPointer();
203 int aNbMainComponents = aMainImageData->GetNumberOfScalarComponents();
204 int aNbAlphaComponents = anAlphaImageData->GetNumberOfScalarComponents();
205 int aCompositeSize = aMainImageSize[0] * aMainImageSize[1] * aNbCompositeComponents;
207 int aMainId = 0, anAlphaId = 0, aCompositeId = 0;
208 for(; aCompositeId < aCompositeSize;) {
209 aCompositeDataPtr[aCompositeId] = aMainDataPtr[aMainId];
210 aCompositeDataPtr[aCompositeId + 1] = aMainDataPtr[aMainId + 1];
211 aCompositeDataPtr[aCompositeId + 2] = aMainDataPtr[aMainId + 2];
212 aCompositeDataPtr[aCompositeId + 3] = anAlphaDataPtr[anAlphaId];
214 aMainId += aNbMainComponents;
215 anAlphaId += aNbAlphaComponents;
216 aCompositeId += aNbCompositeComponents;
218 aMainReader->Delete();
219 anAlphaReader->Delete();
220 return aCompositeImageData;
224 //-----------------------------------------------------------------------------
225 bool VTKViewer_PolyDataMapper::GetBallEnabled()
227 return this->BallEnabled;
229 //-----------------------------------------------------------------------------
230 void VTKViewer_PolyDataMapper::SetBallEnabled( bool theBallEnabled )
232 if( this->BallEnabled == theBallEnabled )
235 this->BallEnabled = theBallEnabled;
237 if(!this->BallEnabled) {
238 this->ImageData = NULL;
241 if(this->BallEnabled) {
242 if(this->SpecialTextures.find(BallTextureId) == SpecialTextures.end()){
243 QString aMainTexture = getenv( "GUI_ROOT_DIR" );
244 aMainTexture.append("/share/salome/resources/gui/sprite_texture.vti");
246 QString anAlphaTexture = getenv( "GUI_ROOT_DIR" );
247 anAlphaTexture.append( "/share/salome/resources/gui/sprite_alpha.vti" );
248 vtkSmartPointer<vtkImageData> aTextureValue = VTK::MakeTexture( aMainTexture.toLatin1().constData(), anAlphaTexture.toLatin1().constData() );
249 this->SpecialTextures[BallTextureId] = aTextureValue;
251 this->ImageData = this->SpecialTextures[BallTextureId];
256 //-----------------------------------------------------------------------------
257 double VTKViewer_PolyDataMapper::GetBallScale()
259 return this->BallScale;
261 //-----------------------------------------------------------------------------
262 void VTKViewer_PolyDataMapper::SetBallScale( double theBallScale )
264 if( this->BallScale == theBallScale )
266 this->BallScale = theBallScale;
269 //-----------------------------------------------------------------------------
270 void VTKViewer_PolyDataMapper::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
272 if( this->MarkerType == theMarkerType && this->MarkerScale == theMarkerScale )
275 this->MarkerType = theMarkerType;
276 this->MarkerScale = theMarkerScale;
278 if( this->MarkerType == VTK::MT_NONE || this->MarkerType == VTK::MT_USER ) {
279 this->ImageData = NULL;
284 int aMarkerType = (int)this->MarkerType;
285 int aMarkerScale = (int)this->MarkerScale;
287 int anId = (int)VTK::MS_70 * aMarkerType + aMarkerScale;
289 if( this->StandardTextures.find( anId ) == this->StandardTextures.end() )
291 QString aFileName = QString( ":/textures/texture%1.dat" ).arg( aMarkerType );
292 VTK::MarkerTexture aMarkerTexture;
293 if( VTK::LoadTextureData( aFileName, theMarkerScale, aMarkerTexture ) )
294 this->StandardTextures[ anId ] = VTK::MakeVTKImage( aMarkerTexture );
297 this->ImageData = this->StandardTextures[ anId ];
301 //-----------------------------------------------------------------------------
302 void VTKViewer_PolyDataMapper::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
304 if( this->MarkerType == VTK::MT_USER && this->MarkerId == theMarkerId )
307 this->MarkerType = VTK::MT_USER;
308 this->MarkerId = theMarkerId;
310 if( this->CustomTextures.find( theMarkerId ) == this->CustomTextures.end() )
311 this->CustomTextures[ theMarkerId ] = VTK::MakeVTKImage( theMarkerTexture );
313 this->ImageData = this->CustomTextures[ theMarkerId ];
317 //-----------------------------------------------------------------------------
318 VTK::MarkerType VTKViewer_PolyDataMapper::GetMarkerType()
320 return this->MarkerType;
323 //-----------------------------------------------------------------------------
324 VTK::MarkerScale VTKViewer_PolyDataMapper::GetMarkerScale()
326 return this->MarkerScale;
329 //-----------------------------------------------------------------------------
330 int VTKViewer_PolyDataMapper::GetMarkerTexture()
332 return this->MarkerId;
335 //-----------------------------------------------------------------------------
336 int VTKViewer_PolyDataMapper::InitExtensions()
340 std::ostringstream strm;
341 glGetIntegerv(GL_NUM_EXTENSIONS, &n);
342 for (int i = 0; i < n; i++)
344 const char *exti = (const char *)glGetStringi(GL_EXTENSIONS, i);
347 std::string s = strm.str();
348 const char* ext = s.c_str();
350 const char* ext = (const char*)glGetString( GL_EXTENSIONS );
352 if( !this->OpenGLHelper.IsInitialized() || !ext ||
353 strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
354 strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL ||
355 strstr( ext, "GL_ARB_shader_objects") == NULL )
357 MESSAGE("Initializing ARB extensions failed");
361 return this->InitShader();
364 //-----------------------------------------------------------------------------
365 void VTKViewer_PolyDataMapper::InitPointSprites()
367 glEnable( GL_POINT_SPRITE_ARB );
368 glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
370 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT );
372 glDepthFunc( GL_LEQUAL );
373 glEnable( GL_DEPTH_TEST );
375 glEnable( GL_ALPHA_TEST );
376 if(!this->BallEnabled) {
377 glAlphaFunc( GL_GREATER, 0.0 );
380 glAlphaFunc( GL_GREATER, 0.5 );
383 glDisable( GL_LIGHTING );
385 glDisable( GL_COLOR_MATERIAL );
388 //-----------------------------------------------------------------------------
389 void VTKViewer_PolyDataMapper::CleanupPointSprites()
393 glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
394 glDisable( GL_POINT_SPRITE_ARB );
397 //-----------------------------------------------------------------------------
398 void VTKViewer_PolyDataMapper::InitTextures()
400 if( !this->ImageData.GetPointer() )
403 glEnable( GL_TEXTURE_2D );
404 if( this->PointSpriteTexture == 0 ) {
405 glGenTextures( 1, &this->PointSpriteTexture );
408 glActiveTexture( GL_TEXTURE0 );
410 glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
411 glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
412 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
413 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
415 if(this->BallEnabled) {
416 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
417 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
419 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
420 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
423 int* aSize = this->ImageData->GetDimensions();
424 unsigned char* dataPtr = (unsigned char*)this->ImageData->GetScalarPointer();
425 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
426 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
430 this->OpenGLHelper.vglUniform1iARB( this->myLocations.PointSprite, GL_TEXTURE0 );
434 //-----------------------------------------------------------------------------
435 void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
437 bool isUsePointSprites = (this->MarkerEnabled && this->MarkerType != VTK::MT_NONE) ||
439 if( isUsePointSprites )
441 if( this->ExtensionsInitialized != ES_Ok )
442 this->ExtensionsInitialized = this->InitExtensions();
443 this->InitPointSprites();
444 this->InitTextures();
447 if(!this->BallEnabled || this->ExtensionsInitialized != ES_Ok) {
448 MAPPER_SUPERCLASS::RenderPiece( ren, act );
449 if( isUsePointSprites )
450 this->CleanupPointSprites();
451 glBindTexture( GL_TEXTURE_2D, 0 );
454 vtkPolyData *input= this->GetInput();
457 // make sure that we've been properly initialized
459 if (ren->GetRenderWindow()->CheckAbortStatus())
464 vtkErrorMacro(<< "No input!");
469 this->InvokeEvent(vtkCommand::StartEvent,NULL);
471 this->InvokeEvent(vtkCommand::EndEvent,NULL);
472 numPts = input->GetNumberOfPoints();
477 vtkDebugMacro(<< "No points!");
481 // make sure our window is current
482 ren->GetRenderWindow()->MakeCurrent();
485 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
489 // if something has changed regenerate colors and display lists
493 if ( this->GetMTime() > this->BuildTime ||
494 input->GetMTime() > this->BuildTime ||
495 act->GetProperty()->GetMTime() > this->BuildTime ||
496 ren->GetRenderWindow() != this->LastWindow)
498 // sets this->Colors as side effect
499 this->MapScalars( act->GetProperty()->GetOpacity() );
501 if (!this->ImmediateModeRendering &&
502 !this->GetGlobalImmediateModeRendering())
504 this->ReleaseGraphicsResources(ren->GetRenderWindow());
505 this->LastWindow = ren->GetRenderWindow();
507 // get a unique display list id
508 this->ListId = glGenLists(1);
509 glNewList(this->ListId,GL_COMPILE);
511 noAbort = this->Draw(ren,act);
514 // Time the actual drawing
515 this->Timer->StartTimer();
516 glCallList(this->ListId);
517 this->Timer->StopTimer();
521 this->ReleaseGraphicsResources(ren->GetRenderWindow());
522 this->LastWindow = ren->GetRenderWindow();
525 this->BuildTime.Modified();
527 // if nothing changed but we are using display lists, draw it
530 if (!this->ImmediateModeRendering &&
531 !this->GetGlobalImmediateModeRendering())
533 // Time the actual drawing
534 this->Timer->StartTimer();
535 glCallList(this->ListId);
536 this->Timer->StopTimer();
540 // if we are in immediate mode rendering we always
541 // want to draw the primitives here
542 if (this->ImmediateModeRendering ||
543 this->GetGlobalImmediateModeRendering())
545 // sets this->Colors as side effect
546 this->MapScalars( act->GetProperty()->GetOpacity() );
548 // Time the actual drawing
549 this->Timer->StartTimer();
551 this->Timer->StopTimer();
554 this->TimeToDraw = (float)this->Timer->GetElapsedTime();
556 // If the timer is not accurate enough, set it to a small
557 // time so that it is not zero
558 if ( this->TimeToDraw == 0.0 )
559 this->TimeToDraw = 0.0001;
561 //this->RenderPieceStart(ren, act);
562 this->RenderPieceDraw(ren, act);
563 // this->RenderEdges(ren,act);
564 //this->RenderPieceFinish(ren, act);
566 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
567 this->CleanupPointSprites();
568 glBindTexture( GL_TEXTURE_2D, 0 );
572 //-----------------------------------------------------------------------------
573 // Definition of structures and fuctions used in Draw() method
576 //-----------------------------------------------------------------------------
583 //-----------------------------------------------------------------------------
584 struct TColorFunctorBase
588 TColorFunctorBase( vtkProperty* theProperty )
590 myAlpha = theProperty->GetOpacity();
595 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
598 //-----------------------------------------------------------------------------
599 struct TPropertyColor : TColorFunctorBase
603 TPropertyColor( vtkProperty* theProperty ):
604 TColorFunctorBase( theProperty )
606 theProperty->GetColor( myColor );
611 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
613 theVertex.r = myColor[0];
614 theVertex.g = myColor[1];
615 theVertex.b = myColor[2];
616 theVertex.a = myAlpha;
620 //-----------------------------------------------------------------------------
621 struct TColors2Color : TColorFunctorBase
623 vtkUnsignedCharArray* myColors;
625 TColors2Color( vtkProperty* theProperty,
626 vtkUnsignedCharArray* theColors ):
627 TColorFunctorBase( theProperty ),
628 myColors( theColors )
633 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
635 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
636 unsigned char* aColor = myColors->GetPointer( aTupleId << 2 );
638 theVertex.r = int( aColor[0] ) / 255.0;
639 theVertex.g = int( aColor[1] ) / 255.0;
640 theVertex.b = int( aColor[2] ) / 255.0;
641 theVertex.a = myAlpha;
646 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
649 //-----------------------------------------------------------------------------
650 struct TPointColors2Color : TColors2Color
652 TPointColors2Color( vtkProperty* theProperty,
653 vtkUnsignedCharArray* theColors ):
654 TColors2Color( theProperty, theColors )
659 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
665 //-----------------------------------------------------------------------------
666 struct TCellColors2Color : TColors2Color
668 TCellColors2Color( vtkProperty* theProperty,
669 vtkUnsignedCharArray* theColors ):
670 TColors2Color( theProperty, theColors )
675 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
681 //-----------------------------------------------------------------------------
682 template < class TCoordinates >
683 void DrawPoints( TCoordinates* theStartPoints,
684 vtkCellArray* theCells,
685 TColorFunctorBase* theColorFunctor,
686 TVertex* theVertexArr,
687 vtkIdType &theCellId,
688 vtkIdType &theVertexId,
690 vtkDataArray* theDiamArray,
691 double theBallScale )
693 vtkIdType* ptIds = theCells->GetPointer();
694 vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
696 bool mapBalls = false;
697 if(theBallArr && theDiamArray) {
701 while ( ptIds < endPtIds ) {
702 vtkIdType nPts = *ptIds;
706 TVertex& aVertex = theVertexArr[ theVertexId ];
707 vtkIdType aPointId = *ptIds;
709 TCoordinates* anOffsetPoints = theStartPoints + 3 * aPointId;
710 aVertex.vx = anOffsetPoints[0];
711 aVertex.vy = anOffsetPoints[1];
712 aVertex.vz = anOffsetPoints[2];
714 theColorFunctor->get( aVertex, aPointId, theCellId );
722 theBallArr[theCellId] = (TBall)theDiamArray->GetTuple(theCellId)[0]*theBallScale;
729 //-----------------------------------------------------------------------------
730 template < class TCoordinates >
731 void DrawCellsPoints( vtkPolyData* theInput,
732 vtkPoints* thePoints,
733 TColorFunctorBase* theColorFunctor,
734 TVertex* theVertexArr,
736 double theBallScale )
738 vtkIdType aCellId = 0, aVertexId = 0;
740 TCoordinates* aStartPoints = (TCoordinates*)thePoints->GetVoidPointer(0);
741 vtkDataArray* aDiams = theInput->GetCellData() ? theInput->GetCellData()->GetScalars() : 0;
743 if ( vtkCellArray* aCellArray = theInput->GetVerts() ) {
744 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
747 if ( vtkCellArray* aCellArray = theInput->GetLines() )
748 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
750 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
751 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
753 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
754 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
759 //-----------------------------------------------------------------------------
760 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
763 if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled)
764 return MAPPER_SUPERCLASS::Draw( ren, act );
766 InternalDraw( ren, act );
771 //-----------------------------------------------------------------------------
772 void VTKViewer_PolyDataMapper::RenderPieceDraw( vtkRenderer* ren, vtkActor* act ) {
774 if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled) {
775 MAPPER_SUPERCLASS::RenderPieceDraw( ren, act );
778 InternalDraw( ren, act );
783 #include <vtkCamera.h>
784 #include <vtkOpenGLCamera.h>
785 #include <vtkOpenGLActor.h>
788 void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
789 vtkUnsignedCharArray* colors = NULL;
790 vtkPolyData* input = this->GetInput();
793 vtkProperty* prop = act->GetProperty();
795 points = input->GetPoints();
799 if(!this->BallEnabled) {
800 colors = this->Colors;
801 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
802 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
803 !input->GetPointData()->GetScalars() )
804 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA )
810 vtkIdType aTotalConnectivitySize = 0;
811 vtkIdType aNbCells = 0;
813 if ( vtkCellArray* aCellArray = input->GetVerts() ) {
814 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
815 aNbCells += aCellArray->GetNumberOfCells();
818 if ( vtkCellArray* aCellArray = input->GetLines() ) {
819 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
820 aNbCells += aCellArray->GetNumberOfCells();
823 if ( vtkCellArray* aCellArray = input->GetPolys() ) {
824 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
825 aNbCells += aCellArray->GetNumberOfCells();
828 if ( vtkCellArray* aCellArray = input->GetStrips() ) {
829 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
830 aNbCells += aCellArray->GetNumberOfCells();
833 if ( aTotalConnectivitySize > 0 ) {
834 VTK::TVertex* aVertexArr = new VTK::TVertex[ aTotalConnectivitySize ];
836 TBall* aBallArray = 0;
838 if(this->BallEnabled) {
839 aBallArray = new TBall[aNbCells];
842 int* aSize = this->ImageData->GetDimensions();
843 //glPointSize( std::max( aSize[0], aSize[1] ) );
845 int aMode = 0; // to remove
847 VTK::TColorFunctorBase* aColorFunctor = NULL;
848 if( colors && aMode != 1 ) {
850 aColorFunctor = new VTK::TCellColors2Color( prop, colors );
852 aColorFunctor = new VTK::TPointColors2Color( prop, colors );
854 aColorFunctor = new VTK::TPropertyColor( prop );
856 if ( points->GetDataType() == VTK_FLOAT )
857 VTK::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
859 VTK::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
861 delete aColorFunctor;
864 if( this->ExtensionsInitialized == ES_Ok ) {
866 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
868 vtkOpenGLCamera *cam = (vtkOpenGLCamera *)(ren->GetActiveCamera());
873 cam->GetKeyMatrices(ren,wcvc,norms,vcdc,wcdc);
874 if (!act->GetIsIdentity())
877 vtkMatrix3x3 *anorms;
878 ((vtkOpenGLActor *)act)->GetKeyMatrices( mcwc, anorms );
879 vtkMatrix4x4::Multiply4x4( mcwc, wcdc, this->TempMatrix4 );
881 this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, this->TempMatrix4 );
885 this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, wcdc );
887 this->OpenGLHelper.SetUniformMatrix( this->myLocations.Projection, vcdc );
889 this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, std::max( aSize[0], aSize[1] ) );
891 GLuint aBufferObjectID, aDiamsID = 0;
893 this->OpenGLHelper.vglBindVertexArrayARB( this->VertexArrayObject );
894 this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
895 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
897 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
898 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
900 delete [] aVertexArr;
903 GLint colorAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Color" );
904 GLint vertexAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Vertex" );
905 GLint diamAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Diameter" );
907 GLsizei vertexSize = sizeof(VTK::TVertex);
909 this->OpenGLHelper.vglVertexAttribPointerARB( colorAttrib, 4, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)0 );
910 this->OpenGLHelper.vglEnableVertexAttribArrayARB( colorAttrib );
912 this->OpenGLHelper.vglVertexAttribPointerARB( vertexAttrib, 3, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)(sizeof(GLfloat) * 4) );
913 this->OpenGLHelper.vglEnableVertexAttribArrayARB( vertexAttrib );
915 if(this->BallEnabled) {
916 // Don't use uniform variable.
917 this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, -1 );
918 this->OpenGLHelper.vglGenBuffersARB( 1, &aDiamsID);
919 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
921 int aDiamsSize = sizeof(TBall)*aNbCells;
922 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
924 delete [] aBallArray;
926 this->OpenGLHelper.vglVertexAttribPointerARB( diamAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0 );
927 this->OpenGLHelper.vglEnableVertexAttribArrayARB( diamAttrib );
930 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
932 if( this->BallEnabled ) {
933 this->OpenGLHelper.vglDisableVertexAttribArrayARB( diamAttrib );
934 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
935 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aDiamsID );
938 this->OpenGLHelper.vglDisableVertexAttribArrayARB( colorAttrib );
939 this->OpenGLHelper.vglDisableVertexAttribArrayARB( vertexAttrib );
940 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
941 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
942 this->OpenGLHelper.vglBindVertexArrayARB( 0 );
944 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
946 GLuint aBufferObjectID, aDiamsID = 0;
947 GLint attribute_diams = -1;
949 this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
950 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
952 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
953 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
955 delete [] aVertexArr;
957 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
958 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
960 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), (void*)0 );
961 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), (void*)(4*sizeof(GLfloat)) );
963 glEnableClientState( GL_VERTEX_ARRAY );
964 glEnableClientState( GL_COLOR_ARRAY );
966 if(this->BallEnabled) {
967 this->OpenGLHelper.vglGenBuffersARB( 1, &aDiamsID);
968 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
970 int aDiamsSize = sizeof(TBall)*aNbCells;
971 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
973 delete [] aBallArray;
974 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
975 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID );
977 attribute_diams = vglGetAttribLocationARB(this->PointProgram, "diameter");
978 this->OpenGLHelper.vglEnableVertexAttribArrayARB(attribute_diams);
979 this->OpenGLHelper.vglBindBufferARB(GL_ARRAY_BUFFER_ARB, aDiamsID);
980 this->OpenGLHelper.vglVertexAttribPointerARB(
981 attribute_diams, // attribute
982 1, // number of elements per vertex, here (diameter)
983 GL_FLOAT, // the type of each element
984 GL_FALSE, // take our values as-is
985 0, // no extra data between each position
986 0 // offset of first element
990 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
992 glDisableClientState( GL_COLOR_ARRAY );
993 glDisableClientState( GL_VERTEX_ARRAY );
994 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
996 if(this->BallEnabled) {
997 this->OpenGLHelper.vglDisableVertexAttribArrayARB(attribute_diams);
998 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aDiamsID );
1002 } else { // there are no extensions
1003 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), aVertexArr );
1004 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex),
1005 (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
1007 glEnableClientState( GL_VERTEX_ARRAY );
1008 glEnableClientState( GL_COLOR_ARRAY );
1010 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1012 glDisableClientState( GL_COLOR_ARRAY );
1013 glDisableClientState( GL_VERTEX_ARRAY );
1015 delete [] aVertexArr;
1020 this->UpdateProgress(1.0);