openscenegraph
BufferTemplate
Go to the documentation of this file.
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
16#ifndef OSG_BUFFERTEMPLATE
17#define OSG_BUFFERTEMPLATE 1
18
19#include <osg/BufferObject>
20#include <vector>
21
22namespace osg
23{
24
25/** Template buffer class to be used with a struct as template parameter.
26 * 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 !
27 */
28template <typename T>
29class BufferTemplate : public BufferData
30{
31 public:
32 BufferTemplate():
33 BufferData(),
34 _data(T())
35 {}
36
37 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
38 BufferTemplate(const BufferTemplate<T>& bt,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
39 osg::BufferData(bt,copyop),
40 _data(bt._data)
41 {}
42
43 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const BufferTemplate<T>*>(obj)!=NULL; }
44 virtual const char* libraryName() const { return "osg"; }
45 virtual const char* className() const { return "BufferTemplate<T>"; }
46
47 virtual Object* cloneType() const { return new BufferTemplate<T>(); }
48 virtual Object* clone(const CopyOp& copyop) const { return new BufferTemplate<T>(*this,copyop); }
49
50 virtual const GLvoid* getDataPointer() const { return &_data; }
51 virtual unsigned int getTotalDataSize() const { return sizeof(T); }
52
53 const T& getData() const { return _data; }
54 T& getData() { return _data; }
55 void setData(const T& data) { _data = data; dirty(); }
56
57 protected:
58 virtual ~BufferTemplate() {};
59
60 private:
61 T _data;
62};
63
64/** BufferTemplate specialization for std::vector
65 */
66template <typename T>
67class BufferTemplate< std::vector<T> > : public BufferData
68{
69 public:
70 BufferTemplate():
71 BufferData(),
72 _data()
73 {}
74
75 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
76 BufferTemplate(const BufferTemplate< std::vector<T> >& bt,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
77 osg::BufferData(bt,copyop),
78 _data(bt._data)
79 {}
80
81 virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const BufferTemplate< std::vector<T> >*>(obj)!=NULL; }
82 virtual const char* libraryName() const { return "osg"; }
83 virtual const char* className() const { return "BufferTemplate<std::vector<T> >"; }
84
85 virtual Object* cloneType() const { return new BufferTemplate< std::vector<T> >(); }
86 virtual Object* clone(const CopyOp& copyop) const { return new BufferTemplate< std::vector<T> >(*this,copyop); }
87
88 virtual const GLvoid* getDataPointer() const { return &_data[0]; }
89 virtual unsigned int getTotalDataSize() const { return _data.size() * sizeof(T); }
90
91 const std::vector<T>& getData() const { return _data; }
92 std::vector<T>& getData() { return _data; }
93 void setData(const std::vector<T>& data) { _data = data; dirty(); }
94
95 protected:
96 virtual ~BufferTemplate() {};
97
98 private:
99 std::vector<T> _data;
100};
101
102}
103
104#endif