1
/* OpenSceneGraph example, osgsharedarray.
3
* Permission is hereby granted, free of charge, to any person obtaining a copy
4
* of this software and associated documentation files (the "Software"), to deal
5
* in the Software without restriction, including without limitation the rights
6
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
* copies of the Software, and to permit persons to whom the Software is
8
* furnished to do so, subject to the following conditions:
10
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
#include <osg/Geometry>
22
#include <osgViewer/Viewer>
24
/** This class is an example of how to create your own subclass of osg::Array. This
25
* is useful if your application has data in its own form of storage and you don't
26
* want to make another copy into one of the predefined osg::Array classes.
28
* @note This is not really intended to be a useful subclass of osg::Array. It
29
* doesn't do anything smart about memory management. It is simply intended as
30
* an example you can follow to create your own subclasses of osg::Array for
31
* your application's storage requirements.
33
class MyArray : public osg::Array {
35
/** Default ctor. Creates an empty array. */
37
osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT),
44
* @param no The number of elements in the array.
45
* @param ptr Pointer to the data. This class just keeps that
46
* pointer. It doesn't manage the memory.
48
MyArray(unsigned int no, osg::Vec3* ptr) :
49
osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT),
55
MyArray(const MyArray& other, const osg::CopyOp& copyop) :
56
osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT),
57
_numElements(other._numElements),
61
/** What type of object would clone return? */
62
virtual Object* cloneType() const {
66
/** Create a copy of the object. */
67
virtual osg::Object* clone(const osg::CopyOp& copyop) const {
68
return new MyArray(*this,copyop);
71
/** Accept method for ArrayVisitors.
73
* @note This will end up in ArrayVisitor::apply(osg::Array&).
75
virtual void accept(osg::ArrayVisitor& av) {
79
/** Const accept method for ArrayVisitors.
81
* @note This will end up in ConstArrayVisitor::apply(const osg::Array&).
83
virtual void accept(osg::ConstArrayVisitor& cav) const {
87
/** Accept method for ValueVisitors. */
88
virtual void accept(unsigned int index, osg::ValueVisitor& vv) {
89
vv.apply(_ptr[index]);
92
/** Const accept method for ValueVisitors. */
93
virtual void accept(unsigned int index, osg::ConstValueVisitor& cvv) const {
94
cvv.apply(_ptr[index]);
98
* Return -1 if lhs element is less than rhs element, 0 if equal,
99
* 1 if lhs element is greater than rhs element.
101
virtual int compare(unsigned int lhs,unsigned int rhs) const {
102
const osg::Vec3& elem_lhs = _ptr[lhs];
103
const osg::Vec3& elem_rhs = _ptr[rhs];
104
if (elem_lhs<elem_rhs) return -1;
105
if (elem_rhs<elem_lhs) return 1;
109
/** Returns a pointer to the first element of the array. */
110
virtual const GLvoid* getDataPointer() const {
114
/** Returns the number of elements in the array. */
115
virtual unsigned int getNumElements() const {
119
/** Returns the number of bytes of storage required to hold
120
* all of the elements of the array.
122
virtual unsigned int getTotalDataSize() const {
123
return _numElements * sizeof(osg::Vec3);
127
unsigned int _numElements;
131
/** The data values for the example. Simply defines a cube with
132
* per-face colors and normals.
136
static const osg::Vec3 myVertices[] = { osg::Vec3(-1.,-1.,-1.),
137
osg::Vec3( 1.,-1.,-1.),
138
osg::Vec3(-1., 1.,-1.),
139
osg::Vec3( 1., 1.,-1.),
140
osg::Vec3(-1.,-1., 1.),
141
osg::Vec3( 1.,-1., 1.),
142
osg::Vec3(-1., 1., 1.),
143
osg::Vec3( 1., 1., 1.)
146
static const osg::Vec3 myNormals[] = { osg::Vec3( 0., 0., 1.),
147
osg::Vec3( 1., 0., 0.),
148
osg::Vec3( 0., 0.,-1.),
149
osg::Vec3(-1., 0., 0.),
150
osg::Vec3( 0., 1., 0.),
151
osg::Vec3( 0.,-1., 0.)
154
static const osg::Vec4 myColors[] = { osg::Vec4( 1., 0., 0., 1.),
155
osg::Vec4( 0., 1., 0., 1.),
156
osg::Vec4( 1., 1., 0., 1.),
157
osg::Vec4( 0., 0., 1., 1.),
158
osg::Vec4( 1., 0., 1., 1.),
159
osg::Vec4( 0., 1., 1., 1.)
162
static const unsigned short myIndices[] = { 4, 5, 7, 6,
171
/** Create a Geode that describes a cube using our own
172
* subclass of osg::Array for the vertices. It uses
173
* the "regular" array classes for all of the other
176
* Creating your own Array class isn't really very
177
* useful for a tiny amount of data like this. You
178
* could just go ahead and copy the data into one of
179
* the "regular" Array classes like this does for
180
* normals and colors. The point of creating your
181
* own subclass of Array is for use with datasets
182
* that are much larger than anything you could
183
* create a simple example from. In that case, you
184
* might not want to create a copy of the data in
185
* one of the Array classes that comes with OSG, but
186
* instead reuse the copy your application already
187
* has and wrap it up in a subclass of osg::Array
188
* that presents the right interface for use with
191
* Note that I'm only using the shared array for the
192
* vertices. You could do something similar for any
193
* of the Geometry node's data arrays.
195
osg::Geode* createGeometry()
197
osg::Geode* geode = new osg::Geode();
200
osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
202
// add vertices using MyArray class
203
unsigned int numVertices = sizeof(myVertices)/sizeof(myVertices[0]);
204
geom->setVertexArray(new MyArray(numVertices,const_cast<osg::Vec3*>(&myVertices[0])));
207
unsigned int numNormals = sizeof(myNormals)/sizeof(myNormals[0]);
208
geom->setNormalArray(new osg::Vec3Array(numNormals,const_cast<osg::Vec3*>(&myNormals[0])));
209
geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);
212
unsigned int numColors = sizeof(myColors)/sizeof(myColors[0]);
213
osg::Vec4Array* normal_array = new osg::Vec4Array(numColors,const_cast<osg::Vec4*>(&myColors[0]));
214
geom->setColorArray(normal_array);
215
geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
218
unsigned int numIndices = sizeof(myIndices)/sizeof(myIndices[0]);
219
geom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS,
221
const_cast<unsigned short *>(myIndices)));
223
// Changing these flags will tickle different cases in
224
// Geometry::drawImplementation. They should all work fine
225
// with the shared array. Setting VertexIndices will hit
227
geom->setUseVertexBufferObjects(false);
228
geom->setUseDisplayList(false);
229
geom->setFastPathHint(false);
231
geode->addDrawable( geom.get() );
236
int main(int , char **)
238
// construct the viewer.
239
osgViewer::Viewer viewer;
241
// add model to viewer.
242
viewer.setSceneData( createGeometry() );
244
// create the windows and run the threads.