Show
Ignore:
Timestamp:
12/19/14 16:43:31 (11 hours ago)
Author:
robert
Message:

Release OpenSceneGraph-3.3.3

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osggeometry/osggeometry.cpp

    r13488 r13521  
    4040 
    4141// This demo illustrates how to create the various different types of geometry that 
    42 // the osg::Geometry class can represent.  This demo uses the OpenGL red book diagram of different  
    43 // OpenGL Primitives as a template for all the equivalent OpenSceneGraph Primitives.  The OpenSceneGraph  
    44 // wraps OpenGL very thinly and therefore uses all the same enum and naming conventions. The coordinate data is also  
     42// the osg::Geometry class can represent.  This demo uses the OpenGL red book diagram of different 
     43// OpenGL Primitives as a template for all the equivalent OpenSceneGraph Primitives.  The OpenSceneGraph 
     44// wraps OpenGL very thinly and therefore uses all the same enum and naming conventions. The coordinate data is also 
    4545// wrapped around OpenGL's vertex arrays and draw arrays/elements calls.  Familiarity with 
    4646// OpenGL will help you understand the osg::Geometry class which encapsulate all this, or if you 
     
    5252// color and texture coordinate arrays required to specify the coordinates of your objects, and the 
    5353// primitives join these coordinates together as the points, lines or surfaces that you will see 
    54 // rendered on your screen.  
     54// rendered on your screen. 
    5555// 
    5656// This demo is split into two functions, the createScene() function which creates the scene graph 
     
    6161struct NormalPrint 
    6262{ 
    63     void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const  
     63    void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const 
    6464    { 
    6565        osg::Vec3 normal = (v2-v1)^(v3-v2); 
     
    7373{ 
    7474    std::cout<<name<<std::endl; 
    75      
     75 
    7676    osg::TriangleFunctor<NormalPrint> tf; 
    7777    drawable.accept(tf); 
    78   
     78 
    7979    std::cout<<std::endl; 
    8080} 
     
    9595        // create Geometry object to store all the vertices and points primitive. 
    9696        osg::Geometry* pointsGeom = new osg::Geometry(); 
    97          
     97 
    9898        // create a Vec3Array and add to it all my coordinates. 
    99         // Like all the *Array variants (see include/osg/Array) , Vec3Array is derived from both osg::Array  
     99        // Like all the *Array variants (see include/osg/Array) , Vec3Array is derived from both osg::Array 
    100100        // and std::vector<>.  osg::Array's are reference counted and hence sharable, 
    101101        // which std::vector<> provides all the convenience, flexibility and robustness 
     
    107107        vertices->push_back(osg::Vec3(-0.836299, -2.15188e-09, 0.885735)); 
    108108        vertices->push_back(osg::Vec3(-0.790982, 9.18133e-09, 0.959889)); 
    109          
     109 
    110110        // pass the created vertex array to the points geometry object. 
    111111        pointsGeom->setVertexArray(vertices); 
    112          
    113          
    114          
     112 
     113 
     114 
    115115        // create the color of the geometry, one single for the whole geometry. 
    116116        // for consistency of design even one single color must added as an element 
     
    119119        // add a white color, colors take the form r,g,b,a with 0.0 off, 1.0 full on. 
    120120        colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); 
    121          
     121 
    122122        // pass the color array to points geometry, note the binding to tell the geometry 
    123123        // that only use one color for the whole object. 
    124124        pointsGeom->setColorArray(colors); 
    125125        pointsGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    126          
    127          
     126 
     127 
    128128        // set the normal in the same way color. 
    129129        osg::Vec3Array* normals = new osg::Vec3Array; 
     
    139139        // to draw, and the third parameter is the number of points to draw. 
    140140        pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices->size())); 
    141          
    142          
     141 
     142 
    143143        // add the points geometry to the geode. 
    144144        geode->addDrawable(pointsGeom); 
     
    149149        // create Geometry object to store all the vertices and lines primitive. 
    150150        osg::Geometry* linesGeom = new osg::Geometry(); 
    151          
     151 
    152152        // this time we'll preallocate the vertex array to the size we 
    153153        // need and then simple set them as array elements, 8 points 
     
    163163        (*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533); 
    164164 
    165          
     165 
    166166        // pass the created vertex array to the points geometry object. 
    167167        linesGeom->setVertexArray(vertices); 
    168          
     168 
    169169        // set the colors as before, plus using the above 
    170170        osg::Vec4Array* colors = new osg::Vec4Array; 
     
    172172        linesGeom->setColorArray(colors); 
    173173        linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    174          
     174 
    175175 
    176176        // set the normal in the same way color. 
     
    181181 
    182182 
    183         // This time we simply use primitive, and hardwire the number of coords to use  
     183        // This time we simply use primitive, and hardwire the number of coords to use 
    184184        // since we know up front, 
    185185        linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8)); 
    186          
    187          
     186 
     187 
    188188        // add the points geometry to the geode. 
    189189        geode->addDrawable(linesGeom); 
    190190    } 
    191      
     191 
    192192    // create LINE_STRIP 
    193193    { 
    194194        // create Geometry object to store all the vertices and lines primitive. 
    195195        osg::Geometry* linesGeom = new osg::Geometry(); 
    196          
    197         // this time we'll preallocate the vertex array to the size  
     196 
     197        // this time we'll preallocate the vertex array to the size 
    198198        // and then use an iterator to fill in the values, a bit perverse 
    199199        // but does demonstrate that we have just a standard std::vector underneath. 
     
    205205        (vitr++)->set(-0.0288379, -2.15188e-09, 0.333695); 
    206206        (vitr++)->set(-0.0453167, -2.15188e-09, 0.280139); 
    207         
     207 
    208208        // pass the created vertex array to the points geometry object. 
    209209        linesGeom->setVertexArray(vertices); 
    210          
     210 
    211211        // set the colors as before, plus using the above 
    212212        osg::Vec4Array* colors = new osg::Vec4Array; 
     
    223223 
    224224 
    225         // This time we simply use primitive, and hardwire the number of coords to use  
     225        // This time we simply use primitive, and hardwire the number of coords to use 
    226226        // since we know up front, 
    227227        linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,5)); 
    228          
    229          
     228 
     229 
    230230        // add the points geometry to the geode. 
    231231        geode->addDrawable(linesGeom); 
     
    236236        // create Geometry object to store all the vertices and lines primitive. 
    237237        osg::Geometry* linesGeom = new osg::Geometry(); 
    238          
     238 
    239239        // this time we'll a C arrays to initialize the vertices. 
    240          
     240 
    241241        osg::Vec3 myCoords[] = 
    242242        { 
     
    248248            osg::Vec3(0.869256, -2.15188e-09, 0.531441) 
    249249        }; 
    250          
     250 
    251251        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); 
    252          
     252 
    253253        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); 
    254         
     254 
    255255        // pass the created vertex array to the points geometry object. 
    256256        linesGeom->setVertexArray(vertices); 
    257          
     257 
    258258        // set the colors as before, plus using the above 
    259259        osg::Vec4Array* colors = new osg::Vec4Array; 
     
    261261        linesGeom->setColorArray(colors); 
    262262        linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    263          
     263 
    264264 
    265265        // set the normal in the same way color. 
     
    268268        linesGeom->setNormalArray(normals); 
    269269        linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); 
    270          
    271  
    272         // This time we simply use primitive, and hardwire the number of coords to use  
     270 
     271 
     272        // This time we simply use primitive, and hardwire the number of coords to use 
    273273        // since we know up front, 
    274274        linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,numCoords)); 
    275          
    276          
     275 
     276 
    277277        // add the points geometry to the geode. 
    278278        geode->addDrawable(linesGeom); 
     
    302302    // In reality you need to specify coords for polygons in a anticlockwise direction 
    303303    // for their front face to be pointing towards you; get this wrong and you could 
    304     // find back face culling removing the wrong faces of your models.  The OpenGL diagram  
     304    // find back face culling removing the wrong faces of your models.  The OpenGL diagram 
    305305    // is just plain wrong, but it's a nice diagram so we'll keep it for now! 
    306306 
     
    309309        // create Geometry object to store all the vertices and lines primitive. 
    310310        osg::Geometry* polyGeom = new osg::Geometry(); 
    311          
     311 
    312312        // this time we'll use C arrays to initialize the vertices. 
    313313        // note, anticlockwise ordering. 
    314         // note II, OpenGL polygons must be convex, planar polygons, otherwise  
     314        // note II, OpenGL polygons must be convex, planar polygons, otherwise 
    315315        // undefined results will occur.  If you have concave polygons or ones 
    316316        // that cross over themselves then use the osgUtil::Tessellator to fix 
     
    324324            osg::Vec3(-0.980488, 0.0f, -0.094753) 
    325325        }; 
    326          
     326 
    327327        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); 
    328          
     328 
    329329        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); 
    330         
     330 
    331331        // pass the created vertex array to the points geometry object. 
    332332        polyGeom->setVertexArray(vertices); 
    333          
     333 
    334334        // use the shared color array. 
    335335        polyGeom->setColorArray(shared_colors.get()); 
    336336        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    337          
     337 
    338338 
    339339        // use the shared normal array. 
    340340        polyGeom->setNormalArray(shared_normals.get()); 
    341341        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); 
    342          
    343  
    344         // This time we simply use primitive, and hardwire the number of coords to use  
     342 
     343 
     344        // This time we simply use primitive, and hardwire the number of coords to use 
    345345        // since we know up front, 
    346346        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,numCoords)); 
    347347 
    348348        printTriangles("Polygon",*polyGeom); 
    349          
     349 
    350350        // add the points geometry to the geode. 
    351351        geode->addDrawable(polyGeom); 
     
    357357        // create Geometry object to store all the vertices and lines primitive. 
    358358        osg::Geometry* polyGeom = new osg::Geometry(); 
    359          
     359 
    360360        // note, anticlockwise ordering. 
    361361        osg::Vec3 myCoords[] = 
     
    371371            osg::Vec3(0.13595,  0.0f, -0.255421) 
    372372        }; 
    373          
     373 
    374374        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); 
    375          
     375 
    376376        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); 
    377         
     377 
    378378        // pass the created vertex array to the points geometry object. 
    379379        polyGeom->setVertexArray(vertices); 
    380          
     380 
    381381        // use the shared color array. 
    382382        polyGeom->setColorArray(shared_colors.get()); 
    383383        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    384          
     384 
    385385 
    386386        // use the shared normal array. 
    387387        polyGeom->setNormalArray(shared_normals.get()); 
    388388        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); 
    389          
    390  
    391         // This time we simply use primitive, and hardwire the number of coords to use  
     389 
     390 
     391        // This time we simply use primitive, and hardwire the number of coords to use 
    392392        // since we know up front, 
    393393        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,numCoords)); 
    394          
    395          
     394 
     395 
    396396        printTriangles("Quads",*polyGeom); 
    397397 
     
    404404        // create Geometry object to store all the vertices and lines primitive. 
    405405        osg::Geometry* polyGeom = new osg::Geometry(); 
    406          
     406 
    407407        // note, first coord at top, second at bottom, reverse to that buggy OpenGL image.. 
    408408        osg::Vec3 myCoords[] = 
     
    420420            osg::Vec3(1.12468, 9.18133e-09, -0.164788), 
    421421        }; 
    422          
     422 
    423423        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); 
    424          
     424 
    425425        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); 
    426         
     426 
    427427        // pass the created vertex array to the points geometry object. 
    428428        polyGeom->setVertexArray(vertices); 
    429          
     429 
    430430        // use the shared color array. 
    431431        polyGeom->setColorArray(shared_colors.get()); 
    432432        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    433          
     433 
    434434 
    435435        // use the shared normal array. 
    436436        polyGeom->setNormalArray(shared_normals.get()); 
    437437        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); 
    438          
    439  
    440         // This time we simply use primitive, and hardwire the number of coords to use  
     438 
     439 
     440        // This time we simply use primitive, and hardwire the number of coords to use 
    441441        // since we know up front, 
    442442        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords)); 
    443          
    444          
     443 
     444 
    445445        printTriangles("Quads strip",*polyGeom); 
    446446 
     
    453453        // create Geometry object to store all the vertices and lines primitive. 
    454454        osg::Geometry* polyGeom = new osg::Geometry(); 
    455          
     455 
    456456        // note, first coord at top, second at bottom, reverse to that buggy OpenGL image.. 
    457457        osg::Vec3 myCoords[] = 
     
    469469 
    470470            // TRIANGLE STRIP 6 vertices, v6..v11 
    471             // note defined top point first,  
     471            // note defined top point first, 
    472472            // then anticlockwise for the next two points, 
    473473            // then alternating to bottom there after. 
     
    488488 
    489489        }; 
    490          
     490 
    491491        int numCoords = sizeof(myCoords)/sizeof(osg::Vec3); 
    492          
     492 
    493493        osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords); 
    494         
     494 
    495495        // pass the created vertex array to the points geometry object. 
    496496        polyGeom->setVertexArray(vertices); 
    497          
     497 
    498498        // use the shared color array. 
    499499        polyGeom->setColorArray(shared_colors.get()); 
    500500        polyGeom->setColorBinding(osg::Geometry::BIND_OVERALL); 
    501          
     501 
    502502 
    503503        // use the shared normal array. 
    504504        polyGeom->setNormalArray(shared_normals.get()); 
    505505        polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); 
    506          
    507  
    508         // This time we simply use primitive, and hardwire the number of coords to use  
     506 
     507 
     508        // This time we simply use primitive, and hardwire the number of coords to use 
    509509        // since we know up front, 
    510510        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,6)); 
    511511        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,6,6)); 
    512512        polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,12,5)); 
    513          
     513 
    514514        // polygon stipple 
    515515        osg::StateSet* stateSet = new osg::StateSet(); 
    516516        polyGeom->setStateSet(stateSet); 
    517          
    518         #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE)  
     517 
     518        #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE) 
    519519        osg::PolygonStipple* polygonStipple = new osg::PolygonStipple; 
    520520        stateSet->setAttributeAndModes(polygonStipple,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); 
    521521        #endif 
    522          
     522 
    523523        printTriangles("Triangles/Strip/Fan",*polyGeom); 
    524524 
     
    526526        geode->addDrawable(polyGeom); 
    527527    } 
    528      
    529     return geode;    
     528 
     529    return geode; 
    530530} 
    531531 
     
    544544        virtual void operator() (osg::Node* node, osg::NodeVisitor* nv) 
    545545        { 
    546             osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node);                 
     546            osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node); 
    547547            if (nv && transform && nv->getFrameStamp()) 
    548548            { 
     
    550550                transform->setMatrix(osg::Matrix::translate(0.0f,1.0f+cosf(time*_angular_velocity),0.0f)); 
    551551            } 
    552              
     552 
    553553            // must continue subgraph traversal. 
    554             traverse(node,nv);             
    555              
     554            traverse(node,nv); 
     555 
    556556        } 
    557          
     557 
    558558    protected: 
    559      
     559 
    560560        float               _angular_velocity; 
    561561 
     
    564564 
    565565osg::Node* createBackground() 
    566 {     
    567  
    568     // we'll create a texture mapped quad to sit behind the Geometry  
     566{ 
     567 
     568    // we'll create a texture mapped quad to sit behind the Geometry 
    569569    osg::Image* image = osgDB::readImageFile("Images/primitives.gif"); 
    570570    if (!image) return NULL; 
    571      
    572   
     571 
     572 
    573573    // create Geometry object to store all the vertices and lines primitive. 
    574574    osg::Geometry* polyGeom = new osg::Geometry(); 
     
    627627    // There are three variants of the DrawElements osg::Primitive, UByteDrawElements which 
    628628    // contains unsigned char indices, UShortDrawElements which contains unsigned short indices, 
    629     // and UIntDrawElements which contains ... unsigned int indices.   
    630     // The first parameter to DrawElements is  
     629    // and UIntDrawElements which contains ... unsigned int indices. 
     630    // The first parameter to DrawElements is 
    631631    polyGeom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP,numIndices,myIndices)); 
    632632 
    633     // new we need to add the texture to the Drawable, we do so by creating a  
     633    // new we need to add the texture to the Drawable, we do so by creating a 
    634634    // StateSet to contain the Texture2D StateAttribute. 
    635635    osg::StateSet* stateset = new osg::StateSet; 
     
    643643    polyGeom->setStateSet(stateset); 
    644644 
    645   
     645 
    646646    // create the Geode (Geometry Node) to contain all our osg::Geometry objects. 
    647647    osg::Geode* geode = new osg::Geode(); 
     
    653653 
    654654    // create a transform to move the background back and forward with. 
    655   
     655 
    656656    osg::MatrixTransform* transform = new osg::MatrixTransform(); 
    657657    transform->setUpdateCallback(new MyTransformCallback(1.0f));