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 = GUI_OPENGL::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;
597 virtual ~TColorFunctorBase() {}
600 //-----------------------------------------------------------------------------
601 struct TPropertyColor : TColorFunctorBase
605 TPropertyColor( vtkProperty* theProperty ):
606 TColorFunctorBase( theProperty )
608 theProperty->GetColor( myColor );
613 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
615 theVertex.r = myColor[0];
616 theVertex.g = myColor[1];
617 theVertex.b = myColor[2];
618 theVertex.a = myAlpha;
622 //-----------------------------------------------------------------------------
623 struct TColors2Color : TColorFunctorBase
625 vtkUnsignedCharArray* myColors;
627 TColors2Color( vtkProperty* theProperty,
628 vtkUnsignedCharArray* theColors ):
629 TColorFunctorBase( theProperty ),
630 myColors( theColors )
635 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
637 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
638 unsigned char* aColor = myColors->GetPointer( aTupleId << 2 );
640 theVertex.r = int( aColor[0] ) / 255.0;
641 theVertex.g = int( aColor[1] ) / 255.0;
642 theVertex.b = int( aColor[2] ) / 255.0;
643 theVertex.a = myAlpha;
648 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
651 //-----------------------------------------------------------------------------
652 struct TPointColors2Color : TColors2Color
654 TPointColors2Color( vtkProperty* theProperty,
655 vtkUnsignedCharArray* theColors ):
656 TColors2Color( theProperty, theColors )
661 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
667 //-----------------------------------------------------------------------------
668 struct TCellColors2Color : TColors2Color
670 TCellColors2Color( vtkProperty* theProperty,
671 vtkUnsignedCharArray* theColors ):
672 TColors2Color( theProperty, theColors )
677 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
683 //-----------------------------------------------------------------------------
684 template < class TCoordinates >
685 void DrawPoints( TCoordinates* theStartPoints,
686 vtkCellArray* theCells,
687 TColorFunctorBase* theColorFunctor,
688 TVertex* theVertexArr,
689 vtkIdType &theCellId,
690 vtkIdType &theVertexId,
692 vtkDataArray* theDiamArray,
693 double theBallScale )
695 vtkIdType* ptIds = theCells->GetPointer();
696 vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
698 bool mapBalls = false;
699 if(theBallArr && theDiamArray) {
703 while ( ptIds < endPtIds ) {
704 vtkIdType nPts = *ptIds;
708 TVertex& aVertex = theVertexArr[ theVertexId ];
709 vtkIdType aPointId = *ptIds;
711 TCoordinates* anOffsetPoints = theStartPoints + 3 * aPointId;
712 aVertex.vx = anOffsetPoints[0];
713 aVertex.vy = anOffsetPoints[1];
714 aVertex.vz = anOffsetPoints[2];
716 theColorFunctor->get( aVertex, aPointId, theCellId );
724 theBallArr[theCellId] = (TBall)theDiamArray->GetTuple(theCellId)[0]*theBallScale;
731 //-----------------------------------------------------------------------------
732 template < class TCoordinates >
733 void DrawCellsPoints( vtkPolyData* theInput,
734 vtkPoints* thePoints,
735 TColorFunctorBase* theColorFunctor,
736 TVertex* theVertexArr,
738 double theBallScale )
740 vtkIdType aCellId = 0, aVertexId = 0;
742 TCoordinates* aStartPoints = (TCoordinates*)thePoints->GetVoidPointer(0);
743 vtkDataArray* aDiams = theInput->GetCellData() ? theInput->GetCellData()->GetScalars() : 0;
745 if ( vtkCellArray* aCellArray = theInput->GetVerts() ) {
746 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
749 if ( vtkCellArray* aCellArray = theInput->GetLines() )
750 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
752 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
753 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
755 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
756 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
761 //-----------------------------------------------------------------------------
762 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
765 if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled)
766 return MAPPER_SUPERCLASS::Draw( ren, act );
768 InternalDraw( ren, act );
773 //-----------------------------------------------------------------------------
774 void VTKViewer_PolyDataMapper::RenderPieceDraw( vtkRenderer* ren, vtkActor* act ) {
776 if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled) {
777 MAPPER_SUPERCLASS::RenderPieceDraw( ren, act );
780 InternalDraw( ren, act );
785 #include <vtkCamera.h>
786 #include <vtkOpenGLCamera.h>
787 #include <vtkOpenGLActor.h>
790 void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
791 vtkUnsignedCharArray* colors = NULL;
792 vtkPolyData* input = this->GetInput();
795 vtkProperty* prop = act->GetProperty();
797 points = input->GetPoints();
801 if(!this->BallEnabled) {
802 colors = this->Colors;
803 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
804 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
805 !input->GetPointData()->GetScalars() )
806 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA )
812 vtkIdType aTotalConnectivitySize = 0;
813 vtkIdType aNbCells = 0;
815 if ( vtkCellArray* aCellArray = input->GetVerts() ) {
816 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
817 aNbCells += aCellArray->GetNumberOfCells();
820 if ( vtkCellArray* aCellArray = input->GetLines() ) {
821 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
822 aNbCells += aCellArray->GetNumberOfCells();
825 if ( vtkCellArray* aCellArray = input->GetPolys() ) {
826 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
827 aNbCells += aCellArray->GetNumberOfCells();
830 if ( vtkCellArray* aCellArray = input->GetStrips() ) {
831 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
832 aNbCells += aCellArray->GetNumberOfCells();
835 if ( aTotalConnectivitySize > 0 ) {
836 VTK::TVertex* aVertexArr = new VTK::TVertex[ aTotalConnectivitySize ];
838 TBall* aBallArray = 0;
840 if(this->BallEnabled) {
841 aBallArray = new TBall[aNbCells];
844 int* aSize = this->ImageData->GetDimensions();
846 int aMode = 0; // to remove
848 VTK::TColorFunctorBase* aColorFunctor = NULL;
849 if( colors && aMode != 1 ) {
851 aColorFunctor = new VTK::TCellColors2Color( prop, colors );
853 aColorFunctor = new VTK::TPointColors2Color( prop, colors );
855 aColorFunctor = new VTK::TPropertyColor( prop );
857 if ( points->GetDataType() == VTK_FLOAT )
858 VTK::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
860 VTK::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
862 delete aColorFunctor;
865 if( this->ExtensionsInitialized == ES_Ok ) {
867 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
869 vtkOpenGLCamera *cam = (vtkOpenGLCamera *)(ren->GetActiveCamera());
874 cam->GetKeyMatrices(ren,wcvc,norms,vcdc,wcdc);
875 if (!act->GetIsIdentity())
878 vtkMatrix3x3 *anorms;
879 ((vtkOpenGLActor *)act)->GetKeyMatrices( mcwc, anorms );
880 vtkMatrix4x4::Multiply4x4( mcwc, wcdc, this->TempMatrix4 );
882 this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, this->TempMatrix4 );
886 this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, wcdc );
888 this->OpenGLHelper.SetUniformMatrix( this->myLocations.Projection, vcdc );
890 this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, std::max( aSize[0], aSize[1] ) );
892 GLuint aBufferObjectID, aDiamsID = 0;
894 this->OpenGLHelper.vglBindVertexArrayARB( this->VertexArrayObject );
895 this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
896 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
898 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
899 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
901 delete [] aVertexArr;
904 GLint colorAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Color" );
905 GLint vertexAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Vertex" );
906 GLint diamAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Diameter" );
908 GLsizei vertexSize = sizeof(VTK::TVertex);
910 this->OpenGLHelper.vglVertexAttribPointerARB( colorAttrib, 4, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)0 );
911 this->OpenGLHelper.vglEnableVertexAttribArrayARB( colorAttrib );
913 this->OpenGLHelper.vglVertexAttribPointerARB( vertexAttrib, 3, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)(sizeof(GLfloat) * 4) );
914 this->OpenGLHelper.vglEnableVertexAttribArrayARB( vertexAttrib );
916 if(this->BallEnabled) {
917 // Don't use uniform variable.
918 this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, -1 );
919 this->OpenGLHelper.vglGenBuffersARB( 1, &aDiamsID);
920 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
922 int aDiamsSize = sizeof(TBall)*aNbCells;
923 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
925 delete [] aBallArray;
927 this->OpenGLHelper.vglVertexAttribPointerARB( diamAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0 );
928 this->OpenGLHelper.vglEnableVertexAttribArrayARB( diamAttrib );
931 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
933 if( this->BallEnabled ) {
934 this->OpenGLHelper.vglDisableVertexAttribArrayARB( diamAttrib );
935 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
936 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aDiamsID );
939 this->OpenGLHelper.vglDisableVertexAttribArrayARB( colorAttrib );
940 this->OpenGLHelper.vglDisableVertexAttribArrayARB( vertexAttrib );
941 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
942 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
943 this->OpenGLHelper.vglBindVertexArrayARB( 0 );
945 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
947 GLuint aBufferObjectID, aDiamsID = 0;
948 GLint attribute_diams = -1;
949 glPointSize( std::max( aSize[0], aSize[1] ) );
950 this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
951 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
953 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
954 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
956 delete [] aVertexArr;
958 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
959 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
961 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), (void*)0 );
962 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), (void*)(4*sizeof(GLfloat)) );
964 glEnableClientState( GL_VERTEX_ARRAY );
965 glEnableClientState( GL_COLOR_ARRAY );
967 if(this->BallEnabled) {
968 this->OpenGLHelper.vglGenBuffersARB( 1, &aDiamsID);
969 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
971 int aDiamsSize = sizeof(TBall)*aNbCells;
972 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
974 delete [] aBallArray;
975 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
976 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID );
978 attribute_diams = this->OpenGLHelper.vglGetAttribLocationARB(this->PointProgram, "diameter");
979 this->OpenGLHelper.vglEnableVertexAttribArrayARB(attribute_diams);
980 this->OpenGLHelper.vglBindBufferARB(GL_ARRAY_BUFFER_ARB, aDiamsID);
981 this->OpenGLHelper.vglVertexAttribPointerARB(
982 attribute_diams, // attribute
983 1, // number of elements per vertex, here (diameter)
984 GL_FLOAT, // the type of each element
985 GL_FALSE, // take our values as-is
986 0, // no extra data between each position
987 0 // offset of first element
991 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
993 glDisableClientState( GL_COLOR_ARRAY );
994 glDisableClientState( GL_VERTEX_ARRAY );
995 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
997 if(this->BallEnabled) {
998 this->OpenGLHelper.vglDisableVertexAttribArrayARB(attribute_diams);
999 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aDiamsID );
1003 } else { // there are no extensions
1004 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), aVertexArr );
1005 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex),
1006 (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
1008 glEnableClientState( GL_VERTEX_ARRAY );
1009 glEnableClientState( GL_COLOR_ARRAY );
1011 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1013 glDisableClientState( GL_COLOR_ARRAY );
1014 glDisableClientState( GL_VERTEX_ARRAY );
1016 delete [] aVertexArr;
1021 this->UpdateProgress(1.0);