~ubuntu-branches/ubuntu/trusty/openscenegraph/trusty

« back to all changes in this revision

Viewing changes to OpenSceneGraph/examples/osgsharedarray/osgsharedarray.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-07-29 04:34:38 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20080729043438-no1h9h0dpsrlzp1y
* Non-maintainer upload.
* No longer try to detect (using /proc/cpuinfo when available) how many
  CPUs are available, fixing the FTBFS (due to -j0) on various platforms
  (Closes: #477353). The right way to do it is to support parallel=n in
  DEB_BUILD_OPTIONS (see Debian Policy §4.9.1), and adequate support has
  been implemented.
* Add patch to fix FTBFS due to the build system now refusing to handle
  whitespaces (Policy CMP0004 say the logs), thanks to Andreas Putzo who
  provided it (Closes: #482239):
   - debian/patches/fix-cmp0004-build-failure.dpatch
* Remove myself from Uploaders, as requested a while ago, done by Luk in
  his 2.2.0-2.1 NMU, which was never acknowledged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    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.) 
 
144
                                          };
 
145
 
 
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.) 
 
152
                                         };
 
153
    
 
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.) 
 
160
                                        };
 
161
 
 
162
    static const unsigned short myIndices[] = { 4, 5, 7, 6,
 
163
                                                5, 1, 3, 7,
 
164
                                                1, 0, 2, 3,
 
165
                                                0, 4, 6, 2,
 
166
                                                6, 7, 3, 2,
 
167
                                                0, 1, 5, 4
 
168
                                              };
 
169
}
 
170
 
 
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 
 
174
  * arrays.
 
175
  *
 
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 
 
189
  * OpenSceneGraph.
 
190
  *
 
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.
 
194
  */
 
195
osg::Geode* createGeometry()
 
196
{
 
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
}