From: rnv Date: Mon, 8 Aug 2016 13:34:51 +0000 (+0300) Subject: Start potring to the VTK OpenGL2 backend. X-Git-Tag: V8_1_0rc1~8^2~10 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=3b746db8acf33b76223afdc0a607d4dcc01b53a2;p=modules%2Fgui.git Start potring to the VTK OpenGL2 backend. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index f2ac6fa11..d15f636ef 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/SalomeGUIConfig.cmake.in b/SalomeGUIConfig.cmake.in index 0c9f163cd..74af92640 100644 --- a/SalomeGUIConfig.cmake.in +++ b/SalomeGUIConfig.cmake.in @@ -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") diff --git a/src/OpenGLUtils/OpenGLUtils_FrameBuffer.cxx b/src/OpenGLUtils/OpenGLUtils_FrameBuffer.cxx index 95c68c1df..d0f822174 100755 --- a/src/OpenGLUtils/OpenGLUtils_FrameBuffer.cxx +++ b/src/OpenGLUtils/OpenGLUtils_FrameBuffer.cxx @@ -20,6 +20,10 @@ // File : OpenGLUtils_FrameBuffer.cxx // Module : SALOME // +#ifdef VTK_OPENGL2 +#define GL_GLEXT_PROTOTYPES +#endif + #include "OpenGLUtils_FrameBuffer.h" #include @@ -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" ); diff --git a/src/VTKViewer/VTKViewer_OpenGLRenderer.cxx b/src/VTKViewer/VTKViewer_OpenGLRenderer.cxx index 0a681e443..117a2939b 100644 --- a/src/VTKViewer/VTKViewer_OpenGLRenderer.cxx +++ b/src/VTKViewer/VTKViewer_OpenGLRenderer.cxx @@ -30,8 +30,10 @@ #include #include #include +#ifndef VTK_OPENGL2 #include #include // vtkgl namespace +#endif #include #include #include diff --git a/src/VTKViewer/VTKViewer_PolyDataMapper.cxx b/src/VTKViewer/VTKViewer_PolyDataMapper.cxx index af561e0f3..9eb011530 100644 --- a/src/VTKViewer/VTKViewer_PolyDataMapper.cxx +++ b/src/VTKViewer/VTKViewer_PolyDataMapper.cxx @@ -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; } diff --git a/src/VTKViewer/VTKViewer_PolyDataMapper.h b/src/VTKViewer/VTKViewer_PolyDataMapper.h index f945ed59e..20aaca708 100644 --- a/src/VTKViewer/VTKViewer_PolyDataMapper.h +++ b/src/VTKViewer/VTKViewer_PolyDataMapper.h @@ -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; diff --git a/src/VTKViewer/VTKViewer_Texture.cxx b/src/VTKViewer/VTKViewer_Texture.cxx index 2a54a03ee..7b20cd5e1 100644 --- a/src/VTKViewer/VTKViewer_Texture.cxx +++ b/src/VTKViewer/VTKViewer_Texture.cxx @@ -28,12 +28,17 @@ #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 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(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(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(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)) diff --git a/src/VTKViewer/resources/Vertex_Program_ARB.txt b/src/VTKViewer/resources/Vertex_Program_ARB.txt index 46d01a88f..10eb1b536 100644 --- a/src/VTKViewer/resources/Vertex_Program_ARB.txt +++ b/src/VTKViewer/resources/Vertex_Program_ARB.txt @@ -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; }