root/OpenSceneGraph/trunk/examples/osgsharedarray/osgsharedarray.cpp
@
12941
| Revision 12941, 9.0 kB (checked in by robert, 16 months ago) | |
|---|---|
|
|
| Rev | Line | |
|---|---|---|
| [7667] | 1 | /* OpenSceneGraph example, osgsharedarray. |
| 2 | * | |
| 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: | |
| 9 | * | |
| 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 | |
| 16 | * THE SOFTWARE. | |
| 17 | */ | |
| 18 | ||
| 19 | #include <osg/Array> | |
| 20 | #include <osg/Geode> | |
| 21 | #include <osg/Geometry> | |
| 22 | #include <osgViewer/Viewer> | |
| 23 | ||
| 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. | |
| 27 | * | |
| 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. | |
| 32 | */ | |
| 33 | class MyArray : public osg::Array { | |
| 34 | public: | |
| 35 | /** Default ctor. Creates an empty array. */ | |
| 36 | MyArray() : | |
| 37 | osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT), | |
| 38 | _numElements(0), | |
| 39 | _ptr(NULL) { | |
| 40 | } | |
| 41 | ||
| 42 | /** "Normal" ctor. | |
| 43 | * | |
| 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. | |
| 47 | */ | |
| 48 | MyArray(unsigned int no, osg::Vec3* ptr) : | |
| 49 | osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT), | |
| 50 | _numElements(no), | |
| 51 | _ptr(ptr) { | |
| 52 | } | |
| 53 | ||
| 54 | /** Copy ctor. */ | |
| 55 | MyArray(const MyArray& other, const osg::CopyOp& copyop) : | |
| 56 | osg::Array(osg::Array::Vec3ArrayType,3,GL_FLOAT), | |
| 57 | _numElements(other._numElements), | |
| 58 | _ptr(other._ptr) { | |
| 59 | } | |
| 60 | ||
| 61 | /** What type of object would clone return? */ | |
| 62 | virtual Object* cloneType() const { | |
| 63 | return new MyArray(); | |
| 64 | } | |
| 65 | ||
| 66 | /** Create a copy of the object. */ | |
| 67 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { | |
| 68 | return new MyArray(*this,copyop); | |
| 69 | } | |
| 70 | ||
| 71 | /** Accept method for ArrayVisitors. | |
| 72 | * | |
| 73 | * @note This will end up in ArrayVisitor::apply(osg::Array&). | |
| 74 | */ | |
| 75 | virtual void accept(osg::ArrayVisitor& av) { | |
| 76 | av.apply(*this); | |
| 77 | } | |
| 78 | ||
| 79 | /** Const accept method for ArrayVisitors. | |
| 80 | * | |
| 81 | * @note This will end up in ConstArrayVisitor::apply(const osg::Array&). | |
| 82 | */ | |
| 83 | virtual void accept(osg::ConstArrayVisitor& cav) const { | |
| 84 | cav.apply(*this); | |
| 85 | } | |
| 86 | ||
| 87 | /** Accept method for ValueVisitors. */ | |
| 88 | virtual void accept(unsigned int index, osg::ValueVisitor& vv) { | |
| 89 | vv.apply(_ptr[index]); | |
| 90 | } | |
| 91 | ||
| 92 | /** Const accept method for ValueVisitors. */ | |
| 93 | virtual void accept(unsigned int index, osg::ConstValueVisitor& cvv) const { | |
| 94 | cvv.apply(_ptr[index]); | |
| 95 | } | |
| 96 | ||
| 97 | /** Compare method. | |
| 98 | * Return -1 if lhs element is less than rhs element, 0 if equal, | |
| 99 | * 1 if lhs element is greater than rhs element. | |
| 100 | */ | |
| 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; | |
| 106 | return 0; | |
| 107 | } | |
| 108 | ||
| 109 | /** Returns a pointer to the first element of the array. */ | |
| 110 | virtual const GLvoid* getDataPointer() const { | |
| 111 | return _ptr; | |
| 112 | } | |
| 113 | ||
| 114 | /** Returns the number of elements in the array. */ | |
| 115 | virtual unsigned int getNumElements() const { | |
| 116 | return _numElements; | |
| 117 | } | |
| 118 | ||
| 119 | /** Returns the number of bytes of storage required to hold | |
| 120 | * all of the elements of the array. | |
| 121 | */ | |
| 122 | virtual unsigned int getTotalDataSize() const { | |
| 123 | return _numElements * sizeof(osg::Vec3); | |
| 124 | } | |
| 125 | ||
| 126 | private: | |
| 127 | unsigned int _numElements; | |
| 128 | osg::Vec3* _ptr; | |
| 129 | }; | |
| 130 | ||
| 131 | /** The data values for the example. Simply defines a cube with | |
| 132 | * per-face colors and normals. | |
| 133 | */ | |
| 134 | namespace { | |
| 135 | } | |
| 136 | ||
| 137 | /** Create a Geode that describes a cube using our own | |
| 138 | * subclass of osg::Array for the vertices. It uses | |
| 139 | * the "regular" array classes for all of the other | |
| 140 | * arrays. | |
| 141 | * | |
| 142 | * Creating your own Array class isn't really very | |
| 143 | * useful for a tiny amount of data like this. You | |
| 144 | * could just go ahead and copy the data into one of | |
| 145 | * the "regular" Array classes like this does for | |
| 146 | * normals and colors. The point of creating your | |
| 147 | * own subclass of Array is for use with datasets | |
| 148 | * that are much larger than anything you could | |
| 149 | * create a simple example from. In that case, you | |
| 150 | * might not want to create a copy of the data in | |
| 151 | * one of the Array classes that comes with OSG, but | |
| 152 | * instead reuse the copy your application already | |
| 153 | * has and wrap it up in a subclass of osg::Array | |
| 154 | * that presents the right interface for use with | |
| 155 | * OpenSceneGraph. | |
| 156 | * | |
| 157 | * Note that I'm only using the shared array for the | |
| 158 | * vertices. You could do something similar for any | |
| 159 | * of the Geometry node's data arrays. | |
| 160 | */ | |
| 161 | osg::Geode* createGeometry() | |
| 162 | { | |
| [12941] | 163 | const osg::Vec3 myVertices[] = { osg::Vec3(-1.,-1.,-1.), |
| 164 | osg::Vec3( 1.,-1.,-1.), | |
| 165 | osg::Vec3(-1., 1.,-1.), | |
| 166 | osg::Vec3( 1., 1.,-1.), | |
| 167 | osg::Vec3(-1.,-1., 1.), | |
| 168 | osg::Vec3( 1.,-1., 1.), | |
| 169 | osg::Vec3(-1., 1., 1.), | |
| 170 | osg::Vec3( 1., 1., 1.) | |
| 171 | }; | |
| 172 | ||
| 173 | const osg::Vec3 myNormals[] = { osg::Vec3( 0., 0., 1.), | |
| 174 | osg::Vec3( 1., 0., 0.), | |
| 175 | osg::Vec3( 0., 0.,-1.), | |
| 176 | osg::Vec3(-1., 0., 0.), | |
| 177 | osg::Vec3( 0., 1., 0.), | |
| 178 | osg::Vec3( 0.,-1., 0.) | |
| 179 | }; | |
| 180 | ||
| 181 | const osg::Vec4 myColors[] = { osg::Vec4( 1., 0., 0., 1.), | |
| 182 | osg::Vec4( 0., 1., 0., 1.), | |
| 183 | osg::Vec4( 1., 1., 0., 1.), | |
| 184 | osg::Vec4( 0., 0., 1., 1.), | |
| 185 | osg::Vec4( 1., 0., 1., 1.), | |
| 186 | osg::Vec4( 0., 1., 1., 1.) | |
| 187 | }; | |
| 188 | ||
| 189 | const unsigned short myIndices[] = { 4, 5, 7, 6, | |
| 190 | 5, 1, 3, 7, | |
| 191 | 1, 0, 2, 3, | |
| 192 | 0, 4, 6, 2, | |
| 193 | 6, 7, 3, 2, | |
| 194 | 0, 1, 5, 4 | |
| 195 | }; | |
| 196 | ||
| [7667] | 197 | osg::Geode* geode = new osg::Geode(); |
| 198 | ||
| 199 | // create Geometry | |
| 200 | osg::ref_ptr<osg::Geometry> geom(new osg::Geometry()); | |
| 201 | ||
| 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]))); | |
| 205 | ||
| 206 | // add normals | |
| 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); | |
| 210 | ||
| 211 | // add colors | |
| 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); | |
| 216 | ||
| 217 | // add PrimitiveSet | |
| 218 | unsigned int numIndices = sizeof(myIndices)/sizeof(myIndices[0]); | |
| 219 | geom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS, | |
| 220 | numIndices, | |
| 221 | const_cast<unsigned short *>(myIndices))); | |
| 222 | ||
| 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 | |
| 226 | // some other cases. | |
| 227 | geom->setUseVertexBufferObjects(false); | |
| 228 | geom->setUseDisplayList(false); | |
| 229 | geom->setFastPathHint(false); | |
| 230 | ||
| 231 | geode->addDrawable( geom.get() ); | |
| 232 | ||
| 233 | return geode; | |
| 234 | } | |
| 235 | ||
| 236 | int main(int , char **) | |
| 237 | { | |
| 238 | // construct the viewer. | |
| 239 | osgViewer::Viewer viewer; | |
| 240 | ||
| 241 | // add model to viewer. | |
| 242 | viewer.setSceneData( createGeometry() ); | |
| 243 | ||
| 244 | // create the windows and run the threads. | |
| 245 | return viewer.run(); | |
| 246 | } |
Note: See TracBrowser
for help on using the browser.
