1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-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 #include <OpenGl_VertexBufferCompat.hxx>
17 #include <NCollection_AlignedAllocator.hxx>
19 IMPLEMENT_STANDARD_HANDLE (OpenGl_VertexBufferCompat, OpenGl_VertexBuffer)
20 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_VertexBufferCompat, OpenGl_VertexBuffer)
22 // =======================================================================
23 // function : OpenGl_VertexBufferCompat
25 // =======================================================================
26 OpenGl_VertexBufferCompat::OpenGl_VertexBufferCompat()
31 // =======================================================================
32 // function : ~OpenGl_VertexBufferCompat
34 // =======================================================================
35 OpenGl_VertexBufferCompat::~OpenGl_VertexBufferCompat()
40 // =======================================================================
43 // =======================================================================
44 bool OpenGl_VertexBufferCompat::Create (const Handle(OpenGl_Context)& )
46 if (myBufferId == NO_BUFFER)
48 myBufferId = (GLuint )-1; // dummy identifier...
49 Handle(NCollection_AlignedAllocator) anAlloc = new NCollection_AlignedAllocator (16);
50 myData = new NCollection_Buffer (anAlloc);
52 return myBufferId != NO_BUFFER;
55 // =======================================================================
58 // =======================================================================
59 void OpenGl_VertexBufferCompat::Release (OpenGl_Context* )
61 if (myBufferId == NO_BUFFER)
67 myBufferId = NO_BUFFER;
71 // =======================================================================
74 // =======================================================================
75 void OpenGl_VertexBufferCompat::Bind (const Handle(OpenGl_Context)& ) const
80 // =======================================================================
83 // =======================================================================
84 void OpenGl_VertexBufferCompat::Unbind (const Handle(OpenGl_Context)& ) const
89 //! Convert GL type to Graphic3d enumeration
90 static inline bool toGraphic3dDataType (const GLuint theNbComponents,
91 const GLenum theGlType,
92 Graphic3d_TypeOfData& theType)
96 case GL_UNSIGNED_BYTE:
98 if (theNbComponents == 4)
100 theType = Graphic3d_TOD_VEC4UB;
105 case GL_UNSIGNED_SHORT:
107 if (theNbComponents == 1)
109 theType = Graphic3d_TOD_USHORT;
114 case GL_UNSIGNED_INT:
116 if (theNbComponents == 1)
118 theType = Graphic3d_TOD_UINT;
125 switch (theNbComponents)
127 case 2: theType = Graphic3d_TOD_VEC2; return true;
128 case 3: theType = Graphic3d_TOD_VEC3; return true;
129 case 4: theType = Graphic3d_TOD_VEC4; return true;
137 // =======================================================================
138 // function : initLink
140 // =======================================================================
141 bool OpenGl_VertexBufferCompat::initLink (const Handle(NCollection_Buffer)& theData,
142 const GLuint theComponentsNb,
143 const GLsizei theElemsNb,
144 const GLenum theDataType)
146 if (theData.IsNull())
152 if (myBufferId == NO_BUFFER)
154 myBufferId = (GLuint )-1; // dummy identifier...
157 myDataType = theDataType;
158 myComponentsNb = theComponentsNb;
159 myElemsNb = theElemsNb;
160 myOffset = myData->ChangeData();
164 // =======================================================================
167 // =======================================================================
168 bool OpenGl_VertexBufferCompat::init (const Handle(OpenGl_Context)& theCtx,
169 const GLuint theComponentsNb,
170 const GLsizei theElemsNb,
172 const GLenum theDataType,
173 const GLsizei theStride)
175 if (!Create (theCtx))
181 myDataType = theDataType;
182 myComponentsNb = theComponentsNb;
183 myElemsNb = theElemsNb;
185 const size_t aNbBytes = size_t(myElemsNb) * theStride;
186 if (!myData->Allocate (aNbBytes))
192 myOffset = myData->ChangeData();
195 memcpy (myData->ChangeData(), theData, aNbBytes);
200 // =======================================================================
201 // function : subData
203 // =======================================================================
204 bool OpenGl_VertexBufferCompat::subData (const Handle(OpenGl_Context)& ,
205 const GLsizei theElemFrom,
206 const GLsizei theElemsNb,
208 const GLenum theDataType)
210 if (!IsValid() || myDataType != theDataType ||
211 theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb))
215 else if (theData == NULL)
220 const size_t aDataSize = sizeOfGlType (theDataType);
221 const size_t anOffset = size_t(theElemFrom) * size_t(myComponentsNb) * aDataSize;
222 const size_t aNbBytes = size_t(theElemsNb) * size_t(myComponentsNb) * aDataSize;
223 memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);