BufferTemplate 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
  2. * Copyright (C) 2012 David Callu
  3. * std::vector specialization : Pawel Ksiezopolski
  4. *
  5. * This library is open source and may be redistributed and/or modified under
  6. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  7. * (at your option) any later version. The full license is in LICENSE file
  8. * included with this distribution, and on the openscenegraph.org website.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * OpenSceneGraph Public License for more details.
  14. */
  15. #ifndef OSG_BUFFERTEMPLATE
  16. #define OSG_BUFFERTEMPLATE 1
  17. #include <osg/BufferObject>
  18. #include <vector>
  19. namespace osg
  20. {
  21. /** Template buffer class to be used with a struct as template parameter.
  22. * This class is useful to send C++ structures on the GPU (e.g. for uniform blocks) but be careful to the alignments rules on the GPU side !
  23. */
  24. template <typename T>
  25. class BufferTemplate : public BufferData
  26. {
  27. public:
  28. BufferTemplate():
  29. BufferData(),
  30. _data(T())
  31. {}
  32. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  33. BufferTemplate(const BufferTemplate<T>& bt,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  34. osg::BufferData(bt,copyop),
  35. _data(bt._data)
  36. {}
  37. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const BufferTemplate<T>*>(obj)!=NULL; }
  38. virtual const char* libraryName() const { return "osg"; }
  39. virtual const char* className() const { return "BufferTemplate<T>"; }
  40. virtual Object* cloneType() const { return new BufferTemplate<T>(); }
  41. virtual Object* clone(const CopyOp& copyop) const { return new BufferTemplate<T>(*this,copyop); }
  42. virtual const GLvoid* getDataPointer() const { return &_data; }
  43. virtual unsigned int getTotalDataSize() const { return sizeof(T); }
  44. const T& getData() const { return _data; }
  45. T& getData() { return _data; }
  46. void setData(const T& data) { _data = data; dirty(); }
  47. protected:
  48. virtual ~BufferTemplate() {};
  49. private:
  50. T _data;
  51. };
  52. /** BufferTemplate specialization for std::vector
  53. */
  54. template <typename T>
  55. class BufferTemplate< std::vector<T> > : public BufferData
  56. {
  57. public:
  58. BufferTemplate():
  59. BufferData(),
  60. _data()
  61. {}
  62. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  63. BufferTemplate(const BufferTemplate< std::vector<T> >& bt,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
  64. osg::BufferData(bt,copyop),
  65. _data(bt._data)
  66. {}
  67. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const BufferTemplate< std::vector<T> >*>(obj)!=NULL; }
  68. virtual const char* libraryName() const { return "osg"; }
  69. virtual const char* className() const { return "BufferTemplate<std::vector<T> >"; }
  70. virtual Object* cloneType() const { return new BufferTemplate< std::vector<T> >(); }
  71. virtual Object* clone(const CopyOp& copyop) const { return new BufferTemplate< std::vector<T> >(*this,copyop); }
  72. virtual const GLvoid* getDataPointer() const { return &_data[0]; }
  73. virtual unsigned int getTotalDataSize() const { return _data.size() * sizeof(T); }
  74. const std::vector<T>& getData() const { return _data; }
  75. std::vector<T>& getData() { return _data; }
  76. void setData(const std::vector<T>& data) { _data = data; dirty(); }
  77. protected:
  78. virtual ~BufferTemplate() {};
  79. private:
  80. std::vector<T> _data;
  81. };
  82. }
  83. #endif