]> OCCT Git - occt-copy.git/commitdiff
0025464: Visualization - provide package for Volume Rendering.
authorisk <isk@opencascade.com>
Thu, 29 Oct 2015 08:39:18 +0000 (11:39 +0300)
committerisk <isk@opencascade.com>
Thu, 29 Oct 2015 08:39:18 +0000 (11:39 +0300)
Add two functions OSD_OpenStream for std::ifstream.
Add four functions SetUnfirom in the OpenGl_ShaderProgram for 64-bit unsigned integer variables.
Add OpenGl_Texture::Init3D.
Update Declarations.glsl.

src/BVH/BVH_Box.hxx
src/NCollection/NCollection_Vec3.hxx
src/OSD/OSD_OpenFile.cxx
src/OSD/OSD_OpenFile.hxx
src/OpenGl/OpenGl_Context.hxx
src/OpenGl/OpenGl_Texture.cxx
src/OpenGl/OpenGl_Texture.hxx
src/Shaders/Declarations.glsl

index 838fb90352d6192b579003af364ce1436296d2cd..e0755db224a729892c2d77e7aaebcecec3f82aa6 100644 (file)
@@ -281,4 +281,18 @@ namespace BVH
 
 #include <BVH_Box.lxx>
 
+//! 2D box of double precision reals.
+typedef BVH_Box<Standard_Real, 2> BVH_Box2d;
+//! 3D box of double precision reals.
+typedef BVH_Box<Standard_Real, 3> BVH_Box3d;
+//! 4D box of double precision reals.
+typedef BVH_Box<Standard_Real, 4> BVH_Box4d;
+
+//! 2D box of single precision reals.
+typedef BVH_Box<Standard_ShortReal, 2> BVH_Box2f;
+//! 3D box of single precision reals.
+typedef BVH_Box<Standard_ShortReal, 3> BVH_Box3f;
+//! 4D box of single precision reals.
+typedef BVH_Box<Standard_ShortReal, 4> BVH_Box4f;
+
 #endif // _BVH_Box_Header
index 73b97433689712512b8da9dc8b043ff882498b92..82efc597bd94ad3ce203b05404563238e7bb0b55 100644 (file)
@@ -351,6 +351,15 @@ public:
     return NCollection_Vec3 (Element_t(0), Element_t(0), Element_t(1));
   }
 
+  //! Convert the vector.
+  template<class T>
+  NCollection_Vec3<T> Convert() const
+  {
+    return NCollection_Vec3<T> (static_cast<T> (v[0]),
+                                static_cast<T> (v[1]),
+                                static_cast<T> (v[2]));
+  }
+
 private:
 
   Element_t v[3]; //!< define the vector as array to avoid structure alignment issues
index c2e37cd7037ccda183ab6f2f911f499fba040ea1..d6d29fabd6c87bf73fdc9cc6a7fae679b674a0af 100644 (file)
@@ -123,3 +123,37 @@ void OSD_OpenStream(std::ofstream& theStream,
 #endif
 }
 
+// ==============================================
+// function : OSD_OpenStream
+// purpose : Opens file stream
+// ==============================================
+void OSD_OpenStream (std::ifstream&                theStream,
+                     const char*                   theName,
+                     const std::ios_base::openmode theMode)
+{
+#ifdef _WIN32
+  // file name is treated as UTF-8 string and converted to UTF-16 one
+  const TCollection_ExtendedString aFileNameW (theName, Standard_True);
+  theStream.open ((const wchar_t* )aFileNameW.ToExtString(), theMode);
+#else
+  theStream.open (theName, theMode);
+#endif
+}
+
+// ==============================================
+// function : OSD_OpenStream
+// purpose : Opens file stream
+// ==============================================
+void OSD_OpenStream (std::ifstream&                    theStream,
+                     const TCollection_ExtendedString& theName,
+                     const std::ios_base::openmode     theMode)
+{
+#ifdef _WIN32
+  theStream.open ((const wchar_t* )theName.ToExtString(), theMode);
+#else
+  // conversion in UTF-8 for linux
+  NCollection_Utf8String aString ((const Standard_Utf16Char*)theName.ToExtString());
+  theStream.open (aString.ToCString(),theMode);
+#endif
+}
+
index 5c4a70aafd51eb3bc18962b5ec6bacfb1ad5a7c5..6254f448a0e31ea0ffe8f64123b23b87c2364705 100644 (file)
@@ -40,6 +40,22 @@ __Standard_API void OSD_OpenStream (std::ofstream& theStream,
                                     const TCollection_ExtendedString& theName,
                                     const std::ios_base::openmode theMode);
 
+//! Function opens the file stream.
+//! @param theStream stream to open
+//! @param theName name of file encoded in UTF-8
+//! @param theMode opening mode
+__Standard_API void OSD_OpenStream (std::ifstream& theStream,
+                                    const char* theName,
+                                    const std::ios_base::openmode theMode);
+
+//! Function opens the file stream.
+//! @param theStream stream to open
+//! @param theName name of file encoded in UTF-16
+//! @param theMode opening mode
+__Standard_API void OSD_OpenStream (std::ifstream& theStream,
+                                    const TCollection_ExtendedString& theName,
+                                    const std::ios_base::openmode theMode);
+
 //! Function opens the file buffer.
 //! @param theBuff file buffer to open
 //! @param theName name of file encoded in UTF-8
index 074f467e5da275fd44122ee88775d4b2fd6943ec..23053e8f860c07634b71b319071472e8382f2738 100644 (file)
@@ -284,6 +284,12 @@ public:
   Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
                                                           const char* theExtName);
 
+  //! Returns true if hardware supports floating-point texture.
+  bool HasFloatingPointTexture()
+  {
+    return (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_texture_float"));
+  }
+
   //! Auxiliary template to retrieve GL function pointer.
   //! Pointer to function retrieved from library is statically casted
   //! to requested type - there no way to check real signature of exported function.
index dde0ff748aaf38723b9780ff46416adb0571cb28..f380b125e0c79289c0306f2b4f4ab548db1493ed 100644 (file)
@@ -183,7 +183,7 @@ bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
     {
       if (theCtx->core11 == NULL)
       {
-        theTextFormat  = GL_R8;  // GL_R32F
+        theTextFormat  = GL_R32F;
         thePixelFormat = GL_RED;
       }
       else
@@ -198,7 +198,7 @@ bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
     {
       if (theCtx->core11 == NULL)
       {
-        theTextFormat  = GL_R8;  // GL_R32F
+        theTextFormat  = GL_R32F;
         thePixelFormat = GL_RED;
       }
       else
@@ -211,7 +211,7 @@ bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
     }
     case Image_PixMap::ImgRGBAF:
     {
-      theTextFormat  = GL_RGBA8; // GL_RGBA32F
+      theTextFormat  = GL_RGBA32F;
       thePixelFormat = GL_RGBA;
       theDataType    = GL_FLOAT;
       return true;
@@ -222,14 +222,14 @@ bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
       {
         return false;
       }
-      theTextFormat  = GL_RGBA8;    // GL_RGBA32F
+      theTextFormat  = GL_RGBA32F;
       thePixelFormat = GL_BGRA_EXT; // equals to GL_BGRA
       theDataType    = GL_FLOAT;
       return true;
     }
     case Image_PixMap::ImgRGBF:
     {
-      theTextFormat  = GL_RGB8; // GL_RGB32F
+      theTextFormat  = GL_RGB32F;
       thePixelFormat = GL_RGB;
       theDataType    = GL_FLOAT;
       return true;
@@ -237,7 +237,7 @@ bool OpenGl_Texture::GetDataFormat (const Handle(OpenGl_Context)& theCtx,
     case Image_PixMap::ImgBGRF:
     {
     #if !defined(GL_ES_VERSION_2_0)
-      theTextFormat  = GL_RGB8; // GL_RGB32F
+      theTextFormat  = GL_RGB32F;
       thePixelFormat = GL_BGR;  // equals to GL_BGR_EXT
       theDataType    = GL_FLOAT;
       return true;
@@ -373,6 +373,19 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
   myHasMipmaps             = Standard_False;
   myTextFormat             = thePixelFormat;
 #if !defined(GL_ES_VERSION_2_0)
+  if (theTextFormat >= Image_PixMap::ImgGrayF
+   && !theCtx->HasFloatingPointTexture())
+  {
+    TCollection_ExtendedString aMsg ("Error: floating-point textures are not supproted by hardware.");
+    theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
+                         GL_DEBUG_TYPE_ERROR,
+                         0,
+                         GL_DEBUG_SEVERITY_HIGH,
+                         aMsg);
+
+    Release (theCtx.operator->());
+    return false;
+  }
   const GLint anIntFormat  = theTextFormat;
 #else
   // ES does not support sized formats and format conversions - them detected from data type
@@ -473,7 +486,7 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
       glTexImage1D (GL_PROXY_TEXTURE_1D, 0, anIntFormat,
                     aWidth, 0,
                     thePixelFormat, theDataType, NULL);
-      glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
+      glGetTexLevelParameteriv (GL_PROXY_TEXTURE_1D, 0, GL_TEXTURE_WIDTH, &aTestWidth);
       if (aTestWidth == 0)
       {
         // no memory or broken input parameters
@@ -612,7 +625,7 @@ bool OpenGl_Texture::Init (const Handle(OpenGl_Context)& theCtx,
       {
         const TCollection_ExtendedString aWarnMessage ("Warning: generating mipmaps requires GL_ARB_framebuffer_object extension which is missing.");
 
-        theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aWarnMessage);
+        theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aWarnMessage);
 
         Unbind (theCtx);
         Release (theCtx.operator->());
@@ -691,6 +704,22 @@ bool OpenGl_Texture::InitRectangle (const Handle(OpenGl_Context)& theCtx,
   const GLint anIntFormat = theFormat.Internal();
   myTextFormat = theFormat.Format();
 
+  if (anIntFormat == GL_FLOAT
+   || !theCtx->HasFloatingPointTexture())
+  {
+    TCollection_ExtendedString aMsg ("Error: floating-point textures are not supproted by hardware.");
+
+    theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+                         GL_DEBUG_TYPE_ERROR_ARB,
+                         0,
+                         GL_DEBUG_SEVERITY_HIGH_ARB,
+                         aMsg);
+
+    Release (theCtx.operator->());
+    Unbind (theCtx);
+    return false;
+  }
+
   glTexImage2D (GL_PROXY_TEXTURE_RECTANGLE,
                 0,
                 anIntFormat,
@@ -740,3 +769,123 @@ bool OpenGl_Texture::InitRectangle (const Handle(OpenGl_Context)& theCtx,
   return false;
 #endif
 }
+
+// =======================================================================
+// function : Init3D
+// purpose  :
+// =======================================================================
+bool OpenGl_Texture::Init3D (const Handle(OpenGl_Context)& theCtx,
+                             const Standard_Integer        theSizeX,
+                             const Standard_Integer        theSizeY,
+                             const Standard_Integer        theSizeZ,
+                             const OpenGl_TextureFormat&   theFormat,
+                             const void*                   thePixels)
+{
+  if (!Create (theCtx) || !theCtx->IsGlGreaterEqual (1, 2))
+  {
+    return false;
+  }
+
+  myTarget = GL_TEXTURE_3D;
+
+  const GLsizei aSizeX = Min (theCtx->MaxTextureSize(), theSizeX);
+  const GLsizei aSizeY = Min (theCtx->MaxTextureSize(), theSizeY);
+  const GLsizei aSizeZ = Min (theCtx->MaxTextureSize(), theSizeZ);
+
+  Bind (theCtx);
+
+  if (myParams->Filter() == Graphic3d_TOTF_NEAREST)
+  {
+    glTexParameteri (myTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+    glTexParameteri (myTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+  }
+  else
+  {
+    glTexParameteri (myTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri (myTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+  }
+
+  if (theFormat.Internal() == GL_FLOAT
+   && !theCtx->HasFloatingPointTexture())
+  {
+    TCollection_ExtendedString aMsg ("Error: floating-point textures are not supproted by hardware.");
+
+    theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
+                         GL_DEBUG_TYPE_ERROR,
+                         0,
+                         GL_DEBUG_SEVERITY_HIGH,
+                         aMsg);
+
+    Release (theCtx.operator->());
+    Unbind (theCtx);
+    return false;
+  }
+
+#if !defined(OPENGL_ES_2_0)
+  theCtx->core15fwd->glTexImage3D (GL_PROXY_TEXTURE_3D,
+                                   0,
+                                   theFormat.Internal(),
+                                   aSizeX,
+                                   aSizeY,
+                                   aSizeZ,
+                                   0,
+                                   theFormat.Format(),
+                                   theFormat.DataType(),
+                                   NULL);
+
+  GLint aTestSizeX = 0;
+  GLint aTestSizeY = 0;
+  GLint aTestSizeZ = 0;
+
+  glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH,  &aTestSizeX);
+  glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, &aTestSizeY);
+  glGetTexLevelParameteriv (GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_DEPTH,  &aTestSizeZ);
+
+  if (aTestSizeX == 0 || aTestSizeY == 0 || aTestSizeZ == 0)
+  {
+    Unbind (theCtx);
+    return false;
+  }
+#endif
+
+  GLint anInternal  = theFormat.Internal();
+
+  if (anInternal == GL_R8_SNORM
+   || anInternal == GL_R16_SNORM
+   || anInternal == GL_RED_SNORM)
+  {
+    glPixelTransferf (GL_RED_SCALE, 0.5f);
+    glPixelTransferf (GL_RED_BIAS, 0.5f);
+  }
+
+  glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+  glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+  glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
+
+  theCtx->core15fwd->glTexImage3D (GL_TEXTURE_3D,
+                                   0,
+                                   anInternal,
+                                   aSizeX,
+                                   aSizeY,
+                                   aSizeZ,
+                                   0,
+                                   theFormat.Format(),
+                                   theFormat.DataType(),
+                                   thePixels);
+
+  glPixelTransferf (GL_RED_SCALE, 1.0f);
+  glPixelTransferf (GL_RED_BIAS, 0.0f);
+
+  if (glGetError() != GL_NO_ERROR)
+  {
+    Unbind (theCtx);
+    return false;
+  }
+
+  mySizeX = aSizeX;
+  mySizeY = aSizeY;
+  mySizeZ = aSizeZ;
+
+  Unbind (theCtx);
+  return true;
+}
index 1a1a0ed955b85bd9eb62d087719b6e4256f9eda2..5a1878725c4fd716a5fb89ee40025bf3d980422d 100644 (file)
@@ -49,6 +49,37 @@ struct OpenGl_TextureFormatSelector<GLubyte>
         return GL_NONE;
     }
   }
+
+  static GLint DataType()
+  {
+    return GL_UNSIGNED_BYTE;
+  }
+};
+
+template<>
+struct OpenGl_TextureFormatSelector<GLbyte>
+{
+  static GLint Internal (GLuint theChannels)
+  {
+    switch (theChannels)
+    {
+      case 1:
+        return GL_R8_SNORM;
+      case 2:
+        return GL_RG8_SNORM;
+      case 3:
+        return GL_RGB8_SNORM;
+      case 4:
+        return GL_RGBA8_SNORM;
+      default:
+        return GL_NONE;
+    }
+  }
+
+  static GLint DataType()
+  {
+    return GL_BYTE;
+  }
 };
 
 template<>
@@ -70,6 +101,37 @@ struct OpenGl_TextureFormatSelector<GLushort>
         return GL_NONE;
     }
   }
+
+  static GLint DataType()
+  {
+    return GL_UNSIGNED_SHORT;
+  }
+};
+
+template<>
+struct OpenGl_TextureFormatSelector<GLshort>
+{
+  static GLint Internal (GLuint theChannels)
+  {
+    switch (theChannels)
+    {
+      case 1:
+        return GL_R16_SNORM;
+      case 2:
+        return GL_RG16_SNORM;
+      case 3:
+        return GL_RGB16_SNORM;
+      case 4:
+        return GL_RGBA16_SNORM;
+      default:
+        return GL_NONE;
+    }
+  }
+
+  static GLint DataType()
+  {
+    return GL_SHORT;
+  }
 };
 
 template<>
@@ -91,6 +153,63 @@ struct OpenGl_TextureFormatSelector<GLfloat>
         return GL_NONE;
     }
   }
+
+  static GLint DataType()
+  {
+    return GL_FLOAT;
+  }
+};
+
+template<>
+struct OpenGl_TextureFormatSelector<GLuint>
+{
+  static GLint Internal (GLuint theChannels)
+  {
+    switch (theChannels)
+    {
+      case 1:
+        return GL_RED;
+      case 2:
+        return GL_RG;
+      case 3:
+        return GL_RGB;
+      case 4:
+        return GL_RGBA;
+      default:
+        return GL_NONE;
+    }
+  }
+
+  static GLint DataType()
+  {
+    return GL_UNSIGNED_INT;
+  }
+};
+
+template<>
+struct OpenGl_TextureFormatSelector<GLint>
+{
+  static GLint Internal (GLuint theChannels)
+  {
+    switch (theChannels)
+    {
+      case 1:
+        return GL_RED_SNORM;
+      case 2:
+        return GL_RG_SNORM;
+      case 3:
+        return GL_RGB_SNORM;
+      case 4:
+        return GL_RGBA_SNORM;
+      default:
+        return GL_NONE;
+    }
+  }
+
+  static GLint DataType()
+  {
+    return GL_INT;
+  }
 };
 
 //! Stores parameters of OpenGL texture format.
@@ -124,25 +243,36 @@ public:
     return myInternal;
   }
 
+  //! Returns OpenGL data type of the pixel data.
+  inline GLint DataType() const
+  {
+    return myDataType;
+  }
+
   //! Returns texture format for specified type and number of channels.
   template<class T, int N>
   static OpenGl_TextureFormat Create()
   {
-    return OpenGl_TextureFormat (N, OpenGl_TextureFormatSelector<T>::Internal (N));
+    return OpenGl_TextureFormat (N,
+                                 OpenGl_TextureFormatSelector<T>::Internal (N),
+                                 OpenGl_TextureFormatSelector<T>::DataType ());
   }
 
 private:
 
   //! Creates new texture format.
   OpenGl_TextureFormat (const GLint theChannels,
-                        const GLint theInternal)
+                        const GLint theInternal,
+                        const GLint theDataType)
   : myInternal (theInternal),
-    myChannels (theChannels) {}
+    myChannels (theChannels),
+    myDataType (theDataType) {}
 
 private:
 
   GLint myInternal; //!< OpenGL internal format of the pixel data
   GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
+  GLint myDataType; //!< OpenGL data type of input pixel data
 
 };
 
@@ -254,6 +384,14 @@ public:
                                       const Standard_Integer        theSizeY,
                                       const OpenGl_TextureFormat&   theFormat);
 
+  //! Initializes 3D texture rectangle with specified format and size.
+  Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx,
+                               const Standard_Integer        theSizeX,
+                               const Standard_Integer        theSizeY,
+                               const Standard_Integer        theSizeZ,
+                               const OpenGl_TextureFormat&   theFormat,
+                               const void*                   thePixels);
+
   //! @return true if texture was generated within mipmaps
   Standard_EXPORT const Standard_Boolean HasMipmaps() const;
 
@@ -273,9 +411,10 @@ public:
 protected:
 
   GLuint           myTextureId;  //!< GL resource ID
-  GLenum           myTarget;     //!< GL_TEXTURE_1D/GL_TEXTURE_2D
+  GLenum           myTarget;     //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
   GLsizei          mySizeX;      //!< texture width
   GLsizei          mySizeY;      //!< texture height
+  GLsizei          mySizeZ;      //!< texture depth
   GLenum           myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
   Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
   bool             myIsAlpha;    //!< indicates alpha format
index 6dd9f62c3d525bb7a846bbd61dda88032f138e04..e8af5db3d2ab9df1358983d4b54dbd946152b580 100644 (file)
   #define THE_SHADER_IN  in
   #define THE_SHADER_OUT out
   #define THE_OUT        out
+  #define occTexture1D   texture
   #define occTexture2D   texture
+  #define occTexture3D   texture
 #else
   #define THE_ATTRIBUTE  attribute
   #define THE_SHADER_IN  varying
   #define THE_SHADER_OUT varying
   #define THE_OUT
+  #define occTexture1D   texture1D
   #define occTexture2D   texture2D
+  #define occTexture3D   texture3D
 #endif
 
 #ifdef GL_ES
   THE_ATTRIBUTE vec4 occTexCoord;
   THE_ATTRIBUTE vec4 occVertColor;
 #elif (__VERSION__ >= 130)
-  out vec4 occFragColor;
+  out vec4  occFragColor;
+  out float occFragDepth;
 #else
   #define occFragColor gl_FragColor
+  #define occFragDepth gl_FragDepth
 #endif
 
 // Matrix state