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
50 #import <mach-o/dyld.h>
53 void * glXGetProcAddressARB (const GLubyte *name)
58 symbolName = (char *)malloc (strlen ((const char *)name) + 2); // 1
59 strcpy(symbolName + 1, (const char *)name); // 2
60 symbolName[0] = '_'; // 3
62 if (NSIsSymbolNameDefined (symbolName)) // 4
63 symbol = NSLookupAndBindSymbol (symbolName);
64 free (symbolName); // 5
65 return symbol ? NSAddressOfSymbol (symbol) : NULL; // 6
75 #ifndef VTK_IMPLEMENT_MESA_CXX
76 vtkStandardNewMacro(VTKViewer_PolyDataMapper);
79 // some definitions for what the polydata has in it
80 #define VTK_PDPSM_COLORS 0x0001
81 #define VTK_PDPSM_CELL_COLORS 0x0002
82 #define VTK_PDPSM_POINT_TYPE_FLOAT 0x0004
83 #define VTK_PDPSM_POINT_TYPE_DOUBLE 0x0008
84 #define VTK_PDPSM_NORMAL_TYPE_FLOAT 0x0010
85 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
86 #define VTK_PDPSM_OPAQUE_COLORS 0x0040
88 typedef GLfloat TBall;
96 // ----------------------------------------------- Special Textures -----------------------------------
97 // texture id for balls drawing
98 #define BallTextureId 0
101 //-----------------------------------------------------------------------------
102 VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
104 Q_INIT_RESOURCE( VTKViewer );
106 this->ExtensionsInitialized = ES_None;
108 this->PointSpriteTexture = 0;
110 this->MarkerEnabled = false;
111 this->MarkerType = VTK::MT_NONE;
112 this->MarkerScale = VTK::MS_NONE;
114 this->BallEnabled = false;
115 this->BallScale = 1.0;
116 this->PointProgram = 0;
118 this->VertexShader = 0;
119 this->FragmentShader = 0;
122 this->OpenGLHelper.Init();
125 //-----------------------------------------------------------------------------
126 VTKViewer_PolyDataMapper::~VTKViewer_PolyDataMapper()
128 if( PointSpriteTexture > 0 )
129 glDeleteTextures( 1, &PointSpriteTexture );
132 this->OpenGLHelper.DestroyShaderProgram( this->PointProgram, this->VertexShader, this->FragmentShader);
136 //-----------------------------------------------------------------------------
137 int VTKViewer_PolyDataMapper::InitShader()
140 std::string filePath = std::string( getenv( "GUI_ROOT_DIR") ) + "/share/salome/resources/gui/Point";
141 if( !this->OpenGLHelper.CreateShaderProgram(filePath, this->PointProgram, this->VertexShader, this->FragmentShader) )
144 // Get uniform locations.
145 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
147 this->myLocations.ModelViewProjection = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uModelViewProjectionMatrix" );
148 this->myLocations.Projection = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uProjectionMatrix" );
149 this->myLocations.GeneralPointSize = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uGeneralPointSize" );
150 this->myLocations.PointSprite = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uPointSprite" );
152 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
154 this->OpenGLHelper.vglGenVertexArraysARB(1, &this->VertexArrayObject);
156 std::string fileName = std::string( getenv( "GUI_ROOT_DIR") ) +
157 "/share/salome/resources/gui/Vertex_Program_ARB.txt";
159 char* shader = GUI_OPENGL::readFromFile( fileName );
161 GLhandleARB VertexShader = this->OpenGLHelper.vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
162 this->OpenGLHelper.vglShaderSourceARB( VertexShader, 1, (const GLcharARB**)&shader, NULL );
163 this->OpenGLHelper.vglCompileShaderARB( VertexShader );
165 this->PointProgram = this->OpenGLHelper.vglCreateProgramObjectARB();
166 this->OpenGLHelper.vglAttachObjectARB( this->PointProgram, VertexShader );
168 this->OpenGLHelper.vglLinkProgramARB( this->PointProgram );
176 //-----------------------------------------------------------------------------
177 void VTKViewer_PolyDataMapper::SetMarkerEnabled( bool theMarkerEnabled )
179 if( this->MarkerEnabled == theMarkerEnabled )
182 this->MarkerEnabled = theMarkerEnabled;
186 //-----------------------------------------------------------------------------
187 // Definition of structures and fuctions used in SetBallEnabled() method
190 //----------------------------------------------------------------------------
191 vtkSmartPointer<vtkImageData> MakeTexture( const char* theMainTexture, const char* theAlphaTexture ) {
192 if( !theMainTexture || !theAlphaTexture )
195 vtkXMLImageDataReader* aMainReader = vtkXMLImageDataReader::New();
196 vtkXMLImageDataReader* anAlphaReader = vtkXMLImageDataReader::New();
198 aMainReader->SetFileName( theMainTexture );
199 anAlphaReader->SetFileName( theAlphaTexture );
201 aMainReader->Update();
202 anAlphaReader->Update();
204 vtkImageData* aMainImageData = aMainReader->GetOutput();
205 vtkImageData* anAlphaImageData = anAlphaReader->GetOutput();
207 int* aMainImageSize = aMainImageData->GetDimensions();
208 int* anAlphaImageSize = anAlphaImageData->GetDimensions();
209 if(aMainImageSize[0] != anAlphaImageSize[0] || aMainImageSize[1] != anAlphaImageSize[1])
212 vtkSmartPointer<vtkImageData> aCompositeImageData = vtkImageData::New();
213 aCompositeImageData->Delete();
215 int aNbCompositeComponents = 4;
216 aCompositeImageData->SetDimensions(aMainImageSize);
217 aCompositeImageData->AllocateScalars( VTK_UNSIGNED_CHAR, aNbCompositeComponents );
219 unsigned char* aMainDataPtr = (unsigned char*)aMainImageData->GetScalarPointer();
220 unsigned char* anAlphaDataPtr = (unsigned char*)anAlphaImageData->GetScalarPointer();
221 unsigned char *aCompositeDataPtr = (unsigned char * )aCompositeImageData->GetScalarPointer();
223 int aNbMainComponents = aMainImageData->GetNumberOfScalarComponents();
224 int aNbAlphaComponents = anAlphaImageData->GetNumberOfScalarComponents();
225 int aCompositeSize = aMainImageSize[0] * aMainImageSize[1] * aNbCompositeComponents;
227 int aMainId = 0, anAlphaId = 0, aCompositeId = 0;
228 for(; aCompositeId < aCompositeSize;) {
229 aCompositeDataPtr[aCompositeId] = aMainDataPtr[aMainId];
230 aCompositeDataPtr[aCompositeId + 1] = aMainDataPtr[aMainId + 1];
231 aCompositeDataPtr[aCompositeId + 2] = aMainDataPtr[aMainId + 2];
232 aCompositeDataPtr[aCompositeId + 3] = anAlphaDataPtr[anAlphaId];
234 aMainId += aNbMainComponents;
235 anAlphaId += aNbAlphaComponents;
236 aCompositeId += aNbCompositeComponents;
238 aMainReader->Delete();
239 anAlphaReader->Delete();
240 return aCompositeImageData;
244 //-----------------------------------------------------------------------------
245 bool VTKViewer_PolyDataMapper::GetBallEnabled()
247 return this->BallEnabled;
249 //-----------------------------------------------------------------------------
250 void VTKViewer_PolyDataMapper::SetBallEnabled( bool theBallEnabled )
252 if( this->BallEnabled == theBallEnabled )
255 this->BallEnabled = theBallEnabled;
257 if(!this->BallEnabled) {
258 this->ImageData = NULL;
261 if(this->BallEnabled) {
262 if(this->SpecialTextures.find(BallTextureId) == SpecialTextures.end()){
263 QString aMainTexture = getenv( "GUI_ROOT_DIR" );
264 aMainTexture.append("/share/salome/resources/gui/sprite_texture.vti");
266 QString anAlphaTexture = getenv( "GUI_ROOT_DIR" );
267 anAlphaTexture.append( "/share/salome/resources/gui/sprite_alpha.vti" );
268 vtkSmartPointer<vtkImageData> aTextureValue = VTK::MakeTexture( aMainTexture.toLatin1().constData(), anAlphaTexture.toLatin1().constData() );
269 this->SpecialTextures[BallTextureId] = aTextureValue;
271 this->ImageData = this->SpecialTextures[BallTextureId];
276 //-----------------------------------------------------------------------------
277 double VTKViewer_PolyDataMapper::GetBallScale()
279 return this->BallScale;
281 //-----------------------------------------------------------------------------
282 void VTKViewer_PolyDataMapper::SetBallScale( double theBallScale )
284 if( this->BallScale == theBallScale )
286 this->BallScale = theBallScale;
289 //-----------------------------------------------------------------------------
290 void VTKViewer_PolyDataMapper::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
292 if( this->MarkerType == theMarkerType && this->MarkerScale == theMarkerScale )
295 this->MarkerType = theMarkerType;
296 this->MarkerScale = theMarkerScale;
298 if( this->MarkerType == VTK::MT_NONE || this->MarkerType == VTK::MT_USER ) {
299 this->ImageData = NULL;
304 int aMarkerType = (int)this->MarkerType;
305 int aMarkerScale = (int)this->MarkerScale;
307 int anId = (int)VTK::MS_70 * aMarkerType + aMarkerScale;
309 if( this->StandardTextures.find( anId ) == this->StandardTextures.end() )
311 QString aFileName = QString( ":/textures/texture%1.dat" ).arg( aMarkerType );
312 VTK::MarkerTexture aMarkerTexture;
313 if( VTK::LoadTextureData( aFileName, theMarkerScale, aMarkerTexture ) )
314 this->StandardTextures[ anId ] = VTK::MakeVTKImage( aMarkerTexture );
317 this->ImageData = this->StandardTextures[ anId ];
321 //-----------------------------------------------------------------------------
322 void VTKViewer_PolyDataMapper::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
324 if( this->MarkerType == VTK::MT_USER && this->MarkerId == theMarkerId )
327 this->MarkerType = VTK::MT_USER;
328 this->MarkerId = theMarkerId;
330 if( this->CustomTextures.find( theMarkerId ) == this->CustomTextures.end() )
331 this->CustomTextures[ theMarkerId ] = VTK::MakeVTKImage( theMarkerTexture );
333 this->ImageData = this->CustomTextures[ theMarkerId ];
337 //-----------------------------------------------------------------------------
338 VTK::MarkerType VTKViewer_PolyDataMapper::GetMarkerType()
340 return this->MarkerType;
343 //-----------------------------------------------------------------------------
344 VTK::MarkerScale VTKViewer_PolyDataMapper::GetMarkerScale()
346 return this->MarkerScale;
349 //-----------------------------------------------------------------------------
350 int VTKViewer_PolyDataMapper::GetMarkerTexture()
352 return this->MarkerId;
355 //-----------------------------------------------------------------------------
356 int VTKViewer_PolyDataMapper::InitExtensions()
360 std::ostringstream strm;
361 glGetIntegerv(GL_NUM_EXTENSIONS, &n);
362 for (int i = 0; i < n; i++)
364 const char *exti = (const char *)glGetStringi(GL_EXTENSIONS, i);
367 std::string s = strm.str();
368 const char* ext = s.c_str();
370 const char* ext = (const char*)glGetString( GL_EXTENSIONS );
372 if( !this->OpenGLHelper.IsInitialized() || !ext ||
373 strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
374 strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL ||
375 strstr( ext, "GL_ARB_shader_objects") == NULL )
377 MESSAGE("Initializing ARB extensions failed");
381 return this->InitShader();
384 //-----------------------------------------------------------------------------
385 void VTKViewer_PolyDataMapper::InitPointSprites()
387 glEnable( GL_POINT_SPRITE_ARB );
388 glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
390 glPushAttrib( GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT );
392 glDepthFunc( GL_LEQUAL );
393 glEnable( GL_DEPTH_TEST );
395 glEnable( GL_ALPHA_TEST );
396 if(!this->BallEnabled) {
397 glAlphaFunc( GL_GREATER, 0.0 );
400 glAlphaFunc( GL_GREATER, 0.5 );
403 glDisable( GL_LIGHTING );
405 glDisable( GL_COLOR_MATERIAL );
408 //-----------------------------------------------------------------------------
409 void VTKViewer_PolyDataMapper::CleanupPointSprites()
413 glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
414 glDisable( GL_POINT_SPRITE_ARB );
417 //-----------------------------------------------------------------------------
418 void VTKViewer_PolyDataMapper::InitTextures()
420 if( !this->ImageData.GetPointer() )
423 glEnable( GL_TEXTURE_2D );
424 if( this->PointSpriteTexture == 0 ) {
425 glGenTextures( 1, &this->PointSpriteTexture );
428 glActiveTexture( GL_TEXTURE0 );
430 glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
431 glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
432 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
433 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
435 if(this->BallEnabled) {
436 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
437 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
439 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
440 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
443 int* aSize = this->ImageData->GetDimensions();
444 unsigned char* dataPtr = (unsigned char*)this->ImageData->GetScalarPointer();
445 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
446 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
450 this->OpenGLHelper.vglUniform1iARB( this->myLocations.PointSprite, GL_TEXTURE0 );
454 //-----------------------------------------------------------------------------
455 void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
457 bool isUsePointSprites = (this->MarkerEnabled && this->MarkerType != VTK::MT_NONE) ||
459 if( isUsePointSprites )
461 if( this->ExtensionsInitialized != ES_Ok )
462 this->ExtensionsInitialized = this->InitExtensions();
463 this->InitPointSprites();
464 this->InitTextures();
467 if(!this->BallEnabled || this->ExtensionsInitialized != ES_Ok) {
468 MAPPER_SUPERCLASS::RenderPiece( ren, act );
469 if( isUsePointSprites )
470 this->CleanupPointSprites();
471 glBindTexture( GL_TEXTURE_2D, 0 );
474 vtkPolyData *input= this->GetInput();
477 // make sure that we've been properly initialized
479 if (ren->GetRenderWindow()->CheckAbortStatus())
484 vtkErrorMacro(<< "No input!");
489 this->InvokeEvent(vtkCommand::StartEvent,NULL);
491 this->InvokeEvent(vtkCommand::EndEvent,NULL);
492 numPts = input->GetNumberOfPoints();
497 vtkDebugMacro(<< "No points!");
501 // make sure our window is current
502 ren->GetRenderWindow()->MakeCurrent();
505 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
509 // if something has changed regenerate colors and display lists
513 if ( this->GetMTime() > this->BuildTime ||
514 input->GetMTime() > this->BuildTime ||
515 act->GetProperty()->GetMTime() > this->BuildTime ||
516 ren->GetRenderWindow() != this->LastWindow)
518 // sets this->Colors as side effect
519 this->MapScalars( act->GetProperty()->GetOpacity() );
521 if (!this->ImmediateModeRendering &&
522 !this->GetGlobalImmediateModeRendering())
524 this->ReleaseGraphicsResources(ren->GetRenderWindow());
525 this->LastWindow = ren->GetRenderWindow();
527 // get a unique display list id
528 this->ListId = glGenLists(1);
529 glNewList(this->ListId,GL_COMPILE);
531 noAbort = this->Draw(ren,act);
534 // Time the actual drawing
535 this->Timer->StartTimer();
536 glCallList(this->ListId);
537 this->Timer->StopTimer();
541 this->ReleaseGraphicsResources(ren->GetRenderWindow());
542 this->LastWindow = ren->GetRenderWindow();
545 this->BuildTime.Modified();
547 // if nothing changed but we are using display lists, draw it
550 if (!this->ImmediateModeRendering &&
551 !this->GetGlobalImmediateModeRendering())
553 // Time the actual drawing
554 this->Timer->StartTimer();
555 glCallList(this->ListId);
556 this->Timer->StopTimer();
560 // if we are in immediate mode rendering we always
561 // want to draw the primitives here
562 if (this->ImmediateModeRendering ||
563 this->GetGlobalImmediateModeRendering())
565 // sets this->Colors as side effect
566 this->MapScalars( act->GetProperty()->GetOpacity() );
568 // Time the actual drawing
569 this->Timer->StartTimer();
571 this->Timer->StopTimer();
574 this->TimeToDraw = (float)this->Timer->GetElapsedTime();
576 // If the timer is not accurate enough, set it to a small
577 // time so that it is not zero
578 if ( this->TimeToDraw == 0.0 )
579 this->TimeToDraw = 0.0001;
581 //this->RenderPieceStart(ren, act);
582 this->RenderPieceDraw(ren, act);
583 // this->RenderEdges(ren,act);
584 //this->RenderPieceFinish(ren, act);
586 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
587 this->CleanupPointSprites();
588 glBindTexture( GL_TEXTURE_2D, 0 );
592 //-----------------------------------------------------------------------------
593 // Definition of structures and fuctions used in Draw() method
596 //-----------------------------------------------------------------------------
603 //-----------------------------------------------------------------------------
604 struct TColorFunctorBase
608 TColorFunctorBase( vtkProperty* theProperty )
610 myAlpha = theProperty->GetOpacity();
615 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
617 virtual ~TColorFunctorBase() {}
620 //-----------------------------------------------------------------------------
621 struct TPropertyColor : TColorFunctorBase
625 TPropertyColor( vtkProperty* theProperty ):
626 TColorFunctorBase( theProperty )
628 theProperty->GetColor( myColor );
633 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
635 theVertex.r = myColor[0];
636 theVertex.g = myColor[1];
637 theVertex.b = myColor[2];
638 theVertex.a = myAlpha;
642 //-----------------------------------------------------------------------------
643 struct TColors2Color : TColorFunctorBase
645 vtkUnsignedCharArray* myColors;
647 TColors2Color( vtkProperty* theProperty,
648 vtkUnsignedCharArray* theColors ):
649 TColorFunctorBase( theProperty ),
650 myColors( theColors )
655 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
657 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
658 unsigned char* aColor = myColors->GetPointer( aTupleId << 2 );
660 theVertex.r = int( aColor[0] ) / 255.0;
661 theVertex.g = int( aColor[1] ) / 255.0;
662 theVertex.b = int( aColor[2] ) / 255.0;
663 theVertex.a = myAlpha;
668 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
671 //-----------------------------------------------------------------------------
672 struct TPointColors2Color : TColors2Color
674 TPointColors2Color( vtkProperty* theProperty,
675 vtkUnsignedCharArray* theColors ):
676 TColors2Color( theProperty, theColors )
681 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
687 //-----------------------------------------------------------------------------
688 struct TCellColors2Color : TColors2Color
690 TCellColors2Color( vtkProperty* theProperty,
691 vtkUnsignedCharArray* theColors ):
692 TColors2Color( theProperty, theColors )
697 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
703 //-----------------------------------------------------------------------------
704 template < class TCoordinates >
705 void DrawPoints( TCoordinates* theStartPoints,
706 vtkCellArray* theCells,
707 TColorFunctorBase* theColorFunctor,
708 TVertex* theVertexArr,
709 vtkIdType &theCellId,
710 vtkIdType &theVertexId,
712 vtkDataArray* theDiamArray,
713 double theBallScale )
715 vtkIdType* ptIds = theCells->GetPointer();
716 vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
718 bool mapBalls = false;
719 if(theBallArr && theDiamArray) {
723 while ( ptIds < endPtIds ) {
724 vtkIdType nPts = *ptIds;
728 TVertex& aVertex = theVertexArr[ theVertexId ];
729 vtkIdType aPointId = *ptIds;
731 TCoordinates* anOffsetPoints = theStartPoints + 3 * aPointId;
732 aVertex.vx = anOffsetPoints[0];
733 aVertex.vy = anOffsetPoints[1];
734 aVertex.vz = anOffsetPoints[2];
736 theColorFunctor->get( aVertex, aPointId, theCellId );
744 theBallArr[theCellId] = (TBall)theDiamArray->GetTuple(theCellId)[0]*theBallScale;
751 //-----------------------------------------------------------------------------
752 template < class TCoordinates >
753 void DrawCellsPoints( vtkPolyData* theInput,
754 vtkPoints* thePoints,
755 TColorFunctorBase* theColorFunctor,
756 TVertex* theVertexArr,
758 double theBallScale )
760 vtkIdType aCellId = 0, aVertexId = 0;
762 TCoordinates* aStartPoints = (TCoordinates*)thePoints->GetVoidPointer(0);
763 vtkDataArray* aDiams = theInput->GetCellData() ? theInput->GetCellData()->GetScalars() : 0;
765 if ( vtkCellArray* aCellArray = theInput->GetVerts() ) {
766 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
769 if ( vtkCellArray* aCellArray = theInput->GetLines() )
770 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
772 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
773 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
775 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
776 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
781 //-----------------------------------------------------------------------------
782 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
785 if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled)
786 return MAPPER_SUPERCLASS::Draw( ren, act );
788 InternalDraw( ren, act );
793 //-----------------------------------------------------------------------------
794 void VTKViewer_PolyDataMapper::RenderPieceDraw( vtkRenderer* ren, vtkActor* act ) {
796 if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled) {
797 MAPPER_SUPERCLASS::RenderPieceDraw( ren, act );
800 InternalDraw( ren, act );
805 #include <vtkCamera.h>
806 #include <vtkOpenGLCamera.h>
807 #include <vtkOpenGLActor.h>
810 void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
811 vtkUnsignedCharArray* colors = NULL;
812 vtkPolyData* input = this->GetInput();
815 vtkProperty* prop = act->GetProperty();
817 points = input->GetPoints();
821 if(!this->BallEnabled) {
822 colors = this->Colors;
823 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
824 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
825 !input->GetPointData()->GetScalars() )
826 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA )
832 vtkIdType aTotalConnectivitySize = 0;
833 vtkIdType aNbCells = 0;
835 if ( vtkCellArray* aCellArray = input->GetVerts() ) {
836 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
837 aNbCells += aCellArray->GetNumberOfCells();
840 if ( vtkCellArray* aCellArray = input->GetLines() ) {
841 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
842 aNbCells += aCellArray->GetNumberOfCells();
845 if ( vtkCellArray* aCellArray = input->GetPolys() ) {
846 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
847 aNbCells += aCellArray->GetNumberOfCells();
850 if ( vtkCellArray* aCellArray = input->GetStrips() ) {
851 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
852 aNbCells += aCellArray->GetNumberOfCells();
855 if ( aTotalConnectivitySize > 0 ) {
856 VTK::TVertex* aVertexArr = new VTK::TVertex[ aTotalConnectivitySize ];
858 TBall* aBallArray = 0;
860 if(this->BallEnabled) {
861 aBallArray = new TBall[aNbCells];
864 int* aSize = this->ImageData->GetDimensions();
866 int aMode = 0; // to remove
868 VTK::TColorFunctorBase* aColorFunctor = NULL;
869 if( colors && aMode != 1 ) {
871 aColorFunctor = new VTK::TCellColors2Color( prop, colors );
873 aColorFunctor = new VTK::TPointColors2Color( prop, colors );
875 aColorFunctor = new VTK::TPropertyColor( prop );
877 if ( points->GetDataType() == VTK_FLOAT )
878 VTK::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
880 VTK::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
882 delete aColorFunctor;
885 if( this->ExtensionsInitialized == ES_Ok ) {
887 this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
889 vtkOpenGLCamera *cam = (vtkOpenGLCamera *)(ren->GetActiveCamera());
894 cam->GetKeyMatrices(ren,wcvc,norms,vcdc,wcdc);
895 if (!act->GetIsIdentity())
898 vtkMatrix3x3 *anorms;
899 ((vtkOpenGLActor *)act)->GetKeyMatrices( mcwc, anorms );
900 vtkMatrix4x4::Multiply4x4( mcwc, wcdc, this->TempMatrix4 );
902 this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, this->TempMatrix4 );
906 this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, wcdc );
908 this->OpenGLHelper.SetUniformMatrix( this->myLocations.Projection, vcdc );
910 this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, std::max( aSize[0], aSize[1] ) );
912 GLuint aBufferObjectID, aDiamsID = 0;
914 this->OpenGLHelper.vglBindVertexArrayARB( this->VertexArrayObject );
915 this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
916 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
918 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
919 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
921 delete [] aVertexArr;
924 GLint colorAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Color" );
925 GLint vertexAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Vertex" );
926 GLint diamAttrib = this->OpenGLHelper.vglGetAttribLocationARB( this->PointProgram, "Diameter" );
928 GLsizei vertexSize = sizeof(VTK::TVertex);
930 this->OpenGLHelper.vglVertexAttribPointerARB( colorAttrib, 4, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)0 );
931 this->OpenGLHelper.vglEnableVertexAttribArrayARB( colorAttrib );
933 this->OpenGLHelper.vglVertexAttribPointerARB( vertexAttrib, 3, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)(sizeof(GLfloat) * 4) );
934 this->OpenGLHelper.vglEnableVertexAttribArrayARB( vertexAttrib );
936 if(this->BallEnabled) {
937 // Don't use uniform variable.
938 this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, -1 );
939 this->OpenGLHelper.vglGenBuffersARB( 1, &aDiamsID);
940 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
942 int aDiamsSize = sizeof(TBall)*aNbCells;
943 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
945 delete [] aBallArray;
947 this->OpenGLHelper.vglVertexAttribPointerARB( diamAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0 );
948 this->OpenGLHelper.vglEnableVertexAttribArrayARB( diamAttrib );
951 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
953 if( this->BallEnabled ) {
954 this->OpenGLHelper.vglDisableVertexAttribArrayARB( diamAttrib );
955 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
956 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aDiamsID );
959 this->OpenGLHelper.vglDisableVertexAttribArrayARB( colorAttrib );
960 this->OpenGLHelper.vglDisableVertexAttribArrayARB( vertexAttrib );
961 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
962 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
963 this->OpenGLHelper.vglBindVertexArrayARB( 0 );
965 this->OpenGLHelper.vglUseProgramObjectARB( 0 );
967 GLuint aBufferObjectID, aDiamsID = 0;
968 GLint attribute_diams = -1;
969 glPointSize( std::max( aSize[0], aSize[1] ) );
970 this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
971 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
973 int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
974 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
976 delete [] aVertexArr;
978 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
979 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
981 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), (void*)0 );
982 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), (void*)(4*sizeof(GLfloat)) );
984 glEnableClientState( GL_VERTEX_ARRAY );
985 glEnableClientState( GL_COLOR_ARRAY );
987 if(this->BallEnabled) {
988 this->OpenGLHelper.vglGenBuffersARB( 1, &aDiamsID);
989 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
991 int aDiamsSize = sizeof(TBall)*aNbCells;
992 this->OpenGLHelper.vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
994 delete [] aBallArray;
995 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
996 this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID );
998 attribute_diams = this->OpenGLHelper.vglGetAttribLocationARB(this->PointProgram, "diameter");
999 this->OpenGLHelper.vglEnableVertexAttribArrayARB(attribute_diams);
1000 this->OpenGLHelper.vglBindBufferARB(GL_ARRAY_BUFFER_ARB, aDiamsID);
1001 this->OpenGLHelper.vglVertexAttribPointerARB(
1002 attribute_diams, // attribute
1003 1, // number of elements per vertex, here (diameter)
1004 GL_FLOAT, // the type of each element
1005 GL_FALSE, // take our values as-is
1006 0, // no extra data between each position
1007 0 // offset of first element
1011 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1013 glDisableClientState( GL_COLOR_ARRAY );
1014 glDisableClientState( GL_VERTEX_ARRAY );
1015 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
1017 if(this->BallEnabled) {
1018 this->OpenGLHelper.vglDisableVertexAttribArrayARB(attribute_diams);
1019 this->OpenGLHelper.vglDeleteBuffersARB( 1, &aDiamsID );
1023 } else { // there are no extensions
1024 glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), aVertexArr );
1025 glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex),
1026 (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
1028 glEnableClientState( GL_VERTEX_ARRAY );
1029 glEnableClientState( GL_COLOR_ARRAY );
1031 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1033 glDisableClientState( GL_COLOR_ARRAY );
1034 glDisableClientState( GL_VERTEX_ARRAY );
1036 delete [] aVertexArr;
1041 this->UpdateProgress(1.0);