1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _OpenGl_BufferCompatT_HeaderFile
16 #define _OpenGl_BufferCompatT_HeaderFile
18 #include <NCollection_Buffer.hxx>
19 #include <OpenGl_Buffer.hxx>
21 //! Compatibility layer for old OpenGL without VBO.
22 //! Make sure to pass pointer from GetDataOffset() instead of NULL.
23 //! Method GetDataOffset() returns pointer to real data in this class
24 //! (while base class OpenGl_VertexBuffer always return NULL).
26 //! Methods Bind()/Unbind() do nothing (do not affect OpenGL state)
27 //! and ::GetTarget() is never used.
28 //! For this reason there is no analog for OpenGl_IndexBuffer.
29 //! Just pass GetDataOffset() to glDrawElements() directly as last argument.
31 //! Class overrides methods init() and subData() to copy data into own memory buffer.
32 //! Extra method initLink() might be used to pass existing buffer through handle without copying the data.
34 //! Method Create() creates dummy identifier for this object which should NOT be passed to OpenGL functions.
35 template<class BaseBufferT>
36 class OpenGl_BufferCompatT : public BaseBufferT
41 //! Create uninitialized VBO.
42 OpenGl_BufferCompatT()
48 virtual ~OpenGl_BufferCompatT()
54 virtual bool IsVirtual() const Standard_OVERRIDE { return true; }
56 //! Creates VBO name (id) if not yet generated.
57 //! Data should be initialized by another method.
58 inline bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE;
60 //! Destroy object - will release memory if any.
61 inline virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
64 virtual void Bind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
70 virtual void Unbind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
75 public: //! @name advanced methods
77 //! Initialize buffer with existing data.
78 //! Data will NOT be copied by this method!
79 inline bool initLink (const Handle(NCollection_Buffer)& theData,
80 const unsigned int theComponentsNb,
81 const Standard_Integer theElemsNb,
82 const unsigned int theDataType);
84 //! Initialize buffer with new data (data will be copied).
85 inline virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
86 const unsigned int theComponentsNb,
87 const Standard_Integer theElemsNb,
89 const unsigned int theDataType,
90 const Standard_Integer theStride) Standard_OVERRIDE;
92 //! Update part of the buffer with new data.
93 inline virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
94 const Standard_Integer theElemFrom,
95 const Standard_Integer theElemsNb,
97 const unsigned int theDataType) Standard_OVERRIDE;
99 //! Read back buffer sub-range.
100 inline virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx,
101 const Standard_Integer theElemFrom,
102 const Standard_Integer theElemsNb,
104 const unsigned int theDataType) Standard_OVERRIDE;
108 Handle(NCollection_Buffer) myData; //!< buffer data
112 // =======================================================================
115 // =======================================================================
116 template<class BaseBufferT>
117 bool OpenGl_BufferCompatT<BaseBufferT>::Create (const Handle(OpenGl_Context)& )
119 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
121 BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier...
122 myData = new NCollection_Buffer (Graphic3d_Buffer::DefaultAllocator());
124 return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER;
127 // =======================================================================
128 // function : Release
130 // =======================================================================
131 template<class BaseBufferT>
132 void OpenGl_BufferCompatT<BaseBufferT>::Release (OpenGl_Context* )
134 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
139 BaseBufferT::myOffset = NULL;
140 BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER;
144 // =======================================================================
145 // function : initLink
147 // =======================================================================
148 template<class BaseBufferT>
149 bool OpenGl_BufferCompatT<BaseBufferT>::initLink (const Handle(NCollection_Buffer)& theData,
150 const unsigned int theComponentsNb,
151 const Standard_Integer theElemsNb,
152 const unsigned int theDataType)
154 if (theData.IsNull())
156 BaseBufferT::myOffset = NULL;
160 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
162 BaseBufferT::myBufferId = (unsigned int )-1; // dummy identifier...
165 BaseBufferT::myDataType = theDataType;
166 BaseBufferT::myComponentsNb = theComponentsNb;
167 BaseBufferT::myElemsNb = theElemsNb;
168 BaseBufferT::myOffset = myData->ChangeData();
172 // =======================================================================
175 // =======================================================================
176 template<class BaseBufferT>
177 bool OpenGl_BufferCompatT<BaseBufferT>::init (const Handle(OpenGl_Context)& theCtx,
178 const unsigned int theComponentsNb,
179 const Standard_Integer theElemsNb,
181 const unsigned int theDataType,
182 const Standard_Integer theStride)
184 if (!Create (theCtx))
186 BaseBufferT::myOffset = NULL;
190 BaseBufferT::myDataType = theDataType;
191 BaseBufferT::myComponentsNb = theComponentsNb;
192 BaseBufferT::myElemsNb = theElemsNb;
194 const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride;
195 if (!myData->Allocate (aNbBytes))
197 BaseBufferT::myOffset = NULL;
201 BaseBufferT::myOffset = myData->ChangeData();
204 memcpy (myData->ChangeData(), theData, aNbBytes);
209 // =======================================================================
210 // function : subData
212 // =======================================================================
213 template<class BaseBufferT>
214 bool OpenGl_BufferCompatT<BaseBufferT>::subData (const Handle(OpenGl_Context)& ,
215 const Standard_Integer theElemFrom,
216 const Standard_Integer theElemsNb,
218 const unsigned int theDataType)
220 if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
221 || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb))
225 else if (theData == NULL)
230 const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
231 const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
232 const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
233 memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);
237 // =======================================================================
238 // function : getSubData
240 // =======================================================================
241 template<class BaseBufferT>
242 bool OpenGl_BufferCompatT<BaseBufferT>::getSubData (const Handle(OpenGl_Context)& ,
243 const Standard_Integer theElemFrom,
244 const Standard_Integer theElemsNb,
246 const unsigned int theDataType)
248 if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
249 || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb)
255 const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
256 const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
257 const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
258 memcpy (theData, myData->Data() + anOffset, aNbBytes);
262 #endif // _OpenGl_VertexBufferCompat_HeaderFile