0027860: Visualization - clean up Transformation Persistence API
[occt.git] / src / Graphic3d / Graphic3d_Buffer.hxx
CommitLineData
871fa103 1// Copyright (c) 2014 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
14#ifndef _Graphic3d_Buffer_HeaderFile
15#define _Graphic3d_Buffer_HeaderFile
16
17#include <Graphic3d_Vec.hxx>
4a535d3f 18#include <NCollection_Array1.hxx>
871fa103 19#include <NCollection_Buffer.hxx>
20
21//! Type of attribute in Vertex Buffer
22enum Graphic3d_TypeOfAttribute
23{
24 Graphic3d_TOA_POS = 0, //!< vertex position
25 Graphic3d_TOA_NORM, //!< normal
26 Graphic3d_TOA_UV, //!< texture coordinates
27 Graphic3d_TOA_COLOR, //!< per-vertex color
4a535d3f 28 Graphic3d_TOA_CUSTOM, //!< custom attributes
871fa103 29};
30
31//! Type of the element in Vertex or Index Buffer
32enum Graphic3d_TypeOfData
33{
34 Graphic3d_TOD_USHORT, //!< unsigned 16-bit integer
35 Graphic3d_TOD_UINT, //!< unsigned 32-bit integer
36 Graphic3d_TOD_VEC2, //!< 2-components float vector
37 Graphic3d_TOD_VEC3, //!< 3-components float vector
38 Graphic3d_TOD_VEC4, //!< 4-components float vector
39 Graphic3d_TOD_VEC4UB, //!< 4-components unsigned byte vector
4a535d3f 40 Graphic3d_TOD_FLOAT, //!< float value
871fa103 41};
42
43//! Vertex attribute definition.
44struct Graphic3d_Attribute
45{
46 Graphic3d_TypeOfAttribute Id; //!< attribute identifier in vertex shader, 0 is reserved for vertex position
47 Graphic3d_TypeOfData DataType; //!< vec2,vec3,vec4,vec4ub
48
49 Standard_Integer Stride() const { return Stride (DataType); }
50
51 //! @return size of attribute of specified data type
52 static Standard_Integer Stride (const Graphic3d_TypeOfData theType)
53 {
54 switch (theType)
55 {
56 case Graphic3d_TOD_USHORT: return sizeof(unsigned short);
57 case Graphic3d_TOD_UINT: return sizeof(unsigned int);
58 case Graphic3d_TOD_VEC2: return sizeof(Graphic3d_Vec2);
59 case Graphic3d_TOD_VEC3: return sizeof(Graphic3d_Vec3);
60 case Graphic3d_TOD_VEC4: return sizeof(Graphic3d_Vec4);
61 case Graphic3d_TOD_VEC4UB: return sizeof(Graphic3d_Vec4ub);
4a535d3f 62 case Graphic3d_TOD_FLOAT: return sizeof(float);
871fa103 63 }
64 return 0;
65 }
66
67};
68
4a535d3f 69typedef NCollection_Array1<Graphic3d_Attribute> Graphic3d_Array1OfAttribute;
70
871fa103 71//! Buffer of vertex attributes.
72class Graphic3d_Buffer : public NCollection_Buffer
73{
74public:
75
76 //! Empty constructor.
77 Graphic3d_Buffer (const Handle(NCollection_BaseAllocator)& theAlloc)
78 : NCollection_Buffer (theAlloc),
79 Stride (0),
80 NbElements (0),
81 NbAttributes (0)
82 {
83 //
84 }
85
86 //! @return array of attributes definitions
87 const Graphic3d_Attribute* AttributesArray() const
88 {
89 return (Graphic3d_Attribute* )(myData + mySize);
90 }
91
92 //! @return attribute definition
93 const Graphic3d_Attribute& Attribute (const Standard_Integer theAttribIndex) const
94 {
95 return AttributesArray()[theAttribIndex];
96 }
97
98 //! @return attribute definition
99 Graphic3d_Attribute& ChangeAttribute (const Standard_Integer theAttribIndex)
100 {
101 return *((Graphic3d_Attribute* )(myData + mySize) + theAttribIndex);
102 }
103
104 //! @return data offset to specified attribute
105 Standard_Integer AttributeOffset (const Standard_Integer theAttribIndex) const
106 {
107 Standard_Integer anOffset = 0;
108 for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribIndex; ++anAttribIter)
109 {
110 anOffset += Graphic3d_Attribute::Stride (Attribute (anAttribIter).DataType);
111 }
112 return anOffset;
113 }
114
115 using NCollection_Buffer::Data;
116 using NCollection_Buffer::ChangeData;
117
118 //! @return data for specified attribute
119 const Standard_Byte* Data (const Standard_Integer theAttribIndex) const
120 {
121 return myData + AttributeOffset (theAttribIndex);
122 }
123
124 //! @return data for specified attribute
125 Standard_Byte* ChangeData (const Standard_Integer theAttribIndex)
126 {
127 return myData + AttributeOffset (theAttribIndex);
128 }
129
130 //! Access specified element.
131 inline const Standard_Byte* value (const Standard_Integer theElem) const
132 {
133 return myData + Stride * size_t(theElem);
134 }
135
136 //! Access specified element.
137 inline Standard_Byte* changeValue (const Standard_Integer theElem)
138 {
139 return myData + Stride * size_t(theElem);
140 }
141
142 //! Access element with specified position and type.
143 template <typename Type_t>
144 inline const Type_t& Value (const Standard_Integer theElem) const
145 {
146 return *reinterpret_cast<const Type_t*>(value (theElem));
147 }
148
149 //! Access element with specified position and type.
150 template <typename Type_t>
151 inline Type_t& ChangeValue (const Standard_Integer theElem)
152 {
153 return *reinterpret_cast<Type_t* >(changeValue (theElem));
154 }
155
156 //! Release buffer.
157 void release()
158 {
159 Free();
160 Stride = 0;
161 NbElements = 0;
162 NbAttributes = 0;
163 }
164
165 //! Allocates new empty array
166 bool Init (const Standard_Integer theNbElems,
167 const Graphic3d_Attribute* theAttribs,
168 const Standard_Integer theNbAttribs)
169 {
170 release();
171 Standard_Integer aStride = 0;
172 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
173 {
174 const Graphic3d_Attribute& anAttrib = theAttribs[anAttribIter];
175 aStride += anAttrib.Stride();
176 }
177 if (aStride == 0)
178 {
179 return false;
180 }
181
182 Stride = aStride;
183 NbElements = theNbElems;
184 NbAttributes = theNbAttribs;
185 if (NbElements != 0)
186 {
187 const size_t aDataSize = size_t(Stride) * size_t(NbElements);
188 if (!Allocate (aDataSize + sizeof(Graphic3d_Attribute) * NbAttributes))
189 {
190 release();
191 return false;
192 }
193
194 mySize = aDataSize;
195 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
196 {
197 ChangeAttribute (anAttribIter) = theAttribs[anAttribIter];
198 }
199 }
200 return true;
201 }
202
4a535d3f 203 //! Allocates new empty array
204 bool Init (const Standard_Integer theNbElems,
205 const Graphic3d_Array1OfAttribute& theAttribs)
206 {
207 return Init (theNbElems, &theAttribs.First(), theAttribs.Size());
208 }
209
871fa103 210public:
211
212 Standard_Integer Stride; //!< the distance to the attributes of the next vertex
213 Standard_Integer NbElements; //!< number of the elements
214 Standard_Integer NbAttributes; //!< number of vertex attributes
215
7d3e64ef 216public:
217
92efcf78 218 DEFINE_STANDARD_RTTI_INLINE(Graphic3d_Buffer,NCollection_Buffer) // Type definition
7d3e64ef 219
871fa103 220};
221
7d3e64ef 222DEFINE_STANDARD_HANDLE(Graphic3d_Buffer, NCollection_Buffer)
871fa103 223
224#endif // _Graphic3d_Buffer_HeaderFile