Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Created by: Kirill GAVRILOV
0002 // Copyright (c) 2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _OpenGl_BufferCompatT_HeaderFile
0016 #define _OpenGl_BufferCompatT_HeaderFile
0017 
0018 #include <NCollection_Buffer.hxx>
0019 #include <OpenGl_Buffer.hxx>
0020 
0021 //! Compatibility layer for old OpenGL without VBO.
0022 //! Make sure to pass pointer from GetDataOffset() instead of NULL.
0023 //! Method GetDataOffset() returns pointer to real data in this class
0024 //! (while base class OpenGl_VertexBuffer always return NULL).
0025 //!
0026 //! Methods Bind()/Unbind() do nothing (do not affect OpenGL state)
0027 //! and ::GetTarget() is never used.
0028 //! For this reason there is no analog for OpenGl_IndexBuffer.
0029 //! Just pass GetDataOffset() to glDrawElements() directly as last argument.
0030 //!
0031 //! Class overrides methods init() and subData() to copy data into own memory buffer.
0032 //! Extra method initLink() might be used to pass existing buffer through handle without copying the data.
0033 //!
0034 //! Method Create() creates dummy identifier for this object which should NOT be passed to OpenGL functions.
0035 template<class BaseBufferT>
0036 class OpenGl_BufferCompatT : public BaseBufferT
0037 {
0038 
0039 public:
0040 
0041   //! Create uninitialized VBO.
0042   OpenGl_BufferCompatT()
0043   {
0044     //
0045   }
0046 
0047   //! Destroy object.
0048   virtual ~OpenGl_BufferCompatT()
0049   {
0050     Release (NULL);
0051   }
0052 
0053   //! Return TRUE.
0054   virtual bool IsVirtual() const Standard_OVERRIDE { return true; }
0055 
0056   //! Creates VBO name (id) if not yet generated.
0057   //! Data should be initialized by another method.
0058   inline bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE;
0059 
0060   //! Destroy object - will release memory if any.
0061   inline virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
0062 
0063   //! Bind this VBO.
0064   virtual void Bind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
0065   {
0066     //
0067   }
0068 
0069   //! Unbind this VBO.
0070   virtual void Unbind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
0071   {
0072     //
0073   }
0074 
0075 public: //! @name advanced methods
0076 
0077   //! Initialize buffer with existing data.
0078   //! Data will NOT be copied by this method!
0079   inline bool initLink (const Handle(NCollection_Buffer)& theData,
0080                         const unsigned int     theComponentsNb,
0081                         const Standard_Integer theElemsNb,
0082                         const unsigned int     theDataType);
0083 
0084   //! Initialize buffer with new data (data will be copied).
0085   inline virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
0086                             const unsigned int     theComponentsNb,
0087                             const Standard_Integer theElemsNb,
0088                             const void*            theData,
0089                             const unsigned int     theDataType,
0090                             const Standard_Integer theStride) Standard_OVERRIDE;
0091 
0092   //! Update part of the buffer with new data.
0093   inline virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
0094                                const Standard_Integer theElemFrom,
0095                                const Standard_Integer theElemsNb,
0096                                const void*        theData,
0097                                const unsigned int theDataType) Standard_OVERRIDE;
0098 
0099   //! Read back buffer sub-range.
0100   inline virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx,
0101                                   const Standard_Integer theElemFrom,
0102                                   const Standard_Integer theElemsNb,
0103                                   void* theData,
0104                                   const unsigned int theDataType) Standard_OVERRIDE;
0105 
0106 protected:
0107 
0108   Handle(NCollection_Buffer) myData; //!< buffer data
0109 
0110 };
0111 
0112 // =======================================================================
0113 // function : Create
0114 // purpose  :
0115 // =======================================================================
0116 template<class BaseBufferT>
0117 bool OpenGl_BufferCompatT<BaseBufferT>::Create (const Handle(OpenGl_Context)& )
0118 {
0119   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0120   {
0121     BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier...
0122     myData = new NCollection_Buffer (Graphic3d_Buffer::DefaultAllocator());
0123   }
0124   return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER;
0125 }
0126 
0127 // =======================================================================
0128 // function : Release
0129 // purpose  :
0130 // =======================================================================
0131 template<class BaseBufferT>
0132 void OpenGl_BufferCompatT<BaseBufferT>::Release (OpenGl_Context* )
0133 {
0134   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0135   {
0136     return;
0137   }
0138 
0139   BaseBufferT::myOffset   = NULL;
0140   BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER;
0141   myData.Nullify();
0142 }
0143 
0144 // =======================================================================
0145 // function : initLink
0146 // purpose  :
0147 // =======================================================================
0148 template<class BaseBufferT>
0149 bool OpenGl_BufferCompatT<BaseBufferT>::initLink (const Handle(NCollection_Buffer)& theData,
0150                                                   const unsigned int     theComponentsNb,
0151                                                   const Standard_Integer theElemsNb,
0152                                                   const unsigned int     theDataType)
0153 {
0154   if (theData.IsNull())
0155   {
0156     BaseBufferT::myOffset = NULL;
0157     return false;
0158   }
0159 
0160   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0161   {
0162     BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier...
0163   }
0164   myData = theData;
0165   BaseBufferT::myDataType     = theDataType;
0166   BaseBufferT::myComponentsNb = theComponentsNb;
0167   BaseBufferT::myElemsNb      = theElemsNb;
0168   BaseBufferT::myOffset       = myData->ChangeData();
0169   return true;
0170 }
0171 
0172 // =======================================================================
0173 // function : init
0174 // purpose  :
0175 // =======================================================================
0176 template<class BaseBufferT>
0177 bool OpenGl_BufferCompatT<BaseBufferT>::init (const Handle(OpenGl_Context)& theCtx,
0178                                               const unsigned int     theComponentsNb,
0179                                               const Standard_Integer theElemsNb,
0180                                               const void*            theData,
0181                                               const unsigned int     theDataType,
0182                                               const Standard_Integer theStride)
0183 {
0184   if (!Create (theCtx))
0185   {
0186     BaseBufferT::myOffset = NULL;
0187     return false;
0188   }
0189 
0190   BaseBufferT::myDataType     = theDataType;
0191   BaseBufferT::myComponentsNb = theComponentsNb;
0192   BaseBufferT::myElemsNb      = theElemsNb;
0193 
0194   const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride;
0195   if (!myData->Allocate (aNbBytes))
0196   {
0197     BaseBufferT::myOffset = NULL;
0198     return false;
0199   }
0200 
0201   BaseBufferT::myOffset = myData->ChangeData();
0202   if (theData != NULL)
0203   {
0204     memcpy (myData->ChangeData(), theData, aNbBytes);
0205   }
0206   return true;
0207 }
0208 
0209 // =======================================================================
0210 // function : subData
0211 // purpose  :
0212 // =======================================================================
0213 template<class BaseBufferT>
0214 bool OpenGl_BufferCompatT<BaseBufferT>::subData (const Handle(OpenGl_Context)& ,
0215                                                  const Standard_Integer theElemFrom,
0216                                                  const Standard_Integer theElemsNb,
0217                                                  const void*            theData,
0218                                                  const unsigned int     theDataType)
0219 {
0220   if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
0221     || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb))
0222   {
0223     return false;
0224   }
0225   else if (theData == NULL)
0226   {
0227     return true;
0228   }
0229 
0230   const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
0231   const size_t anOffset  = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0232   const size_t aNbBytes  = size_t(theElemsNb)  * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0233   memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);
0234   return true;
0235 }
0236 
0237 // =======================================================================
0238 // function : getSubData
0239 // purpose  :
0240 // =======================================================================
0241 template<class BaseBufferT>
0242 bool OpenGl_BufferCompatT<BaseBufferT>::getSubData (const Handle(OpenGl_Context)& ,
0243                                                     const Standard_Integer theElemFrom,
0244                                                     const Standard_Integer theElemsNb,
0245                                                     void* theData,
0246                                                     const unsigned int theDataType)
0247 {
0248   if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
0249    || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb)
0250    || theData == NULL)
0251   {
0252     return false;
0253   }
0254 
0255   const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
0256   const size_t anOffset  = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0257   const size_t aNbBytes  = size_t(theElemsNb)  * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0258   memcpy (theData, myData->Data() + anOffset, aNbBytes);
0259   return true;
0260 }
0261 
0262 #endif // _OpenGl_VertexBufferCompat_HeaderFile