Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:18:29

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
0033 //! data.
0034 //!
0035 //! Method Create() creates dummy identifier for this object which should NOT be passed to OpenGL
0036 //! functions.
0037 template <class BaseBufferT>
0038 class OpenGl_BufferCompatT : public BaseBufferT
0039 {
0040 
0041 public:
0042   //! Create uninitialized VBO.
0043   OpenGl_BufferCompatT()
0044   {
0045     //
0046   }
0047 
0048   //! Destroy object.
0049   ~OpenGl_BufferCompatT() override { Release(nullptr); }
0050 
0051   //! Return TRUE.
0052   bool IsVirtual() const override { return true; }
0053 
0054   //! Creates VBO name (id) if not yet generated.
0055   //! Data should be initialized by another method.
0056   inline bool Create(const occ::handle<OpenGl_Context>& theGlCtx) override;
0057 
0058   //! Destroy object - will release memory if any.
0059   inline void Release(OpenGl_Context* theGlCtx) override;
0060 
0061   //! Bind this VBO.
0062   void Bind(const occ::handle<OpenGl_Context>&) const override
0063   {
0064     //
0065   }
0066 
0067   //! Unbind this VBO.
0068   void Unbind(const occ::handle<OpenGl_Context>&) const override
0069   {
0070     //
0071   }
0072 
0073 public: //! @name advanced methods
0074   //! Initialize buffer with existing data.
0075   //! Data will NOT be copied by this method!
0076   inline bool initLink(const occ::handle<NCollection_Buffer>& theData,
0077                        const unsigned int                     theComponentsNb,
0078                        const int                              theElemsNb,
0079                        const unsigned int                     theDataType);
0080 
0081   //! Initialize buffer with new data (data will be copied).
0082   inline bool init(const occ::handle<OpenGl_Context>& theGlCtx,
0083                    const unsigned int                 theComponentsNb,
0084                    const int                          theElemsNb,
0085                    const void*                        theData,
0086                    const unsigned int                 theDataType,
0087                    const int                          theStride) override;
0088 
0089   //! Update part of the buffer with new data.
0090   inline bool subData(const occ::handle<OpenGl_Context>& theGlCtx,
0091                       const int                          theElemFrom,
0092                       const int                          theElemsNb,
0093                       const void*                        theData,
0094                       const unsigned int                 theDataType) override;
0095 
0096   //! Read back buffer sub-range.
0097   inline bool getSubData(const occ::handle<OpenGl_Context>& theGlCtx,
0098                          const int                          theElemFrom,
0099                          const int                          theElemsNb,
0100                          void*                              theData,
0101                          const unsigned int                 theDataType) override;
0102 
0103 protected:
0104   occ::handle<NCollection_Buffer> myData; //!< buffer data
0105 };
0106 
0107 //=================================================================================================
0108 
0109 template <class BaseBufferT>
0110 bool OpenGl_BufferCompatT<BaseBufferT>::Create(const occ::handle<OpenGl_Context>&)
0111 {
0112   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0113   {
0114     BaseBufferT::myBufferId = (unsigned int)-1; // dummy identifier...
0115     myData                  = new NCollection_Buffer(Graphic3d_Buffer::DefaultAllocator());
0116   }
0117   return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER;
0118 }
0119 
0120 //=================================================================================================
0121 
0122 template <class BaseBufferT>
0123 void OpenGl_BufferCompatT<BaseBufferT>::Release(OpenGl_Context*)
0124 {
0125   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0126   {
0127     return;
0128   }
0129 
0130   BaseBufferT::myOffset   = nullptr;
0131   BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER;
0132   myData.Nullify();
0133 }
0134 
0135 //=================================================================================================
0136 
0137 template <class BaseBufferT>
0138 bool OpenGl_BufferCompatT<BaseBufferT>::initLink(const occ::handle<NCollection_Buffer>& theData,
0139                                                  const unsigned int theComponentsNb,
0140                                                  const int          theElemsNb,
0141                                                  const unsigned int theDataType)
0142 {
0143   if (theData.IsNull())
0144   {
0145     BaseBufferT::myOffset = nullptr;
0146     return false;
0147   }
0148 
0149   if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0150   {
0151     BaseBufferT::myBufferId = (unsigned int)-1; // dummy identifier...
0152   }
0153   myData                      = theData;
0154   BaseBufferT::myDataType     = theDataType;
0155   BaseBufferT::myComponentsNb = theComponentsNb;
0156   BaseBufferT::myElemsNb      = theElemsNb;
0157   BaseBufferT::myOffset       = myData->ChangeData();
0158   return true;
0159 }
0160 
0161 //=================================================================================================
0162 
0163 template <class BaseBufferT>
0164 bool OpenGl_BufferCompatT<BaseBufferT>::init(const occ::handle<OpenGl_Context>& theCtx,
0165                                              const unsigned int                 theComponentsNb,
0166                                              const int                          theElemsNb,
0167                                              const void*                        theData,
0168                                              const unsigned int                 theDataType,
0169                                              const int                          theStride)
0170 {
0171   if (!Create(theCtx))
0172   {
0173     BaseBufferT::myOffset = nullptr;
0174     return false;
0175   }
0176 
0177   BaseBufferT::myDataType     = theDataType;
0178   BaseBufferT::myComponentsNb = theComponentsNb;
0179   BaseBufferT::myElemsNb      = theElemsNb;
0180 
0181   const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride;
0182   if (!myData->Allocate(aNbBytes))
0183   {
0184     BaseBufferT::myOffset = nullptr;
0185     return false;
0186   }
0187 
0188   BaseBufferT::myOffset = myData->ChangeData();
0189   if (theData != nullptr)
0190   {
0191     memcpy(myData->ChangeData(), theData, aNbBytes);
0192   }
0193   return true;
0194 }
0195 
0196 //=================================================================================================
0197 
0198 template <class BaseBufferT>
0199 bool OpenGl_BufferCompatT<BaseBufferT>::subData(const occ::handle<OpenGl_Context>&,
0200                                                 const int          theElemFrom,
0201                                                 const int          theElemsNb,
0202                                                 const void*        theData,
0203                                                 const unsigned int theDataType)
0204 {
0205   if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType || theElemFrom < 0
0206       || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb))
0207   {
0208     return false;
0209   }
0210   else if (theData == nullptr)
0211   {
0212     return true;
0213   }
0214 
0215   const size_t aDataSize = BaseBufferT::sizeOfGlType(theDataType);
0216   const size_t anOffset  = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0217   const size_t aNbBytes  = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0218   memcpy(myData->ChangeData() + anOffset, theData, aNbBytes);
0219   return true;
0220 }
0221 
0222 //=================================================================================================
0223 
0224 template <class BaseBufferT>
0225 bool OpenGl_BufferCompatT<BaseBufferT>::getSubData(const occ::handle<OpenGl_Context>&,
0226                                                    const int          theElemFrom,
0227                                                    const int          theElemsNb,
0228                                                    void*              theData,
0229                                                    const unsigned int theDataType)
0230 {
0231   if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType || theElemFrom < 0
0232       || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb) || theData == nullptr)
0233   {
0234     return false;
0235   }
0236 
0237   const size_t aDataSize = BaseBufferT::sizeOfGlType(theDataType);
0238   const size_t anOffset  = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0239   const size_t aNbBytes  = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0240   memcpy(theData, myData->Data() + anOffset, aNbBytes);
0241   return true;
0242 }
0243 
0244 #endif // _OpenGl_VertexBufferCompat_HeaderFile