Salome HOME
updated copyright message
[modules/gui.git] / src / VTKViewer / VTKViewer_PolyDataMapper.cxx
index f61d2af2165adb49115956402d39580767249308..d98944141878f194bd83d22005a662f631d8b13b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #include <vtkRenderWindow.h>
 #include <vtkCommand.h>
 #include <vtkCellData.h>
+#include <vtkUnsignedCharArray.h>
+#include <vtkIdTypeArray.h>
+
+#include <Basics_Utils.hxx>
 
 #ifndef WIN32
 # ifndef GLX_GLXEXT_LEGACY
 #  define GLX_GLXEXT_LEGACY
 # endif
+#ifdef __APPLE__
+#import <mach-o/dyld.h>
+#import <stdlib.h>
+#import <string.h>
+void * glXGetProcAddressARB (const GLubyte *name)
+
+{
+    NSSymbol symbol;
+    char *symbolName;
+    symbolName = (char *)malloc (strlen ((const char *)name) + 2); // 1
+    strcpy(symbolName + 1, (const char *)name); // 2
+    symbolName[0] = '_'; // 3
+    symbol = NULL;
+    if (NSIsSymbolNameDefined (symbolName)) // 4
+        symbol = NSLookupAndBindSymbol (symbolName);
+    free (symbolName); // 5
+    return symbol ? NSAddressOfSymbol (symbol) : NULL; // 6
+}
+#else
 # include <GL/glx.h>
+#endif
 # include <dlfcn.h>
 #else
 # include <wingdi.h>
 #endif
 
 #ifndef VTK_IMPLEMENT_MESA_CXX
-vtkStandardNewMacro(VTKViewer_PolyDataMapper);
+vtkStandardNewMacro(VTKViewer_PolyDataMapper)
 #endif
 
 // some definitions for what the polydata has in it
@@ -78,6 +102,21 @@ typedef GLfloat TBall;
 #define BallTextureId 0 
 
 
+
+void MessageCallback( GLenum /*source*/,
+                      GLenum type,
+                      GLuint /*id*/,
+                      GLenum severity,
+                      GLsizei /*length*/,
+                      const GLcharARB* message,
+                      const void* /*userParam*/ )
+{
+  fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
+           ( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
+            type, severity, message );
+}
+
+
 //-----------------------------------------------------------------------------
 VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
 {
@@ -100,6 +139,11 @@ VTKViewer_PolyDataMapper::VTKViewer_PolyDataMapper()
 #endif
 
   this->OpenGLHelper.Init();
+
+  // For debug purposes only
+  // glEnable              ( GL_DEBUG_OUTPUT );
+  // glDebugMessageCallback( (GLDEBUGPROC) MessageCallback, 0 );
+
 }
 
 //-----------------------------------------------------------------------------
@@ -117,25 +161,38 @@ VTKViewer_PolyDataMapper::~VTKViewer_PolyDataMapper()
 int VTKViewer_PolyDataMapper::InitShader()
 {
 #ifdef VTK_OPENGL2
+#if defined(WIN32) && defined(UNICODE)
+       std::wstring wFilePath = std::wstring(_wgetenv(L"GUI_ROOT_DIR")) + L"/share/salome/resources/gui/Point";
+       std::string filePath = Kernel_Utils::utf8_encode_s(wFilePath.c_str());
+#else
   std::string filePath = std::string( getenv( "GUI_ROOT_DIR") ) + "/share/salome/resources/gui/Point";
+#endif
   if( !this->OpenGLHelper.CreateShaderProgram(filePath, this->PointProgram, this->VertexShader, this->FragmentShader) )
     return false;
 
   // Get uniform locations.
+  GLint current_program;
+  glGetIntegerv( GL_CURRENT_PROGRAM, &current_program );
   this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
 
   this->myLocations.ModelViewProjection = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uModelViewProjectionMatrix" );
+  this->myLocations.Projection          = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uProjectionMatrix" );
   this->myLocations.GeneralPointSize    = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uGeneralPointSize" );
   this->myLocations.PointSprite         = this->OpenGLHelper.vglGetUniformLocationARB( this->PointProgram, "uPointSprite" );
 
-  this->OpenGLHelper.vglUseProgramObjectARB( 0 );
+  this->OpenGLHelper.vglUseProgramObjectARB( current_program );
 
   this->OpenGLHelper.vglGenVertexArraysARB(1, &this->VertexArrayObject);
+#else
+#if defined(WIN32) && defined(UNICODE)
+       std::wstring wFilePath = std::wstring(_wgetenv( L"GUI_ROOT_DIR" ) ) + L"/share/salome/resources/gui/Vertex_Program_ARB.txt";
+       std::string fileName = Kernel_Utils::utf8_encode( wFilePath.c_str() );
 #else
   std::string fileName = std::string( getenv( "GUI_ROOT_DIR") ) +
                          "/share/salome/resources/gui/Vertex_Program_ARB.txt";
+#endif
 
-  char* shader = readFromFile( fileName );
+  char* shader = GUI_OPENGL::readFromFile( fileName );
 
   GLhandleARB VertexShader = this->OpenGLHelper.vglCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
   this->OpenGLHelper.vglShaderSourceARB( VertexShader, 1, (const GLcharARB**)&shader, NULL );
@@ -244,7 +301,7 @@ void VTKViewer_PolyDataMapper::SetBallEnabled( bool theBallEnabled )
       
       QString anAlphaTexture = getenv( "GUI_ROOT_DIR" );
       anAlphaTexture.append( "/share/salome/resources/gui/sprite_alpha.vti" );
-      vtkSmartPointer<vtkImageData> aTextureValue = VTK::MakeTexture( aMainTexture.toLatin1().constData(), anAlphaTexture.toLatin1().constData() );
+      vtkSmartPointer<vtkImageData> aTextureValue = VTK::MakeTexture( aMainTexture.toUtf8().constData(), anAlphaTexture.toUtf8().constData() );
       this->SpecialTextures[BallTextureId] = aTextureValue;
     }
     this->ImageData = this->SpecialTextures[BallTextureId];
@@ -340,7 +397,7 @@ int VTKViewer_PolyDataMapper::InitExtensions()
   glGetIntegerv(GL_NUM_EXTENSIONS, &n);
   for (int i = 0; i < n; i++)
     {
-      const char *exti = (const char *)glGetStringi(GL_EXTENSIONS, i);
+      const char *exti = (const char *)this->OpenGLHelper.vglGetStringiARB(GL_EXTENSIONS, i);
       strm<< exti <<" ";
     }
   std::string s = strm.str();
@@ -404,7 +461,7 @@ void VTKViewer_PolyDataMapper::InitTextures()
     glGenTextures( 1, &this->PointSpriteTexture );
   }
 #ifdef VTK_OPENGL2
-  glActiveTexture( GL_TEXTURE0 );
+  this->OpenGLHelper.vglActiveTextureARB( GL_TEXTURE0 );
 #endif
   glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
   glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
@@ -480,7 +537,8 @@ void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
     // make sure our window is current
     ren->GetRenderWindow()->MakeCurrent();
 
-
+    GLint current_program;
+    glGetIntegerv( GL_CURRENT_PROGRAM, &current_program );
     this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
 
 #ifndef VTK_OPENGL2
@@ -562,7 +620,7 @@ void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
     //    this->RenderEdges(ren,act);
     //this->RenderPieceFinish(ren, act);
 #endif    
-    this->OpenGLHelper.vglUseProgramObjectARB( 0 );
+    this->OpenGLHelper.vglUseProgramObjectARB( current_program );
     this->CleanupPointSprites();
     glBindTexture( GL_TEXTURE_2D, 0 );
   }  
@@ -592,6 +650,8 @@ namespace VTK
     virtual
     void
     get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId ) = 0;
+
+    virtual ~TColorFunctorBase() {}
   };
 
   //-----------------------------------------------------------------------------
@@ -607,7 +667,7 @@ namespace VTK
 
     virtual
     void
-    get( TVertex& theVertex, vtkIdType thePointId, vtkIdType theCellId )
+    get( TVertex& theVertex, vtkIdType /*thePointId*/, vtkIdType /*theCellId*/ )
     {
       theVertex.r = myColor[0];
       theVertex.g = myColor[1];
@@ -655,7 +715,7 @@ namespace VTK
 
     virtual
     vtkIdType
-    GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
+    GetTupleId( vtkIdType thePointId, vtkIdType /*theCellId*/ )
     {
       return thePointId;
     }
@@ -671,7 +731,7 @@ namespace VTK
 
     virtual
     vtkIdType
-    GetTupleId( vtkIdType thePointId, vtkIdType theCellId )
+    GetTupleId( vtkIdType /*thePointId*/, vtkIdType theCellId )
     {
       return theCellId;
     }
@@ -689,7 +749,7 @@ namespace VTK
                    vtkDataArray* theDiamArray,
                    double theBallScale )
   {
-    vtkIdType* ptIds = theCells->GetPointer();
+    vtkIdType* ptIds = theCells->GetData()->GetPointer(0);
     vtkIdType* endPtIds = ptIds + theCells->GetNumberOfConnectivityEntries();
 
     bool mapBalls = false; 
@@ -769,7 +829,6 @@ int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
 #else
 //-----------------------------------------------------------------------------
 void VTKViewer_PolyDataMapper::RenderPieceDraw( vtkRenderer* ren, vtkActor* act ) {
-  int noAbort = 1;
   if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled) {
     MAPPER_SUPERCLASS::RenderPieceDraw( ren, act );
     return;
@@ -839,7 +898,6 @@ void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
       }
 
       int* aSize = this->ImageData->GetDimensions();
-      //glPointSize( std::max( aSize[0], aSize[1] ) );
 
       int aMode = 0; // to remove
       {
@@ -862,6 +920,8 @@ void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
 
       if( this->ExtensionsInitialized == ES_Ok ) {
 #ifdef VTK_OPENGL2
+       GLint current_program;
+       glGetIntegerv( GL_CURRENT_PROGRAM, &current_program );
         this->OpenGLHelper.vglUseProgramObjectARB( this->PointProgram );
 
         vtkOpenGLCamera *cam = (vtkOpenGLCamera *)(ren->GetActiveCamera());
@@ -883,6 +943,7 @@ void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
         {
                this->OpenGLHelper.SetUniformMatrix( this->myLocations.ModelViewProjection, wcdc );
         }
+        this->OpenGLHelper.SetUniformMatrix( this->myLocations.Projection, vcdc );
 
         this->OpenGLHelper.vglUniform1iARB( this->myLocations.GeneralPointSize, std::max( aSize[0], aSize[1] ) );
 
@@ -939,11 +1000,11 @@ void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
        this->OpenGLHelper.vglDeleteBuffersARB( 1, &aBufferObjectID );
        this->OpenGLHelper.vglBindVertexArrayARB( 0 );
 
-       this->OpenGLHelper.vglUseProgramObjectARB( 0 );
+       this->OpenGLHelper.vglUseProgramObjectARB( current_program );
 #else
        GLuint aBufferObjectID, aDiamsID = 0;
        GLint attribute_diams = -1;
-
+       glPointSize( std::max( aSize[0], aSize[1] ) );
        this->OpenGLHelper.vglGenBuffersARB( 1, &aBufferObjectID );
        this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aBufferObjectID );
 
@@ -972,7 +1033,7 @@ void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
          this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
          this->OpenGLHelper.vglBindBufferARB( GL_ARRAY_BUFFER_ARB, aDiamsID );
 
-         attribute_diams = vglGetAttribLocationARB(this->PointProgram, "diameter");
+         attribute_diams = this->OpenGLHelper.vglGetAttribLocationARB(this->PointProgram, "diameter");
          this->OpenGLHelper.vglEnableVertexAttribArrayARB(attribute_diams);
          this->OpenGLHelper.vglBindBufferARB(GL_ARRAY_BUFFER_ARB, aDiamsID);
          this->OpenGLHelper.vglVertexAttribPointerARB(