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>
20 // =======================================================================
21 // function : OpenGl_VertexBufferCompat
23 // =======================================================================
24 OpenGl_VertexBufferCompat::OpenGl_VertexBufferCompat()
29 // =======================================================================
30 // function : ~OpenGl_VertexBufferCompat
32 // =======================================================================
33 OpenGl_VertexBufferCompat::~OpenGl_VertexBufferCompat()
38 // =======================================================================
41 // =======================================================================
42 bool OpenGl_VertexBufferCompat::Create (const Handle(OpenGl_Context)& )
44 if (myBufferId == NO_BUFFER)
46 myBufferId = (GLuint )-1; // dummy identifier...
47 Handle(NCollection_AlignedAllocator) anAlloc = new NCollection_AlignedAllocator (16);
48 myData = new NCollection_Buffer (anAlloc);
50 return myBufferId != NO_BUFFER;
53 // =======================================================================
56 // =======================================================================
57 void OpenGl_VertexBufferCompat::Release (OpenGl_Context* )
59 if (myBufferId == NO_BUFFER)
65 myBufferId = NO_BUFFER;
69 // =======================================================================
72 // =======================================================================
73 void OpenGl_VertexBufferCompat::Bind (const Handle(OpenGl_Context)& ) const
78 // =======================================================================
81 // =======================================================================
82 void OpenGl_VertexBufferCompat::Unbind (const Handle(OpenGl_Context)& ) const
87 //! Convert GL type to Graphic3d enumeration
88 static inline bool toGraphic3dDataType (const GLuint theNbComponents,
89 const GLenum theGlType,
90 Graphic3d_TypeOfData& theType)
94 case GL_UNSIGNED_BYTE:
96 if (theNbComponents == 4)
98 theType = Graphic3d_TOD_VEC4UB;
103 case GL_UNSIGNED_SHORT:
105 if (theNbComponents == 1)
107 theType = Graphic3d_TOD_USHORT;
112 case GL_UNSIGNED_INT:
114 if (theNbComponents == 1)
116 theType = Graphic3d_TOD_UINT;
123 switch (theNbComponents)
125 case 2: theType = Graphic3d_TOD_VEC2; return true;
126 case 3: theType = Graphic3d_TOD_VEC3; return true;
127 case 4: theType = Graphic3d_TOD_VEC4; return true;
135 // =======================================================================
136 // function : initLink
138 // =======================================================================
139 bool OpenGl_VertexBufferCompat::initLink (const Handle(NCollection_Buffer)& theData,
140 const GLuint theComponentsNb,
141 const GLsizei theElemsNb,
142 const GLenum theDataType)
144 if (theData.IsNull())
150 if (myBufferId == NO_BUFFER)
152 myBufferId = (GLuint )-1; // dummy identifier...
155 myDataType = theDataType;
156 myComponentsNb = theComponentsNb;
157 myElemsNb = theElemsNb;
158 myOffset = myData->ChangeData();
162 // =======================================================================
165 // =======================================================================
166 bool OpenGl_VertexBufferCompat::init (const Handle(OpenGl_Context)& theCtx,
167 const GLuint theComponentsNb,
168 const GLsizei theElemsNb,
170 const GLenum theDataType,
171 const GLsizei theStride)
173 if (!Create (theCtx))
179 myDataType = theDataType;
180 myComponentsNb = theComponentsNb;
181 myElemsNb = theElemsNb;
183 const size_t aNbBytes = size_t(myElemsNb) * theStride;
184 if (!myData->Allocate (aNbBytes))
190 myOffset = myData->ChangeData();
193 memcpy (myData->ChangeData(), theData, aNbBytes);
198 // =======================================================================
199 // function : subData
201 // =======================================================================
202 bool OpenGl_VertexBufferCompat::subData (const Handle(OpenGl_Context)& ,
203 const GLsizei theElemFrom,
204 const GLsizei theElemsNb,
206 const GLenum theDataType)
208 if (!IsValid() || myDataType != theDataType ||
209 theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb))
213 else if (theData == NULL)
218 const size_t aDataSize = sizeOfGlType (theDataType);
219 const size_t anOffset = size_t(theElemFrom) * size_t(myComponentsNb) * aDataSize;
220 const size_t aNbBytes = size_t(theElemsNb) * size_t(myComponentsNb) * aDataSize;
221 memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);