0027670: Visualization - avoid duplication of structures defining primitive array...
[occt.git] / src / OpenGl / OpenGl_VertexBufferCompat.cxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <OpenGl_VertexBufferCompat.hxx>
16
17 #include <NCollection_AlignedAllocator.hxx>
18
19
20 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_VertexBufferCompat,OpenGl_VertexBuffer)
21
22 // =======================================================================
23 // function : OpenGl_VertexBufferCompat
24 // purpose  :
25 // =======================================================================
26 OpenGl_VertexBufferCompat::OpenGl_VertexBufferCompat()
27 {
28   //
29 }
30
31 // =======================================================================
32 // function : ~OpenGl_VertexBufferCompat
33 // purpose  :
34 // =======================================================================
35 OpenGl_VertexBufferCompat::~OpenGl_VertexBufferCompat()
36 {
37   Release (NULL);
38 }
39
40 // =======================================================================
41 // function : Create
42 // purpose  :
43 // =======================================================================
44 bool OpenGl_VertexBufferCompat::Create (const Handle(OpenGl_Context)& )
45 {
46   if (myBufferId == NO_BUFFER)
47   {
48     myBufferId = (GLuint )-1; // dummy identifier...
49     Handle(NCollection_AlignedAllocator) anAlloc = new NCollection_AlignedAllocator (16);
50     myData = new NCollection_Buffer (anAlloc);
51   }
52   return myBufferId != NO_BUFFER;
53 }
54
55 // =======================================================================
56 // function : Release
57 // purpose  :
58 // =======================================================================
59 void OpenGl_VertexBufferCompat::Release (OpenGl_Context* )
60 {
61   if (myBufferId == NO_BUFFER)
62   {
63     return;
64   }
65
66   myOffset   = NULL;
67   myBufferId = NO_BUFFER;
68   myData.Nullify();
69 }
70
71 // =======================================================================
72 // function : Bind
73 // purpose  :
74 // =======================================================================
75 void OpenGl_VertexBufferCompat::Bind (const Handle(OpenGl_Context)& ) const
76 {
77   //
78 }
79
80 // =======================================================================
81 // function : Unbind
82 // purpose  :
83 // =======================================================================
84 void OpenGl_VertexBufferCompat::Unbind (const Handle(OpenGl_Context)& ) const
85 {
86   //
87 }
88
89 // =======================================================================
90 // function : initLink
91 // purpose  :
92 // =======================================================================
93 bool OpenGl_VertexBufferCompat::initLink (const Handle(NCollection_Buffer)& theData,
94                                           const GLuint   theComponentsNb,
95                                           const GLsizei  theElemsNb,
96                                           const GLenum   theDataType)
97 {
98   if (theData.IsNull())
99   {
100     myOffset = NULL;
101     return false;
102   }
103
104   if (myBufferId == NO_BUFFER)
105   {
106     myBufferId = (GLuint )-1; // dummy identifier...
107   }
108   myData         = theData;
109   myDataType     = theDataType;
110   myComponentsNb = theComponentsNb;
111   myElemsNb      = theElemsNb;
112   myOffset       = myData->ChangeData();
113   return true;
114 }
115
116 // =======================================================================
117 // function : init
118 // purpose  :
119 // =======================================================================
120 bool OpenGl_VertexBufferCompat::init (const Handle(OpenGl_Context)& theCtx,
121                                       const GLuint   theComponentsNb,
122                                       const GLsizei  theElemsNb,
123                                       const void*    theData,
124                                       const GLenum   theDataType,
125                                       const GLsizei  theStride)
126 {
127   if (!Create (theCtx))
128   {
129     myOffset = NULL;
130     return false;
131   }
132
133   myDataType     = theDataType;
134   myComponentsNb = theComponentsNb;
135   myElemsNb      = theElemsNb;
136
137   const size_t aNbBytes = size_t(myElemsNb) * theStride;
138   if (!myData->Allocate (aNbBytes))
139   {
140     myOffset = NULL;
141     return false;
142   }
143
144   myOffset = myData->ChangeData();
145   if (theData != NULL)
146   {
147     memcpy (myData->ChangeData(), theData, aNbBytes);
148   }
149   return true;
150 }
151
152 // =======================================================================
153 // function : subData
154 // purpose  :
155 // =======================================================================
156 bool OpenGl_VertexBufferCompat::subData (const Handle(OpenGl_Context)& ,
157                                          const GLsizei  theElemFrom,
158                                          const GLsizei  theElemsNb,
159                                          const void*    theData,
160                                          const GLenum   theDataType)
161 {
162   if (!IsValid() || myDataType != theDataType ||
163       theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb))
164   {
165     return false;
166   }
167   else if (theData == NULL)
168   {
169     return true;
170   }
171
172   const size_t aDataSize = sizeOfGlType (theDataType);
173   const size_t anOffset  = size_t(theElemFrom) * size_t(myComponentsNb) * aDataSize;
174   const size_t aNbBytes  = size_t(theElemsNb)  * size_t(myComponentsNb) * aDataSize;
175   memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);
176   return true;
177 }