0026122: Visualization, TKOpenGl - clipping and capping is broken when ffp is disable...
[occt.git] / src / OpenGl / OpenGl_Context.cxx
index e03a187..2a960a2 100644 (file)
@@ -29,6 +29,7 @@
 #include <OpenGl_FrameBuffer.hxx>
 #include <OpenGl_Sampler.hxx>
 #include <OpenGl_ShaderManager.hxx>
+#include <Graphic3d_TransformUtils.hxx>
 
 #include <Message_Messenger.hxx>
 
   //
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
   #include <dlfcn.h>
+  #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+    //
+  #else
+    #include <OpenGL/OpenGL.h>
+  #endif
 #else
   #include <GL/glx.h> // glXGetProcAddress()
 #endif
@@ -56,8 +62,6 @@
   #endif
 #endif
 
-IMPLEMENT_STANDARD_HANDLE (OpenGl_Context, Standard_Transient)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient)
 
 namespace
 {
@@ -217,7 +221,7 @@ OpenGl_Context::~OpenGl_Context()
       myUnusedResources->RemoveFirst();
     }
   }
-  else
+  else if (myShaderManager->IsSameContext (this))
   {
     myShaderManager->SetContext (NULL);
   }
@@ -526,6 +530,48 @@ void OpenGl_Context::SwapBuffers()
 
 #endif // __APPLE__
 
+// =======================================================================
+// function : SetSwapInterval
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
+{
+#if defined(HAVE_EGL)
+  if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
+  {
+    return Standard_True;
+  }
+#elif defined(_WIN32)
+  if (myFuncs->wglSwapIntervalEXT != NULL)
+  {
+    myFuncs->wglSwapIntervalEXT (theInterval);
+    return Standard_True;
+  }
+#elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+  //
+#elif defined(__APPLE__)
+  if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
+  {
+    return Standard_True;
+  }
+#else
+  if (theInterval == -1
+   && myFuncs->glXSwapIntervalEXT != NULL)
+  {
+    typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
+    glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
+    aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
+    return Standard_True;
+  }
+  else if (myFuncs->glXSwapIntervalSGI != NULL)
+  {
+    myFuncs->glXSwapIntervalSGI (theInterval);
+    return Standard_True;
+  }
+#endif
+  return Standard_False;
+}
+
 // =======================================================================
 // function : findProc
 // purpose  :
@@ -995,7 +1041,7 @@ void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
     isCoreProfile = (theIsCoreProfile == Standard_True);
 
     // detect Core profile
-    if (isCoreProfile)
+    if (!isCoreProfile)
     {
       GLint aProfile = 0;
       ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile);
@@ -1127,7 +1173,9 @@ void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
   #define FindProcShort(theFunc) FindProc(#theFunc, myFuncs->theFunc)
 
   // retrieve platform-dependent extensions
-#if defined(_WIN32) && !defined(HAVE_EGL)
+#if defined(HAVE_EGL)
+  //
+#elif defined(_WIN32)
   if (FindProcShort (wglGetExtensionsStringARB))
   {
     const char* aWglExts = myFuncs->wglGetExtensionsStringARB (wglGetCurrentDC());
@@ -1155,6 +1203,19 @@ void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
       FindProcShort (wglDXUnlockObjectsNV);
     }
   }
+#elif defined(__APPLE__)
+    //
+#else
+    const char* aGlxExts = ::glXQueryExtensionsString ((Display* )myDisplay, DefaultScreen ((Display* )myDisplay));
+    if (CheckExtension (aGlxExts, "GLX_EXT_swap_control"))
+    {
+      FindProcShort (glXSwapIntervalEXT);
+    }
+    if (CheckExtension (aGlxExts, "GLX_SGI_swap_control"))
+    {
+      FindProcShort (glXSwapIntervalSGI);
+    }
+    //extSwapTear = CheckExtension (aGlxExts, "GLX_EXT_swap_control_tear");
 #endif
 
   // initialize debug context extension
@@ -1579,7 +1640,21 @@ void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
 
   // load GL_ARB_vertex_type_2_10_10_10_rev (added to OpenGL 3.3 core)
   const bool hasVertType21010101rev = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_vertex_type_2_10_10_10_rev"))
-       && FindProcShort (glVertexP2ui)
+       && FindProcShort (glVertexAttribP1ui)
+       && FindProcShort (glVertexAttribP1uiv)
+       && FindProcShort (glVertexAttribP2ui)
+       && FindProcShort (glVertexAttribP2uiv)
+       && FindProcShort (glVertexAttribP3ui)
+       && FindProcShort (glVertexAttribP3uiv)
+       && FindProcShort (glVertexAttribP4ui)
+       && FindProcShort (glVertexAttribP4uiv);
+
+  if ( hasVertType21010101rev
+   && !isCoreProfile)
+  {
+    // load deprecated functions
+    const bool hasVertType21010101revExt =
+          FindProcShort (glVertexP2ui)
        && FindProcShort (glVertexP2uiv)
        && FindProcShort (glVertexP3ui)
        && FindProcShort (glVertexP3uiv)
@@ -1608,15 +1683,9 @@ void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
        && FindProcShort (glColorP4ui)
        && FindProcShort (glColorP4uiv)
        && FindProcShort (glSecondaryColorP3ui)
-       && FindProcShort (glSecondaryColorP3uiv)
-       && FindProcShort (glVertexAttribP1ui)
-       && FindProcShort (glVertexAttribP1uiv)
-       && FindProcShort (glVertexAttribP2ui)
-       && FindProcShort (glVertexAttribP2uiv)
-       && FindProcShort (glVertexAttribP3ui)
-       && FindProcShort (glVertexAttribP3uiv)
-       && FindProcShort (glVertexAttribP4ui)
-       && FindProcShort (glVertexAttribP4uiv);
+       && FindProcShort (glSecondaryColorP3uiv);
+    (void )hasVertType21010101revExt;
+  }
 
   // load OpenGL 3.3 extra functions
   has33 = IsGlGreaterEqual (3, 3)
@@ -2455,7 +2524,7 @@ void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine  theType,
     }
   }
 
-  if (myActiveProgram != NULL)
+  if (!myActiveProgram.IsNull())
   {
     myActiveProgram->SetUniform (this, "uPattern", aPattern);
     myActiveProgram->SetUniform (this, "uFactor",  theFactor);
@@ -2516,6 +2585,53 @@ void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
 #endif
 }
 
+// =======================================================================
+// function : SetTextureMatrix
+// purpose  :
+// =======================================================================
+void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams)
+{
+  if (theParams.IsNull())
+  {
+    return;
+  }
+  else if (!myActiveProgram.IsNull())
+  {
+    const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
+    if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
+    {
+      return;
+    }
+
+    // pack transformation parameters
+    OpenGl_Vec4 aTrsf[2];
+    aTrsf[0].xy() = theParams->Translation();
+    aTrsf[0].zw() = theParams->Scale();
+    aTrsf[1].x()  = std::sin (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
+    aTrsf[1].y()  = std::cos (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
+    myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
+    return;
+  }
+
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    GLint aMatrixMode = GL_TEXTURE;
+    ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
+
+    core11->glMatrixMode (GL_TEXTURE);
+    OpenGl_Mat4 aTextureMat;
+    const Graphic3d_Vec2& aScale = theParams->Scale();
+    const Graphic3d_Vec2& aTrans = theParams->Translation();
+    Graphic3d_TransformUtils::Scale     (aTextureMat,  aScale.x(),  aScale.y(), 1.0f);
+    Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
+    Graphic3d_TransformUtils::Rotate    (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
+    core11->glLoadMatrixf (aTextureMat);
+    core11->glMatrixMode (aMatrixMode);
+  }
+#endif
+}
+
 // =======================================================================
 // function : SetPointSize
 // purpose  :
@@ -2557,13 +2673,16 @@ Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabl
   myIsGlNormalizeEnabled = isEnabled;
 
 #if !defined(GL_ES_VERSION_2_0)
-  if (isEnabled)
-  {
-    glEnable (GL_NORMALIZE);
-  }
-  else
+  if (core11 != NULL)
   {
-    glDisable (GL_NORMALIZE);
+    if (isEnabled)
+    {
+      ::glEnable  (GL_NORMALIZE);
+    }
+    else
+    {
+      ::glDisable (GL_NORMALIZE);
+    }
   }
 #endif
 
@@ -2651,3 +2770,87 @@ void OpenGl_Context::ApplyProjectionMatrix()
     myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
   }
 }
+
+
+// =======================================================================
+// function : EnableFeatures
+// purpose  :
+// =======================================================================
+void OpenGl_Context::EnableFeatures() const
+{
+  //
+}
+
+// =======================================================================
+// function : DisableFeatures
+// purpose  :
+// =======================================================================
+void OpenGl_Context::DisableFeatures() const
+{
+#if !defined(GL_ES_VERSION_2_0)
+  glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
+#endif
+
+  /*
+  * Disable stuff that's likely to slow down glDrawPixels.
+  * (Omit as much of this as possible, when you know in advance
+  * that the OpenGL state will already be set correctly.)
+  */
+  glDisable(GL_DITHER);
+  glDisable(GL_BLEND);
+  glDisable(GL_DEPTH_TEST);
+  glDisable(GL_TEXTURE_2D);
+  glDisable(GL_STENCIL_TEST);
+
+#if !defined(GL_ES_VERSION_2_0)
+  glDisable(GL_LIGHTING);
+  glDisable(GL_ALPHA_TEST);
+  glDisable(GL_FOG);
+  glDisable(GL_LOGIC_OP);
+  glDisable(GL_TEXTURE_1D);
+
+  glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
+  glPixelTransferi(GL_RED_SCALE, 1);
+  glPixelTransferi(GL_RED_BIAS, 0);
+  glPixelTransferi(GL_GREEN_SCALE, 1);
+  glPixelTransferi(GL_GREEN_BIAS, 0);
+  glPixelTransferi(GL_BLUE_SCALE, 1);
+  glPixelTransferi(GL_BLUE_BIAS, 0);
+  glPixelTransferi(GL_ALPHA_SCALE, 1);
+  glPixelTransferi(GL_ALPHA_BIAS, 0);
+
+  /*
+  * Disable extensions that could slow down glDrawPixels.
+  * (Actually, you should check for the presence of the proper
+  * extension before making these calls.  I've omitted that
+  * code for simplicity.)
+  */
+
+  if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2))
+  {
+#ifdef GL_EXT_convolution
+    if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
+      glDisable(GL_CONVOLUTION_1D_EXT);
+
+    if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
+      glDisable(GL_CONVOLUTION_2D_EXT);
+
+    if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
+      glDisable(GL_SEPARABLE_2D_EXT);
+#endif
+
+#ifdef GL_EXT_histogram
+    if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
+      glDisable(GL_HISTOGRAM_EXT);
+
+    if (CheckExtension ("GL_MINMAX_EXT"))
+      glDisable(GL_MINMAX_EXT);
+#endif
+
+#ifdef GL_EXT_texture3D
+    if (CheckExtension ("GL_TEXTURE_3D_EXT"))
+      glDisable(GL_TEXTURE_3D_EXT);
+#endif
+  }
+#endif
+}