Salome HOME
Start potring to the VTK OpenGL2 backend.
authorrnv <rnv@opencascade.com>
Mon, 8 Aug 2016 13:34:51 +0000 (16:34 +0300)
committerrnv <rnv@opencascade.com>
Mon, 8 Aug 2016 13:34:51 +0000 (16:34 +0300)
CMakeLists.txt
SalomeGUIConfig.cmake.in
src/OpenGLUtils/OpenGLUtils_FrameBuffer.cxx
src/VTKViewer/VTKViewer_OpenGLRenderer.cxx
src/VTKViewer/VTKViewer_PolyDataMapper.cxx
src/VTKViewer/VTKViewer_PolyDataMapper.h
src/VTKViewer/VTKViewer_Texture.cxx
src/VTKViewer/resources/Vertex_Program_ARB.txt

index f2ac6fa11ee2623df8f8702512bc7c0bead5a3ce..d15f636ef9ac49864fc5ad313a3464140d17fa6b 100755 (executable)
@@ -204,10 +204,15 @@ IF(SALOME_USE_VTKVIEWER)
   # Required components are listed in the FindSalomeVTK.cmake file: 
   FIND_PACKAGE(SalomeVTK) 
   SALOME_LOG_OPTIONAL_PACKAGE(VTK SALOME_USE_VTKVIEWER)
+  IF(${VTK_RENDERING_BACKEND} STREQUAL "OpenGL2")
+    ADD_DEFINITIONS("-DVTK_OPENGL2")
+    SET(SALOME_GUI_USE_OPENGL2 TRUE)
+  ENDIF()
 ELSE()
   ADD_DEFINITIONS("-DDISABLE_VTKVIEWER")
 ENDIF()
 
+
 # - Plot2d viewer: Qwt
 IF(SALOME_USE_PLOT2DVIEWER)
   FIND_PACKAGE(SalomeQwt)
index 0c9f163cd6700c7302ae58b3741c205a4865a2b4..74af926403192301270c2d3906ea3719b9d352fb 100644 (file)
@@ -65,6 +65,7 @@ SET(SALOME_USE_PYCONSOLE      @SALOME_USE_PYCONSOLE@)
 SET(SALOME_USE_SALOMEOBJECT   @SALOME_USE_SALOMEOBJECT@)
 SET(SALOME_USE_SINGLE_DESKTOP @SALOME_USE_SINGLE_DESKTOP@)
 SET(SALOME_GUI_USE_OBSERVERS  @SALOME_GUI_USE_OBSERVERS@)
+SET(SALOME_GUI_USE_OPENGL2    @SALOME_GUI_USE_OPENGL2@)
 
 IF(SALOME_GUI_LIGHT_ONLY)
   LIST(APPEND GUI_DEFINITIONS "-DGUI_DISABLE_CORBA")
@@ -84,6 +85,10 @@ ENDIF()
 
 IF(NOT SALOME_USE_VTKVIEWER)
   LIST(APPEND GUI_DEFINITIONS "-DDISABLE_VTKVIEWER")
+ELSE()
+  IF(SALOME_GUI_USE_OPENGL2)
+    LIST(APPEND GUI_DEFINITIONS "-DVTK_OPENGL2")
+  ENDIF()
 ENDIF()
 IF(NOT SALOME_USE_PLOT2DVIEWER)
   LIST(APPEND GUI_DEFINITIONS "-DDISABLE_PLOT2DVIEWER")
index 95c68c1df37299808a625193e9086ec375d9485f..d0f822174e4680f80afa7096117bb1796cafc0db 100755 (executable)
 //  File   : OpenGLUtils_FrameBuffer.cxx
 //  Module : SALOME
 //
+#ifdef VTK_OPENGL2
+#define GL_GLEXT_PROTOTYPES
+#endif
+
 #include "OpenGLUtils_FrameBuffer.h"
 
 #include <utilities.h>
@@ -128,8 +132,21 @@ OpenGLUtils_FrameBuffer::~OpenGLUtils_FrameBuffer()
 
 bool OpenGLUtils_FrameBuffer::init( const GLsizei& xSize, const GLsizei& ySize )
 {
+#ifdef VTK_OPENGL2
+  int n = 0;
+  std::ostringstream strm;
+  glGetIntegerv(GL_NUM_EXTENSIONS, &n);
+  for (int i = 0; i < n; i++)
+    {
+      const char *exti = (const char *)glGetStringi(GL_EXTENSIONS, i);
+      strm<< exti <<" ";
+    }
+  std::string s = strm.str();
+  const char* ext = s.c_str();
+#else  
   char* ext = (char*)glGetString( GL_EXTENSIONS );
-  if( !IsEXTInitialized ||
+#endif  
+  if( !IsEXTInitialized || !ext ||
       strstr( ext, "GL_EXT_framebuffer_object" ) == NULL )
   {
     MESSAGE( "Initializing OpenGL FrameBuffer extension failed" );
index 0a681e44349dc7147cf81ae1392c0a8b03ad4ebd..117a2939b2d27b2bb878c4de2eadddfeba35afc9 100644 (file)
 #include <vtkOpenGLLight.h>
 #include <vtkOpenGLProperty.h>
 #include <vtkRenderWindow.h>
+#ifndef VTK_OPENGL2
 #include <vtkOpenGLExtensionManager.h>
 #include <vtkgl.h> // vtkgl namespace
+#endif
 #include <vtkImageImport.h>
 #include <vtkPNGWriter.h>
 #include <vtkOpenGLTexture.h>
index af561e0f3def5c7733aff6f856bbac952c21de47..9eb01153080ce957d2cb42a73b3519ee8b936d19 100644 (file)
@@ -16,6 +16,9 @@
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+#ifdef VTK_OPENGL2
+#define GL_GLEXT_PROTOTYPES
+#endif
 
 #include "VTKViewer_PolyDataMapper.h"
 #include "VTKViewer_MarkerUtils.h"
@@ -467,8 +470,21 @@ int VTKViewer_PolyDataMapper::GetMarkerTexture()
 //-----------------------------------------------------------------------------
 int VTKViewer_PolyDataMapper::InitExtensions()
 {
-  char* ext = (char*)glGetString( GL_EXTENSIONS );
-  if( !IsBufferExtensionsInitialized ||
+#ifdef VTK_OPENGL2
+  int n = 0;
+  std::ostringstream strm;
+  glGetIntegerv(GL_NUM_EXTENSIONS, &n);
+  for (int i = 0; i < n; i++)
+    {
+      const char *exti = (const char *)glGetStringi(GL_EXTENSIONS, i);
+      strm<< exti <<" ";
+    }
+  std::string s = strm.str();
+  const char* ext = s.c_str();
+#else  
+  const char* ext = (const char*)glGetString( GL_EXTENSIONS );
+#endif  
+  if( !IsBufferExtensionsInitialized || !ext ||
       strstr( ext, "GL_ARB_point_sprite" ) == NULL ||
       strstr( ext, "GL_ARB_vertex_buffer_object" ) == NULL ||
       strstr( ext, "GL_ARB_shader_objects") == NULL )
@@ -598,6 +614,7 @@ void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
 
     vglUseProgramObjectARB( this->VertexProgram );
 
+#ifndef VTK_OPENGL2
     //
     // if something has changed regenerate colors and display lists
     // if required
@@ -670,7 +687,12 @@ void VTKViewer_PolyDataMapper::RenderPiece( vtkRenderer* ren, vtkActor* act )
     // time so that it is not zero
     if ( this->TimeToDraw == 0.0 )
       this->TimeToDraw = 0.0001;
-
+#else
+    //this->RenderPieceStart(ren, act);
+    this->RenderPieceDraw(ren, act);
+    //    this->RenderEdges(ren,act);
+    //this->RenderPieceFinish(ren, act);
+#endif    
     vglUseProgramObjectARB( 0 );
     this->CleanupPointSprites();
     glBindTexture( GL_TEXTURE_2D, 0 );
@@ -863,16 +885,34 @@ namespace VTK
   }
 } // namespace VTK
 
+#ifndef VTK_OPENGL2
 //-----------------------------------------------------------------------------
 int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
-{  
+{
+  int noAbort = 1;
   if( (!this->MarkerEnabled || this->MarkerType == VTK::MT_NONE || !this->ImageData.GetPointer()) && !this->BallEnabled)
     return MAPPER_SUPERCLASS::Draw( ren, act );
+  
+  void InternalDraw( ren, act );
+    
+  return noAbort;
+}
+#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;
+  }
+  InternalDraw( ren, act );
+}
+#endif
 
+void VTKViewer_PolyDataMapper::InternalDraw(vtkRenderer* ren, vtkActor* act ) {
   vtkUnsignedCharArray* colors = NULL;
   vtkPolyData* input  = this->GetInput();
   vtkPoints* points;
-  int noAbort = 1;
   int cellScalars = 0;
   vtkProperty* prop = act->GetProperty();
 
@@ -1019,5 +1059,4 @@ int VTKViewer_PolyDataMapper::Draw( vtkRenderer* ren, vtkActor* act )
   }
 
   this->UpdateProgress(1.0);
-  return noAbort;
 }
index f945ed59e69581235375a20dc878af2acde71bdb..20aaca7082e98d3121c315baca9236862cc1fa63 100644 (file)
@@ -97,9 +97,12 @@ public:
 
   //! Implement superclass render method.
   virtual void RenderPiece( vtkRenderer*, vtkActor* );
-
   //! Draw method for OpenGL.
+#ifndef VTK_OPENGL2
   virtual int Draw( vtkRenderer*, vtkActor* );
+#else
+  virtual void RenderPieceDraw( vtkRenderer*, vtkActor* );
+#endif    
 
 protected:
   VTKViewer_PolyDataMapper();
@@ -120,6 +123,8 @@ protected:
   //! Initializing of the Vertex Shader.
   void              InitShader();
 
+  void              InternalDraw(vtkRenderer*, vtkActor*); 
+
 private:
   int               ExtensionsInitialized;
 
index 2a54a03ee893b4739657e55f9b3e798981e12a50..7b20cd5e167f49139a8eafb3db0d57712d8ab828 100644 (file)
 #include "vtkOpenGLRenderer.h"
 #include "vtkPointData.h"
 #include "vtkRenderWindow.h"
+#ifndef VTK_OPENGL2
 #include "vtkOpenGLExtensionManager.h"
+#include "vtkgl.h" // vtkgl namespace
+#else
+#include "vtkTextureObject.h"
+#include "vtkOpenGLError.h"
+#endif
 #include "vtkOpenGLRenderWindow.h"
 #include "vtkTransform.h"
 #include "vtkPixelBufferObject.h"
 #include "vtkOpenGL.h"
-#include "vtkgl.h" // vtkgl namespace
 #include <vtkObjectFactory.h>
 
 vtkStandardNewMacro(VTKViewer_Texture);
@@ -56,6 +61,7 @@ VTKViewer_Texture::~VTKViewer_Texture()
 // Implement base class method.
 void VTKViewer_Texture::Load(vtkRenderer *ren)
 {
+#ifndef VTK_OPENGL2  
   GLenum format = GL_LUMINANCE;
   vtkImageData *input = this->GetInput();
 
@@ -464,6 +470,204 @@ void VTKViewer_Texture::Load(vtkRenderer *ren)
     vtkgl::Uniform1i(uTexture,0); // active texture 0
     }
     */
+#else
+    if (!this->ExternalTextureObject)
+    {
+    vtkImageData *input = this->GetInput();
+
+    // Need to reload the texture.
+    // There used to be a check on the render window's mtime, but
+    // this is too broad of a check (e.g. it would cause all textures
+    // to load when only the desired update rate changed).
+    // If a better check is required, check something more specific,
+    // like the graphics context.
+    vtkOpenGLRenderWindow* renWin =
+      static_cast<vtkOpenGLRenderWindow*>(ren->GetRenderWindow());
+
+    // has something changed so that we need to rebuild the texture?
+    if (this->GetMTime() > this->LoadTime.GetMTime() ||
+        input->GetMTime() > this->LoadTime.GetMTime() ||
+        (this->GetLookupTable() && this->GetLookupTable()->GetMTime () >
+         this->LoadTime.GetMTime()) ||
+         renWin != this->RenderWindow.GetPointer() ||
+         renWin->GetContextCreationTime() > this->LoadTime)
+      {
+      int size[3];
+      unsigned char *dataPtr;
+      unsigned char *resultData = 0;
+      int xsize, ysize;
+
+      this->RenderWindow = renWin;
+      if (this->TextureObject == 0)
+        {
+        this->TextureObject = vtkTextureObject::New();
+        }
+      this->TextureObject->ResetFormatAndType();
+      this->TextureObject->SetContext(renWin);
+
+      // Get the scalars the user choose to color with.
+      vtkDataArray* scalars = this->GetInputArrayToProcess(0, input);
+
+      // make sure scalars are non null
+      if (!scalars)
+        {
+        vtkErrorMacro(<< "No scalar values found for texture input!");
+        return;
+        }
+
+      // get some info
+      input->GetDimensions(size);
+
+      if (input->GetNumberOfCells() == scalars->GetNumberOfTuples())
+        {
+        // we are using cell scalars. Adjust image size for cells.
+        for (int kk = 0; kk < 3; kk++)
+          {
+          if (size[kk]>1)
+            {
+            size[kk]--;
+            }
+          }
+        }
+
+      int bytesPerPixel = scalars->GetNumberOfComponents();
+
+      // make sure using unsigned char data of color scalars type
+      if (this->IsDepthTexture != 1 &&
+        (this->MapColorScalarsThroughLookupTable ||
+         scalars->GetDataType() != VTK_UNSIGNED_CHAR ))
+        {
+        dataPtr = this->MapScalarsToColors (scalars);
+        bytesPerPixel = 4;
+        }
+      else
+        {
+        dataPtr = static_cast<vtkUnsignedCharArray *>(scalars)->GetPointer(0);
+        }
+
+      // we only support 2d texture maps right now
+      // so one of the three sizes must be 1, but it
+      // could be any of them, so lets find it
+      if (size[0] == 1)
+        {
+        xsize = size[1]; ysize = size[2];
+        }
+      else
+        {
+        xsize = size[0];
+        if (size[1] == 1)
+          {
+          ysize = size[2];
+          }
+        else
+          {
+          ysize = size[1];
+          if (size[2] != 1)
+            {
+            vtkErrorMacro(<< "3D texture maps currently are not supported!");
+            return;
+            }
+          }
+        }
+
+      // -- decide whether the texture needs to be resampled --
+      GLint maxDimGL;
+      glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxDimGL);
+      vtkOpenGLCheckErrorMacro("failed at glGetIntegerv");
+      // if larger than permitted by the graphics library then must resample
+      bool resampleNeeded = xsize > maxDimGL || ysize > maxDimGL;
+      if(resampleNeeded)
+        {
+        vtkDebugMacro( "Texture too big for gl, maximum is " << maxDimGL);
+        }
+
+      if (resampleNeeded)
+        {
+        vtkDebugMacro(<< "Resampling texture to power of two for OpenGL");
+        resultData = this->ResampleToPowerOfTwo(xsize, ysize, dataPtr,
+                                                bytesPerPixel);
+        }
+
+      if (!resultData)
+        {
+        resultData = dataPtr;
+        }
+
+      // create the texture
+      if (this->IsDepthTexture)
+        {
+        this->TextureObject->CreateDepthFromRaw(
+          xsize, ysize, vtkTextureObject::Float32, scalars->GetDataType(), resultData);
+        }
+      else
+        {
+        this->TextureObject->Create2DFromRaw(
+          xsize, ysize, bytesPerPixel, VTK_UNSIGNED_CHAR, resultData);
+        }
+      myWidth = xsize;
+      myHeight = ysize;
+      // activate a free texture unit for this texture
+      this->TextureObject->Activate();
+
+      // update parameters
+      if (this->Interpolate)
+        {
+        this->TextureObject->SetMinificationFilter(vtkTextureObject::Linear);
+        this->TextureObject->SetMagnificationFilter(vtkTextureObject::Linear);
+        }
+      else
+        {
+        this->TextureObject->SetMinificationFilter(vtkTextureObject::Nearest);
+        this->TextureObject->SetMagnificationFilter(vtkTextureObject::Nearest);
+        }
+      if (this->Repeat)
+        {
+        this->TextureObject->SetWrapS(vtkTextureObject::Repeat);
+        this->TextureObject->SetWrapT(vtkTextureObject::Repeat);
+        this->TextureObject->SetWrapR(vtkTextureObject::Repeat);
+        }
+      else
+        {
+        this->TextureObject->SetWrapS(vtkTextureObject::ClampToEdge);
+        this->TextureObject->SetWrapT(vtkTextureObject::ClampToEdge);
+        this->TextureObject->SetWrapR(vtkTextureObject::ClampToEdge);
+        }
+
+      // modify the load time to the current time
+      this->LoadTime.Modified();
+
+      // free memory
+      if (resultData != dataPtr)
+        {
+        delete [] resultData;
+        resultData = 0;
+        }
+      }
+    }
+  else
+    {
+    vtkOpenGLRenderWindow* renWin =
+      static_cast<vtkOpenGLRenderWindow*>(ren->GetRenderWindow());
+
+      // has something changed so that we need to rebuild the texture?
+      if (this->GetMTime() > this->LoadTime.GetMTime() ||
+         renWin != this->RenderWindow.GetPointer() ||
+         renWin->GetContextCreationTime() > this->LoadTime)
+        {
+        this->RenderWindow = renWin;
+        this->TextureObject->SetContext(renWin);
+        }
+    }        
+  // activate a free texture unit for this texture
+  this->TextureObject->Activate();
+
+  if (this->PremultipliedAlpha)
+    {
+    // make the blend function correct for textures premultiplied by alpha.
+    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+    }
+  vtkOpenGLCheckErrorMacro("failed after Load");
+#endif  
 }
 
 void VTKViewer_Texture::Initialize(vtkRenderer * vtkNotUsed(ren))
index 46d01a88f968ebc79e5880cdaf13026d3cb1828b..10eb1b536431447a91e81f151b52d6d46bb393aa 100644 (file)
@@ -4,5 +4,5 @@ void main()
 {
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   gl_FrontColor = gl_Color;
-  gl_PointSize = 1400 * gl_ProjectionMatrix[1].y * diameter;
+  gl_PointSize = 1400.0 * gl_ProjectionMatrix[1].y * diameter;
 }