File indexing completed on 2025-01-18 10:03:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _Graphic3d_Buffer_HeaderFile
0015 #define _Graphic3d_Buffer_HeaderFile
0016
0017 #include <Graphic3d_BufferRange.hxx>
0018 #include <Graphic3d_Vec.hxx>
0019 #include <NCollection_Array1.hxx>
0020 #include <NCollection_Buffer.hxx>
0021 #include <Standard_NotImplemented.hxx>
0022
0023
0024 enum Graphic3d_TypeOfAttribute
0025 {
0026 Graphic3d_TOA_POS = 0,
0027 Graphic3d_TOA_NORM,
0028 Graphic3d_TOA_UV,
0029 Graphic3d_TOA_COLOR,
0030 Graphic3d_TOA_CUSTOM,
0031 };
0032
0033
0034 enum Graphic3d_TypeOfData
0035 {
0036 Graphic3d_TOD_USHORT,
0037 Graphic3d_TOD_UINT,
0038 Graphic3d_TOD_VEC2,
0039 Graphic3d_TOD_VEC3,
0040 Graphic3d_TOD_VEC4,
0041 Graphic3d_TOD_VEC4UB,
0042 Graphic3d_TOD_FLOAT,
0043 };
0044
0045
0046 struct Graphic3d_Attribute
0047 {
0048 Graphic3d_TypeOfAttribute Id;
0049 Graphic3d_TypeOfData DataType;
0050
0051 Standard_Integer Stride() const { return Stride (DataType); }
0052
0053
0054 static Standard_Integer Stride (const Graphic3d_TypeOfData theType)
0055 {
0056 switch (theType)
0057 {
0058 case Graphic3d_TOD_USHORT: return sizeof(unsigned short);
0059 case Graphic3d_TOD_UINT: return sizeof(unsigned int);
0060 case Graphic3d_TOD_VEC2: return sizeof(Graphic3d_Vec2);
0061 case Graphic3d_TOD_VEC3: return sizeof(Graphic3d_Vec3);
0062 case Graphic3d_TOD_VEC4: return sizeof(Graphic3d_Vec4);
0063 case Graphic3d_TOD_VEC4UB: return sizeof(Graphic3d_Vec4ub);
0064 case Graphic3d_TOD_FLOAT: return sizeof(float);
0065 }
0066 return 0;
0067 }
0068
0069 };
0070
0071 typedef NCollection_Array1<Graphic3d_Attribute> Graphic3d_Array1OfAttribute;
0072
0073
0074 class Graphic3d_Buffer : public NCollection_Buffer
0075 {
0076 DEFINE_STANDARD_RTTIEXT(Graphic3d_Buffer, NCollection_Buffer)
0077 public:
0078
0079
0080 Standard_EXPORT static const Handle(NCollection_BaseAllocator)& DefaultAllocator();
0081
0082 public:
0083
0084
0085 Graphic3d_Buffer (const Handle(NCollection_BaseAllocator)& theAlloc)
0086 : NCollection_Buffer (theAlloc),
0087 Stride (0),
0088 NbElements (0),
0089 NbAttributes (0)
0090 {
0091
0092 }
0093
0094
0095
0096 Standard_Integer NbMaxElements() const { return Stride != 0 ? Standard_Integer(mySize / size_t(Stride)) : 0; }
0097
0098
0099 const Graphic3d_Attribute* AttributesArray() const
0100 {
0101 return (Graphic3d_Attribute* )(myData + mySize);
0102 }
0103
0104
0105 const Graphic3d_Attribute& Attribute (const Standard_Integer theAttribIndex) const
0106 {
0107 return AttributesArray()[theAttribIndex];
0108 }
0109
0110
0111 Graphic3d_Attribute& ChangeAttribute (const Standard_Integer theAttribIndex)
0112 {
0113 return *((Graphic3d_Attribute* )(myData + mySize) + theAttribIndex);
0114 }
0115
0116
0117
0118
0119 Standard_Integer FindAttribute (Graphic3d_TypeOfAttribute theAttrib) const
0120 {
0121 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0122 {
0123 const Graphic3d_Attribute& anAttrib = Attribute (anAttribIter);
0124 if (anAttrib.Id == theAttrib)
0125 {
0126 return anAttribIter;
0127 }
0128 }
0129 return -1;
0130 }
0131
0132
0133 public:
0134
0135
0136 Standard_Integer AttributeOffset (const Standard_Integer theAttribIndex) const
0137 {
0138 Standard_Integer anOffset = 0;
0139 for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribIndex; ++anAttribIter)
0140 {
0141 anOffset += Graphic3d_Attribute::Stride (Attribute (anAttribIter).DataType);
0142 }
0143 return anOffset;
0144 }
0145
0146
0147 const Standard_Byte* Data (const Standard_Integer theAttribIndex) const
0148 {
0149 return myData + AttributeOffset (theAttribIndex);
0150 }
0151
0152
0153 Standard_Byte* ChangeData (const Standard_Integer theAttribIndex)
0154 {
0155 return myData + AttributeOffset (theAttribIndex);
0156 }
0157
0158
0159 inline const Standard_Byte* value (const Standard_Integer theElem) const
0160 {
0161 return myData + Stride * size_t(theElem);
0162 }
0163
0164
0165 inline Standard_Byte* changeValue (const Standard_Integer theElem)
0166 {
0167 return myData + Stride * size_t(theElem);
0168 }
0169
0170
0171 template <typename Type_t>
0172 inline const Type_t& Value (const Standard_Integer theElem) const
0173 {
0174 return *reinterpret_cast<const Type_t*>(value (theElem));
0175 }
0176
0177
0178 template <typename Type_t>
0179 inline Type_t& ChangeValue (const Standard_Integer theElem)
0180 {
0181 return *reinterpret_cast<Type_t* >(changeValue (theElem));
0182 }
0183
0184
0185 public:
0186
0187 using NCollection_Buffer::Data;
0188 using NCollection_Buffer::ChangeData;
0189
0190
0191
0192
0193
0194 Standard_Byte* ChangeAttributeData (Graphic3d_TypeOfAttribute theAttrib,
0195 Standard_Integer& theAttribIndex,
0196 Standard_Size& theAttribStride)
0197 {
0198 return (Standard_Byte* )AttributeData (theAttrib, theAttribIndex, theAttribStride);
0199 }
0200
0201
0202
0203
0204
0205 const Standard_Byte* AttributeData (Graphic3d_TypeOfAttribute theAttrib,
0206 Standard_Integer& theAttribIndex,
0207 Standard_Size& theAttribStride) const
0208 {
0209 const Standard_Byte* aDataPtr = Data();
0210 if (IsInterleaved())
0211 {
0212 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0213 {
0214 const Graphic3d_Attribute& anAttrib = Attribute (anAttribIter);
0215 const Standard_Size anAttribStride = Graphic3d_Attribute::Stride (anAttrib.DataType);
0216 if (anAttrib.Id == theAttrib)
0217 {
0218 theAttribIndex = anAttribIter;
0219 theAttribStride = Stride;
0220 return aDataPtr;
0221 }
0222
0223 aDataPtr += anAttribStride;
0224 }
0225 }
0226 else
0227 {
0228 const Standard_Integer aNbMaxVerts = NbMaxElements();
0229 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0230 {
0231 const Graphic3d_Attribute& anAttrib = Attribute (anAttribIter);
0232 const Standard_Size anAttribStride = Graphic3d_Attribute::Stride (anAttrib.DataType);
0233 if (anAttrib.Id == theAttrib)
0234 {
0235 theAttribIndex = anAttribIter;
0236 theAttribStride = anAttribStride;
0237 return aDataPtr;
0238 }
0239
0240 aDataPtr += anAttribStride * aNbMaxVerts;
0241 }
0242 }
0243 return NULL;
0244 }
0245
0246 public:
0247
0248
0249 void release()
0250 {
0251 Free();
0252 Stride = 0;
0253 NbElements = 0;
0254 NbAttributes = 0;
0255 }
0256
0257
0258 bool Init (const Standard_Integer theNbElems,
0259 const Graphic3d_Attribute* theAttribs,
0260 const Standard_Integer theNbAttribs)
0261 {
0262 release();
0263 Standard_Integer aStride = 0;
0264 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0265 {
0266 const Graphic3d_Attribute& anAttrib = theAttribs[anAttribIter];
0267 aStride += anAttrib.Stride();
0268 }
0269 if (aStride == 0)
0270 {
0271 return false;
0272 }
0273
0274 Stride = aStride;
0275 NbElements = theNbElems;
0276 NbAttributes = theNbAttribs;
0277 if (NbElements != 0)
0278 {
0279 const size_t aDataSize = size_t(Stride) * size_t(NbElements);
0280 if (!Allocate (aDataSize + sizeof(Graphic3d_Attribute) * NbAttributes))
0281 {
0282 release();
0283 return false;
0284 }
0285
0286 mySize = aDataSize;
0287 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0288 {
0289 ChangeAttribute (anAttribIter) = theAttribs[anAttribIter];
0290 }
0291 }
0292 return true;
0293 }
0294
0295
0296 bool Init (const Standard_Integer theNbElems,
0297 const Graphic3d_Array1OfAttribute& theAttribs)
0298 {
0299 return Init (theNbElems, &theAttribs.First(), theAttribs.Size());
0300 }
0301
0302 public:
0303
0304
0305
0306 virtual Standard_Boolean IsInterleaved() const { return Standard_True; }
0307
0308
0309
0310 virtual Standard_Boolean IsMutable() const { return Standard_False; }
0311
0312
0313
0314 virtual Graphic3d_BufferRange InvalidatedRange() const { return Graphic3d_BufferRange(); }
0315
0316
0317
0318 virtual void Validate() {}
0319
0320
0321 virtual void Invalidate() {}
0322
0323
0324 Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const Standard_OVERRIDE;
0325
0326 public:
0327
0328 Standard_Integer Stride;
0329 Standard_Integer NbElements;
0330 Standard_Integer NbAttributes;
0331
0332 };
0333
0334 DEFINE_STANDARD_HANDLE(Graphic3d_Buffer, NCollection_Buffer)
0335
0336 #endif