1 // Copyright (c) 2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _Graphic3d_Buffer_HeaderFile
15 #define _Graphic3d_Buffer_HeaderFile
17 #include <Graphic3d_Vec.hxx>
18 #include <NCollection_Buffer.hxx>
20 //! Type of attribute in Vertex Buffer
21 enum Graphic3d_TypeOfAttribute
23 Graphic3d_TOA_POS = 0, //!< vertex position
24 Graphic3d_TOA_NORM, //!< normal
25 Graphic3d_TOA_UV, //!< texture coordinates
26 Graphic3d_TOA_COLOR, //!< per-vertex color
27 Graphic3d_TOA_CUSTOM = 10, //!< custom attributes
30 //! Type of the element in Vertex or Index Buffer
31 enum Graphic3d_TypeOfData
33 Graphic3d_TOD_USHORT, //!< unsigned 16-bit integer
34 Graphic3d_TOD_UINT, //!< unsigned 32-bit integer
35 Graphic3d_TOD_VEC2, //!< 2-components float vector
36 Graphic3d_TOD_VEC3, //!< 3-components float vector
37 Graphic3d_TOD_VEC4, //!< 4-components float vector
38 Graphic3d_TOD_VEC4UB, //!< 4-components unsigned byte vector
41 //! Vertex attribute definition.
42 struct Graphic3d_Attribute
44 Graphic3d_TypeOfAttribute Id; //!< attribute identifier in vertex shader, 0 is reserved for vertex position
45 Graphic3d_TypeOfData DataType; //!< vec2,vec3,vec4,vec4ub
47 Standard_Integer Stride() const { return Stride (DataType); }
49 //! @return size of attribute of specified data type
50 static Standard_Integer Stride (const Graphic3d_TypeOfData theType)
54 case Graphic3d_TOD_USHORT: return sizeof(unsigned short);
55 case Graphic3d_TOD_UINT: return sizeof(unsigned int);
56 case Graphic3d_TOD_VEC2: return sizeof(Graphic3d_Vec2);
57 case Graphic3d_TOD_VEC3: return sizeof(Graphic3d_Vec3);
58 case Graphic3d_TOD_VEC4: return sizeof(Graphic3d_Vec4);
59 case Graphic3d_TOD_VEC4UB: return sizeof(Graphic3d_Vec4ub);
66 //! Buffer of vertex attributes.
67 class Graphic3d_Buffer : public NCollection_Buffer
71 //! Empty constructor.
72 Graphic3d_Buffer (const Handle(NCollection_BaseAllocator)& theAlloc)
73 : NCollection_Buffer (theAlloc),
81 //! @return array of attributes definitions
82 const Graphic3d_Attribute* AttributesArray() const
84 return (Graphic3d_Attribute* )(myData + mySize);
87 //! @return attribute definition
88 const Graphic3d_Attribute& Attribute (const Standard_Integer theAttribIndex) const
90 return AttributesArray()[theAttribIndex];
93 //! @return attribute definition
94 Graphic3d_Attribute& ChangeAttribute (const Standard_Integer theAttribIndex)
96 return *((Graphic3d_Attribute* )(myData + mySize) + theAttribIndex);
99 //! @return data offset to specified attribute
100 Standard_Integer AttributeOffset (const Standard_Integer theAttribIndex) const
102 Standard_Integer anOffset = 0;
103 for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribIndex; ++anAttribIter)
105 anOffset += Graphic3d_Attribute::Stride (Attribute (anAttribIter).DataType);
110 using NCollection_Buffer::Data;
111 using NCollection_Buffer::ChangeData;
113 //! @return data for specified attribute
114 const Standard_Byte* Data (const Standard_Integer theAttribIndex) const
116 return myData + AttributeOffset (theAttribIndex);
119 //! @return data for specified attribute
120 Standard_Byte* ChangeData (const Standard_Integer theAttribIndex)
122 return myData + AttributeOffset (theAttribIndex);
125 //! Access specified element.
126 inline const Standard_Byte* value (const Standard_Integer theElem) const
128 return myData + Stride * size_t(theElem);
131 //! Access specified element.
132 inline Standard_Byte* changeValue (const Standard_Integer theElem)
134 return myData + Stride * size_t(theElem);
137 //! Access element with specified position and type.
138 template <typename Type_t>
139 inline const Type_t& Value (const Standard_Integer theElem) const
141 return *reinterpret_cast<const Type_t*>(value (theElem));
144 //! Access element with specified position and type.
145 template <typename Type_t>
146 inline Type_t& ChangeValue (const Standard_Integer theElem)
148 return *reinterpret_cast<Type_t* >(changeValue (theElem));
160 //! Allocates new empty array
161 bool Init (const Standard_Integer theNbElems,
162 const Graphic3d_Attribute* theAttribs,
163 const Standard_Integer theNbAttribs)
166 Standard_Integer aStride = 0;
167 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
169 const Graphic3d_Attribute& anAttrib = theAttribs[anAttribIter];
170 aStride += anAttrib.Stride();
178 NbElements = theNbElems;
179 NbAttributes = theNbAttribs;
182 const size_t aDataSize = size_t(Stride) * size_t(NbElements);
183 if (!Allocate (aDataSize + sizeof(Graphic3d_Attribute) * NbAttributes))
190 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
192 ChangeAttribute (anAttribIter) = theAttribs[anAttribIter];
200 Standard_Integer Stride; //!< the distance to the attributes of the next vertex
201 Standard_Integer NbElements; //!< number of the elements
202 Standard_Integer NbAttributes; //!< number of vertex attributes
206 DEFINE_STANDARD_RTTI(Graphic3d_Buffer, NCollection_Buffer) // Type definition
210 DEFINE_STANDARD_HANDLE(Graphic3d_Buffer, NCollection_Buffer)
212 #endif // _Graphic3d_Buffer_HeaderFile