1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
2 * Copyright (C) 2012 David Callu
3 * std::vector specialization : Pawel Ksiezopolski
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.
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.
16#ifndef OSG_BUFFERTEMPLATE
17#define OSG_BUFFERTEMPLATE 1
19#include <osg/BufferObject>
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 !
29class BufferTemplate : public BufferData
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),
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>"; }
47 virtual Object* cloneType() const { return new BufferTemplate<T>(); }
48 virtual Object* clone(const CopyOp& copyop) const { return new BufferTemplate<T>(*this,copyop); }
50 virtual const GLvoid* getDataPointer() const { return &_data; }
51 virtual unsigned int getTotalDataSize() const { return sizeof(T); }
53 const T& getData() const { return _data; }
54 T& getData() { return _data; }
55 void setData(const T& data) { _data = data; dirty(); }
58 virtual ~BufferTemplate() {};
64/** BufferTemplate specialization for std::vector
67class BufferTemplate< std::vector<T> > : public BufferData
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),
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> >"; }
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); }
88 virtual const GLvoid* getDataPointer() const { return &_data[0]; }
89 virtual unsigned int getTotalDataSize() const { return _data.size() * sizeof(T); }
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(); }
96 virtual ~BufferTemplate() {};