root/OpenSceneGraph/trunk/examples/osgsharedarray/osgsharedarray.cpp @ 13479

Revision 13479, 11.3 kB (checked in by robert, 4 days ago)

Added shaders to support experimental shader based displacement mapping technique osgTerrain::ShaderTerrain?.

  • Property svn:eol-style set to native
Line 
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  */
33class MyArray : public osg::Array {
34public:
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
126private:
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
135const osg::Vec3 myVertices[] = { osg::Vec3(-1.,-1., 1.),
136                                 osg::Vec3( 1.,-1., 1.),
137                                 osg::Vec3( 1., 1., 1.),
138                                 osg::Vec3(-1., 1., 1.),
139
140                                 osg::Vec3( 1.,-1., 1.),
141                                 osg::Vec3( 1.,-1.,-1.),
142                                 osg::Vec3( 1., 1.,-1.),
143                                 osg::Vec3( 1., 1., 1.),
144
145                                 osg::Vec3( 1.,-1.,-1.),
146                                 osg::Vec3(-1.,-1.,-1.),
147                                 osg::Vec3(-1., 1.,-1.),
148                                 osg::Vec3( 1., 1.,-1.),
149
150                                 osg::Vec3(-1.,-1.,-1.),
151                                 osg::Vec3(-1.,-1., 1.),
152                                 osg::Vec3(-1., 1., 1.),
153                                 osg::Vec3(-1., 1.,-1.),
154
155                                 osg::Vec3(-1., 1., 1.),
156                                 osg::Vec3( 1., 1., 1.),
157                                 osg::Vec3( 1., 1.,-1.),
158                                 osg::Vec3(-1., 1.,-1.),
159
160                                 osg::Vec3(-1.,-1.,-1.),
161                                 osg::Vec3( 1.,-1.,-1.),
162                                 osg::Vec3( 1.,-1., 1.),
163                                 osg::Vec3(-1.,-1., 1.),
164                               };
165
166
167const osg::Vec3 myNormals[] = { osg::Vec3( 0., 0., 1.),
168                                osg::Vec3( 0., 0., 1.),
169                                osg::Vec3( 0., 0., 1.),
170                                osg::Vec3( 0., 0., 1.),
171
172                                osg::Vec3( 1., 0., 0.),
173                                osg::Vec3( 1., 0., 0.),
174                                osg::Vec3( 1., 0., 0.),
175                                osg::Vec3( 1., 0., 0.),
176
177                                osg::Vec3( 0., 0.,-1.),
178                                osg::Vec3( 0., 0.,-1.),
179                                osg::Vec3( 0., 0.,-1.),
180                                osg::Vec3( 0., 0.,-1.),
181
182                                osg::Vec3(-1., 0., 0.),
183                                osg::Vec3(-1., 0., 0.),
184                                osg::Vec3(-1., 0., 0.),
185                                osg::Vec3(-1., 0., 0.),
186
187                                osg::Vec3( 0., 1., 0.),
188                                osg::Vec3( 0., 1., 0.),
189                                osg::Vec3( 0., 1., 0.),
190                                osg::Vec3( 0., 1., 0.),
191
192                                osg::Vec3( 0.,-1., 0.),
193                                osg::Vec3( 0.,-1., 0.),
194                                osg::Vec3( 0.,-1., 0.),
195                                osg::Vec3( 0.,-1., 0.)
196                              };
197
198const osg::Vec4 myColors[] = { osg::Vec4( 1., 0., 0., 1.),
199                               osg::Vec4( 1., 0., 0., 1.),
200                               osg::Vec4( 1., 0., 0., 1.),
201                               osg::Vec4( 1., 0., 0., 1.),
202
203                               osg::Vec4( 0., 1., 0., 1.),
204                               osg::Vec4( 0., 1., 0., 1.),
205                               osg::Vec4( 0., 1., 0., 1.),
206                               osg::Vec4( 0., 1., 0., 1.),
207
208                               osg::Vec4( 1., 1., 0., 1.),
209                               osg::Vec4( 1., 1., 0., 1.),
210                               osg::Vec4( 1., 1., 0., 1.),
211                               osg::Vec4( 1., 1., 0., 1.),
212
213                               osg::Vec4( 0., 0., 1., 1.),
214                               osg::Vec4( 0., 0., 1., 1.),
215                               osg::Vec4( 0., 0., 1., 1.),
216                               osg::Vec4( 0., 0., 1., 1.),
217
218                               osg::Vec4( 1., 0., 1., 1.),
219                               osg::Vec4( 1., 0., 1., 1.),
220                               osg::Vec4( 1., 0., 1., 1.),
221                               osg::Vec4( 1., 0., 1., 1.),
222
223                               osg::Vec4( 0., 1., 1., 1.),
224                               osg::Vec4( 0., 1., 1., 1.),
225                               osg::Vec4( 0., 1., 1., 1.),
226                               osg::Vec4( 0., 1., 1., 1.)
227                             };
228
229/** Create a Geode that describes a cube using our own
230  * subclass of osg::Array for the vertices. It uses
231  * the "regular" array classes for all of the other
232  * arrays.
233  *
234  * Creating your own Array class isn't really very
235  * useful for a tiny amount of data like this. You
236  * could just go ahead and copy the data into one of
237  * the "regular" Array classes like this does for
238  * normals and colors. The point of creating your
239  * own subclass of Array is for use with datasets
240  * that are much larger than anything you could
241  * create a simple example from. In that case, you
242  * might not want to create a copy of the data in
243  * one of the Array classes that comes with OSG, but
244  * instead reuse the copy your application already
245  * has and wrap it up in a subclass of osg::Array
246  * that presents the right interface for use with
247  * OpenSceneGraph.
248  *
249  * Note that I'm only using the shared array for the
250  * vertices. You could do something similar for any
251  * of the Geometry node's data arrays.
252  */
253osg::Geode* createGeometry()
254{
255    osg::Geode* geode = new osg::Geode();
256
257    // create Geometry
258    osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
259
260    // add vertices using MyArray class
261    unsigned int numVertices = sizeof(myVertices)/sizeof(myVertices[0]);
262    geom->setVertexArray(new MyArray(numVertices,const_cast<osg::Vec3*>(&myVertices[0])));
263
264    // add normals
265    unsigned int numNormals = sizeof(myNormals)/sizeof(myNormals[0]);
266    geom->setNormalArray(new osg::Vec3Array(numNormals,const_cast<osg::Vec3*>(&myNormals[0])));
267    geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
268
269    // add colors
270    unsigned int numColors = sizeof(myColors)/sizeof(myColors[0]);
271    osg::Vec4Array* normal_array = new osg::Vec4Array(numColors,const_cast<osg::Vec4*>(&myColors[0]));
272    geom->setColorArray(normal_array);
273    geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
274
275    // add PrimitiveSet
276    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, numVertices));
277
278    // Changing these flags will tickle different cases in
279    // Geometry::drawImplementation. They should all work fine
280    // with the shared array. Setting VertexIndices will hit
281    // some other cases.
282    geom->setUseVertexBufferObjects(false);
283    geom->setUseDisplayList(false);
284    geom->setFastPathHint(false);
285
286    geode->addDrawable( geom.get() );
287
288    return geode;
289}
290
291int main(int , char **)
292{
293    // construct the viewer.
294    osgViewer::Viewer viewer;
295
296    // add model to viewer.
297    viewer.setSceneData( createGeometry() );
298
299    // create the windows and run the threads.
300    return viewer.run();
301}
Note: See TracBrowser for help on using the browser.