Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:35

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_OccAllocator.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 Standard_Boolean IsRaytracedGroup (const OpenGl_Group* theGroup);
0034 
0035   //! Checks to see if the element contains ray-trace geometry.
0036   Standard_EXPORT Standard_Boolean IsRaytracedElement (const OpenGl_ElementNode* theNode);
0037 
0038   //! Checks to see if the element contains ray-trace geometry.
0039   Standard_EXPORT Standard_Boolean IsRaytracedElement (const OpenGl_Element* theElement);
0040 }
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 
0069   //! Empty constructor.
0070   Standard_EXPORT OpenGl_RaytraceMaterial();
0071 
0072   //! Returns packed (serialized) representation of material.
0073   const Standard_ShortReal* Packed()
0074   {
0075     return reinterpret_cast<Standard_ShortReal*> (this);
0076   }
0077 };
0078 
0079 //! Stores properties of OpenGL light source.
0080 struct OpenGl_RaytraceLight
0081 {
0082 
0083   BVH_Vec4f Emission; //!< Diffuse intensity (in terms of OpenGL)
0084   BVH_Vec4f Position; //!< Position of light source (in terms of OpenGL)
0085 
0086 public:
0087 
0088   //! Creates new light source.
0089   OpenGl_RaytraceLight() { }
0090 
0091   //! Creates new light source.
0092   Standard_EXPORT OpenGl_RaytraceLight (const BVH_Vec4f& theEmission,
0093                                         const BVH_Vec4f& thePosition);
0094 
0095   //! Returns packed (serialized) representation of light source.
0096   const Standard_ShortReal* Packed()
0097   {
0098     return reinterpret_cast<Standard_ShortReal*> (this);
0099   }
0100 };
0101 
0102 //! Shared pointer to quad BVH (QBVH) tree.
0103 typedef opencascade::handle<BVH_Tree<Standard_ShortReal, 3, BVH_QuadTree> > QuadBvhHandle;
0104 typedef BVH_Triangulation<Standard_ShortReal, 3> OpenGl_BVHTriangulation3f;
0105 
0106 //! Triangulation of single OpenGL primitive array.
0107 class OpenGl_TriangleSet : public OpenGl_BVHTriangulation3f
0108 {
0109   DEFINE_STANDARD_RTTIEXT(OpenGl_TriangleSet, OpenGl_BVHTriangulation3f)
0110 public:
0111 
0112   //! Value of invalid material index to return in case of errors.
0113   static const Standard_Integer INVALID_MATERIAL = -1;
0114 
0115 public:
0116 
0117   //! Creates new OpenGL element triangulation.
0118   Standard_EXPORT OpenGl_TriangleSet (const Standard_Size theArrayID,
0119                                       const opencascade::handle<BVH_Builder<Standard_ShortReal, 3> >& theBuilder);
0120 
0121   //! Returns ID of associated primitive array.
0122   Standard_Size AssociatedPArrayID() const
0123   {
0124     return myArrayID;
0125   }
0126 
0127   //! Returns material index of triangle set.
0128   Standard_Integer MaterialIndex() const
0129   {
0130     if (Elements.size() == 0)
0131     {
0132       return INVALID_MATERIAL;
0133     }
0134 
0135     return Elements.front().w();
0136   }
0137 
0138   //! Sets material index for entire triangle set.
0139   void SetMaterialIndex (Standard_Integer theMatID)
0140   {
0141     for (Standard_Size anIdx = 0; anIdx < Elements.size(); ++anIdx)
0142     {
0143       Elements[anIdx].w() = theMatID;
0144     }
0145   }
0146 
0147   //! Returns AABB of primitive set.
0148   virtual BVH_BoxNt Box() const Standard_OVERRIDE;
0149 
0150   //! Returns AABB of the given object.
0151   using BVH_Triangulation<Standard_ShortReal, 3>::Box;
0152 
0153   //! Returns centroid position along the given axis.
0154   Standard_EXPORT virtual Standard_ShortReal Center (const Standard_Integer theIndex, const Standard_Integer theAxis) const Standard_OVERRIDE;
0155 
0156   //! Returns quad BVH (QBVH) tree produced from binary BVH.
0157   Standard_EXPORT const QuadBvhHandle& QuadBVH();
0158 
0159 public:
0160 
0161   BVH_Array3f Normals; //!< Array of vertex normals.
0162   BVH_Array2f TexCrds; //!< Array of texture coords.
0163 
0164 private:
0165 
0166   Standard_Size myArrayID; //!< ID of associated primitive array.
0167 
0168   QuadBvhHandle myQuadBVH; //!< QBVH produced from binary BVH tree.
0169 
0170 };
0171 
0172 //! Stores geometry of ray-tracing scene.
0173 class OpenGl_RaytraceGeometry : public BVH_Geometry<Standard_ShortReal, 3>
0174 {
0175 public:
0176 
0177   //! Value of invalid offset to return in case of errors.
0178   static const Standard_Integer INVALID_OFFSET = -1;
0179 
0180   //! Maximum number of textures used in ray-tracing shaders.
0181   //! This is not restriction of the solution implemented, but
0182   //! rather the reasonable limit of the number of textures in
0183   //! various applications (can be increased if needed).
0184   static const Standard_Integer MAX_TEX_NUMBER = 32;
0185 
0186 public:
0187 
0188   //! Array of properties of light sources.
0189   std::vector<OpenGl_RaytraceLight,
0190     NCollection_OccAllocator<OpenGl_RaytraceLight> > Sources;
0191 
0192   //! Array of 'front' material properties.
0193   std::vector<OpenGl_RaytraceMaterial,
0194     NCollection_OccAllocator<OpenGl_RaytraceMaterial> > Materials;
0195 
0196   //! Global ambient from all light sources.
0197   BVH_Vec4f Ambient;
0198 
0199 public:
0200 
0201   //! Creates uninitialized ray-tracing geometry.
0202   OpenGl_RaytraceGeometry()
0203   : BVH_Geometry<Standard_ShortReal, 3>(),
0204     myTopLevelTreeDepth (0),
0205     myBotLevelTreeDepth (0)
0206   {
0207     //
0208   }
0209 
0210   //! Releases resources of ray-tracing geometry.
0211   ~OpenGl_RaytraceGeometry()
0212   {
0213     //
0214   }
0215 
0216   //! Clears only ray-tracing materials.
0217   void ClearMaterials()
0218   {
0219     std::vector<OpenGl_RaytraceMaterial,
0220       NCollection_OccAllocator<OpenGl_RaytraceMaterial> > anEmptyMaterials;
0221 
0222     Materials.swap (anEmptyMaterials);
0223 
0224     myTextures.Clear();
0225   }
0226 
0227   //! Clears ray-tracing geometry.
0228   Standard_EXPORT virtual void Clear() Standard_OVERRIDE;
0229 
0230 public: //! @name methods related to acceleration structure
0231 
0232   //! Performs post-processing of high-level scene BVH.
0233   Standard_EXPORT Standard_Boolean ProcessAcceleration();
0234 
0235   //! Returns offset of bottom-level BVH for given leaf node.
0236   //! If the node index is not valid the function returns -1.
0237   //! @note Can be used after processing acceleration structure.
0238   Standard_EXPORT Standard_Integer AccelerationOffset (Standard_Integer theNodeIdx);
0239 
0240   //! Returns offset of triangulation vertices for given leaf node.
0241   //! If the node index is not valid the function returns -1.
0242   //! @note Can be used after processing acceleration structure.
0243   Standard_EXPORT Standard_Integer VerticesOffset (Standard_Integer theNodeIdx);
0244 
0245   //! Returns offset of triangulation elements for given leaf node.
0246   //! If the node index is not valid the function returns -1.
0247   //! @note Can be used after processing acceleration structure.
0248   Standard_EXPORT Standard_Integer ElementsOffset (Standard_Integer theNodeIdx);
0249 
0250   //! Returns triangulation data for given leaf node.
0251   //! If the node index is not valid the function returns NULL.
0252   //! @note Can be used after processing acceleration structure.
0253   Standard_EXPORT OpenGl_TriangleSet* TriangleSet (Standard_Integer theNodeIdx);
0254 
0255   //! Returns quad BVH (QBVH) tree produced from binary BVH.
0256   Standard_EXPORT const QuadBvhHandle& QuadBVH();
0257 
0258 public: //! @name methods related to texture management
0259 
0260   //! Checks if scene contains textured objects.
0261   Standard_Boolean HasTextures() const
0262   {
0263     return !myTextures.IsEmpty();
0264   }
0265 
0266   //! Adds new OpenGL texture to the scene and returns its index.
0267   Standard_EXPORT Standard_Integer AddTexture (const Handle(OpenGl_Texture)& theTexture);
0268 
0269   //! Updates unique 64-bit texture handles to use in shaders.
0270   Standard_EXPORT Standard_Boolean UpdateTextureHandles (const Handle(OpenGl_Context)& theContext);
0271 
0272   //! Makes the OpenGL texture handles resident (must be called before using).
0273   Standard_EXPORT Standard_Boolean AcquireTextures (const Handle(OpenGl_Context)& theContext);
0274 
0275   //! Makes the OpenGL texture handles non-resident (must be called after using).
0276   Standard_EXPORT Standard_Boolean ReleaseTextures (const Handle(OpenGl_Context)& theContext) const;
0277 
0278   //! Returns array of texture handles.
0279   const std::vector<GLuint64>& TextureHandles() const
0280   {
0281     return myTextureHandles;
0282   }
0283 
0284   //! Releases OpenGL resources.
0285   void ReleaseResources (const Handle(OpenGl_Context)& )
0286   {
0287     //
0288   }
0289 
0290 public: //! @name auxiliary methods
0291 
0292   //! Returns depth of top-level scene BVH from last build.
0293   Standard_Integer TopLevelTreeDepth() const
0294   {
0295     return myTopLevelTreeDepth;
0296   }
0297 
0298   //! Returns maximum depth of bottom-level scene BVHs from last build.
0299   Standard_Integer BotLevelTreeDepth() const
0300   {
0301     return myBotLevelTreeDepth;
0302   }
0303 
0304 protected:
0305 
0306   NCollection_Vector<Handle(OpenGl_Texture)> myTextures;           //!< Array of texture maps shared between rendered objects
0307   std::vector<GLuint64>                      myTextureHandles;     //!< Array of unique 64-bit texture handles obtained from OpenGL
0308   Standard_Integer                           myTopLevelTreeDepth;  //!< Depth of high-level scene BVH from last build
0309   Standard_Integer                           myBotLevelTreeDepth;  //!< Maximum depth of bottom-level scene BVHs from last build
0310 
0311   QuadBvhHandle myQuadBVH; //!< QBVH produced from binary BVH tree.
0312 
0313 };
0314 
0315 #endif