Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-16 09:18:16

0001 // Created on: 2013-08-27
0002 // Created by: Denis BOGOLEPOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef OpenGl_SceneGeometry_HeaderFile
0017 #define OpenGl_SceneGeometry_HeaderFile
0018 
0019 #include <BVH_Geometry.hxx>
0020 #include <BVH_Triangulation.hxx>
0021 #include <BVH_BinnedBuilder.hxx>
0022 #include <NCollection_LinearVector.hxx>
0023 #include <OpenGl_Texture.hxx>
0024 #include <OpenGl_Sampler.hxx>
0025 
0026 class OpenGl_Element;
0027 struct OpenGl_ElementNode;
0028 class OpenGl_Group;
0029 
0030 namespace OpenGl_Raytrace
0031 {
0032 //! Checks to see if the group contains ray-trace geometry.
0033 Standard_EXPORT bool IsRaytracedGroup(const OpenGl_Group* theGroup);
0034 
0035 //! Checks to see if the element contains ray-trace geometry.
0036 Standard_EXPORT bool IsRaytracedElement(const OpenGl_ElementNode* theNode);
0037 
0038 //! Checks to see if the element contains ray-trace geometry.
0039 Standard_EXPORT bool IsRaytracedElement(const OpenGl_Element* theElement);
0040 } // namespace OpenGl_Raytrace
0041 
0042 //! Stores properties of surface material.
0043 struct OpenGl_RaytraceMaterial
0044 {
0045   BVH_Vec4f Ambient;          //!< Ambient reflection coefficient
0046   BVH_Vec4f Diffuse;          //!< Diffuse reflection coefficient
0047   BVH_Vec4f Specular;         //!< Glossy  reflection coefficient
0048   BVH_Vec4f Emission;         //!< Material emission
0049   BVH_Vec4f Reflection;       //!< Specular reflection coefficient
0050   BVH_Vec4f Refraction;       //!< Specular refraction coefficient
0051   BVH_Vec4f Transparency;     //!< Material transparency
0052   BVH_Mat4f TextureTransform; //!< Texture transformation matrix
0053 
0054   //! Physically-based material properties (used in path tracing engine).
0055   struct Physical
0056   {
0057     BVH_Vec4f Kc;          //!< Weight of coat specular/glossy BRDF
0058     BVH_Vec4f Kd;          //!< Weight of base diffuse BRDF
0059     BVH_Vec4f Ks;          //!< Weight of base specular/glossy BRDF
0060     BVH_Vec4f Kt;          //!< Weight of base specular/glossy BTDF
0061     BVH_Vec4f Le;          //!< Radiance emitted by the surface
0062     BVH_Vec4f FresnelCoat; //!< Fresnel coefficients of coat layer
0063     BVH_Vec4f FresnelBase; //!< Fresnel coefficients of base layer
0064     BVH_Vec4f Absorption;  //!< Absorption color/intensity
0065   } BSDF;
0066 
0067 public:
0068   //! Empty constructor.
0069   Standard_EXPORT OpenGl_RaytraceMaterial();
0070 
0071   //! Returns packed (serialized) representation of material.
0072   const float* Packed() { return reinterpret_cast<float*>(this); }
0073 };
0074 
0075 //! Stores properties of OpenGL light source.
0076 struct OpenGl_RaytraceLight
0077 {
0078 
0079   BVH_Vec4f Emission; //!< Diffuse intensity (in terms of OpenGL)
0080   BVH_Vec4f Position; //!< Position of light source (in terms of OpenGL)
0081 
0082 public:
0083   //! Creates new light source.
0084   OpenGl_RaytraceLight() = default;
0085 
0086   //! Creates new light source.
0087   Standard_EXPORT OpenGl_RaytraceLight(const BVH_Vec4f& theEmission, const BVH_Vec4f& thePosition);
0088 
0089   //! Returns packed (serialized) representation of light source.
0090   const float* Packed() { return reinterpret_cast<float*>(this); }
0091 };
0092 
0093 //! Shared pointer to quad BVH (QBVH) tree.
0094 typedef opencascade::handle<BVH_Tree<float, 3, BVH_QuadTree>> QuadBvhHandle;
0095 typedef BVH_Triangulation<float, 3>                           OpenGl_BVHTriangulation3f;
0096 
0097 //! Triangulation of single OpenGL primitive array.
0098 class OpenGl_TriangleSet : public OpenGl_BVHTriangulation3f
0099 {
0100   DEFINE_STANDARD_RTTIEXT(OpenGl_TriangleSet, OpenGl_BVHTriangulation3f)
0101 public:
0102   //! Value of invalid material index to return in case of errors.
0103   static const int INVALID_MATERIAL = -1;
0104 
0105 public:
0106   //! Creates new OpenGL element triangulation.
0107   Standard_EXPORT OpenGl_TriangleSet(const size_t                                      theArrayID,
0108                                      const opencascade::handle<BVH_Builder<float, 3>>& theBuilder);
0109 
0110   //! Returns ID of associated primitive array.
0111   size_t AssociatedPArrayID() const { return myArrayID; }
0112 
0113   //! Returns material index of triangle set.
0114   int MaterialIndex() const
0115   {
0116     if (Elements.IsEmpty())
0117     {
0118       return INVALID_MATERIAL;
0119     }
0120 
0121     return Elements.First().w();
0122   }
0123 
0124   //! Sets material index for entire triangle set.
0125   void SetMaterialIndex(int theMatID)
0126   {
0127     for (size_t anIdx = 0; anIdx < Elements.Size(); ++anIdx)
0128     {
0129       Elements[anIdx].w() = theMatID;
0130     }
0131   }
0132 
0133   //! Returns AABB of primitive set.
0134   BVH_BoxNt Box() const override;
0135 
0136   //! Returns AABB of the given object.
0137   using BVH_Triangulation<float, 3>::Box;
0138 
0139   //! Returns centroid position along the given axis.
0140   Standard_EXPORT float Center(const int theIndex, const int theAxis) const override;
0141 
0142   //! Returns quad BVH (QBVH) tree produced from binary BVH.
0143   Standard_EXPORT const QuadBvhHandle& QuadBVH();
0144 
0145 public:
0146   BVH_Array3f Normals; //!< Array of vertex normals.
0147   BVH_Array2f TexCrds; //!< Array of texture coords.
0148 
0149 private:
0150   size_t myArrayID; //!< ID of associated primitive array.
0151 
0152   QuadBvhHandle myQuadBVH; //!< QBVH produced from binary BVH tree.
0153 };
0154 
0155 //! Stores geometry of ray-tracing scene.
0156 class OpenGl_RaytraceGeometry : public BVH_Geometry<float, 3>
0157 {
0158 public:
0159   //! Value of invalid offset to return in case of errors.
0160   static const int INVALID_OFFSET = -1;
0161 
0162   //! Maximum number of textures used in ray-tracing shaders.
0163   //! This is not restriction of the solution implemented, but
0164   //! rather the reasonable limit of the number of textures in
0165   //! various applications (can be increased if needed).
0166   static const int MAX_TEX_NUMBER = 32;
0167 
0168 public:
0169   //! Array of properties of light sources.
0170   NCollection_LinearVector<OpenGl_RaytraceLight> Sources;
0171 
0172   //! Array of 'front' material properties.
0173   NCollection_LinearVector<OpenGl_RaytraceMaterial> Materials;
0174 
0175   //! Global ambient from all light sources.
0176   BVH_Vec4f Ambient;
0177 
0178 public:
0179   //! Creates uninitialized ray-tracing geometry.
0180   OpenGl_RaytraceGeometry()
0181       : myTopLevelTreeDepth(0),
0182         myBotLevelTreeDepth(0)
0183   {
0184     //
0185   }
0186 
0187   //! Releases resources of ray-tracing geometry.
0188   ~OpenGl_RaytraceGeometry() override
0189   {
0190     //
0191   }
0192 
0193   //! Clears only ray-tracing materials.
0194   void ClearMaterials()
0195   {
0196     Materials.Clear(true);
0197     myTextures.Clear();
0198   }
0199 
0200   //! Clears ray-tracing geometry.
0201   Standard_EXPORT void Clear() override;
0202 
0203 public: //! @name methods related to acceleration structure
0204   //! Performs post-processing of high-level scene BVH.
0205   Standard_EXPORT bool ProcessAcceleration();
0206 
0207   //! Returns offset of bottom-level BVH for given leaf node.
0208   //! If the node index is not valid the function returns -1.
0209   //! @note Can be used after processing acceleration structure.
0210   Standard_EXPORT int AccelerationOffset(int theNodeIdx);
0211 
0212   //! Returns offset of triangulation vertices for given leaf node.
0213   //! If the node index is not valid the function returns -1.
0214   //! @note Can be used after processing acceleration structure.
0215   Standard_EXPORT int VerticesOffset(int theNodeIdx);
0216 
0217   //! Returns offset of triangulation elements for given leaf node.
0218   //! If the node index is not valid the function returns -1.
0219   //! @note Can be used after processing acceleration structure.
0220   Standard_EXPORT int ElementsOffset(int theNodeIdx);
0221 
0222   //! Returns triangulation data for given leaf node.
0223   //! If the node index is not valid the function returns NULL.
0224   //! @note Can be used after processing acceleration structure.
0225   Standard_EXPORT OpenGl_TriangleSet* TriangleSet(int theNodeIdx);
0226 
0227   //! Returns quad BVH (QBVH) tree produced from binary BVH.
0228   Standard_EXPORT const QuadBvhHandle& QuadBVH();
0229 
0230 public: //! @name methods related to texture management
0231   //! Checks if scene contains textured objects.
0232   bool HasTextures() const { return !myTextures.IsEmpty(); }
0233 
0234   //! Adds new OpenGL texture to the scene and returns its index.
0235   Standard_EXPORT int AddTexture(const occ::handle<OpenGl_Texture>& theTexture);
0236 
0237   //! Updates unique 64-bit texture handles to use in shaders.
0238   Standard_EXPORT bool UpdateTextureHandles(const occ::handle<OpenGl_Context>& theContext);
0239 
0240   //! Makes the OpenGL texture handles resident (must be called before using).
0241   Standard_EXPORT bool AcquireTextures(const occ::handle<OpenGl_Context>& theContext);
0242 
0243   //! Makes the OpenGL texture handles non-resident (must be called after using).
0244   Standard_EXPORT bool ReleaseTextures(const occ::handle<OpenGl_Context>& theContext) const;
0245 
0246   //! Returns array of texture handles.
0247   const NCollection_LinearVector<GLuint64>& TextureHandles() const { return myTextureHandles; }
0248 
0249   //! Releases OpenGL resources.
0250   void ReleaseResources(const occ::handle<OpenGl_Context>&)
0251   {
0252     //
0253   }
0254 
0255 public: //! @name auxiliary methods
0256   //! Returns depth of top-level scene BVH from last build.
0257   int TopLevelTreeDepth() const { return myTopLevelTreeDepth; }
0258 
0259   //! Returns maximum depth of bottom-level scene BVHs from last build.
0260   int BotLevelTreeDepth() const { return myBotLevelTreeDepth; }
0261 
0262 protected:
0263   // clang-format off
0264   NCollection_DynamicArray<occ::handle<OpenGl_Texture>> myTextures;           //!< Array of texture maps shared between rendered objects
0265   NCollection_LinearVector<GLuint64>                    myTextureHandles;     //!< Array of unique 64-bit texture handles obtained from OpenGL
0266   int                           myTopLevelTreeDepth;  //!< Depth of high-level scene BVH from last build
0267   int                           myBotLevelTreeDepth;  //!< Maximum depth of bottom-level scene BVHs from last build
0268   // clang-format on
0269 
0270   QuadBvhHandle myQuadBVH; //!< QBVH produced from binary BVH tree.
0271 };
0272 
0273 #endif