File indexing completed on 2026-09-20 09:18:29
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
0036
0037 template <class BaseBufferT>
0038 class OpenGl_BufferCompatT : public BaseBufferT
0039 {
0040
0041 public:
0042
0043 OpenGl_BufferCompatT()
0044 {
0045
0046 }
0047
0048
0049 ~OpenGl_BufferCompatT() override { Release(nullptr); }
0050
0051
0052 bool IsVirtual() const override { return true; }
0053
0054
0055
0056 inline bool Create(const occ::handle<OpenGl_Context>& theGlCtx) override;
0057
0058
0059 inline void Release(OpenGl_Context* theGlCtx) override;
0060
0061
0062 void Bind(const occ::handle<OpenGl_Context>&) const override
0063 {
0064
0065 }
0066
0067
0068 void Unbind(const occ::handle<OpenGl_Context>&) const override
0069 {
0070
0071 }
0072
0073 public:
0074
0075
0076 inline bool initLink(const occ::handle<NCollection_Buffer>& theData,
0077 const unsigned int theComponentsNb,
0078 const int theElemsNb,
0079 const unsigned int theDataType);
0080
0081
0082 inline bool init(const occ::handle<OpenGl_Context>& theGlCtx,
0083 const unsigned int theComponentsNb,
0084 const int theElemsNb,
0085 const void* theData,
0086 const unsigned int theDataType,
0087 const int theStride) override;
0088
0089
0090 inline bool subData(const occ::handle<OpenGl_Context>& theGlCtx,
0091 const int theElemFrom,
0092 const int theElemsNb,
0093 const void* theData,
0094 const unsigned int theDataType) override;
0095
0096
0097 inline bool getSubData(const occ::handle<OpenGl_Context>& theGlCtx,
0098 const int theElemFrom,
0099 const int theElemsNb,
0100 void* theData,
0101 const unsigned int theDataType) override;
0102
0103 protected:
0104 occ::handle<NCollection_Buffer> myData;
0105 };
0106
0107
0108
0109 template <class BaseBufferT>
0110 bool OpenGl_BufferCompatT<BaseBufferT>::Create(const occ::handle<OpenGl_Context>&)
0111 {
0112 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0113 {
0114 BaseBufferT::myBufferId = (unsigned int)-1;
0115 myData = new NCollection_Buffer(Graphic3d_Buffer::DefaultAllocator());
0116 }
0117 return BaseBufferT::myBufferId != OpenGl_Buffer::NO_BUFFER;
0118 }
0119
0120
0121
0122 template <class BaseBufferT>
0123 void OpenGl_BufferCompatT<BaseBufferT>::Release(OpenGl_Context*)
0124 {
0125 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0126 {
0127 return;
0128 }
0129
0130 BaseBufferT::myOffset = nullptr;
0131 BaseBufferT::myBufferId = OpenGl_Buffer::NO_BUFFER;
0132 myData.Nullify();
0133 }
0134
0135
0136
0137 template <class BaseBufferT>
0138 bool OpenGl_BufferCompatT<BaseBufferT>::initLink(const occ::handle<NCollection_Buffer>& theData,
0139 const unsigned int theComponentsNb,
0140 const int theElemsNb,
0141 const unsigned int theDataType)
0142 {
0143 if (theData.IsNull())
0144 {
0145 BaseBufferT::myOffset = nullptr;
0146 return false;
0147 }
0148
0149 if (BaseBufferT::myBufferId == OpenGl_Buffer::NO_BUFFER)
0150 {
0151 BaseBufferT::myBufferId = (unsigned int)-1;
0152 }
0153 myData = theData;
0154 BaseBufferT::myDataType = theDataType;
0155 BaseBufferT::myComponentsNb = theComponentsNb;
0156 BaseBufferT::myElemsNb = theElemsNb;
0157 BaseBufferT::myOffset = myData->ChangeData();
0158 return true;
0159 }
0160
0161
0162
0163 template <class BaseBufferT>
0164 bool OpenGl_BufferCompatT<BaseBufferT>::init(const occ::handle<OpenGl_Context>& theCtx,
0165 const unsigned int theComponentsNb,
0166 const int theElemsNb,
0167 const void* theData,
0168 const unsigned int theDataType,
0169 const int theStride)
0170 {
0171 if (!Create(theCtx))
0172 {
0173 BaseBufferT::myOffset = nullptr;
0174 return false;
0175 }
0176
0177 BaseBufferT::myDataType = theDataType;
0178 BaseBufferT::myComponentsNb = theComponentsNb;
0179 BaseBufferT::myElemsNb = theElemsNb;
0180
0181 const size_t aNbBytes = size_t(BaseBufferT::myElemsNb) * theStride;
0182 if (!myData->Allocate(aNbBytes))
0183 {
0184 BaseBufferT::myOffset = nullptr;
0185 return false;
0186 }
0187
0188 BaseBufferT::myOffset = myData->ChangeData();
0189 if (theData != nullptr)
0190 {
0191 memcpy(myData->ChangeData(), theData, aNbBytes);
0192 }
0193 return true;
0194 }
0195
0196
0197
0198 template <class BaseBufferT>
0199 bool OpenGl_BufferCompatT<BaseBufferT>::subData(const occ::handle<OpenGl_Context>&,
0200 const int theElemFrom,
0201 const int theElemsNb,
0202 const void* theData,
0203 const unsigned int theDataType)
0204 {
0205 if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType || theElemFrom < 0
0206 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb))
0207 {
0208 return false;
0209 }
0210 else if (theData == nullptr)
0211 {
0212 return true;
0213 }
0214
0215 const size_t aDataSize = BaseBufferT::sizeOfGlType(theDataType);
0216 const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0217 const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0218 memcpy(myData->ChangeData() + anOffset, theData, aNbBytes);
0219 return true;
0220 }
0221
0222
0223
0224 template <class BaseBufferT>
0225 bool OpenGl_BufferCompatT<BaseBufferT>::getSubData(const occ::handle<OpenGl_Context>&,
0226 const int theElemFrom,
0227 const int theElemsNb,
0228 void* theData,
0229 const unsigned int theDataType)
0230 {
0231 if (!BaseBufferT::IsValid() || BaseBufferT::myDataType != theDataType || theElemFrom < 0
0232 || ((theElemFrom + theElemsNb) > BaseBufferT::myElemsNb) || theData == nullptr)
0233 {
0234 return false;
0235 }
0236
0237 const size_t aDataSize = BaseBufferT::sizeOfGlType(theDataType);
0238 const size_t anOffset = size_t(theElemFrom) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0239 const size_t aNbBytes = size_t(theElemsNb) * size_t(BaseBufferT::myComponentsNb) * aDataSize;
0240 memcpy(theData, myData->Data() + anOffset, aNbBytes);
0241 return true;
0242 }
0243
0244 #endif