- Timestamp:
- 12/10/07 18:30:18 (6 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osggeometry/osggeometry.cpp
r6941 r7648 40 40 // This demos uses the illustrates how to creates the various different types of geometry that 41 41 // the osg::Geometry class can represent. This demos uses the OpenGL red books diagram of different 42 // OpenGL Primitives as a template for all the equiv ilant OpenSceneGraph Primitives. The OpenSceneGraph42 // OpenGL Primitives as a template for all the equivalent OpenSceneGraph Primitives. The OpenSceneGraph 43 43 // wraps OpenGL very thinly so uses all the same enum and naming conventions. The coordinate data is also 44 // wrapped around OpenGL's vertex arrays and draw arrays/elements calls. Famil arity with44 // wrapped around OpenGL's vertex arrays and draw arrays/elements calls. Familiarity with 45 45 // OpenGL will help understand the the osg::Geometry class which encapsulate all this, or if you 46 // havn't learn tOpenGL yet, learning osg::Geometry will help you understand how OpenGL46 // havn't learned OpenGL yet, learning osg::Geometry will help you understand how OpenGL 47 47 // works! 48 48 49 49 // The osg::Geometry class "is a" subclass of osg::Drawable base class, so is an object that provides 50 50 // a draw method for drawing objects in the scene. osg::Geometry contains all the vertex, normal 51 // color and texture coord ate arrays required to specify the coordinates of your objects, and the52 // prim tives join these coordinates together as the points, lines or surfaces that you will see51 // color and texture coordinate arrays required to specify the coordinates of your objects, and the 52 // primitives join these coordinates together as the points, lines or surfaces that you will see 53 53 // rendered on your screen. 54 54 // … … 68 68 }; 69 69 70 // decompose Drawable prim tives into triangles, print out these triangles and computed normals.70 // decompose Drawable primitives into triangles, print out these triangles and computed normals. 71 71 void printTriangles(const std::string& name, osg::Drawable& drawable) 72 72 { … … 86 86 87 87 // follows are separate blocks for creating POINTS, LINES, LINE_STRIP, LINE_LOOP, POLYGON, QUADS, 88 // QUAD_STRIP, TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN prim tives. A image of these primtives89 // are provided in the distribution : OpenSceneGraph-Data/Images/prim tives.gif.88 // QUAD_STRIP, TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN primitives. A image of these primitives 89 // are provided in the distribution : OpenSceneGraph-Data/Images/primitives.gif. 90 90 91 91 92 92 // create POINTS 93 93 { 94 // create Geometry object to store all the ve tices and points primtive.94 // create Geometry object to store all the vertices and points primitive. 95 95 osg::Geometry* pointsGeom = new osg::Geometry(); 96 96 97 97 // create a Vec3Array and add to it all my coordinates. 98 // Like all the *Array variants (see include/osg/Array) , Vec3Array is deriv ied from both osg::Array98 // Like all the *Array variants (see include/osg/Array) , Vec3Array is derived from both osg::Array 99 99 // and std::vector<>. osg::Array's are reference counted and hence sharable, 100 // which std::vector<> provides all the conv inience, flexibility and robustness100 // which std::vector<> provides all the convenience, flexibility and robustness 101 101 // of the most popular of all STL containers. 102 102 osg::Vec3Array* vertices = new osg::Vec3Array; … … 113 113 114 114 // create the color of the geometry, one single for the whole geometry. 115 // for consi tency of design even one single color must added as an element115 // for consistency of design even one single color must added as an element 116 116 // in a color array. 117 117 osg::Vec4Array* colors = new osg::Vec4Array; … … 119 119 colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); 120 120 121 // pass the color arr y to points geometry, note the binding to tell the geometry121 // pass the color array to points geometry, note the binding to tell the geometry 122 122 // that only use one color for the whole object. 123 123 pointsGeom->setColorArray(colors); … … 132 132 133 133 134 // create and add a DrawArray Primitive (see include/osg/Prim tive). The first135 // param ter passed to the DrawArrays constructor is the Primtive::Mode which134 // create and add a DrawArray Primitive (see include/osg/Primitive). The first 135 // parameter passed to the DrawArrays constructor is the Primitive::Mode which 136 136 // in this case is POINTS (which has the same value GL_POINTS), the second 137 137 // parameter is the index position into the vertex array of the first point … … 140 140 141 141 142 // add the points geom try to the geode.142 // add the points geometry to the geode. 143 143 geode->addDrawable(pointsGeom); 144 144 } … … 146 146 // create LINES 147 147 { 148 // create Geometry object to store all the ve tices and lines primtive.148 // create Geometry object to store all the vertices and lines primitive. 149 149 osg::Geometry* linesGeom = new osg::Geometry(); 150 150 151 // this time we'll preallo acte the vertex array to the size we151 // this time we'll preallocate the vertex array to the size we 152 152 // need and then simple set them as array elements, 8 points 153 153 // makes 4 line segments. … … 166 166 linesGeom->setVertexArray(vertices); 167 167 168 // set the colors as before, plus using the a obve168 // set the colors as before, plus using the above 169 169 osg::Vec4Array* colors = new osg::Vec4Array; 170 170 colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); … … 185 185 186 186 187 // add the points geom try to the geode.187 // add the points geometry to the geode. 188 188 geode->addDrawable(linesGeom); 189 189 } … … 191 191 // create LINE_STRIP 192 192 { 193 // create Geometry object to store all the ve tices and lines primtive.193 // create Geometry object to store all the vertices and lines primitive. 194 194 osg::Geometry* linesGeom = new osg::Geometry(); 195 195 196 // this time we'll preallo acte the vertex array to the size196 // this time we'll preallocate the vertex array to the size 197 197 // and then use an iterator to fill in the values, a bit perverse 198 198 // but does demonstrate that we have just a standard std::vector underneath. … … 208 208 linesGeom->setVertexArray(vertices); 209 209 210 // set the colors as before, plus using the a obve210 // set the colors as before, plus using the above 211 211 osg::Vec4Array* colors = new osg::Vec4Array; 212 212 colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); … … 227 227 228 228 229 // add the points geom try to the geode.229 // add the points geometry to the geode. 230 230 geode->addDrawable(linesGeom); 231 231 } … … 233 233 // create LINE_LOOP 234 234 { 235 // create Geometry object to store all the ve tices and lines primtive.235 // create Geometry object to store all the vertices and lines primitive. 236 236 osg::Geometry* linesGeom = new osg::Geometry(); 237 237 238 // this time we'll a C arrays to initi lize the vertices.238 // this time we'll a C arrays to initialize the vertices. 239 239 240 240 osg::Vec3 myCoords[] = … … 255 255 linesGeom->setVertexArray(vertices); 256 256 257 // set the colors as before, plus using the a obve257 // set the colors as before, plus using the above 258 258 osg::Vec4Array* colors = new osg::Vec4Array; 259 259 colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); … … 274 274 275 275 276 // add the points geom try to the geode.276 // add the points geometry to the geode. 277 277 geode->addDrawable(linesGeom); 278 278 } … … 306 306 // create POLYGON 307 307 { 308 // create Geometry object to store all the ve tices and lines primtive.308 // create Geometry object to store all the vertices and lines primitive. 309 309 osg::Geometry* polyGeom = new osg::Geometry(); 310 310 311 // this time we'll a C arrays to initi lize the vertices.312 // note, anticlockw sie ordering.311 // this time we'll a C arrays to initialize the vertices. 312 // note, anticlockwise ordering. 313 313 // note II, OpenGL polygons must be convex plan polygons, otherwise 314 314 // undefined results will occur. If you have concave polygons or ones … … 347 347 printTriangles("Polygon",*polyGeom); 348 348 349 // add the points geom try to the geode.349 // add the points geometry to the geode. 350 350 geode->addDrawable(polyGeom); 351 351 } … … 354 354 // create QUADS 355 355 { 356 // create Geometry object to store all the ve tices and lines primtive.356 // create Geometry object to store all the vertices and lines primitive. 357 357 osg::Geometry* polyGeom = new osg::Geometry(); 358 358 359 // note, anticlockw sie ordering.359 // note, anticlockwise ordering. 360 360 osg::Vec3 myCoords[] = 361 361 { … … 395 395 printTriangles("Quads",*polyGeom); 396 396 397 // add the points geom try to the geode.397 // add the points geometry to the geode. 398 398 geode->addDrawable(polyGeom); 399 399 } … … 401 401 // create QUAD_STRIP 402 402 { 403 // create Geometry object to store all the ve tices and lines primtive.403 // create Geometry object to store all the vertices and lines primitive. 404 404 osg::Geometry* polyGeom = new osg::Geometry(); 405 405 … … 444 444 printTriangles("Quads strip",*polyGeom); 445 445 446 // add the points geom try to the geode.446 // add the points geometry to the geode. 447 447 geode->addDrawable(polyGeom); 448 448 } … … 450 450 // create TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN all in one Geometry/ 451 451 { 452 // create Geometry object to store all the ve tices and lines primtive.452 // create Geometry object to store all the vertices and lines primitive. 453 453 osg::Geometry* polyGeom = new osg::Geometry(); 454 454 … … 457 457 { 458 458 // TRIANGLES 6 vertices, v0..v5 459 // note in an iclockwise order.459 // note in anticlockwise order. 460 460 osg::Vec3(-1.12056, -2.15188e-09, -0.840418), 461 461 osg::Vec3(-0.95165, -2.15188e-09, -0.840418), 462 462 osg::Vec3(-1.11644, 9.18133e-09, -0.716827), 463 463 464 // note in an iclockwise order.464 // note in anticlockwise order. 465 465 osg::Vec3(-0.840418, 9.18133e-09, -0.778623), 466 466 osg::Vec3(-0.622074, 9.18133e-09, -0.613835), … … 479 479 480 480 // TRIANGLE FAN 5 vertices, v12..v16 481 // note defined in anticlockw sie order.481 // note defined in anticlockwise order. 482 482 osg::Vec3(0.844538, 9.18133e-09, -0.712708), 483 483 osg::Vec3(1.0258, 9.18133e-09, -0.799221), … … 519 519 printTriangles("Triangles/Strip/Fan",*polyGeom); 520 520 521 // add the points geom try to the geode.521 // add the points geometry to the geode. 522 522 geode->addDrawable(polyGeom); 523 523 } … … 567 567 568 568 569 // create Geometry object to store all the ve tices and lines primtive.569 // create Geometry object to store all the vertices and lines primitive. 570 570 osg::Geometry* polyGeom = new osg::Geometry(); 571 571 572 // note, anticlockw sie ordering.572 // note, anticlockwise ordering. 573 573 osg::Vec3 myCoords[] = 574 574 { … … 621 621 int numIndices = sizeof(myIndices)/sizeof(unsigned short); 622 622 623 // The ere are three variants of the DrawElements osg::Primitive, UByteDrawElements which624 // contains unsigned char indic ies, UShortDrawElements which contains unsigned short indices,625 // and UIntDrawElements wh cih contains ... unsigned int indices.623 // There are three variants of the DrawElements osg::Primitive, UByteDrawElements which 624 // contains unsigned char indices, UShortDrawElements which contains unsigned short indices, 625 // and UIntDrawElements which contains ... unsigned int indices. 626 626 // The first parameter to DrawElements is 627 627 polyGeom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::QUADS,numIndices,myIndices)); … … 643 643 osg::Geode* geode = new osg::Geode(); 644 644 645 // add the points geom try to the geode.645 // add the points geometry to the geode. 646 646 geode->addDrawable(polyGeom); 647 647 648 648 //return geode; 649 649 650 // create a tran form to move the background back and forward with.650 // create a transform to move the background back and forward with. 651 651 652 652 osg::MatrixTransform* transform = new osg::MatrixTransform();
