File indexing completed on 2025-01-18 10:04:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef _OpenGl_BufferCompatT_HeaderFile
0016 #define _OpenGl_BufferCompatT_HeaderFile
0017
0018 #include <NCollection_Buffer.hxx>
0019 #include <OpenGl_Buffer.hxx>
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 template<class BaseBufferT>
0036 class OpenGl_BufferCompatT : public BaseBufferT
0037 {
0038
0039 public:
0040
0041
0042 OpenGl_BufferCompatT()
0043 {
0044
0045 }
0046
0047
0048 virtual ~OpenGl_BufferCompatT()
0049 {
0050 Release (NULL);
0051 }
0052
0053
0054 virtual bool IsVirtual() const Standard_OVERRIDE { return true; }
0055
0056
0057
0058 inline bool Create (const Handle(OpenGl_Context)& theGlCtx) Standard_OVERRIDE;
0059
0060
0061 inline virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
0062
0063
0064 virtual void Bind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
0065 {
0066
0067 }
0068
0069
0070 virtual void Unbind (const Handle(OpenGl_Context)& ) const Standard_OVERRIDE
0071 {
0072
0073 }
0074
0075 public:
0076
0077
0078
0079 inline bool initLink (const Handle(NCollection_Buffer)& theData,
0080 const unsigned int theComponentsNb,
0081 const Standard_Integer theElemsNb,
0082 const unsigned int theDataType);
0083
0084
0085 inline virtual bool init (const Handle(OpenGl_Context)& theGlCtx,
0086 const unsigned int theComponentsNb,
0087 const Standard_Integer theElemsNb,
0088 const void* theData,
0089 const unsigned int theDataType,
0090 const Standard_Integer theStride) Standard_OVERRIDE;
0091
0092
0093 inline virtual bool subData (const Handle(OpenGl_Context)& theGlCtx,
0094 const Standard_Integer theElemFrom,
0095 const Standard_Integer theElemsNb,
0096 const void* theData,
0097 const unsigned int theDataType) Standard_OVERRIDE;
0098
0099
0100 inline virtual bool getSubData (const Handle(OpenGl_Context)& theGlCtx,
0101 const Standard_Integer theElemFrom,
0102 const Standard_Integer theElemsNb,
0103 void* theData,
0104 const unsigned int theDataType) Standard_OVERRIDE;
0105
0106 protected:
0107
0108 Handle(NCollection_Buffer) myData;
0109
0110 };
0111
0112
0113
0114
0115
0116 template<class BaseBufferT>
0117 bool OpenGl_BufferCompatT<BaseBufferT>::Create (const Handle(OpenGl_Context)& )
0118 {
0119 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0120 {
0121 BaseBufferT::myBufferId = (unsigned int )-1;
0122 myData = new NCollection_Buffer (Graphic3d_Buffer::DefaultAllocator());
0123 }
0124 return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER;
0125 }
0126
0127
0128
0129
0130
0131 template<class BaseBufferT>
0132 void OpenGl_BufferCompatT<BaseBufferT>::Release (OpenGl_Context* )
0133 {
0134 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0135 {
0136 return;
0137 }
0138
0139 BaseBufferT::myOffset = NULL;
0140 BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER;
0141 myData.Nullify();
0142 }
0143
0144
0145
0146
0147
0148 template<class BaseBufferT>
0149 bool OpenGl_BufferCompatT<BaseBufferT>::initLink (const Handle(NCollection_Buffer)& theData,
0150 const unsigned int theComponentsNb,
0151 const Standard_Integer theElemsNb,
0152 const unsigned int theDataType)
0153 {
0154 if (theData.IsNull())
0155 {
0156 BaseBufferT::myOffset = NULL;
0157 return false;
0158 }
0159
0160 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0161 {
0162 BaseBufferT::myBufferId = (unsigned int )-1;
0163 }
0164 myData = theData;
0165 BaseBufferT::myDataType = theDataType;
0166 BaseBufferT::myComponentsNb = theComponentsNb;
0167 BaseBufferT::myElemsNb = theElemsNb;
0168 BaseBufferT::myOffset = myData->ChangeData();
0169 return true;
0170 }
0171
0172
0173
0174
0175
0176 template<class BaseBufferT>
0177 bool OpenGl_BufferCompatT<BaseBufferT>::init (const Handle(OpenGl_Context)& theCtx,
0178 const unsigned int theComponentsNb,
0179 const Standard_Integer theElemsNb,
0180 const void* theData,
0181 const unsigned int theDataType,
0182 const Standard_Integer theStride)
0183 {
0184 if (!Create (theCtx))
0185 {
0186 BaseBufferT::myOffset = NULL;
0187 return false;
0188 }
0189
0190 BaseBufferT::myDataType = theDataType;
0191 BaseBufferT::myComponentsNb = theComponentsNb;
0192 BaseBufferT::myElemsNb = theElemsNb;
0193
0194 const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride;
0195 if (!myData->Allocate (aNbBytes))
0196 {
0197 BaseBufferT::myOffset = NULL;
0198 return false;
0199 }
0200
0201 BaseBufferT::myOffset = myData->ChangeData();
0202 if (theData != NULL)
0203 {
0204 memcpy (myData->ChangeData(), theData, aNbBytes);
0205 }
0206 return true;
0207 }
0208
0209
0210
0211
0212
0213 template<class BaseBufferT>
0214 bool OpenGl_BufferCompatT<BaseBufferT>::subData (const Handle(OpenGl_Context)& ,
0215 const Standard_Integer theElemFrom,
0216 const Standard_Integer theElemsNb,
0217 const void* theData,
0218 const unsigned int theDataType)
0219 {
0220 if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
0221 || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb))
0222 {
0223 return false;
0224 }
0225 else if (theData == NULL)
0226 {
0227 return true;
0228 }
0229
0230 const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
0231 const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0232 const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0233 memcpy (myData->ChangeData() + anOffset, theData, aNbBytes);
0234 return true;
0235 }
0236
0237
0238
0239
0240
0241 template<class BaseBufferT>
0242 bool OpenGl_BufferCompatT<BaseBufferT>::getSubData (const Handle(OpenGl_Context)& ,
0243 const Standard_Integer theElemFrom,
0244 const Standard_Integer theElemsNb,
0245 void* theData,
0246 const unsigned int theDataType)
0247 {
0248 if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType
0249 || theElemFrom < 0 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb)
0250 || theData == NULL)
0251 {
0252 return false;
0253 }
0254
0255 const size_t aDataSize = BaseBufferT::sizeOfGlType (theDataType);
0256 const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0257 const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0258 memcpy (theData, myData->Data() + anOffset, aNbBytes);
0259 return true;
0260 }
0261
0262 #endif