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

Revision 13574, 11.3 kB (checked in by robert, 3 hours ago)

Introduced new scheme for setting up which version of OpenGL/OpenGL ES the OSG is compiled for.

To select standard OpenGL 1/2 build with full backwards and forwards comtability use:

./configure
make

OR

./configure -DOPENGL_PROFILE=GL2

To select OpenGL 3 core profile build using GL3/gl3.h header:

./configure -DOPENGL_PROFILE=GL3

To select OpenGL Arb core profile build using GL/glcorearb.h header:

./configure -DOPENGL_PROFILE=GLCORE

To select OpenGL ES 1.1 profile use:

./configure -DOPENGL_PROFILE=GLES1

To select OpenGL ES 2 profile use:

./configure -DOPENGL_PROFILE=GLES2

Using OPENGL_PROFILE will select all the appropriate features required so no other settings in cmake will need to be adjusted.
The new configuration options are stored in the include/osg/OpenGL header that deprecates the old include/osg/GL header.

  • 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    virtual unsigned int getElementSize() const { return sizeof(osg::Vec3); }
110
111    /** Returns a pointer to the first element of the array. */
112    virtual const GLvoid* getDataPointer() const {
113        return _ptr;
114    }
115
116    /** Returns the number of elements in the array. */
117    virtual unsigned int getNumElements() const {
118        return _numElements;
119    }
120
121    /** Returns the number of bytes of storage required to hold
122      * all of the elements of the array.
123      */
124    virtual unsigned int getTotalDataSize() const {
125        return _numElements * sizeof(osg::Vec3);
126    }
127
128    virtual void reserveArray(unsigned int num) { OSG_NOTICE<<"reserveArray() not supported"<<std::endl; }
129    virtual void resizeArray(unsigned int num) { OSG_NOTICE<<"resizeArray() not supported"<<std::endl; }
130
131private:
132    unsigned int _numElements;
133    osg::Vec3*   _ptr;
134};
135
136/** The data values for the example. Simply defines a cube with
137  * per-face colors and normals.
138  */
139
140const osg::Vec3 myVertices[] = { 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                                 osg::Vec3(-1.,-1.,-1.),
166                                 osg::Vec3( 1.,-1.,-1.),
167                                 osg::Vec3( 1.,-1., 1.),
168                                 osg::Vec3(-1.,-1., 1.),
169                               };
170
171
172const osg::Vec3 myNormals[] = { osg::Vec3( 0., 0., 1.),
173                                osg::Vec3( 0., 0., 1.),
174                                osg::Vec3( 0., 0., 1.),
175                                osg::Vec3( 0., 0., 1.),
176
177                                osg::Vec3( 1., 0., 0.),
178                                osg::Vec3( 1., 0., 0.),
179                                osg::Vec3( 1., 0., 0.),
180                                osg::Vec3( 1., 0., 0.),
181
182                                osg::Vec3( 0., 0.,-1.),
183                                osg::Vec3( 0., 0.,-1.),
184                                osg::Vec3( 0., 0.,-1.),
185                                osg::Vec3( 0., 0.,-1.),
186
187                                osg::Vec3(-1., 0., 0.),
188                                osg::Vec3(-1., 0., 0.),
189                                osg::Vec3(-1., 0., 0.),
190                                osg::Vec3(-1., 0., 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                                osg::Vec3( 0.,-1., 0.),
198                                osg::Vec3( 0.,-1., 0.),
199                                osg::Vec3( 0.,-1., 0.),
200                                osg::Vec3( 0.,-1., 0.)
201                              };
202
203const osg::Vec4 myColors[] = { osg::Vec4( 1., 0., 0., 1.),
204                               osg::Vec4( 1., 0., 0., 1.),
205                               osg::Vec4( 1., 0., 0., 1.),
206                               osg::Vec4( 1., 0., 0., 1.),
207
208                               osg::Vec4( 0., 1., 0., 1.),
209                               osg::Vec4( 0., 1., 0., 1.),
210                               osg::Vec4( 0., 1., 0., 1.),
211                               osg::Vec4( 0., 1., 0., 1.),
212
213                               osg::Vec4( 1., 1., 0., 1.),
214                               osg::Vec4( 1., 1., 0., 1.),
215                               osg::Vec4( 1., 1., 0., 1.),
216                               osg::Vec4( 1., 1., 0., 1.),
217
218                               osg::Vec4( 0., 0., 1., 1.),
219                               osg::Vec4( 0., 0., 1., 1.),
220                               osg::Vec4( 0., 0., 1., 1.),
221                               osg::Vec4( 0., 0., 1., 1.),
222
223                               osg::Vec4( 1., 0., 1., 1.),
224                               osg::Vec4( 1., 0., 1., 1.),
225                               osg::Vec4( 1., 0., 1., 1.),
226                               osg::Vec4( 1., 0., 1., 1.),
227
228                               osg::Vec4( 0., 1., 1., 1.),
229                               osg::Vec4( 0., 1., 1., 1.),
230                               osg::Vec4( 0., 1., 1., 1.),
231                               osg::Vec4( 0., 1., 1., 1.)
232                             };
233
234/** Create a Geode that describes a cube using our own
235  * subclass of osg::Array for the vertices. It uses
236  * the "regular" array classes for all of the other
237  * arrays.
238  *
239  * Creating your own Array class isn't really very
240  * useful for a tiny amount of data like this. You
241  * could just go ahead and copy the data into one of
242  * the "regular" Array classes like this does for
243  * normals and colors. The point of creating your
244  * own subclass of Array is for use with datasets
245  * that are much larger than anything you could
246  * create a simple example from. In that case, you
247  * might not want to create a copy of the data in
248  * one of the Array classes that comes with OSG, but
249  * instead reuse the copy your application already
250  * has and wrap it up in a subclass of osg::Array
251  * that presents the right interface for use with
252  * OpenSceneGraph.
253  *
254  * Note that I'm only using the shared array for the
255  * vertices. You could do something similar for any
256  * of the Geometry node's data arrays.
257  */
258osg::Geode* createGeometry()
259{
260    osg::Geode* geode = new osg::Geode();
261
262    // create Geometry
263    osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
264
265    // add vertices using MyArray class
266    unsigned int numVertices = sizeof(myVertices)/sizeof(myVertices[0]);
267    geom->setVertexArray(new MyArray(numVertices,const_cast<osg::Vec3*>(&myVertices[0])));
268
269    // add normals
270    unsigned int numNormals = sizeof(myNormals)/sizeof(myNormals[0]);
271    geom->setNormalArray(new osg::Vec3Array(numNormals,const_cast<osg::Vec3*>(&myNormals[0])), osg::Array::BIND_PER_VERTEX);
272
273    // add colors
274    unsigned int numColors = sizeof(myColors)/sizeof(myColors[0]);
275    osg::Vec4Array* normal_array = new osg::Vec4Array(numColors,const_cast<osg::Vec4*>(&myColors[0]));
276    geom->setColorArray(normal_array, osg::Array::BIND_PER_VERTEX);
277
278    // add PrimitiveSet
279    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, numVertices));
280
281    // Changing these flags will tickle different cases in
282    // Geometry::drawImplementation. They should all work fine
283    // with the shared array.
284    geom->setUseVertexBufferObjects(false);
285    geom->setUseDisplayList(false);
286
287    geode->addDrawable( geom.get() );
288
289    return geode;
290}
291
292int main(int , char **)
293{
294    // construct the viewer.
295    osgViewer::Viewer viewer;
296
297    // add model to viewer.
298    viewer.setSceneData( createGeometry() );
299
300    // create the windows and run the threads.
301    return viewer.run();
302}
Note: See TracBrowser for help on using the browser.