/usr/share/openscenegraph/examples/osgsharedarray/osgsharedarray.cpp is in openscenegraph-examples 3.2.0~rc1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | /* OpenSceneGraph example, osgsharedarray.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <osg/Array>
#include <osg/Geode>
#include <osg/Geometry>
#include <osgViewer/Viewer>
/** This class is an example of how to create your own subclass of osg::Array. This
* is useful if your application has data in its own form of storage and you don't
* want to make another copy into one of the predefined osg::Array classes.
*
* @note This is not really intended to be a useful subclass of osg::Array. It
* doesn't do anything smart about memory management. It is simply intended as
* an example you can follow to create your own subclasses of osg::Array for
* your application's storage requirements.
*/
class MyArray : public osg::Array {
public:
/** Default ctor. Creates an empty array. */
MyArray() :
osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT),
_numElements(0),
_ptr(NULL) {
}
/** "Normal" ctor.
*
* @param no The number of elements in the array.
* @param ptr Pointer to the data. This class just keeps that
* pointer. It doesn't manage the memory.
*/
MyArray(unsigned int no, osg::Vec3* ptr) :
osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT),
_numElements(no),
_ptr(ptr) {
}
/** Copy ctor. */
MyArray(const MyArray& other, const osg::CopyOp& copyop) :
osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT),
_numElements(other._numElements),
_ptr(other._ptr) {
}
/** What type of object would clone return? */
virtual Object* cloneType() const {
return new MyArray();
}
/** Create a copy of the object. */
virtual osg::Object* clone(const osg::CopyOp& copyop) const {
return new MyArray(*this,copyop);
}
/** Accept method for ArrayVisitors.
*
* @note This will end up in ArrayVisitor::apply(osg::Array&).
*/
virtual void accept(osg::ArrayVisitor& av) {
av.apply(*this);
}
/** Const accept method for ArrayVisitors.
*
* @note This will end up in ConstArrayVisitor::apply(const osg::Array&).
*/
virtual void accept(osg::ConstArrayVisitor& cav) const {
cav.apply(*this);
}
/** Accept method for ValueVisitors. */
virtual void accept(unsigned int index, osg::ValueVisitor& vv) {
vv.apply(_ptr[index]);
}
/** Const accept method for ValueVisitors. */
virtual void accept(unsigned int index, osg::ConstValueVisitor& cvv) const {
cvv.apply(_ptr[index]);
}
/** Compare method.
* Return -1 if lhs element is less than rhs element, 0 if equal,
* 1 if lhs element is greater than rhs element.
*/
virtual int compare(unsigned int lhs,unsigned int rhs) const {
const osg::Vec3& elem_lhs = _ptr[lhs];
const osg::Vec3& elem_rhs = _ptr[rhs];
if (elem_lhs<elem_rhs) return -1;
if (elem_rhs<elem_lhs) return 1;
return 0;
}
virtual unsigned int getElementSize() const { return sizeof(osg::Vec3); }
/** Returns a pointer to the first element of the array. */
virtual const GLvoid* getDataPointer() const {
return _ptr;
}
/** Returns the number of elements in the array. */
virtual unsigned int getNumElements() const {
return _numElements;
}
/** Returns the number of bytes of storage required to hold
* all of the elements of the array.
*/
virtual unsigned int getTotalDataSize() const {
return _numElements * sizeof(osg::Vec3);
}
virtual void reserveArray(unsigned int num) { OSG_NOTICE<<"reserveArray() not supported"<<std::endl; }
virtual void resizeArray(unsigned int num) { OSG_NOTICE<<"resizeArray() not supported"<<std::endl; }
private:
unsigned int _numElements;
osg::Vec3* _ptr;
};
/** The data values for the example. Simply defines a cube with
* per-face colors and normals.
*/
const osg::Vec3 myVertices[] = { osg::Vec3(-1.,-1., 1.),
osg::Vec3( 1.,-1., 1.),
osg::Vec3( 1., 1., 1.),
osg::Vec3(-1., 1., 1.),
osg::Vec3( 1.,-1., 1.),
osg::Vec3( 1.,-1.,-1.),
osg::Vec3( 1., 1.,-1.),
osg::Vec3( 1., 1., 1.),
osg::Vec3( 1.,-1.,-1.),
osg::Vec3(-1.,-1.,-1.),
osg::Vec3(-1., 1.,-1.),
osg::Vec3( 1., 1.,-1.),
osg::Vec3(-1.,-1.,-1.),
osg::Vec3(-1.,-1., 1.),
osg::Vec3(-1., 1., 1.),
osg::Vec3(-1., 1.,-1.),
osg::Vec3(-1., 1., 1.),
osg::Vec3( 1., 1., 1.),
osg::Vec3( 1., 1.,-1.),
osg::Vec3(-1., 1.,-1.),
osg::Vec3(-1.,-1.,-1.),
osg::Vec3( 1.,-1.,-1.),
osg::Vec3( 1.,-1., 1.),
osg::Vec3(-1.,-1., 1.),
};
const osg::Vec3 myNormals[] = { osg::Vec3( 0., 0., 1.),
osg::Vec3( 0., 0., 1.),
osg::Vec3( 0., 0., 1.),
osg::Vec3( 0., 0., 1.),
osg::Vec3( 1., 0., 0.),
osg::Vec3( 1., 0., 0.),
osg::Vec3( 1., 0., 0.),
osg::Vec3( 1., 0., 0.),
osg::Vec3( 0., 0.,-1.),
osg::Vec3( 0., 0.,-1.),
osg::Vec3( 0., 0.,-1.),
osg::Vec3( 0., 0.,-1.),
osg::Vec3(-1., 0., 0.),
osg::Vec3(-1., 0., 0.),
osg::Vec3(-1., 0., 0.),
osg::Vec3(-1., 0., 0.),
osg::Vec3( 0., 1., 0.),
osg::Vec3( 0., 1., 0.),
osg::Vec3( 0., 1., 0.),
osg::Vec3( 0., 1., 0.),
osg::Vec3( 0.,-1., 0.),
osg::Vec3( 0.,-1., 0.),
osg::Vec3( 0.,-1., 0.),
osg::Vec3( 0.,-1., 0.)
};
const osg::Vec4 myColors[] = { osg::Vec4( 1., 0., 0., 1.),
osg::Vec4( 1., 0., 0., 1.),
osg::Vec4( 1., 0., 0., 1.),
osg::Vec4( 1., 0., 0., 1.),
osg::Vec4( 0., 1., 0., 1.),
osg::Vec4( 0., 1., 0., 1.),
osg::Vec4( 0., 1., 0., 1.),
osg::Vec4( 0., 1., 0., 1.),
osg::Vec4( 1., 1., 0., 1.),
osg::Vec4( 1., 1., 0., 1.),
osg::Vec4( 1., 1., 0., 1.),
osg::Vec4( 1., 1., 0., 1.),
osg::Vec4( 0., 0., 1., 1.),
osg::Vec4( 0., 0., 1., 1.),
osg::Vec4( 0., 0., 1., 1.),
osg::Vec4( 0., 0., 1., 1.),
osg::Vec4( 1., 0., 1., 1.),
osg::Vec4( 1., 0., 1., 1.),
osg::Vec4( 1., 0., 1., 1.),
osg::Vec4( 1., 0., 1., 1.),
osg::Vec4( 0., 1., 1., 1.),
osg::Vec4( 0., 1., 1., 1.),
osg::Vec4( 0., 1., 1., 1.),
osg::Vec4( 0., 1., 1., 1.)
};
/** Create a Geode that describes a cube using our own
* subclass of osg::Array for the vertices. It uses
* the "regular" array classes for all of the other
* arrays.
*
* Creating your own Array class isn't really very
* useful for a tiny amount of data like this. You
* could just go ahead and copy the data into one of
* the "regular" Array classes like this does for
* normals and colors. The point of creating your
* own subclass of Array is for use with datasets
* that are much larger than anything you could
* create a simple example from. In that case, you
* might not want to create a copy of the data in
* one of the Array classes that comes with OSG, but
* instead reuse the copy your application already
* has and wrap it up in a subclass of osg::Array
* that presents the right interface for use with
* OpenSceneGraph.
*
* Note that I'm only using the shared array for the
* vertices. You could do something similar for any
* of the Geometry node's data arrays.
*/
osg::Geode* createGeometry()
{
osg::Geode* geode = new osg::Geode();
// create Geometry
osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
// add vertices using MyArray class
unsigned int numVertices = sizeof(myVertices)/sizeof(myVertices[0]);
geom->setVertexArray(new MyArray(numVertices,const_cast<osg::Vec3*>(&myVertices[0])));
// add normals
unsigned int numNormals = sizeof(myNormals)/sizeof(myNormals[0]);
geom->setNormalArray(new osg::Vec3Array(numNormals,const_cast<osg::Vec3*>(&myNormals[0])), osg::Array::BIND_PER_VERTEX);
// add colors
unsigned int numColors = sizeof(myColors)/sizeof(myColors[0]);
osg::Vec4Array* normal_array = new osg::Vec4Array(numColors,const_cast<osg::Vec4*>(&myColors[0]));
geom->setColorArray(normal_array, osg::Array::BIND_PER_VERTEX);
// add PrimitiveSet
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, numVertices));
// Changing these flags will tickle different cases in
// Geometry::drawImplementation. They should all work fine
// with the shared array.
geom->setUseVertexBufferObjects(false);
geom->setUseDisplayList(false);
geode->addDrawable( geom.get() );
return geode;
}
int main(int , char **)
{
// construct the viewer.
osgViewer::Viewer viewer;
// add model to viewer.
viewer.setSceneData( createGeometry() );
// create the windows and run the threads.
return viewer.run();
}
|