1 // VISU OBJECT : interactive object for VISU entities implementation
3 // Copyright (C) 2003 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
24 // File: VISU_OpenGLPointSpriteMapper.cxx
28 #include "VISU_OpenGLPointSpriteMapper.hxx"
30 #include "SVTK_Extension.h"
32 #include <vtkCamera.h>
33 #include <vtkCellArray.h>
34 #include <vtkCellData.h>
35 #include <vtkCommand.h>
36 #include <vtkImageData.h>
37 #include <vtkMatrix4x4.h>
38 #include <vtkObjectFactory.h>
39 #include <vtkRenderer.h>
40 #include <vtkRenderWindow.h>
41 #include <vtkPointData.h>
42 #include <vtkPolyData.h>
43 #include <vtkPolygon.h>
44 #include <vtkProperty.h>
45 #include <vtkTimerLog.h>
46 #include <vtkTriangle.h>
53 # ifndef GLX_GLXEXT_LEGACY
54 # define GLX_GLXEXT_LEGACY
62 #ifndef VTK_IMPLEMENT_MESA_CXX
63 vtkCxxRevisionMacro(VISU_OpenGLPointSpriteMapper, "Revision$");
64 vtkStandardNewMacro(VISU_OpenGLPointSpriteMapper);
67 // some definitions for what the polydata has in it
68 #define VTK_PDPSM_COLORS 0x0001
69 #define VTK_PDPSM_CELL_COLORS 0x0002
70 #define VTK_PDPSM_POINT_TYPE_FLOAT 0x0004
71 #define VTK_PDPSM_POINT_TYPE_DOUBLE 0x0008
72 #define VTK_PDPSM_NORMAL_TYPE_FLOAT 0x0010
73 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
74 #define VTK_PDPSM_OPAQUE_COLORS 0x0040
80 #define APIENTRYP APIENTRY *
83 #ifndef GL_OBJECT_INFO_LOG_LENGTH_ARB
84 #define GL_OBJECT_INFO_LOG_LENGTH_ARB 0x8B84
87 #ifndef GL_VERTEX_SHADER_ARB
88 #define GL_VERTEX_SHADER_ARB 0x8B31
91 #ifndef GL_VERTEX_PROGRAM_POINT_SIZE_ARB
92 #define GL_VERTEX_PROGRAM_POINT_SIZE_ARB 0x8642
95 #ifndef GL_ARB_point_sprite
96 #define GL_POINT_SPRITE_ARB 0x8861
97 #define GL_COORD_REPLACE_ARB 0x8862
100 #ifndef GL_ARB_shader_objects
101 typedef char GLcharARB;
104 #ifndef GL_ARB_vertex_buffer_object
105 typedef ptrdiff_t GLsizeiptrARB;
107 #define GL_ARRAY_BUFFER_ARB 0x8892
108 #define GL_STATIC_DRAW_ARB 0x88E4
111 typedef void (APIENTRYP PFNGLSHADERSOURCEARBPROC) (GLhandleARB shaderObj, GLsizei count, const GLcharARB* *string, const GLint *length);
112 typedef GLhandleARB (APIENTRYP PFNGLCREATESHADEROBJECTARBPROC) (GLenum shaderType);
113 typedef void (APIENTRYP PFNGLCOMPILESHADERARBPROC) (GLhandleARB shaderObj);
114 typedef GLhandleARB (APIENTRYP PFNGLCREATEPROGRAMOBJECTARBPROC) (void);
115 typedef void (APIENTRYP PFNGLATTACHOBJECTARBPROC) (GLhandleARB containerObj, GLhandleARB obj);
116 typedef void (APIENTRYP PFNGLLINKPROGRAMARBPROC) (GLhandleARB programObj);
117 typedef void (APIENTRYP PFNGLUSEPROGRAMOBJECTARBPROC) (GLhandleARB programObj);
118 typedef void (APIENTRYP PFNGLGETOBJECTPARAMETERIVARBPROC) (GLhandleARB obj, GLenum pname, GLint *params);
119 typedef void (APIENTRYP PFNGLGETINFOLOGARBPROC) (GLhandleARB obj, GLsizei maxLength, GLsizei *length, GLcharARB *infoLog);
120 typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONARBPROC) (GLhandleARB programObj, const GLcharARB *name);
121 typedef void (APIENTRYP PFNGLVERTEXATTRIB1FARBPROC) (GLuint index, GLfloat x);
123 typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
124 typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
125 typedef void (APIENTRYP PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
126 typedef void (APIENTRYP PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);
128 static PFNGLSHADERSOURCEARBPROC vglShaderSourceARB = NULL;
129 static PFNGLCREATESHADEROBJECTARBPROC vglCreateShaderObjectARB = NULL;
130 static PFNGLCOMPILESHADERARBPROC vglCompileShaderARB = NULL;
131 static PFNGLCREATEPROGRAMOBJECTARBPROC vglCreateProgramObjectARB = NULL;
132 static PFNGLATTACHOBJECTARBPROC vglAttachObjectARB = NULL;
133 static PFNGLLINKPROGRAMARBPROC vglLinkProgramARB = NULL;
134 static PFNGLUSEPROGRAMOBJECTARBPROC vglUseProgramObjectARB = NULL;
135 static PFNGLGETOBJECTPARAMETERIVARBPROC vglGetObjectParameterivARB = NULL;
136 static PFNGLGETINFOLOGARBPROC vglGetInfoLogARB = NULL;
137 static PFNGLGETATTRIBLOCATIONARBPROC vglGetAttribLocationARB = NULL;
138 static PFNGLVERTEXATTRIB1FARBPROC vglVertexAttrib1fARB = NULL;
140 static PFNGLGENBUFFERSARBPROC vglGenBuffersARB = NULL;
141 static PFNGLBINDBUFFERARBPROC vglBindBufferARB = NULL;
142 static PFNGLBUFFERDATAARBPROC vglBufferDataARB = NULL;
143 static PFNGLDELETEBUFFERSARBPROC vglDeleteBuffersARB = NULL;
146 #define GL_GetProcAddress( x ) glXGetProcAddressARB( (const GLubyte*)x )
148 #define GL_GetProcAddress( x ) wglGetProcAddress( (const LPCSTR)x )
153 vglShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)GL_GetProcAddress( "glShaderSourceARB" );
154 if( !vglShaderSourceARB )
157 vglCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)GL_GetProcAddress( "glCreateShaderObjectARB" );
158 if( !vglCreateShaderObjectARB )
161 vglCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)GL_GetProcAddress( "glCompileShaderARB" );
162 if( !vglCompileShaderARB )
165 vglCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)GL_GetProcAddress( "glCreateProgramObjectARB" );
166 if( !vglCreateProgramObjectARB )
169 vglAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)GL_GetProcAddress( "glAttachObjectARB" );
170 if( !vglAttachObjectARB )
173 vglLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)GL_GetProcAddress( "glLinkProgramARB" );
174 if( !vglLinkProgramARB )
177 vglUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)GL_GetProcAddress( "glUseProgramObjectARB" );
178 if( !vglUseProgramObjectARB )
181 vglGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)GL_GetProcAddress( "glGetObjectParameterivARB" );
182 if( !vglGetObjectParameterivARB )
185 vglGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)GL_GetProcAddress( "glGetInfoLogARB" );
186 if( !vglGetInfoLogARB )
189 vglGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)GL_GetProcAddress( "glGetAttribLocationARB" );
190 if( !vglGetAttribLocationARB )
193 vglVertexAttrib1fARB = (PFNGLVERTEXATTRIB1FARBPROC)GL_GetProcAddress( "glVertexAttrib1fARB" );
194 if( !vglVertexAttrib1fARB )
197 vglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)GL_GetProcAddress( "glGenBuffersARB" );
198 if( !vglGenBuffersARB )
201 vglBindBufferARB = (PFNGLBINDBUFFERARBPROC)GL_GetProcAddress( "glBindBufferARB" );
202 if( !vglBindBufferARB )
205 vglBufferDataARB = (PFNGLBUFFERDATAARBPROC)GL_GetProcAddress( "glBufferDataARB" );
206 if( !vglBufferDataARB )
209 vglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)GL_GetProcAddress( "glDeleteBuffersARB" );
210 if( !vglDeleteBuffersARB )
216 static bool IsARBInitialized = InitializeARB();
217 static vtkFloatingPointType Tolerance = 1.0 / VTK_LARGE_FLOAT;
219 //-----------------------------------------------------------------------------
220 // Construct empty object.
221 VISU_OpenGLPointSpriteMapper::VISU_OpenGLPointSpriteMapper()
223 this->RenderMode = VISU_OpenGLPointSpriteMapper::Occlude;
226 this->TotalCells = 0;
227 this->ExtensionsInitialized = 0;
228 this->DefaultPointSize = 20.0;
229 this->AverageCellSize = 0.0;
231 this->UsePointSprites = true;
232 this->UseTextures = true;
233 this->UseShader = true;
235 this->PrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
237 this->PointSpriteMode = 0;
239 this->PointSpriteClamp = 256.0;
240 this->PointSpriteSize = 0.2;
241 this->PointSpriteMinSize = 0.1;
242 this->PointSpriteMaxSize = 0.3;
243 this->PointSpriteMagnification = 1.0;
245 this->PointSpriteAlphaThreshold = 0.5;
246 this->PointSpriteTexture = 0;
248 this->UseOpenGLMapper = false;
250 //-----------------------------------------------------------------------------
251 VISU_OpenGLPointSpriteMapper::~VISU_OpenGLPointSpriteMapper()
253 if( PointSpriteTexture>0 )
254 glDeleteTextures( 1, &PointSpriteTexture );
256 if( this->LastWindow )
257 this->ReleaseGraphicsResources(this->LastWindow);
260 //-----------------------------------------------------------------------------
261 char* readFromFile( std::string fileName )
263 FILE* file = fopen( fileName.c_str(), "r" );
265 char* content = NULL;
270 fseek( file, 0, SEEK_END );
271 count = ftell( file );
276 content = ( char* )malloc( sizeof( char ) * ( count + 1 ) );
277 count = fread( content, sizeof( char ), count, file );
278 content[ count ] = '\0';
285 //-----------------------------------------------------------------------------
286 void VISU_OpenGLPointSpriteMapper::PrintInfoLog( GLhandleARB obj )
288 int infologLength = 0;
289 int charsWritten = 0;
292 vglGetObjectParameterivARB( obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength );
294 if( infologLength > 0 )
296 infoLog = ( char* )malloc( infologLength );
297 vglGetInfoLogARB( obj, infologLength, &charsWritten, infoLog );
298 printf( "%s\n", infoLog );
302 //-----------------------------------------------------------------------------
303 void VISU_OpenGLPointSpriteMapper::InitShader()
305 //cout << "Initializing vertex program" << endl;
307 std::string fileName = std::string( getenv( "VISU_ROOT_DIR") ) +
308 "/share/salome/resources/visu/Vertex_Program_ARB.txt";
310 char* shader = readFromFile( fileName );
312 GLhandleARB VertexShader = vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
313 vglShaderSourceARB( VertexShader, 1, (const GLcharARB**)&shader, NULL );
314 vglCompileShaderARB( VertexShader );
315 //this->PrintInfoLog( VertexShader );
317 this->VertexProgram = vglCreateProgramObjectARB();
318 vglAttachObjectARB( this->VertexProgram, VertexShader );
320 vglLinkProgramARB( this->VertexProgram );
321 //this->PrintInfoLog( VertexProgram );
323 cout << "Shader from " << fileName << endl;
324 for( int i = 0; i < strlen( shader ); i++ )
328 if( glGetError() == GL_NO_ERROR )
329 cout << "Loading vertex program... ok" << endl << endl;
331 cout << "Loading vertex program... failed" << endl << endl;
336 //-----------------------------------------------------------------------------
337 void VISU_OpenGLPointSpriteMapper::SetShaderVariable( const char* variable, float value )
339 //cout << this->VertexProgram << " ";
340 //cout << vglGetAttribLocationARB( this->VertexProgram, variable ) << " ";
341 //cout << variable << " " << value << endl;
343 vglVertexAttrib1fARB( vglGetAttribLocationARB( this->VertexProgram, variable ), value );
346 //-----------------------------------------------------------------------------
347 void VISU_OpenGLPointSpriteMapper::SetPrimitiveType( int thePrimitiveType )
349 if( this->PrimitiveType == thePrimitiveType )
352 this->PrimitiveType = thePrimitiveType;
355 //-----------------------------------------------------------------------------
356 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMode( int theMode )
358 if( this->PointSpriteMode == theMode )
361 this->PointSpriteMode = theMode;
365 //-----------------------------------------------------------------------------
366 void VISU_OpenGLPointSpriteMapper::SetPointSpriteClamp( float theClamp )
368 if( fabs( this->PointSpriteClamp - theClamp ) < Tolerance )
371 this->PointSpriteClamp = theClamp;
374 //-----------------------------------------------------------------------------
375 void VISU_OpenGLPointSpriteMapper::SetAverageCellSize(float theSize)
377 if( fabs( this->AverageCellSize - theSize ) < Tolerance )
380 this->AverageCellSize = theSize;
383 //-----------------------------------------------------------------------------
384 void VISU_OpenGLPointSpriteMapper::SetPointSpriteSize( float theSize )
386 if( fabs( this->PointSpriteSize - theSize ) < Tolerance )
389 this->PointSpriteSize = theSize;
392 //-----------------------------------------------------------------------------
393 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMinSize( float theMinSize )
395 if( fabs( this->PointSpriteMinSize - theMinSize ) < Tolerance )
398 this->PointSpriteMinSize = theMinSize;
401 //-----------------------------------------------------------------------------
402 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMaxSize( float theMaxSize )
404 if( fabs( this->PointSpriteMaxSize - theMaxSize ) < Tolerance )
407 this->PointSpriteMaxSize = theMaxSize;
410 //-----------------------------------------------------------------------------
411 void VISU_OpenGLPointSpriteMapper::SetPointSpriteMagnification( float theMagnification )
413 if( fabs( this->PointSpriteMagnification - theMagnification ) < Tolerance )
416 this->PointSpriteMagnification = theMagnification;
419 //-----------------------------------------------------------------------------
420 void VISU_OpenGLPointSpriteMapper::SetPointSpriteAlphaThreshold( float theAlphaThreshold )
422 if( fabs( this->PointSpriteAlphaThreshold - theAlphaThreshold ) < Tolerance )
425 this->PointSpriteAlphaThreshold = theAlphaThreshold;
428 //-----------------------------------------------------------------------------
429 bool VISU_OpenGLPointSpriteMapper::InitExtensions()
431 if( this->ExtensionsInitialized )
436 char* ext = (char*)glGetString( GL_EXTENSIONS );
437 //cout << "OpenGL extensions : " << ext << endl;
439 if( !IsARBInitialized ||
440 strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
441 strstr( ext, "GL_ARB_shader_objects" ) == NULL ||
442 strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL )
444 vtkWarningMacro(<<"Initializing ARB extensions failed");
445 this->UseOpenGLMapper = true;
450 if( this->UseShader )
453 this->ExtensionsInitialized = 1;
457 //-----------------------------------------------------------------------------
458 float ViewToDisplay( vtkRenderer* theRenderer )
460 vtkFloatingPointType p1[3], p2[3];
462 theRenderer->SetViewPoint( 0.0, 0.0, 0.0 );
463 theRenderer->ViewToDisplay();
464 theRenderer->GetDisplayPoint( p1 );
466 theRenderer->SetViewPoint( 1.0, 1.0, 1.0 );
467 theRenderer->ViewToDisplay();
468 theRenderer->GetDisplayPoint( p2 );
470 vtkFloatingPointType coefficient = sqrt( pow( p2[0] - p1[0], 2 ) + pow( p2[1] - p1[1], 2 ) ) / sqrt( 2. );
471 //cout << p1[0] << " " << p1[1] << " " << p1[2] << endl;
472 //cout << p2[0] << " " << p2[1] << " " << p2[2] << endl;
473 //cout << "ZOOM : " << coefficient << endl;
478 //-----------------------------------------------------------------------------
480 // Receives from Actor -> maps data to primitives
482 void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
484 bool isUseThisMapper = !( this->UseOpenGLMapper ||
485 this->PrimitiveType == VISU_OpenGLPointSpriteMapper::GeomSphere );
487 if( isUseThisMapper )
488 if( !this->InitExtensions() )
491 if( !isUseThisMapper )
493 MAPPER_SUPERCLASS::RenderPiece( ren, act );
498 vtkPolyData *input= this->GetInput();
501 // make sure that we've been properly initialized
503 if (ren->GetRenderWindow()->CheckAbortStatus())
508 vtkErrorMacro(<< "No input!");
513 this->InvokeEvent(vtkCommand::StartEvent,NULL);
515 this->InvokeEvent(vtkCommand::EndEvent,NULL);
517 numPts = input->GetNumberOfPoints();
522 vtkDebugMacro(<< "No points!");
526 if ( this->LookupTable == NULL )
527 this->CreateDefaultLookupTable();
529 // make sure our window is current
530 ren->GetRenderWindow()->MakeCurrent();
532 if( this->UsePointSprites ) //&& this->PrimitiveType == VISU_OpenGLPointSpriteMapper::PointSprite )
533 this->InitPointSprites();
535 // Initializing the texture for Point Sprites
536 if( this->UseTextures && this->PrimitiveType == VISU_OpenGLPointSpriteMapper::PointSprite )
537 this->InitTextures();
539 vglUseProgramObjectARB( this->VertexProgram );
540 float aViewToDisplay = ViewToDisplay( ren );
541 this->SetShaderVariable( "mode", this->PointSpriteMode );
542 this->SetShaderVariable( "clampSize", this->PointSpriteClamp );
543 this->SetShaderVariable( "geomSize", aViewToDisplay * this->AverageCellSize * this->PointSpriteSize );
544 this->SetShaderVariable( "minSize", aViewToDisplay * this->AverageCellSize * this->PointSpriteMinSize );
545 this->SetShaderVariable( "maxSize", aViewToDisplay * this->AverageCellSize * this->PointSpriteMaxSize );
546 this->SetShaderVariable( "magnification", this->PointSpriteMagnification );
549 // if something has changed regenerate colors and display lists
553 if ( this->GetMTime() > this->BuildTime ||
554 input->GetMTime() > this->BuildTime ||
555 act->GetProperty()->GetMTime() > this->BuildTime ||
556 ren->GetRenderWindow() != this->LastWindow)
558 #ifdef _DEBUG_RENDERING_PERFORMANCE_
559 // To control when the mapper is recalculated
560 cout<<"VISU_OpenGLPointSpriteMapper::RenderPiece - "
561 <<(this->GetMTime() > this->BuildTime)<<"; "
562 <<(input->GetMTime() > this->BuildTime)<<"; "
563 <<(act->GetProperty()->GetMTime() > this->BuildTime)<<"; "
566 // sets this->Colors as side effect
567 this->MapScalars( act->GetProperty()->GetOpacity() );
569 if (!this->ImmediateModeRendering &&
570 !this->GetGlobalImmediateModeRendering())
572 this->ReleaseGraphicsResources(ren->GetRenderWindow());
573 this->LastWindow = ren->GetRenderWindow();
575 // get a unique display list id
576 this->ListId = glGenLists(1);
577 glNewList(this->ListId,GL_COMPILE);
579 noAbort = this->Draw(ren,act);
582 // Time the actual drawing
583 this->Timer->StartTimer();
584 glCallList(this->ListId);
585 this->Timer->StopTimer();
589 this->ReleaseGraphicsResources(ren->GetRenderWindow());
590 this->LastWindow = ren->GetRenderWindow();
593 this->BuildTime.Modified();
595 // if nothing changed but we are using display lists, draw it
598 if (!this->ImmediateModeRendering &&
599 !this->GetGlobalImmediateModeRendering())
601 // Time the actual drawing
602 this->Timer->StartTimer();
603 glCallList(this->ListId);
604 this->Timer->StopTimer();
608 // if we are in immediate mode rendering we always
609 // want to draw the primitives here
610 if (this->ImmediateModeRendering ||
611 this->GetGlobalImmediateModeRendering())
613 // sets this->Colors as side effect
614 this->MapScalars( act->GetProperty()->GetOpacity() );
616 // Time the actual drawing
617 this->Timer->StartTimer();
619 this->Timer->StopTimer();
622 this->TimeToDraw = (float)this->Timer->GetElapsedTime();
624 // If the timer is not accurate enough, set it to a small
625 // time so that it is not zero
626 if ( this->TimeToDraw == 0.0 )
627 this->TimeToDraw = 0.0001;
629 vglUseProgramObjectARB( 0 );
631 if( this->UsePointSprites ) //&& this->PrimitiveType == VISU_OpenGLPointSpriteMapper::PointSprite )
632 this->CleanupPointSprites();
635 //-----------------------------------------------------------------------------
636 float VISU_OpenGLPointSpriteMapper::GetMaximumSupportedSize()
638 float maximumSupportedSize = 512.0;
639 //glGetFloatv( GL_POINT_SIZE_MAX_ARB, &maximumSupportedSize );
641 return maximumSupportedSize;
644 //-----------------------------------------------------------------------------
645 void VISU_OpenGLPointSpriteMapper::InitPointSprites()
647 glEnable( GL_POINT_SPRITE_ARB );
648 glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
650 switch (this->RenderMode)
652 case VISU_OpenGLPointSpriteMapper::Accumulate:
654 glDisable(GL_DEPTH_TEST);
656 glEnable( GL_BLEND );
657 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
659 glEnable( GL_ALPHA_TEST );
660 glAlphaFunc( GL_GREATER, this->PointSpriteAlphaThreshold );
664 case VISU_OpenGLPointSpriteMapper::Occlude:
666 glDepthFunc( GL_LEQUAL );
667 glEnable( GL_DEPTH_TEST );
669 glEnable( GL_ALPHA_TEST );
670 glAlphaFunc( GL_GREATER, this->PointSpriteAlphaThreshold );
672 glDisable( GL_BLEND );
681 // Disable Lighting/Shading.
682 glDisable( GL_LIGHTING );
684 // Disable material properties
685 glDisable( GL_COLOR_MATERIAL );
688 //-----------------------------------------------------------------------------
689 void VISU_OpenGLPointSpriteMapper::CleanupPointSprites()
691 // Set GL params back to normal to stop other vtkMappers displaying wrongly
692 glDisable( GL_ALPHA_TEST );
694 glEnable( GL_BLEND );
696 glEnable( GL_DEPTH_TEST );
697 glEnable( GL_LIGHTING );
698 glEnable( GL_COLOR_MATERIAL );
700 glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
701 glDisable( GL_POINT_SPRITE_ARB );
705 //-----------------------------------------------------------------------------
707 VISU_OpenGLPointSpriteMapper
708 ::SetImageData( vtkImageData* theImageData )
710 if(GetImageData() == theImageData)
712 this->ImageData = theImageData;
717 VISU_OpenGLPointSpriteMapper
720 return this->ImageData.GetPointer();
724 //-----------------------------------------------------------------------------
725 void VISU_OpenGLPointSpriteMapper::InitTextures()
727 //cout << "VISU_OpenGLPointSpriteMapper::InitTextures " << this->GetImageData() << endl;
728 if( !this->GetImageData() )
731 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
732 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
733 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
734 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
736 int* aSize = GetImageData()->GetDimensions();
737 unsigned char* dataPtr = (unsigned char*)GetImageData()->GetScalarPointer();
738 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
739 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
741 //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
742 glEnable( GL_TEXTURE_2D );
743 glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
744 glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
748 //-----------------------------------------------------------------------------
749 int ComputeHue( int r, int g, int b )
765 if ( g < min ) min = g;
766 if ( b < min ) min = b;
771 else if( whatmax == 0 ) {
773 h = (120*(g-b)+delta)/(2*delta);
775 h = (120*(g-b+delta)+delta)/(2*delta) + 300;
777 else if( whatmax == 1 ) {
779 h = 120 + (120*(b-r)+delta)/(2*delta);
781 h = 60 + (120*(b-r+delta)+delta)/(2*delta);
785 h = 240 + (120*(r-g)+delta)/(2*delta);
787 h = 180 + (120*(r-g+delta)+delta)/(2*delta);
793 //-----------------------------------------------------------------------------
796 GLfloat r, g, b, hue;
801 //-----------------------------------------------------------------------------
802 struct TColorFunctorBase
806 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
810 //-----------------------------------------------------------------------------
811 struct TPropertyColor : TColorFunctorBase
813 vtkFloatingPointType myColor[3];
814 vtkFloatingPointType myHue;
816 TPropertyColor( vtkProperty *theProperty )
818 theProperty->GetColor( myColor );
819 int aRed = int( myColor[0] * 255 );
820 int aGreen = int( myColor[1] * 255 );
821 int aBlue = int( myColor[2] * 255 );
823 myHue = ComputeHue( aRed, aGreen, aBlue );
828 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
830 theVertex.r = myColor[0];
831 theVertex.g = myColor[1];
832 theVertex.b = myColor[2];
834 theVertex.hue = myHue;
839 //-----------------------------------------------------------------------------
840 struct TColors2Color : TColorFunctorBase
842 vtkUnsignedCharArray *myColors;
844 TColors2Color( vtkUnsignedCharArray *theColors ):
845 myColors( theColors )
850 get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
852 vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
853 unsigned char *aColor = myColors->GetPointer( aTupleId << 2 );
855 theVertex.r = int( aColor[0] ) / 255.0;
856 theVertex.g = int( aColor[1] ) / 255.0;
857 theVertex.b = int( aColor[2] ) / 255.0;
859 theVertex.hue = ComputeHue( aColor[0], aColor[1], aColor[2] );
864 GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
868 //-----------------------------------------------------------------------------
869 struct TPointColors2Color : TColors2Color
871 TPointColors2Color( vtkUnsignedCharArray *theColors ):
872 TColors2Color( theColors )
877 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
884 //-----------------------------------------------------------------------------
885 struct TCellColors2Color : TColors2Color
887 TCellColors2Color( vtkUnsignedCharArray *theColors ):
888 TColors2Color( theColors )
893 GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
900 //-----------------------------------------------------------------------------
901 template < class TCoordinates >
902 void DrawPoints( TCoordinates *theStartPoints,
903 vtkCellArray *theCells,
904 TColorFunctorBase* theColorFunctor,
905 TVertex* theVertexArr,
906 vtkIdType &theCellId,
907 vtkIdType &theVertexId )
909 vtkIdType *ptIds = theCells->GetPointer();
910 vtkIdType *endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
912 while ( ptIds < endPtIds ) {
913 vtkIdType nPts = *ptIds;
917 TVertex& aVertex = theVertexArr[ theVertexId ];
918 vtkIdType aPointId = *ptIds;
920 TCoordinates *anOffsetPoints = theStartPoints + 3 * aPointId;
921 aVertex.vx = anOffsetPoints[0];
922 aVertex.vy = anOffsetPoints[1];
923 aVertex.vz = anOffsetPoints[2];
925 theColorFunctor->get( aVertex, aPointId, theCellId );
937 //-----------------------------------------------------------------------------
938 template < class TCoordinates >
939 void DrawCellsPoints( vtkPolyData *theInput,
940 vtkPoints* thePoints,
941 TColorFunctorBase* theColorFunctor,
942 TVertex* theVertexArr )
944 vtkIdType aCellId = 0, aVertexId = 0;
946 TCoordinates *aStartPoints = (TCoordinates *) thePoints->GetVoidPointer(0);
948 if ( vtkCellArray* aCellArray = theInput->GetVerts() )
949 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
951 if ( vtkCellArray* aCellArray = theInput->GetLines() )
952 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
954 if ( vtkCellArray* aCellArray = theInput->GetPolys() )
955 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
957 if ( vtkCellArray* aCellArray = theInput->GetStrips() )
958 DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId );
962 //-----------------------------------------------------------------------------
963 int VISU_OpenGLPointSpriteMapper::Draw(vtkRenderer *theRenderer, vtkActor *theActor)
966 if( this->UseOpenGLMapper ||
967 this->PrimitiveType == VISU_OpenGLPointSpriteMapper::GeomSphere )
968 return MAPPER_SUPERCLASS::Draw( theRenderer, theActor );
970 vtkUnsignedCharArray *colors = NULL;
971 vtkPolyData *input = this->GetInput();
977 // get the transparency
978 tran = theActor->GetProperty()->GetOpacity();
980 // if the primitives are invisable then get out of here
986 // and draw the display list
987 points = input->GetPoints();
989 // are they cell or point scalars
992 colors = this->Colors;
993 if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
994 this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
995 !input->GetPointData()->GetScalars() )
996 && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
1003 vtkIdType aTotalConnectivitySize = 0;
1005 if ( vtkCellArray* aCellArray = input->GetVerts() )
1006 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1008 if ( vtkCellArray* aCellArray = input->GetLines() )
1009 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1011 if ( vtkCellArray* aCellArray = input->GetPolys() )
1012 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1014 if ( vtkCellArray* aCellArray = input->GetStrips() )
1015 aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1017 if ( aTotalConnectivitySize > 0 ) {
1018 TVertex* aVertexArr = new TVertex[ aTotalConnectivitySize ];
1020 vtkFloatingPointType aPropertyColor[3];
1021 theActor->GetProperty()->GetColor( aPropertyColor );
1023 glPointSize( this->DefaultPointSize );
1026 TColorFunctorBase* aColorFunctor = NULL;
1027 if( colors && this->PointSpriteMode != 1 ) {
1029 aColorFunctor = new TCellColors2Color( colors );
1031 aColorFunctor = new TPointColors2Color( colors );
1033 aColorFunctor = new TPropertyColor( theActor->GetProperty() );
1035 if ( points->GetDataType() == VTK_FLOAT )
1036 ::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr );
1038 ::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr );
1040 delete aColorFunctor;
1043 GLuint aBufferObjectID = 0;
1044 vglGenBuffersARB( 1, &aBufferObjectID );
1045 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1047 int anArrayObjectSize = sizeof( TVertex ) * aTotalConnectivitySize;
1048 vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
1050 delete [] aVertexArr;
1052 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1053 vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1055 glColorPointer( 4, GL_FLOAT, sizeof(TVertex), (void*)0 );
1056 glVertexPointer( 3, GL_FLOAT, sizeof(TVertex), (void*)(4*sizeof(GLfloat)) );
1058 glEnableClientState( GL_VERTEX_ARRAY );
1059 glEnableClientState( GL_COLOR_ARRAY );
1061 glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1063 glDisableClientState( GL_COLOR_ARRAY );
1064 glDisableClientState( GL_VERTEX_ARRAY );
1066 vglDeleteBuffersARB( 1, &aBufferObjectID );
1069 input->GetVerts()->GetNumberOfCells() +
1070 input->GetLines()->GetNumberOfCells() +
1071 input->GetPolys()->GetNumberOfCells() +
1072 input->GetStrips()->GetNumberOfCells();
1079 this->UpdateProgress(1.0);
1082 //-----------------------------------------------------------------------------
1083 // Release the graphics resources used by this mapper. In this case, release
1084 // the display list if any.
1085 void VISU_OpenGLPointSpriteMapper::ReleaseGraphicsResources(vtkWindow *win)
1087 this->Superclass::ReleaseGraphicsResources(win);
1089 if (this->ListId && win)
1092 glDeleteLists(this->ListId,1);
1095 this->LastWindow = NULL;