1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // VISU OBJECT : interactive object for VISU entities implementation
23 // File: VISU_OpenGLPointSpriteMapper.cxx
27 #include "VISU_OpenGLPointSpriteMapper.hxx"
29 #include "SVTK_Extension.h"
31 #include <vtkCamera.h>
32 #include <vtkCellArray.h>
33 #include <vtkCellData.h>
34 #include <vtkCommand.h>
35 #include <vtkImageData.h>
36 #include <vtkMatrix4x4.h>
37 #include <vtkObjectFactory.h>
38 #include <vtkRenderer.h>
39 #include <vtkRenderWindow.h>
40 #include <vtkPointData.h>
41 #include <vtkPolyData.h>
42 #include <vtkPolygon.h>
43 #include <vtkProperty.h>
44 #include <vtkTimerLog.h>
45 #include <vtkTriangle.h>
51 #include "utilities.h"
54 # ifndef GLX_GLXEXT_LEGACY
55 # define GLX_GLXEXT_LEGACY
63 #ifndef VTK_IMPLEMENT_MESA_CXX
64 vtkCxxRevisionMacro(VISU_OpenGLPointSpriteMapper, "Revision$");
65 vtkStandardNewMacro(VISU_OpenGLPointSpriteMapper);
68 // some definitions for what the polydata has in it
69 #define VTK_PDPSM_COLORS 0x0001
70 #define VTK_PDPSM_CELL_COLORS 0x0002
71 #define VTK_PDPSM_POINT_TYPE_FLOAT 0x0004
72 #define VTK_PDPSM_POINT_TYPE_DOUBLE 0x0008
73 #define VTK_PDPSM_NORMAL_TYPE_FLOAT 0x0010
74 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
75 #define VTK_PDPSM_OPAQUE_COLORS 0x0040
81 #define APIENTRYP APIENTRY *
84 #ifndef GL_OBJECT_INFO_LOG_LENGTH_ARB
85 #define GL_OBJECT_INFO_LOG_LENGTH_ARB 0x8B84
88 #ifndef GL_VERTEX_SHADER_ARB
89 #define GL_VERTEX_SHADER_ARB 0x8B31
92 #ifndef GL_VERTEX_PROGRAM_POINT_SIZE_ARB
93 #define GL_VERTEX_PROGRAM_POINT_SIZE_ARB 0x8642
96 #ifndef GL_ARB_point_sprite
97 #define GL_POINT_SPRITE_ARB 0x8861
98 #define GL_COORD_REPLACE_ARB 0x8862
101 #ifndef GL_ARB_shader_objects
102 typedef char GLcharARB;
105 #ifndef GL_ARB_vertex_buffer_object
106 typedef ptrdiff_t GLsizeiptrARB;
108 #define GL_ARRAY_BUFFER_ARB 0x8892
109 #define GL_STATIC_DRAW_ARB 0x88E4
112 typedef void (APIENTRYP PFNGLSHADERSOURCEARBPROC) (GLhandleARB shaderObj, GLsizei count, const GLcharARB* *string, const GLint *length);
113 typedef GLhandleARB (APIENTRYP PFNGLCREATESHADEROBJECTARBPROC) (GLenum shaderType);
114 typedef void (APIENTRYP PFNGLCOMPILESHADERARBPROC) (GLhandleARB shaderObj);
115 typedef GLhandleARB (APIENTRYP PFNGLCREATEPROGRAMOBJECTARBPROC) (void);
116 typedef void (APIENTRYP PFNGLATTACHOBJECTARBPROC) (GLhandleARB containerObj, GLhandleARB obj);
117 typedef void (APIENTRYP PFNGLLINKPROGRAMARBPROC) (GLhandleARB programObj);
118 typedef void (APIENTRYP PFNGLUSEPROGRAMOBJECTARBPROC) (GLhandleARB programObj);
119 typedef void (APIENTRYP PFNGLGETOBJECTPARAMETERIVARBPROC) (GLhandleARB obj, GLenum pname, GLint *params);
120 typedef void (APIENTRYP PFNGLGETINFOLOGARBPROC) (GLhandleARB obj, GLsizei maxLength, GLsizei *length, GLcharARB *infoLog);
121 typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONARBPROC) (GLhandleARB programObj, const GLcharARB *name);
122 typedef void (APIENTRYP PFNGLVERTEXATTRIB1FARBPROC) (GLuint index, GLfloat x);
124 typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
125 typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
126 typedef void (APIENTRYP PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
127 typedef void (APIENTRYP PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);
129 static PFNGLSHADERSOURCEARBPROC vglShaderSourceARB = NULL;
130 static PFNGLCREATESHADEROBJECTARBPROC vglCreateShaderObjectARB = NULL;
131 static PFNGLCOMPILESHADERARBPROC vglCompileShaderARB = NULL;
132 static PFNGLCREATEPROGRAMOBJECTARBPROC vglCreateProgramObjectARB = NULL;
133 static PFNGLATTACHOBJECTARBPROC vglAttachObjectARB = NULL;
134 static PFNGLLINKPROGRAMARBPROC vglLinkProgramARB = NULL;
135 static PFNGLUSEPROGRAMOBJECTARBPROC vglUseProgramObjectARB = NULL;
136 static PFNGLGETOBJECTPARAMETERIVARBPROC vglGetObjectParameterivARB = NULL;
137 static PFNGLGETINFOLOGARBPROC vglGetInfoLogARB = NULL;
138 static PFNGLGETATTRIBLOCATIONARBPROC vglGetAttribLocationARB = NULL;
139 static PFNGLVERTEXATTRIB1FARBPROC vglVertexAttrib1fARB = NULL;
141 static PFNGLGENBUFFERSARBPROC vglGenBuffersARB = NULL;
142 static PFNGLBINDBUFFERARBPROC vglBindBufferARB = NULL;
143 static PFNGLBUFFERDATAARBPROC vglBufferDataARB = NULL;
144 static PFNGLDELETEBUFFERSARBPROC vglDeleteBuffersARB = NULL;
147 #define GL_GetProcAddress( x ) glXGetProcAddressARB( (const GLubyte*)x )
149 #define GL_GetProcAddress( x ) wglGetProcAddress( (const LPCSTR)x )
154 vglShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)GL_GetProcAddress( "glShaderSourceARB" );
155 if( !vglShaderSourceARB )
158 vglCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)GL_GetProcAddress( "glCreateShaderObjectARB" );
159 if( !vglCreateShaderObjectARB )
162 vglCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)GL_GetProcAddress( "glCompileShaderARB" );
163 if( !vglCompileShaderARB )
166 vglCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)GL_GetProcAddress( "glCreateProgramObjectARB" );
167 if( !vglCreateProgramObjectARB )
170 vglAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)GL_GetProcAddress( "glAttachObjectARB" );
171 if( !vglAttachObjectARB )
174 vglLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)GL_GetProcAddress( "glLinkProgramARB" );
175 if( !vglLinkProgramARB )
178 vglUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)GL_GetProcAddress( "glUseProgramObjectARB" );
179 if( !vglUseProgramObjectARB )
182 vglGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)GL_GetProcAddress( "glGetObjectParameterivARB" );
183 if( !vglGetObjectParameterivARB )
186 vglGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)GL_GetProcAddress( "glGetInfoLogARB" );
187 if( !vglGetInfoLogARB )
190 vglGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)GL_GetProcAddress( "glGetAttribLocationARB" );
191 if( !vglGetAttribLocationARB )
194 vglVertexAttrib1fARB = (PFNGLVERTEXATTRIB1FARBPROC)GL_GetProcAddress( "glVertexAttrib1fARB" );
195 if( !vglVertexAttrib1fARB )
198 vglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)GL_GetProcAddress( "glGenBuffersARB" );
199 if( !vglGenBuffersARB )
202 vglBindBufferARB = (PFNGLBINDBUFFERARBPROC)GL_GetProcAddress( "glBindBufferARB" );
203 if( !vglBindBufferARB )
206 vglBufferDataARB = (PFNGLBUFFERDATAARBPROC)GL_GetProcAddress( "glBufferDataARB" );
207 if( !vglBufferDataARB )
210 vglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)GL_GetProcAddress( "glDeleteBuffersARB" );
211 if( !vglDeleteBuffersARB )
217 static bool IsARBInitialized = InitializeARB();
218 static vtkFloatingPointType Tolerance = 1.0 / VTK_LARGE_FLOAT;
220 //-----------------------------------------------------------------------------
221 // Construct empty object.
222 VISU_OpenGLPointSpriteMapper::VISU_OpenGLPointSpriteMapper()
224 this->RenderMode = VISU_OpenGLPointSpriteMapper::Occlude;
227 this->TotalCells = 0;
228 this->ExtensionsInitialized = 0;
229 this->DefaultPointSize = 20.0;
230 this->AverageCellSize = 0.0;
232 this->UsePointSprites = true;
233 this->UseTextures = true;
234 this->UseShader = true;
236 this->PrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
238 this->PointSpriteMode = 0;
240 this->PointSpriteClamp = 256.0;
241 this->PointSpriteSize = 0.2;
242 this->PointSpriteMinSize = 0.1;
243 this->PointSpriteMaxSize = 0.3;
244 this->PointSpriteMagnification = 1.0;
246 this->PointSpriteAlphaThreshold = 0.5;
247 this->PointSpriteTexture = 0;
249 this->UseOpenGLMapper = false;
251 //-----------------------------------------------------------------------------
252 VISU_OpenGLPointSpriteMapper::~VISU_OpenGLPointSpriteMapper()
254 if( PointSpriteTexture>0 )
255 glDeleteTextures( 1, &PointSpriteTexture );
257 if( this->LastWindow )
258 this->ReleaseGraphicsResources(this->LastWindow);
261 //-----------------------------------------------------------------------------
262 char* readFromFile( std::string fileName )
264 FILE* file = fopen( fileName.c_str(), "r" );
266 char* content = NULL;
271 fseek( file, 0, SEEK_END );
272 count = ftell( file );
277 content = ( char* )malloc( sizeof( char ) * ( count + 1 ) );
278 count = fread( content, sizeof( char ), count, file );
279 content[ count ] = '\0';
286 //-----------------------------------------------------------------------------
287 void VISU_OpenGLPointSpriteMapper::PrintInfoLog( GLhandleARB obj )
289 int infologLength = 0;
290 int charsWritten = 0;
293 vglGetObjectParameterivARB( obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength );
295 if( infologLength > 0 )
297 infoLog = ( char* )malloc( infologLength );
298 vglGetInfoLogARB( obj, infologLength, &charsWritten, infoLog );
299 printf( "%s\n", infoLog );
303 //-----------------------------------------------------------------------------
304 void VISU_OpenGLPointSpriteMapper::InitShader()
306 //cout << "Initializing vertex program" << endl;
308 std::string fileName = std::string( getenv( "VISU_ROOT_DIR") ) +
309 "/share/salome/resources/visu/Vertex_Program_ARB.txt";
311 char* shader = readFromFile( fileName );
313 GLhandleARB VertexShader = vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
314 vglShaderSourceARB( VertexShader, 1, (const GLcharARB**)&shader, NULL );
315 vglCompileShaderARB( VertexShader );
316 //this->PrintInfoLog( VertexShader );
318 this->VertexProgram = vglCreateProgramObjectARB();
319 vglAttachObjectARB( this->VertexProgram, VertexShader );
321 vglLinkProgramARB( this->VertexProgram );
322 //this->PrintInfoLog( VertexProgram );
324 cout << "Shader from " << fileName << endl;
325 for( int i = 0; i < strlen( shader ); i++ )
329 if( glGetError() == GL_NO_ERROR )
330 cout << "Loading vertex program... ok" << endl << endl;
332 cout << "Loading vertex program... failed" << endl << endl;
337 //-----------------------------------------------------------------------------
338 void VISU_OpenGLPointSpriteMapper::SetShaderVariable( const char* variable, float value )
340 //cout << this->VertexProgram << " ";
341 //cout << vglGetAttribLocationARB( this->VertexProgram, variable ) << " ";
342 //cout << variable << " " << value << endl;
344 vglVertexAttrib1fARB( vglGetAttribLocationARB( this->VertexProgram, variable ), value );
347 //-----------------------------------------------------------------------------
348 void VISU_OpenGLPointSpriteMapper::SetPrimitiveType( int thePrimitiveType )
350 if( this->PrimitiveType == thePrimitiveType )
353 this->PrimitiveType = thePrimitiveType;
357 //-----------------------------------------------------------------------------
358 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMode( int theMode )
360 if( this->PointSpriteMode == theMode )
363 this->PointSpriteMode = theMode;
367 //-----------------------------------------------------------------------------
368 void VISU_OpenGLPointSpriteMapper::SetPointSpriteClamp( float theClamp )
370 if( fabs( this->PointSpriteClamp - theClamp ) < Tolerance )
373 this->PointSpriteClamp = theClamp;
377 //-----------------------------------------------------------------------------
378 void VISU_OpenGLPointSpriteMapper::SetAverageCellSize(float theSize)
380 if( fabs( this->AverageCellSize - theSize ) < Tolerance )
383 this->AverageCellSize = theSize;
387 //-----------------------------------------------------------------------------
388 void VISU_OpenGLPointSpriteMapper::SetPointSpriteSize( float theSize )
390 if( fabs( this->PointSpriteSize - theSize ) < Tolerance )
393 this->PointSpriteSize = theSize;
397 //-----------------------------------------------------------------------------
398 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMinSize( float theMinSize )
400 if( fabs( this->PointSpriteMinSize - theMinSize ) < Tolerance )
403 this->PointSpriteMinSize = theMinSize;
407 //-----------------------------------------------------------------------------
408 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMaxSize( float theMaxSize )
410 if( fabs( this->PointSpriteMaxSize - theMaxSize ) < Tolerance )
413 this->PointSpriteMaxSize = theMaxSize;
417 //-----------------------------------------------------------------------------
418 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMagnification( float theMagnification )
420 if( fabs( this->PointSpriteMagnification - theMagnification ) < Tolerance )
423 this->PointSpriteMagnification = theMagnification;
427 //-----------------------------------------------------------------------------
428 void VISU_OpenGLPointSpriteMapper::SetPointSpriteAlphaThreshold( float theAlphaThreshold )
430 if( fabs( this->PointSpriteAlphaThreshold - theAlphaThreshold ) < Tolerance )
433 this->PointSpriteAlphaThreshold = theAlphaThreshold;
437 //-----------------------------------------------------------------------------
438 bool VISU_OpenGLPointSpriteMapper::InitExtensions()
440 if( this->ExtensionsInitialized )
445 char* ext = (char*)glGetString( GL_EXTENSIONS );
446 //cout << "OpenGL extensions : " << ext << endl;
448 if( !IsARBInitialized ||
449 strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
450 strstr( ext, "GL_ARB_shader_objects" ) == NULL ||
451 strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL )
453 vtkWarningMacro(<<"Initializing ARB extensions failed");
454 this->UseOpenGLMapper = true;
459 if( this->UseShader )
462 this->ExtensionsInitialized = 1;
466 //-----------------------------------------------------------------------------
467 float ViewToDisplay( vtkRenderer* theRenderer )
469 vtkFloatingPointType p1[3], p2[3];
471 theRenderer->SetViewPoint( 0.0, 0.0, 0.0 );
472 theRenderer->ViewToDisplay();
473 theRenderer->GetDisplayPoint( p1 );
475 theRenderer->SetViewPoint( 1.0, 1.0, 1.0 );
476 theRenderer->ViewToDisplay();
477 theRenderer->GetDisplayPoint( p2 );
479 vtkFloatingPointType coefficient = sqrt( pow( p2[0] - p1[0], 2 ) + pow( p2[1] - p1[1], 2 ) ) / sqrt( 2. );
480 //cout << p1[0] << " " << p1[1] << " " << p1[2] << endl;
481 //cout << p2[0] << " " << p2[1] << " " << p2[2] << endl;
482 //cout << "ZOOM : " << coefficient << endl;
487 //-----------------------------------------------------------------------------
489 // Receives from Actor -> maps data to primitives
491 void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
493 bool isUseThisMapper = this->PrimitiveType != VISU_OpenGLPointSpriteMapper::GeomSphere;
495 if( isUseThisMapper )
496 this->InitExtensions();
498 if( !isUseThisMapper )
500 MAPPER_SUPERCLASS::RenderPiece( ren, act );
505 vtkPolyData *input= this->GetInput();
508 // make sure that we've been properly initialized
510 if (ren->GetRenderWindow()->CheckAbortStatus())
515 vtkErrorMacro(<< "No input!");
520 this->InvokeEvent(vtkCommand::StartEvent,NULL);
522 this->InvokeEvent(vtkCommand::EndEvent,NULL);
524 numPts = input->GetNumberOfPoints();
529 vtkDebugMacro(<< "No points!");
533 if ( this->LookupTable == NULL )
534 this->CreateDefaultLookupTable();
536 // make sure our window is current
537 ren->GetRenderWindow()->MakeCurrent();
539 if( this->UsePointSprites ) //&& this->PrimitiveType == VISU_OpenGLPointSpriteMapper::PointSprite )
540 this->InitPointSprites();
542 // Initializing the texture for Point Sprites
543 if( this->UseTextures && this->PrimitiveType == VISU_OpenGLPointSpriteMapper::PointSprite )
544 this->InitTextures();
546 vglUseProgramObjectARB( this->VertexProgram );
547 float aViewToDisplay = ViewToDisplay( ren );
548 this->SetShaderVariable( "mode", this->PointSpriteMode );
549 this->SetShaderVariable( "clampSize", this->PointSpriteClamp );
550 this->SetShaderVariable( "geomSize", aViewToDisplay * this->AverageCellSize * this->PointSpriteSize );
551 this->SetShaderVariable( "minSize", aViewToDisplay * this->AverageCellSize * this->PointSpriteMinSize );
552 this->SetShaderVariable( "maxSize", aViewToDisplay * this->AverageCellSize * this->PointSpriteMaxSize );
553 this->SetShaderVariable( "magnification", this->PointSpriteMagnification );
556 // if something has changed regenerate colors and display lists
560 if ( this->GetMTime() > this->BuildTime ||
561 input->GetMTime() > this->BuildTime ||
562 act->GetProperty()->GetMTime() > this->BuildTime ||
563 ren->GetRenderWindow() != this->LastWindow)
565 #ifdef _DEBUG_RENDERING_PERFORMANCE_
566 // To control when the mapper is recalculated
567 MESSAGE( "VISU_OpenGLPointSpriteMapper::RenderPiece - "
568 <<(this->GetMTime() > this->BuildTime)<<"; "
569 <<(input->GetMTime() > this->BuildTime)<<"; "
570 <<(act->GetProperty()->GetMTime() > this->BuildTime)<<"; ");
572 // sets this->Colors as side effect
573 this->MapScalars( act->GetProperty()->GetOpacity() );
575 if (!this->ImmediateModeRendering &&
576 !this->GetGlobalImmediateModeRendering())
578 this->ReleaseGraphicsResources(ren->GetRenderWindow());
579 this->LastWindow = ren->GetRenderWindow();
581 // get a unique display list id
582 this->ListId = glGenLists(1);
583 glNewList(this->ListId,GL_COMPILE);
585 noAbort = this->Draw(ren,act);
588 // Time the actual drawing
589 this->Timer->StartTimer();
590 glCallList(this->ListId);
591 this->Timer->StopTimer();
595 this->ReleaseGraphicsResources(ren->GetRenderWindow());
596 this->LastWindow = ren->GetRenderWindow();
599 this->BuildTime.Modified();
601 // if nothing changed but we are using display lists, draw it
604 if (!this->ImmediateModeRendering &&
605 !this->GetGlobalImmediateModeRendering())
607 // Time the actual drawing
608 this->Timer->StartTimer();
609 glCallList(this->ListId);
610 this->Timer->StopTimer();
614 // if we are in immediate mode rendering we always
615 // want to draw the primitives here
616 if (this->ImmediateModeRendering ||
617 this->GetGlobalImmediateModeRendering())
619 // sets this->Colors as side effect
620 this->MapScalars( act->GetProperty()->GetOpacity() );
622 // Time the actual drawing
623 this->Timer->StartTimer();
625 this->Timer->StopTimer();
628 this->TimeToDraw = (float)this->Timer->GetElapsedTime();
630 // If the timer is not accurate enough, set it to a small
631 // time so that it is not zero
632 if ( this->TimeToDraw == 0.0 )
633 this->TimeToDraw = 0.0001;
635 vglUseProgramObjectARB( 0 );
637 if( this->UsePointSprites ) //&& this->PrimitiveType == VISU_OpenGLPointSpriteMapper::PointSprite )
638 this->CleanupPointSprites();
641 //-----------------------------------------------------------------------------
642 float VISU_OpenGLPointSpriteMapper::GetMaximumSupportedSize()
644 float maximumSupportedSize = 512.0;
645 //glGetFloatv( GL_POINT_SIZE_MAX_ARB, &maximumSupportedSize );
647 return maximumSupportedSize;
650 //-----------------------------------------------------------------------------
651 void VISU_OpenGLPointSpriteMapper::InitPointSprites()
653 glEnable( GL_POINT_SPRITE_ARB );
654 glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
656 switch (this->RenderMode)
658 case VISU_OpenGLPointSpriteMapper::Accumulate:
660 glDisable(GL_DEPTH_TEST);
662 glEnable( GL_BLEND );
663 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
665 glEnable( GL_ALPHA_TEST );
666 glAlphaFunc( GL_GREATER, this->PointSpriteAlphaThreshold );
670 case VISU_OpenGLPointSpriteMapper::Occlude:
672 glDepthFunc( GL_LEQUAL );
673 glEnable( GL_DEPTH_TEST );
675 glEnable( GL_ALPHA_TEST );
676 glAlphaFunc( GL_GREATER, this->PointSpriteAlphaThreshold );
678 glDisable( GL_BLEND );
687 // Disable Lighting/Shading.
688 glDisable( GL_LIGHTING );
690 // Disable material properties
691 glDisable( GL_COLOR_MATERIAL );
694 //-----------------------------------------------------------------------------
695 void VISU_OpenGLPointSpriteMapper::CleanupPointSprites()
697 // Set GL params back to normal to stop other vtkMappers displaying wrongly
698 glDisable( GL_ALPHA_TEST );
700 glEnable( GL_BLEND );
702 glEnable( GL_DEPTH_TEST );
703 glEnable( GL_LIGHTING );
704 glEnable( GL_COLOR_MATERIAL );
706 glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
707 glDisable( GL_POINT_SPRITE_ARB );
711 //-----------------------------------------------------------------------------
713 VISU_OpenGLPointSpriteMapper
714 ::SetImageData( vtkImageData* theImageData )
716 if(GetImageData() == theImageData)
718 this->ImageData = theImageData;
723 VISU_OpenGLPointSpriteMapper
726 return this->ImageData.GetPointer();
730 //-----------------------------------------------------------------------------
731 void VISU_OpenGLPointSpriteMapper::InitTextures()
733 //cout << "VISU_OpenGLPointSpriteMapper::InitTextures " << this->GetImageData() << endl;
734 if( !this->GetImageData() )
737 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
738 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
739 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
740 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
742 int* aSize = GetImageData()->GetDimensions();
743 unsigned char* dataPtr = (unsigned char*)GetImageData()->GetScalarPointer();
744 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
745 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
747 //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
748 glEnable( GL_TEXTURE_2D );
749 glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
750 glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
754 //-----------------------------------------------------------------------------
755 int ComputeHue( int r, int g, int b )
771 if ( g < min ) min = g;
772 if ( b < min ) min = b;
777 else if( whatmax == 0 ) {
779 h = (120*(g-b)+delta)/(2*delta);
781 h = (120*(g-b+delta)+delta)/(2*delta) + 300;
783 else if( whatmax == 1 ) {
785 h = 120 + (120*(b-r)+delta)/(2*delta);
787 h = 60 + (120*(b-r+delta)+delta)/(2*delta);
791 h = 240 + (120*(r-g)+delta)/(2*delta);
793 h = 180 + (120*(r-g+delta)+delta)/(2*delta);
799 //-----------------------------------------------------------------------------
802 GLfloat r, g, b, hue;
807 //-----------------------------------------------------------------------------
808 struct TColorFunctorBase
812 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
816 //-----------------------------------------------------------------------------
817 struct TPropertyColor : TColorFunctorBase
819 vtkFloatingPointType myColor[3];
820 vtkFloatingPointType myHue;
822 TPropertyColor( vtkProperty *theProperty )
824 theProperty->GetColor( myColor );
825 int aRed = int( myColor[0] * 255 );
826 int aGreen = int( myColor[1] * 255 );
827 int aBlue = int( myColor[2] * 255 );
829 myHue = ComputeHue( aRed, aGreen, aBlue );
834 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
836 theVertex.r = myColor[0];
837 theVertex.g = myColor[1];
838 theVertex.b = myColor[2];
840 theVertex.hue = myHue;
845 //-----------------------------------------------------------------------------
846 struct TColors2Color : TColorFunctorBase
848 vtkUnsignedCharArray *myColors;
850 TColors2Color( vtkUnsignedCharArray *theColors ):
851 myColors( theColors )
856 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
858 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
859 unsigned char *aColor = myColors->GetPointer( aTupleId << 2 );
861 theVertex.r = int( aColor[0] ) / 255.0;
862 theVertex.g = int( aColor[1] ) / 255.0;
863 theVertex.b = int( aColor[2] ) / 255.0;
865 theVertex.hue = ComputeHue( aColor[0], aColor[1], aColor[2] );
870 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
874 //-----------------------------------------------------------------------------
875 struct TPointColors2Color : TColors2Color
877 TPointColors2Color( vtkUnsignedCharArray *theColors ):
878 TColors2Color( theColors )
883 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
890 //-----------------------------------------------------------------------------
891 struct TCellColors2Color : TColors2Color
893 TCellColors2Color( vtkUnsignedCharArray *theColors ):
894 TColors2Color( theColors )
899 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
906 //-----------------------------------------------------------------------------
907 template < class TCoordinates >
908 void DrawPoints( TCoordinates *theStartPoints,
909 vtkCellArray *theCells,
910 TColorFunctorBase* theColorFunctor,
911 TVertex* theVertexArr,
912 vtkIdType &theCellId,
913 vtkIdType &theVertexId )
915 vtkIdType *ptIds = theCells->GetPointer();
916 vtkIdType *endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
918 while ( ptIds < endPtIds ) {
919 vtkIdType nPts = *ptIds;
923 TVertex& aVertex = theVertexArr[ theVertexId ];
924 vtkIdType aPointId = *ptIds;
926 TCoordinates *anOffsetPoints = theStartPoints + 3 * aPointId;
927 aVertex.vx = anOffsetPoints[0];
928 aVertex.vy = anOffsetPoints[1];
929 aVertex.vz = anOffsetPoints[2];
931 theColorFunctor->get( aVertex, aPointId, theCellId );
943 //-----------------------------------------------------------------------------
944 template < class TCoordinates >
945 void DrawCellsPoints( vtkPolyData *theInput,
946 vtkPoints* thePoints,
947 TColorFunctorBase* theColorFunctor,
948 TVertex* theVertexArr )
950 vtkIdType aCellId = 0, aVertexId = 0;
952 TCoordinates *aStartPoints = (TCoordinates *) thePoints->GetVoidPointer(0);
954 if ( vtkCellArray* aCellArray = theInput->GetVerts() )
955 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
957 if ( vtkCellArray* aCellArray = theInput->GetLines() )
958 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
960 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
961 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
963 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
964 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
968 //-----------------------------------------------------------------------------
969 int VISU_OpenGLPointSpriteMapper::Draw(vtkRenderer *theRenderer, vtkActor *theActor)
972 if( this->PrimitiveType == VISU_OpenGLPointSpriteMapper::GeomSphere )
973 return MAPPER_SUPERCLASS::Draw( theRenderer, theActor );
975 vtkUnsignedCharArray *colors = NULL;
976 vtkPolyData *input = this->GetInput();
982 // get the transparency
983 tran = theActor->GetProperty()->GetOpacity();
985 // if the primitives are invisable then get out of here
991 // and draw the display list
992 points = input->GetPoints();
994 // are they cell or point scalars
997 colors = this->Colors;
998 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
999 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
1000 !input->GetPointData()->GetScalars() )
1001 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
1008 vtkIdType aTotalConnectivitySize = 0;
1010 if ( vtkCellArray* aCellArray = input->GetVerts() )
1011 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1013 if ( vtkCellArray* aCellArray = input->GetLines() )
1014 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1016 if ( vtkCellArray* aCellArray = input->GetPolys() )
1017 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1019 if ( vtkCellArray* aCellArray = input->GetStrips() )
1020 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1022 if ( aTotalConnectivitySize > 0 ) {
1023 TVertex* aVertexArr = new TVertex[ aTotalConnectivitySize ];
1025 vtkFloatingPointType aPropertyColor[3];
1026 theActor->GetProperty()->GetColor( aPropertyColor );
1028 glPointSize( this->DefaultPointSize );
1031 TColorFunctorBase* aColorFunctor = NULL;
1032 if( colors && this->PointSpriteMode != 1 ) {
1034 aColorFunctor = new TCellColors2Color( colors );
1036 aColorFunctor = new TPointColors2Color( colors );
1038 aColorFunctor = new TPropertyColor( theActor->GetProperty() );
1040 if ( points->GetDataType() == VTK_FLOAT )
1041 ::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr );
1043 ::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr );
1045 delete aColorFunctor;
1048 if( this->ExtensionsInitialized ) {
1049 GLuint aBufferObjectID = 0;
1050 vglGenBuffersARB( 1, &aBufferObjectID );
1051 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1053 int anArrayObjectSize = sizeof( TVertex ) * aTotalConnectivitySize;
1054 vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
1056 delete [] aVertexArr;
1058 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1059 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1061 glColorPointer( 4, GL_FLOAT, sizeof(TVertex), (void*)0 );
1062 glVertexPointer( 3, GL_FLOAT, sizeof(TVertex), (void*)(4*sizeof(GLfloat)) );
1064 glEnableClientState( GL_VERTEX_ARRAY );
1065 glEnableClientState( GL_COLOR_ARRAY );
1067 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1069 glDisableClientState( GL_COLOR_ARRAY );
1070 glDisableClientState( GL_VERTEX_ARRAY );
1072 vglDeleteBuffersARB( 1, &aBufferObjectID );
1073 } else { // there are not extensions
1074 glColorPointer( 4, GL_FLOAT, sizeof(TVertex), aVertexArr );
1075 glVertexPointer( 3, GL_FLOAT, sizeof(TVertex),
1076 (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
1078 glEnableClientState( GL_VERTEX_ARRAY );
1079 glEnableClientState( GL_COLOR_ARRAY );
1081 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1083 glDisableClientState( GL_COLOR_ARRAY );
1084 glDisableClientState( GL_VERTEX_ARRAY );
1086 delete [] aVertexArr;
1090 input->GetVerts()->GetNumberOfCells() +
1091 input->GetLines()->GetNumberOfCells() +
1092 input->GetPolys()->GetNumberOfCells() +
1093 input->GetStrips()->GetNumberOfCells();
1097 this->UpdateProgress(1.0);
1100 //-----------------------------------------------------------------------------
1101 // Release the graphics resources used by this mapper. In this case, release
1102 // the display list if any.
1103 void VISU_OpenGLPointSpriteMapper::ReleaseGraphicsResources(vtkWindow *win)
1105 this->Superclass::ReleaseGraphicsResources(win);
1107 if (this->ListId && win)
1110 glDeleteLists(this->ListId,1);
1113 this->LastWindow = NULL;