Salome HOME
Fix bugs in the new opengl backend.
[modules/gui.git] / src / VTKViewer / VTKViewer_PolyDataMapper.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifdef VTK_OPENGL2
20 #define GL_GLEXT_PROTOTYPES
21 #endif
22
23 #include "VTKViewer_PolyDataMapper.h"
24 #include "VTKViewer_MarkerUtils.h"
25
26 #include <utilities.h>
27
28 #include <QString>
29
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>
44
45 #ifndef WIN32
46 # ifndef GLX_GLXEXT_LEGACY
47 #  define GLX_GLXEXT_LEGACY
48 # endif
49 # include <GL/glx.h>
50 # include <dlfcn.h>
51 #else
52 # include <wingdi.h>
53 #endif
54
55 #ifndef VTK_IMPLEMENT_MESA_CXX
56 vtkStandardNewMacro(VTKViewer_PolyDataMapper);
57 #endif
58
59 // some definitions for what the polydata has in it
60 #define VTK_PDPSM_COLORS             0x0001
61 #define VTK_PDPSM_CELL_COLORS        0x0002
62 #define VTK_PDPSM_POINT_TYPE_FLOAT   0x0004
63 #define VTK_PDPSM_POINT_TYPE_DOUBLE  0x0008
64 #define VTK_PDPSM_NORMAL_TYPE_FLOAT  0x0010
65 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
66 #define VTK_PDPSM_OPAQUE_COLORS      0x0040
67
68 #ifndef APIENTRY
69 #define APIENTRY
70 #endif
71 #ifndef APIENTRYP
72 #define APIENTRYP APIENTRY *
73 #endif
74
75 #ifndef GL_ARB_shader_objects
76 typedef char GLcharARB;
77 #endif
78
79 #ifndef GL_VERTEX_PROGRAM_POINT_SIZE_ARB
80 #define GL_VERTEX_PROGRAM_POINT_SIZE_ARB  0x8642
81 #endif
82
83 #ifndef GL_VERTEX_SHADER_ARB
84 #define GL_VERTEX_SHADER_ARB              0x8B31
85 #endif
86
87 #ifndef GL_FRAGMENT_SHADER_ARB
88 #define GL_FRAGMENT_SHADER_ARB            0x8B30
89 #endif
90
91 #ifndef GL_ARB_point_sprite
92 #define GL_POINT_SPRITE_ARB               0x8861
93 #define GL_COORD_REPLACE_ARB              0x8862
94 #endif
95
96 #ifndef GL_ARB_vertex_buffer_object
97 typedef ptrdiff_t GLsizeiptrARB;
98
99 #define GL_ARRAY_BUFFER_ARB               0x8892
100 #define GL_STATIC_DRAW_ARB                0x88E4
101 #endif
102 typedef void (APIENTRYP PFNGLSHADERSOURCEARBPROC) (GLhandleARB shaderObj, GLsizei count, const GLcharARB* *string, const GLint *length);
103 typedef GLhandleARB (APIENTRYP PFNGLCREATESHADEROBJECTARBPROC) (GLenum shaderType);
104 typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
105 typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
106 typedef void (APIENTRYP PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
107 typedef void (APIENTRYP PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage);
108 typedef void (APIENTRYP PFNGLCOMPILESHADERARBPROC) (GLhandleARB shaderObj);
109 typedef GLhandleARB (APIENTRYP PFNGLCREATEPROGRAMOBJECTARBPROC) (void);
110 typedef void (APIENTRYP PFNGLATTACHOBJECTARBPROC) (GLhandleARB containerObj, GLhandleARB obj);
111 typedef void (APIENTRYP PFNGLLINKPROGRAMARBPROC) (GLhandleARB programObj);
112 typedef void (APIENTRYP PFNGLUSEPROGRAMOBJECTARBPROC) (GLhandleARB programObj);
113 typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONARBPROC) (GLhandleARB programObj, const GLcharARB *name);
114 typedef void (APIENTRYP PFNGLVERTEXATTRIBPOINTERARBPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer);
115 typedef void (APIENTRYP PFNGLENABLEVERTEXATTRIBARRAYARBPROC) (GLuint index);
116 typedef void (APIENTRYP PFNGLDISABLEVERTEXATTRIBARRAYARBPROC) (GLuint index);
117
118 #ifdef VTK_OPENGL2
119 typedef void (APIENTRYP PFNGLDETACHOBJECTARBPROC) (GLhandleARB containerObj, GLhandleARB obj);
120 typedef void (APIENTRYP PFNGLDELETEOBJECTARBPROC) (GLhandleARB obj);
121 typedef void (APIENTRYP PFNGLVALIDATEPROGRAMARBPROC) (GLhandleARB program);
122 typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONARBPROC) ( GLhandleARB program, const GLcharARB *name );
123 typedef void (APIENTRYP PFNGLGETSHADERIVARBPROC) (GLuint shader, GLenum pname, GLint *params);
124 typedef void (APIENTRYP PFNGLGETPROGRAMIVARBPROC) (GLuint program, GLenum pname, GLint *params);
125 typedef void (APIENTRYP PFNGLGETSHADERINFOLOGARBPROC) (GLuint shader, GLsizei maxLength, GLsizei *length, GLchar *infoLog);
126 typedef void (APIENTRYP PFNGLUNIFORMMATRIX4FVARBPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
127 typedef void (APIENTRYP PFNGLGENVERTEXARRAYSARBPROC) (GLsizei n, GLuint *arrays);
128 typedef void (APIENTRYP PFNGLBINDVERTEXARRAYARBPROC) (GLuint array);
129 typedef void (APIENTRYP PFNGLUNIFORM1IARBPROC) (GLint location, GLint v0);
130 #endif
131
132 typedef GLfloat TBall;
133
134
135 static PFNGLSHADERSOURCEARBPROC             vglShaderSourceARB             = NULL;
136 static PFNGLCREATESHADEROBJECTARBPROC       vglCreateShaderObjectARB       = NULL;
137 static PFNGLCOMPILESHADERARBPROC            vglCompileShaderARB            = NULL;
138 static PFNGLCREATEPROGRAMOBJECTARBPROC      vglCreateProgramObjectARB      = NULL;
139 static PFNGLATTACHOBJECTARBPROC             vglAttachObjectARB             = NULL;
140 static PFNGLLINKPROGRAMARBPROC              vglLinkProgramARB              = NULL;
141 static PFNGLUSEPROGRAMOBJECTARBPROC         vglUseProgramObjectARB         = NULL;
142
143 static PFNGLGENBUFFERSARBPROC               vglGenBuffersARB               = NULL;
144 static PFNGLBINDBUFFERARBPROC               vglBindBufferARB               = NULL;
145 static PFNGLBUFFERDATAARBPROC               vglBufferDataARB               = NULL;
146 static PFNGLDELETEBUFFERSARBPROC            vglDeleteBuffersARB            = NULL;
147 static PFNGLGETATTRIBLOCATIONARBPROC        vglGetAttribLocationARB        = NULL;
148 static PFNGLVERTEXATTRIBPOINTERARBPROC      vglVertexAttribPointerARB      = NULL;
149 static PFNGLENABLEVERTEXATTRIBARRAYARBPROC  vglEnableVertexAttribArrayARB  = NULL;
150 static PFNGLDISABLEVERTEXATTRIBARRAYARBPROC vglDisableVertexAttribArrayARB = NULL;
151
152 #ifdef VTK_OPENGL2
153 static PFNGLDETACHOBJECTARBPROC             vglDetachObjectARB             = NULL;
154 static PFNGLDELETEOBJECTARBPROC             vglDeleteObjectARB             = NULL;
155 static PFNGLVALIDATEPROGRAMARBPROC          vglValidateProgramARB          = NULL;
156 static PFNGLGETSHADERIVARBPROC              vglGetShaderivARB              = NULL;
157 static PFNGLGETPROGRAMIVARBPROC             vglGetProgramivARB             = NULL;
158 static PFNGLGETSHADERINFOLOGARBPROC         vglGetShaderInfoLogARB         = NULL;
159
160 static PFNGLUNIFORMMATRIX4FVARBPROC         vglUniformMatrix4fvARB         = NULL;
161 static PFNGLGENVERTEXARRAYSARBPROC          vglGenVertexArraysARB          = NULL;
162 static PFNGLBINDVERTEXARRAYARBPROC          vglBindVertexArrayARB          = NULL;
163 static PFNGLUNIFORM1IARBPROC                vglUniform1iARB                = NULL;
164 static PFNGLGETUNIFORMLOCATIONARBPROC       vglGetUniformLocationARB       = NULL;
165 #endif
166
167 #ifndef WIN32
168 #define GL_GetProcAddress( x )   glXGetProcAddressARB( (const GLubyte*)x )
169 #else
170 #define GL_GetProcAddress( x )   wglGetProcAddress( (const LPCSTR)x )
171 #endif
172
173 #ifdef WIN32
174   #ifdef max
175     #undef max
176   #endif
177 #endif
178
179 // ----------------------------------------------- Special Textures -----------------------------------
180 // texture id for balls drawing
181 #define BallTextureId 0 
182
183 bool InitializeBufferExtensions()
184 {
185   vglShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)GL_GetProcAddress( "glShaderSourceARB" );
186   if( !vglShaderSourceARB )
187     return false;
188
189   vglCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)GL_GetProcAddress( "glCreateShaderObjectARB" );
190   if( !vglCreateShaderObjectARB )
191     return false;
192
193   vglCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)GL_GetProcAddress( "glCompileShaderARB" );
194   if( !vglCompileShaderARB )
195     return false;
196
197   vglCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)GL_GetProcAddress( "glCreateProgramObjectARB" );
198   if( !vglCreateProgramObjectARB )
199     return false;
200
201   vglAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)GL_GetProcAddress( "glAttachObjectARB" );
202   if( !vglAttachObjectARB )
203     return false;
204
205   vglLinkProgramARB = (PFNGLLINKPROGRAMARBPROC)GL_GetProcAddress( "glLinkProgramARB" );
206   if( !vglLinkProgramARB )
207     return false;
208
209   vglUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)GL_GetProcAddress( "glUseProgramObjectARB" );
210   if( !vglUseProgramObjectARB )
211     return false;
212
213   vglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)GL_GetProcAddress( "glGenBuffersARB" );
214   if( !vglGenBuffersARB )
215     return false;
216
217   vglBindBufferARB = (PFNGLBINDBUFFERARBPROC)GL_GetProcAddress( "glBindBufferARB" );
218   if( !vglBindBufferARB )
219     return false;
220
221   vglBufferDataARB = (PFNGLBUFFERDATAARBPROC)GL_GetProcAddress( "glBufferDataARB" );
222   if( !vglBufferDataARB )
223     return false;
224
225   vglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)GL_GetProcAddress( "glDeleteBuffersARB" );
226   if( !vglDeleteBuffersARB )
227     return false;
228
229   vglGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)GL_GetProcAddress( "glGetAttribLocation" );
230   if( !vglGetAttribLocationARB )
231     return false;
232
233   vglVertexAttribPointerARB = (PFNGLVERTEXATTRIBPOINTERARBPROC)GL_GetProcAddress( "glVertexAttribPointer" );
234   if( !vglVertexAttribPointerARB )
235     return false;
236
237   vglEnableVertexAttribArrayARB = (PFNGLENABLEVERTEXATTRIBARRAYARBPROC)GL_GetProcAddress( "glEnableVertexAttribArray" );
238   if(!vglEnableVertexAttribArrayARB)
239     return false;
240
241   vglDisableVertexAttribArrayARB = (PFNGLDISABLEVERTEXATTRIBARRAYARBPROC)GL_GetProcAddress( "glDisableVertexAttribArray" );
242
243   if(!vglDisableVertexAttribArrayARB)
244     return false;
245
246 #ifdef VTK_OPENGL2
247   vglDetachObjectARB = (PFNGLDETACHOBJECTARBPROC)GL_GetProcAddress( "glDetachObjectARB" );
248   if( !vglDetachObjectARB )
249     return false;
250
251   vglDeleteObjectARB = (PFNGLDELETEOBJECTARBPROC)GL_GetProcAddress( "glDeleteObjectARB" );
252   if( !vglDeleteObjectARB )
253     return false;
254
255   vglValidateProgramARB = (PFNGLVALIDATEPROGRAMARBPROC)GL_GetProcAddress( "glValidateProgramARB" );
256   if ( !vglValidateProgramARB )
257     return false;
258
259   vglGetUniformLocationARB = (PFNGLGETUNIFORMLOCATIONARBPROC)GL_GetProcAddress( "glGetUniformLocationARB" );
260   if( !vglGetUniformLocationARB )
261     return false;
262
263   vglGetShaderivARB = (PFNGLGETSHADERIVPROC)GL_GetProcAddress( "glGetShaderiv" );
264   if( !vglGetShaderivARB )
265     return false;
266
267   vglGetProgramivARB = (PFNGLGETPROGRAMIVPROC)GL_GetProcAddress( "glGetProgramiv" );
268   if( !vglGetProgramivARB )
269     return false;
270
271   vglGetShaderInfoLogARB = (PFNGLGETSHADERINFOLOGPROC)GL_GetProcAddress( "glGetShaderInfoLog" );
272   if( !vglGetShaderInfoLogARB )
273     return false;
274
275   vglUniformMatrix4fvARB = (PFNGLUNIFORMMATRIX4FVARBPROC)GL_GetProcAddress( "glUniformMatrix4fv" );
276   if( !vglUniformMatrix4fvARB )
277     return false;
278
279   vglGenVertexArraysARB = (PFNGLGENVERTEXARRAYSARBPROC)GL_GetProcAddress( "glGenVertexArrays" );
280   if( !vglGenVertexArraysARB )
281     return false;
282
283   vglBindVertexArrayARB = (PFNGLBINDVERTEXARRAYARBPROC)GL_GetProcAddress( "glBindVertexArray" );
284   if( !vglBindVertexArrayARB )
285     return false;
286
287   vglUniform1iARB = (PFNGLUNIFORM1IARBPROC)GL_GetProcAddress( "glUniform1i" );
288   if( !vglUniform1iARB )
289     return false;
290 #endif
291
292   return true;
293 };
294
295 //-----------------------------------------------------------------------------
296 char* readFromFile( std::string fileName )
297 {
298   FILE* file = fopen( fileName.c_str(), "r" );
299
300   char* content = NULL;
301   int count = 0;
302
303   if( file != NULL )
304   {
305     fseek( file, 0, SEEK_END );
306     count = ftell( file );
307     rewind( file );
308
309     if( count > 0 )
310     {
311       content = ( char* )malloc( sizeof( char ) * ( count + 1 ) );
312       count = fread( content, sizeof( char ), count, file );
313       content[ count ] = '\0';
314     }
315     fclose( file );
316   }
317
318   return content;
319 }
320
321 static bool IsBufferExtensionsInitialized = InitializeBufferExtensions();
322
323 //-----------------------------------------------------------------------------
324 VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
325 {
326   Q_INIT_RESOURCE( VTKViewer );
327
328   this->ExtensionsInitialized     = ES_None;
329
330   this->PointSpriteTexture        = 0;
331
332   this->MarkerEnabled             = false;
333   this->MarkerType                = VTK::MT_NONE;
334   this->MarkerScale               = VTK::MS_NONE;
335   this->MarkerId                  = 0;
336   this->BallEnabled               = false;
337   this->BallScale                 = 1.0;
338   this->PointProgram              = 0;
339 #ifdef VTK_OPENGL2
340   this->VertexShader              = 0;
341   this->FragmentShader            = 0;
342 #endif
343 }
344
345 //-----------------------------------------------------------------------------
346 VTKViewer_PolyDataMapper::~VTKViewer_PolyDataMapper()
347 {
348   if( PointSpriteTexture > 0 )
349     glDeleteTextures( 1, &PointSpriteTexture );
350
351 #ifdef VTK_OPENGL2
352   vglDetachObjectARB( this->PointProgram, this->VertexShader );
353   vglDetachObjectARB( this->PointProgram, this->FragmentShader );
354
355   vglDeleteObjectARB( this->VertexShader );
356   vglDeleteObjectARB( this->FragmentShader );
357 #endif
358 }
359
360 //-----------------------------------------------------------------------------
361 int VTKViewer_PolyDataMapper::InitShader()
362 {
363 #ifdef VTK_OPENGL2
364   const GLubyte *version = glGetString(GL_VERSION);
365   MESSAGE(version);
366   // Create program.
367   this->PointProgram = vglCreateProgramObjectARB();
368   if (this->PointProgram == 0)
369   {
370     MESSAGE("Can't create opengl program.");
371     return ES_Error;
372   }
373
374   std::string fileName;
375   char*       shaderContent;
376   GLint       linked, compileStatus, validateStatus;
377
378   // Create vertex shader.
379   fileName = std::string( getenv( "GUI_ROOT_DIR") ) +
380                           "/share/salome/resources/gui/Vertex_Shader_ARB.glsl";
381
382   shaderContent = readFromFile( fileName );
383
384   this->VertexShader = vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
385   vglShaderSourceARB( this->VertexShader, 1, (const GLcharARB**)&shaderContent, NULL );
386   vglCompileShaderARB( this->VertexShader );
387
388   free( shaderContent );
389
390   vglGetShaderivARB( this->VertexShader, GL_COMPILE_STATUS, &compileStatus );\r
391   if (compileStatus != GL_TRUE)\r
392   {
393     GLint size;
394     GLchar info[1024];
395
396     vglGetShaderInfoLogARB( this->VertexShader, 1024, &size, info );
397     std::cout << info << std::endl;
398
399     MESSAGE( "Can't compile vertex shader." );
400     return ES_Error;
401   }
402
403   // Create fragment shader.
404   fileName = std::string( getenv( "GUI_ROOT_DIR") ) +
405                           "/share/salome/resources/gui/Fragment_Shader_ARB.glsl";
406
407   shaderContent = readFromFile( fileName );
408
409   this->FragmentShader = vglCreateShaderObjectARB( GL_FRAGMENT_SHADER_ARB );
410   vglShaderSourceARB( this->FragmentShader, 1, (const GLcharARB**)&shaderContent, NULL );
411   vglCompileShaderARB( this->FragmentShader );
412
413   free( shaderContent );
414
415   vglGetShaderivARB( this->FragmentShader, GL_COMPILE_STATUS, &compileStatus );\r
416   if (compileStatus != GL_TRUE)\r
417   {
418     MESSAGE( "Can't compile fragment shader." );
419     return ES_Error;
420   }
421
422   // Attach shaders.
423   vglAttachObjectARB( this->PointProgram, this->VertexShader );
424   vglAttachObjectARB( this->PointProgram, this->FragmentShader );
425   vglLinkProgramARB( this->PointProgram );
426
427   vglGetProgramivARB( this->PointProgram, GL_LINK_STATUS, &linked );
428   if (!linked)
429   {
430     MESSAGE("Can't link program.");
431     return ES_Error;
432   }
433
434   vglValidateProgramARB( this->PointProgram );\r
435   vglGetProgramivARB( this->PointProgram, GL_VALIDATE_STATUS, &validateStatus );
436
437   if (validateStatus != GL_TRUE)\r
438   {\r
439     MESSAGE( "Shader program is not validate." );\r
440     return ES_Error;\r
441   }
442
443   // Get uniform locations.
444   vglUseProgramObjectARB( this->PointProgram );
445
446   this->myLocations.ModelViewProjection = vglGetUniformLocationARB( this->PointProgram, "uModelViewProjectionMatrix" );
447   this->myLocations.GeneralPointSize    = vglGetUniformLocationARB( this->PointProgram, "uGeneralPointSize" );
448   this->myLocations.PointSprite         = vglGetUniformLocationARB( this->PointProgram, "uPointSprite" );
449
450   vglUseProgramObjectARB( 0 );
451
452   vglGenVertexArraysARB(1, &this->VertexArrayObject);
453 #else
454   std::string fileName = std::string( getenv( "GUI_ROOT_DIR") ) +
455                          "/share/salome/resources/gui/Vertex_Program_ARB.txt";
456
457   char* shader = readFromFile( fileName );
458
459   GLhandleARB VertexShader = vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
460   vglShaderSourceARB( VertexShader, 1, (const GLcharARB**)&shader, NULL );
461   vglCompileShaderARB( VertexShader );
462
463   this->PointProgram = vglCreateProgramObjectARB();
464   vglAttachObjectARB( this->PointProgram, VertexShader );
465
466   vglLinkProgramARB( this->PointProgram );
467   free( shader );
468 #endif
469
470   return ES_Ok;
471 }
472
473
474 //-----------------------------------------------------------------------------
475 void VTKViewer_PolyDataMapper::SetMarkerEnabled( bool theMarkerEnabled )
476 {
477   if( this->MarkerEnabled == theMarkerEnabled )
478     return;
479
480   this->MarkerEnabled = theMarkerEnabled;
481   this->Modified();
482 }
483
484 //-----------------------------------------------------------------------------
485 // Definition of structures and fuctions used in SetBallEnabled() method
486 namespace VTK
487 {
488   //----------------------------------------------------------------------------
489   vtkSmartPointer<vtkImageData> MakeTexture( const char* theMainTexture, const char* theAlphaTexture ) {
490     if( !theMainTexture || !theAlphaTexture )
491       return 0;
492     
493     vtkXMLImageDataReader* aMainReader = vtkXMLImageDataReader::New();
494     vtkXMLImageDataReader* anAlphaReader = vtkXMLImageDataReader::New();
495     
496     aMainReader->SetFileName( theMainTexture );
497     anAlphaReader->SetFileName( theAlphaTexture );
498
499     aMainReader->Update();
500     anAlphaReader->Update();
501     
502     vtkImageData* aMainImageData = aMainReader->GetOutput();
503     vtkImageData* anAlphaImageData = anAlphaReader->GetOutput();
504     
505     int* aMainImageSize = aMainImageData->GetDimensions();
506     int* anAlphaImageSize = anAlphaImageData->GetDimensions();
507     if(aMainImageSize[0] != anAlphaImageSize[0] || aMainImageSize[1] != anAlphaImageSize[1])
508       return NULL;
509
510     vtkSmartPointer<vtkImageData> aCompositeImageData = vtkImageData::New();
511     aCompositeImageData->Delete();
512     
513     int aNbCompositeComponents = 4;
514     aCompositeImageData->SetDimensions(aMainImageSize);
515     aCompositeImageData->AllocateScalars( VTK_UNSIGNED_CHAR, aNbCompositeComponents );
516     
517     unsigned char* aMainDataPtr = (unsigned char*)aMainImageData->GetScalarPointer();
518     unsigned char* anAlphaDataPtr = (unsigned char*)anAlphaImageData->GetScalarPointer();
519     unsigned char *aCompositeDataPtr = (unsigned char * )aCompositeImageData->GetScalarPointer();
520
521     int aNbMainComponents = aMainImageData->GetNumberOfScalarComponents();
522     int aNbAlphaComponents = anAlphaImageData->GetNumberOfScalarComponents();
523     int aCompositeSize = aMainImageSize[0] * aMainImageSize[1] * aNbCompositeComponents;
524
525     int aMainId = 0, anAlphaId = 0, aCompositeId = 0;
526     for(; aCompositeId < aCompositeSize;) {
527       aCompositeDataPtr[aCompositeId] = aMainDataPtr[aMainId];
528       aCompositeDataPtr[aCompositeId + 1] = aMainDataPtr[aMainId + 1];
529       aCompositeDataPtr[aCompositeId + 2] = aMainDataPtr[aMainId + 2];
530       aCompositeDataPtr[aCompositeId + 3] = anAlphaDataPtr[anAlphaId];
531
532       aMainId += aNbMainComponents;
533       anAlphaId += aNbAlphaComponents;
534       aCompositeId += aNbCompositeComponents;
535     }
536     aMainReader->Delete();
537     anAlphaReader->Delete();
538     return aCompositeImageData;
539   }  
540 }
541
542 //-----------------------------------------------------------------------------
543 bool VTKViewer_PolyDataMapper::GetBallEnabled()
544 {
545   return this->BallEnabled;
546 }
547 //-----------------------------------------------------------------------------
548 void VTKViewer_PolyDataMapper::SetBallEnabled( bool theBallEnabled )
549
550   if( this->BallEnabled == theBallEnabled )
551     return;
552   else 
553     this->BallEnabled = theBallEnabled;
554
555   if(!this->BallEnabled) {
556     this->ImageData = NULL;
557   }
558
559   if(this->BallEnabled) {
560     if(this->SpecialTextures.find(BallTextureId) == SpecialTextures.end()){
561       QString aMainTexture  = getenv( "GUI_ROOT_DIR" );
562       aMainTexture.append("/share/salome/resources/gui/sprite_texture.vti");
563       
564       QString anAlphaTexture = getenv( "GUI_ROOT_DIR" );
565       anAlphaTexture.append( "/share/salome/resources/gui/sprite_alpha.vti" );
566       vtkSmartPointer<vtkImageData> aTextureValue = VTK::MakeTexture( aMainTexture.toLatin1().constData(), anAlphaTexture.toLatin1().constData() );
567       this->SpecialTextures[BallTextureId] = aTextureValue;
568     }
569     this->ImageData = this->SpecialTextures[BallTextureId];
570   }
571   this->Modified();
572 }
573
574 //-----------------------------------------------------------------------------
575 double VTKViewer_PolyDataMapper::GetBallScale()
576 {
577   return this->BallScale;
578 }
579 //-----------------------------------------------------------------------------
580 void VTKViewer_PolyDataMapper::SetBallScale( double theBallScale )
581 {
582   if( this->BallScale == theBallScale )
583     return;
584   this->BallScale = theBallScale;
585 }
586
587 //-----------------------------------------------------------------------------
588 void VTKViewer_PolyDataMapper::SetMarkerStd( VTK::MarkerType theMarkerType, VTK::MarkerScale theMarkerScale )
589 {
590   if( this->MarkerType == theMarkerType && this->MarkerScale == theMarkerScale )
591     return;
592
593   this->MarkerType = theMarkerType;
594   this->MarkerScale = theMarkerScale;
595
596   if( this->MarkerType == VTK::MT_NONE || this->MarkerType == VTK::MT_USER ) {
597     this->ImageData = NULL;
598     this->Modified();
599     return;
600   }
601
602   int aMarkerType = (int)this->MarkerType;
603   int aMarkerScale = (int)this->MarkerScale;
604
605   int anId = (int)VTK::MS_70 * aMarkerType + aMarkerScale;
606
607   if( this->StandardTextures.find( anId ) == this->StandardTextures.end() )
608   {
609     QString aFileName = QString( ":/textures/texture%1.dat" ).arg( aMarkerType );
610     VTK::MarkerTexture aMarkerTexture;
611     if( VTK::LoadTextureData( aFileName, theMarkerScale, aMarkerTexture ) )
612       this->StandardTextures[ anId ] = VTK::MakeVTKImage( aMarkerTexture );
613   }
614
615   this->ImageData = this->StandardTextures[ anId ];
616   this->Modified();
617 }
618
619 //-----------------------------------------------------------------------------
620 void VTKViewer_PolyDataMapper::SetMarkerTexture( int theMarkerId, VTK::MarkerTexture theMarkerTexture )
621 {
622   if( this->MarkerType == VTK::MT_USER && this->MarkerId == theMarkerId )
623     return;
624
625   this->MarkerType = VTK::MT_USER;
626   this->MarkerId = theMarkerId;
627
628   if( this->CustomTextures.find( theMarkerId ) == this->CustomTextures.end() )
629     this->CustomTextures[ theMarkerId ] = VTK::MakeVTKImage( theMarkerTexture );
630
631   this->ImageData = this->CustomTextures[ theMarkerId ];
632   this->Modified();
633 }
634
635 //-----------------------------------------------------------------------------
636 VTK::MarkerType VTKViewer_PolyDataMapper::GetMarkerType()
637 {
638   return this->MarkerType;
639 }
640
641 //-----------------------------------------------------------------------------
642 VTK::MarkerScale VTKViewer_PolyDataMapper::GetMarkerScale()
643 {
644   return this->MarkerScale;
645 }
646
647 //-----------------------------------------------------------------------------
648 int VTKViewer_PolyDataMapper::GetMarkerTexture()
649 {
650   return this->MarkerId;
651 }
652
653 //-----------------------------------------------------------------------------
654 int VTKViewer_PolyDataMapper::InitExtensions()
655 {
656 #ifdef VTK_OPENGL2
657   int n = 0;
658   std::ostringstream strm;
659   glGetIntegerv(GL_NUM_EXTENSIONS, &n);
660   for (int i = 0; i < n; i++)
661     {
662       const char *exti = (const char *)glGetStringi(GL_EXTENSIONS, i);
663       strm<< exti <<" ";
664     }
665   std::string s = strm.str();
666   const char* ext = s.c_str();
667 #else  
668   const char* ext = (const char*)glGetString( GL_EXTENSIONS );
669 #endif  
670   if( !IsBufferExtensionsInitialized || !ext ||
671       strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
672       strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL ||
673       strstr( ext, "GL_ARB_shader_objects") == NULL )
674   {
675     MESSAGE("Initializing ARB extensions failed");
676     return ES_Error;
677   }
678
679   return this->InitShader();
680 }
681
682 //-----------------------------------------------------------------------------
683 void VTKViewer_PolyDataMapper::InitPointSprites()
684 {
685   glEnable( GL_POINT_SPRITE_ARB );
686   glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
687
688   glPushAttrib( GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT );
689
690   glDepthFunc( GL_LEQUAL );
691   glEnable( GL_DEPTH_TEST );
692
693   glEnable( GL_ALPHA_TEST );
694   if(!this->BallEnabled) {
695     glAlphaFunc( GL_GREATER, 0.0 );
696   }
697   else { 
698     glAlphaFunc( GL_GREATER, 0.5 );
699   }
700
701   glDisable( GL_LIGHTING );
702
703   glDisable( GL_COLOR_MATERIAL );
704 }
705
706 //-----------------------------------------------------------------------------
707 void VTKViewer_PolyDataMapper::CleanupPointSprites()
708 {
709   glPopAttrib();
710
711   glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
712   glDisable( GL_POINT_SPRITE_ARB );
713 }
714
715 //-----------------------------------------------------------------------------
716 void VTKViewer_PolyDataMapper::InitTextures()
717 {
718   if( !this->ImageData.GetPointer() )
719     return;
720
721   glEnable( GL_TEXTURE_2D );
722   if( this->PointSpriteTexture == 0 ) {
723     glGenTextures( 1, &this->PointSpriteTexture );
724   }
725 #ifdef VTK_OPENGL2
726   glActiveTexture( GL_TEXTURE0 );
727 #endif
728   glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
729   glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
730   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
731   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
732   
733   if(this->BallEnabled) {
734     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
735     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
736   } else {
737     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
738     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
739   }
740
741   int* aSize = this->ImageData->GetDimensions();
742   unsigned char* dataPtr = (unsigned char*)this->ImageData->GetScalarPointer();
743   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, aSize[0], aSize[1], 0,
744                 GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
745
746 #ifdef VTK_OPENGL2
747   // Set sampler.
748   vglUniform1iARB( this->myLocations.PointSprite, GL_TEXTURE0 );
749 #endif
750 }
751
752 //-----------------------------------------------------------------------------
753 void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
754 {
755   bool isUsePointSprites = (this->MarkerEnabled && this->MarkerType != VTK::MT_NONE) || 
756     this->BallEnabled;
757   if( isUsePointSprites )
758   {
759     if( this->ExtensionsInitialized != ES_Ok )
760       this->ExtensionsInitialized = this->InitExtensions();
761     this->InitPointSprites();
762     this->InitTextures();
763   }
764
765   if(!this->BallEnabled || this->ExtensionsInitialized != ES_Ok) {
766     MAPPER_SUPERCLASS::RenderPiece( ren, act );
767     if( isUsePointSprites )
768       this->CleanupPointSprites();
769     glBindTexture( GL_TEXTURE_2D, 0 );
770   } else {
771     vtkIdType numPts;
772     vtkPolyData *input= this->GetInput();
773
774     //
775     // make sure that we've been properly initialized
776     //
777     if (ren->GetRenderWindow()->CheckAbortStatus())
778       return;
779
780     if ( input == NULL )
781     {
782       vtkErrorMacro(<< "No input!");
783       return;
784     }
785     else
786     {
787       this->InvokeEvent(vtkCommand::StartEvent,NULL);
788       this->Update();
789       this->InvokeEvent(vtkCommand::EndEvent,NULL);
790       numPts = input->GetNumberOfPoints();
791     }
792
793     if (numPts == 0)
794     {
795       vtkDebugMacro(<< "No points!");
796       return;
797     }
798
799     // make sure our window is current
800     ren->GetRenderWindow()->MakeCurrent();
801
802
803     vglUseProgramObjectARB( this->PointProgram );
804
805 #ifndef VTK_OPENGL2
806     //
807     // if something has changed regenerate colors and display lists
808     // if required
809     //
810     int noAbort=1;
811     if ( this->GetMTime() > this->BuildTime ||
812          input->GetMTime() > this->BuildTime ||
813          act->GetProperty()->GetMTime() > this->BuildTime ||
814          ren->GetRenderWindow() != this->LastWindow)
815     {
816       // sets this->Colors as side effect
817       this->MapScalars( act->GetProperty()->GetOpacity() );
818
819       if (!this->ImmediateModeRendering &&
820           !this->GetGlobalImmediateModeRendering())
821       {
822         this->ReleaseGraphicsResources(ren->GetRenderWindow());
823         this->LastWindow = ren->GetRenderWindow();
824
825         // get a unique display list id
826         this->ListId = glGenLists(1);
827         glNewList(this->ListId,GL_COMPILE);
828
829         noAbort = this->Draw(ren,act);
830         glEndList();
831
832         // Time the actual drawing
833         this->Timer->StartTimer();
834         glCallList(this->ListId);
835         this->Timer->StopTimer();
836       }
837       else
838       {
839         this->ReleaseGraphicsResources(ren->GetRenderWindow());
840         this->LastWindow = ren->GetRenderWindow();
841       }
842       if (noAbort)
843         this->BuildTime.Modified();
844     }
845     // if nothing changed but we are using display lists, draw it
846     else
847     {
848       if (!this->ImmediateModeRendering &&
849           !this->GetGlobalImmediateModeRendering())
850       {
851         // Time the actual drawing
852         this->Timer->StartTimer();
853         glCallList(this->ListId);
854         this->Timer->StopTimer();
855       }
856     }
857
858     // if we are in immediate mode rendering we always
859     // want to draw the primitives here
860     if (this->ImmediateModeRendering ||
861         this->GetGlobalImmediateModeRendering())
862     {
863       // sets this->Colors as side effect
864       this->MapScalars( act->GetProperty()->GetOpacity() );
865
866       // Time the actual drawing
867       this->Timer->StartTimer();
868       this->Draw(ren,act);
869       this->Timer->StopTimer();
870     }
871
872     this->TimeToDraw = (float)this->Timer->GetElapsedTime();
873
874     // If the timer is not accurate enough, set it to a small
875     // time so that it is not zero
876     if ( this->TimeToDraw == 0.0 )
877       this->TimeToDraw = 0.0001;
878 #else
879     //this->RenderPieceStart(ren, act);
880     this->RenderPieceDraw(ren, act);
881     //    this->RenderEdges(ren,act);
882     //this->RenderPieceFinish(ren, act);
883 #endif    
884     vglUseProgramObjectARB( 0 );
885     this->CleanupPointSprites();
886     glBindTexture( GL_TEXTURE_2D, 0 );
887   }  
888 }
889
890 //-----------------------------------------------------------------------------
891 // Definition of structures and fuctions used in Draw() method
892 namespace VTK
893 {
894   //-----------------------------------------------------------------------------
895   struct TVertex
896   {
897     GLfloat r, g, b, a;
898     GLfloat vx, vy, vz;
899   };
900
901   //-----------------------------------------------------------------------------
902   struct TColorFunctorBase
903   {
904     double myAlpha;
905
906     TColorFunctorBase( vtkProperty* theProperty )
907     {
908       myAlpha = theProperty->GetOpacity();
909     }
910
911     virtual
912     void
913     get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
914   };
915
916   //-----------------------------------------------------------------------------
917   struct TPropertyColor : TColorFunctorBase
918   {
919     double myColor[3];
920
921     TPropertyColor( vtkProperty* theProperty ):
922       TColorFunctorBase( theProperty )
923     {
924       theProperty->GetColor( myColor );
925     }
926
927     virtual
928     void
929     get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
930     {
931       theVertex.r = myColor[0];
932       theVertex.g = myColor[1];
933       theVertex.b = myColor[2];
934       theVertex.a = myAlpha;
935     }
936   };
937
938   //-----------------------------------------------------------------------------
939   struct TColors2Color : TColorFunctorBase
940   {
941     vtkUnsignedCharArray* myColors;
942
943     TColors2Color( vtkProperty* theProperty,
944                    vtkUnsignedCharArray* theColors ):
945       TColorFunctorBase( theProperty ),
946       myColors( theColors )
947     {}
948
949     virtual
950     void
951     get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
952     {
953       vtkIdType aTupleId = GetTupleId( thePointId, theCellId );
954       unsigned char* aColor = myColors->GetPointer( aTupleId << 2 );
955
956       theVertex.r = int( aColor[0] ) / 255.0;
957       theVertex.g = int( aColor[1] ) / 255.0;
958       theVertex.b = int( aColor[2] ) / 255.0;
959       theVertex.a = myAlpha;
960     }
961
962     virtual
963     vtkIdType
964     GetTupleId( vtkIdType thePointId, vtkIdType theCellId ) = 0;
965   };
966
967   //-----------------------------------------------------------------------------
968   struct TPointColors2Color : TColors2Color
969   {
970     TPointColors2Color( vtkProperty* theProperty,
971                         vtkUnsignedCharArray* theColors ):
972       TColors2Color( theProperty, theColors )
973     {}
974
975     virtual
976     vtkIdType
977     GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
978     {
979       return thePointId;
980     }
981   };
982
983   //-----------------------------------------------------------------------------
984   struct TCellColors2Color : TColors2Color
985   {
986     TCellColors2Color( vtkProperty* theProperty,
987                        vtkUnsignedCharArray* theColors ):
988       TColors2Color( theProperty, theColors )
989     {}
990
991     virtual
992     vtkIdType
993     GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
994     {
995       return theCellId;
996     }
997   };
998
999   //-----------------------------------------------------------------------------
1000   template < class TCoordinates >
1001   void DrawPoints( TCoordinates* theStartPoints,
1002                    vtkCellArray* theCells,
1003                    TColorFunctorBase* theColorFunctor,
1004                    TVertex* theVertexArr,
1005                    vtkIdType &theCellId,
1006                    vtkIdType &theVertexId,
1007                    TBall* theBallArr,
1008                    vtkDataArray* theDiamArray,
1009                    double theBallScale )
1010   {
1011     vtkIdType* ptIds = theCells->GetPointer();
1012     vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
1013
1014     bool mapBalls = false; 
1015     if(theBallArr && theDiamArray) {
1016       mapBalls = true;
1017     }
1018
1019     while ( ptIds < endPtIds ) {
1020       vtkIdType nPts = *ptIds;
1021       ++ptIds;
1022
1023       while ( nPts > 0 ) {
1024         TVertex& aVertex = theVertexArr[ theVertexId ];
1025         vtkIdType aPointId = *ptIds;
1026
1027         TCoordinates* anOffsetPoints = theStartPoints + 3 * aPointId;
1028         aVertex.vx = anOffsetPoints[0];
1029         aVertex.vy = anOffsetPoints[1];
1030         aVertex.vz = anOffsetPoints[2];
1031
1032         theColorFunctor->get( aVertex, aPointId, theCellId );
1033
1034         ++theVertexId;
1035         ++ptIds; 
1036         --nPts; 
1037       }
1038       
1039       if(mapBalls){
1040         theBallArr[theCellId] = (TBall)theDiamArray->GetTuple(theCellId)[0]*theBallScale;
1041       }
1042
1043       ++theCellId;
1044     }
1045   }
1046
1047   //-----------------------------------------------------------------------------
1048   template < class TCoordinates >
1049   void DrawCellsPoints( vtkPolyData* theInput,
1050                         vtkPoints* thePoints,
1051                         TColorFunctorBase* theColorFunctor,
1052                         TVertex* theVertexArr,
1053                         TBall* theBallArr,
1054                         double theBallScale )
1055   {
1056     vtkIdType aCellId = 0, aVertexId = 0;
1057
1058     TCoordinates* aStartPoints = (TCoordinates*)thePoints->GetVoidPointer(0);
1059     vtkDataArray* aDiams = theInput->GetCellData() ? theInput->GetCellData()->GetScalars() : 0;    
1060
1061     if ( vtkCellArray* aCellArray = theInput->GetVerts() ) {
1062       DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
1063     }
1064   
1065     if ( vtkCellArray* aCellArray = theInput->GetLines() )
1066       DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
1067   
1068     if ( vtkCellArray* aCellArray = theInput->GetPolys() )
1069       DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale );
1070   
1071     if ( vtkCellArray* aCellArray = theInput->GetStrips() )
1072       DrawPoints( aStartPoints, aCellArray, theColorFunctor, theVertexArr, aCellId, aVertexId, theBallArr, aDiams, theBallScale ); 
1073   }
1074 } // namespace VTK
1075
1076 #ifndef VTK_OPENGL2
1077 //-----------------------------------------------------------------------------
1078 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
1079 {
1080   int noAbort = 1;
1081   if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled)
1082     return MAPPER_SUPERCLASS::Draw( ren, act );
1083
1084   InternalDraw( ren, act );
1085
1086   return noAbort;
1087 }
1088 #else
1089 //-----------------------------------------------------------------------------
1090 void VTKViewer_PolyDataMapper::RenderPieceDraw( vtkRenderer* ren, vtkActor* act ) {
1091   int noAbort = 1;
1092   if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled) {
1093     MAPPER_SUPERCLASS::RenderPieceDraw( ren, act );
1094     return;
1095   }
1096   InternalDraw( ren, act );
1097 }
1098 #endif
1099
1100 #ifdef VTK_OPENGL2
1101 #include <vtkCamera.h>
1102 #include <vtkOpenGLCamera.h>
1103 #include <vtkOpenGLActor.h>
1104 #include <vtkMatrix4x4.h>
1105 void SetUniformMatrix (const GLint   theLocation,
1106                        vtkMatrix4x4* theMatrix)
1107 {
1108   float data[16];
1109   for (int i = 0; i < 16; ++i)
1110   {
1111     data[i] = theMatrix->Element[i / 4][i % 4];
1112   }
1113
1114   vglUniformMatrix4fvARB(theLocation, 1, GL_FALSE, data);
1115 }
1116 #endif
1117
1118 void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
1119   vtkUnsignedCharArray* colors = NULL;
1120   vtkPolyData* input  = this->GetInput();
1121   vtkPoints* points;
1122   int cellScalars = 0;
1123   vtkProperty* prop = act->GetProperty();
1124
1125   points = input->GetPoints();
1126
1127   if ( this->Colors )
1128   {
1129     if(!this->BallEnabled) {
1130       colors = this->Colors;
1131       if ( (this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
1132             this->ScalarMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA ||
1133             !input->GetPointData()->GetScalars() )
1134            && this->ScalarMode != VTK_SCALAR_MODE_USE_POINT_FIELD_DATA )
1135         cellScalars = 1;
1136     }
1137   }
1138
1139   {
1140     vtkIdType aTotalConnectivitySize = 0;
1141     vtkIdType aNbCells = 0;
1142
1143     if ( vtkCellArray* aCellArray = input->GetVerts() ) {
1144       aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1145       aNbCells += aCellArray->GetNumberOfCells();
1146     }
1147
1148     if ( vtkCellArray* aCellArray = input->GetLines() ) {
1149       aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1150       aNbCells += aCellArray->GetNumberOfCells();
1151     }
1152
1153     if ( vtkCellArray* aCellArray = input->GetPolys() ) {
1154       aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1155       aNbCells += aCellArray->GetNumberOfCells();
1156     }
1157
1158     if ( vtkCellArray* aCellArray = input->GetStrips() ) {
1159       aTotalConnectivitySize += aCellArray->GetNumberOfConnectivityEntries() - aCellArray->GetNumberOfCells();
1160       aNbCells += aCellArray->GetNumberOfCells();
1161     }
1162
1163     if ( aTotalConnectivitySize > 0 ) {
1164       VTK::TVertex* aVertexArr = new VTK::TVertex[ aTotalConnectivitySize ];
1165       
1166       TBall* aBallArray = 0;
1167
1168       if(this->BallEnabled) {
1169         aBallArray = new TBall[aNbCells];
1170       }
1171
1172       int* aSize = this->ImageData->GetDimensions();
1173       //glPointSize( std::max( aSize[0], aSize[1] ) );
1174
1175       int aMode = 0; // to remove
1176       {
1177         VTK::TColorFunctorBase* aColorFunctor = NULL;
1178         if( colors && aMode != 1 ) {
1179           if ( cellScalars )
1180             aColorFunctor = new VTK::TCellColors2Color( prop, colors );
1181           else
1182             aColorFunctor = new VTK::TPointColors2Color( prop, colors );
1183         } else {
1184           aColorFunctor = new VTK::TPropertyColor( prop );
1185         }
1186         if ( points->GetDataType() == VTK_FLOAT )
1187           VTK::DrawCellsPoints< float >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
1188         else
1189           VTK::DrawCellsPoints< double >( input, points, aColorFunctor, aVertexArr, aBallArray, GetBallScale() );
1190
1191         delete aColorFunctor;
1192       }
1193
1194       if( this->ExtensionsInitialized == ES_Ok ) {
1195 #ifdef VTK_OPENGL2
1196         vglUseProgramObjectARB( this->PointProgram );
1197
1198         vtkOpenGLCamera *cam = (vtkOpenGLCamera *)(ren->GetActiveCamera());
1199         vtkMatrix4x4 *wcdc;
1200         vtkMatrix4x4 *wcvc;
1201         vtkMatrix3x3 *norms;
1202         vtkMatrix4x4 *vcdc;
1203         cam->GetKeyMatrices(ren,wcvc,norms,vcdc,wcdc);
1204         if (!act->GetIsIdentity())
1205         {
1206           vtkMatrix4x4 *mcwc;
1207           vtkMatrix3x3 *anorms;
1208           ((vtkOpenGLActor *)act)->GetKeyMatrices( mcwc, anorms );
1209           vtkMatrix4x4::Multiply4x4( mcwc, wcdc, this->TempMatrix4 );
1210           SetUniformMatrix( this->myLocations.ModelViewProjection, this->TempMatrix4 );
1211         }
1212         else
1213         {
1214           SetUniformMatrix( this->myLocations.ModelViewProjection, wcdc );
1215         }
1216
1217         vglUniform1iARB( this->myLocations.GeneralPointSize, std::max( aSize[0], aSize[1] ) );
1218
1219         GLuint aBufferObjectID, aDiamsID = 0;
1220
1221         vglBindVertexArrayARB( this->VertexArrayObject );
1222         vglGenBuffersARB( 1, &aBufferObjectID );
1223         vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1224         
1225         int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
1226         vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
1227         
1228         delete [] aVertexArr;
1229         
1230
1231         GLint colorAttrib  = vglGetAttribLocationARB( this->PointProgram, "Color" );
1232         GLint vertexAttrib = vglGetAttribLocationARB( this->PointProgram, "Vertex" );
1233         GLint diamAttrib   = vglGetAttribLocationARB( this->PointProgram, "Diameter" );
1234
1235         GLsizei vertexSize = sizeof(VTK::TVertex);
1236
1237         vglVertexAttribPointerARB( colorAttrib, 4, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)0 );
1238         vglEnableVertexAttribArrayARB( colorAttrib );
1239
1240         vglVertexAttribPointerARB( vertexAttrib, 3, GL_FLOAT, GL_FALSE, vertexSize, (const GLvoid*)(sizeof(GLfloat) * 4) );
1241         vglEnableVertexAttribArrayARB( vertexAttrib );
1242
1243        if(this->BallEnabled) {
1244          // Don't use uniform variable.
1245          vglUniform1iARB( this->myLocations.GeneralPointSize, -1 );
1246          vglGenBuffersARB( 1, &aDiamsID);
1247          vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
1248
1249          int aDiamsSize = sizeof(TBall)*aNbCells;
1250          vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
1251
1252          delete [] aBallArray;
1253
1254          vglVertexAttribPointerARB( diamAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0 );
1255          vglEnableVertexAttribArrayARB( diamAttrib );
1256        }
1257
1258        glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1259
1260        if( this->BallEnabled ) {
1261          vglDisableVertexAttribArrayARB( diamAttrib );
1262          vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1263          vglDeleteBuffersARB( 1, &aDiamsID );
1264        }
1265
1266        vglDisableVertexAttribArrayARB( colorAttrib );
1267        vglDisableVertexAttribArrayARB( vertexAttrib );
1268        vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1269        vglDeleteBuffersARB( 1, &aBufferObjectID );
1270        vglBindVertexArrayARB( 0 );
1271
1272        vglUseProgramObjectARB( 0 );
1273 #else
1274        GLuint aBufferObjectID, aDiamsID = 0;
1275        GLint attribute_diams = -1;
1276
1277        vglGenBuffersARB( 1, &aBufferObjectID );
1278        vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1279
1280        int anArrayObjectSize = sizeof( VTK::TVertex ) * aTotalConnectivitySize;
1281        vglBufferDataARB( GL_ARRAY_BUFFER_ARB, anArrayObjectSize, aVertexArr, GL_STATIC_DRAW_ARB );
1282
1283        delete [] aVertexArr;
1284
1285        vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1286        vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
1287         
1288         glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), (void*)0 );
1289         glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), (void*)(4*sizeof(GLfloat)) );
1290         
1291         glEnableClientState( GL_VERTEX_ARRAY );
1292         glEnableClientState( GL_COLOR_ARRAY );
1293
1294         if(this->BallEnabled) {
1295           vglGenBuffersARB( 2, &aDiamsID);
1296           vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID);
1297
1298           int aDiamsSize = sizeof(TBall)*aNbCells;
1299           vglBufferDataARB( GL_ARRAY_BUFFER_ARB, aDiamsSize, aBallArray, GL_STATIC_DRAW_ARB);
1300
1301           delete [] aBallArray;
1302           vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
1303           vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID );
1304
1305           attribute_diams = vglGetAttribLocationARB(this->PointProgram, "diameter");
1306           vglEnableVertexAttribArrayARB(attribute_diams);
1307           vglBindBufferARB(GL_ARRAY_BUFFER_ARB, aDiamsID);
1308           vglVertexAttribPointerARB(
1309                                     attribute_diams,   // attribute
1310                                     1,                 // number of elements per vertex, here (diameter)
1311                                     GL_FLOAT,          // the type of each element
1312                                     GL_FALSE,          // take our values as-is
1313                                     0,                 // no extra data between each position
1314                                     0                  // offset of first element
1315                                     );
1316         }
1317
1318         glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1319         
1320         glDisableClientState( GL_COLOR_ARRAY );
1321         glDisableClientState( GL_VERTEX_ARRAY );       
1322         vglDeleteBuffersARB( 1, &aBufferObjectID );
1323
1324         if(this->BallEnabled) {
1325           vglDisableVertexAttribArrayARB(attribute_diams);
1326           vglDeleteBuffersARB( 2, &aDiamsID );
1327         }
1328
1329 #endif
1330           } else { // there are no extensions
1331         glColorPointer( 4, GL_FLOAT, sizeof(VTK::TVertex), aVertexArr );
1332         glVertexPointer( 3, GL_FLOAT, sizeof(VTK::TVertex), 
1333                          (void*)((GLfloat*)((void*)(aVertexArr)) + 4));
1334
1335         glEnableClientState( GL_VERTEX_ARRAY );
1336         glEnableClientState( GL_COLOR_ARRAY );
1337         
1338         glDrawArrays( GL_POINTS, 0, aTotalConnectivitySize );
1339         
1340         glDisableClientState( GL_COLOR_ARRAY );
1341         glDisableClientState( GL_VERTEX_ARRAY );
1342
1343         delete [] aVertexArr;
1344       }
1345     }
1346   }
1347
1348   this->UpdateProgress(1.0);
1349 }