7d3e64ef |
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 | IMPLEMENT_STANDARD_HANDLE (OpenGl_VertexBufferCompat, OpenGl_VertexBuffer) |
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 | //! Convert GL type to Graphic3d enumeration |
90 | static inline bool toGraphic3dDataType (const GLuint theNbComponents, |
91 | const GLenum theGlType, |
92 | Graphic3d_TypeOfData& theType) |
93 | { |
94 | switch (theGlType) |
95 | { |
96 | case GL_UNSIGNED_BYTE: |
97 | { |
98 | if (theNbComponents == 4) |
99 | { |
100 | theType = Graphic3d_TOD_VEC4UB; |
101 | return true; |
102 | } |
103 | return false; |
104 | } |
105 | case GL_UNSIGNED_SHORT: |
106 | { |
107 | if (theNbComponents == 1) |
108 | { |
109 | theType = Graphic3d_TOD_USHORT; |
110 | return true; |
111 | } |
112 | return false; |
113 | } |
114 | case GL_UNSIGNED_INT: |
115 | { |
116 | if (theNbComponents == 1) |
117 | { |
118 | theType = Graphic3d_TOD_UINT; |
119 | return true; |
120 | } |
121 | return false; |
122 | } |
123 | case GL_FLOAT: |
124 | { |
125 | switch (theNbComponents) |
126 | { |
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; |
130 | } |
131 | return false; |
132 | } |
133 | } |
134 | return false; |
135 | } |
136 | |
137 | // ======================================================================= |
138 | // function : initLink |
139 | // purpose : |
140 | // ======================================================================= |
141 | bool OpenGl_VertexBufferCompat::initLink (const Handle(NCollection_Buffer)& theData, |
142 | const GLuint theComponentsNb, |
143 | const GLsizei theElemsNb, |
144 | const GLenum theDataType) |
145 | { |
146 | if (theData.IsNull()) |
147 | { |
148 | myOffset = NULL; |
149 | return false; |
150 | } |
151 | |
152 | if (myBufferId == NO_BUFFER) |
153 | { |
154 | myBufferId = (GLuint )-1; // dummy identifier... |
155 | } |
156 | myData = theData; |
157 | myDataType = theDataType; |
158 | myComponentsNb = theComponentsNb; |
159 | myElemsNb = theElemsNb; |
160 | myOffset = myData->ChangeData(); |
161 | return true; |
162 | } |
163 | |
164 | // ======================================================================= |
165 | // function : init |
166 | // purpose : |
167 | // ======================================================================= |
168 | bool OpenGl_VertexBufferCompat::init (const Handle(OpenGl_Context)& theCtx, |
169 | const GLuint theComponentsNb, |
170 | const GLsizei theElemsNb, |
171 | const void* theData, |
172 | const GLenum theDataType, |
173 | const GLsizei theStride) |
174 | { |
175 | if (!Create (theCtx)) |
176 | { |
177 | myOffset = NULL; |
178 | return false; |
179 | } |
180 | |
181 | myDataType = theDataType; |
182 | myComponentsNb = theComponentsNb; |
183 | myElemsNb = theElemsNb; |
184 | |
185 | const size_t aNbBytes = size_t(myElemsNb) * theStride; |
186 | if (!myData->Allocate (aNbBytes)) |
187 | { |
188 | myOffset = NULL; |
189 | return false; |
190 | } |
191 | |
192 | myOffset = myData->ChangeData(); |
193 | if (theData != NULL) |
194 | { |
195 | memcpy (myData->ChangeData(), theData, aNbBytes); |
196 | } |
197 | return true; |
198 | } |
199 | |
200 | // ======================================================================= |
201 | // function : subData |
202 | // purpose : |
203 | // ======================================================================= |
204 | bool OpenGl_VertexBufferCompat::subData (const Handle(OpenGl_Context)& , |
205 | const GLsizei theElemFrom, |
206 | const GLsizei theElemsNb, |
207 | const void* theData, |
208 | const GLenum theDataType) |
209 | { |
210 | if (!IsValid() || myDataType != theDataType || |
211 | theElemFrom < 0 || ((theElemFrom + theElemsNb) > myElemsNb)) |
212 | { |
213 | return false; |
214 | } |
215 | else if (theData == NULL) |
216 | { |
217 | return true; |
218 | } |
219 | |
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); |
224 | return true; |
225 | } |