2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osggeometry.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Geode>
|
2013-06-18 19:18:28 +08:00
|
|
|
#include <osg/Geometry>
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/PolygonStipple>
|
|
|
|
#include <osg/TriangleFunctor>
|
2005-04-08 17:45:06 +08:00
|
|
|
#include <osg/io_utils>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/WriteFile>
|
|
|
|
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#include <osg/Math>
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <iostream>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-04 17:32:59 +08:00
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// This demo illustrates how to create the various different types of geometry that
|
2013-06-21 17:40:43 +08:00
|
|
|
// the osg::Geometry class can represent. This demo uses the OpenGL red book diagram of different
|
|
|
|
// OpenGL Primitives as a template for all the equivalent OpenSceneGraph Primitives. The OpenSceneGraph
|
|
|
|
// wraps OpenGL very thinly and therefore uses all the same enum and naming conventions. The coordinate data is also
|
2007-12-11 01:30:18 +08:00
|
|
|
// wrapped around OpenGL's vertex arrays and draw arrays/elements calls. Familiarity with
|
2010-09-15 20:00:12 +08:00
|
|
|
// OpenGL will help you understand the osg::Geometry class which encapsulate all this, or if you
|
2015-04-13 18:43:56 +08:00
|
|
|
// haven't learned OpenGL yet, learning osg::Geometry will help you understand how OpenGL
|
2003-03-15 04:35:45 +08:00
|
|
|
// works!
|
|
|
|
|
|
|
|
// The osg::Geometry class "is a" subclass of osg::Drawable base class, so is an object that provides
|
|
|
|
// a draw method for drawing objects in the scene. osg::Geometry contains all the vertex, normal
|
2007-12-11 01:30:18 +08:00
|
|
|
// color and texture coordinate arrays required to specify the coordinates of your objects, and the
|
|
|
|
// primitives join these coordinates together as the points, lines or surfaces that you will see
|
2013-06-21 17:40:43 +08:00
|
|
|
// rendered on your screen.
|
2003-03-15 04:35:45 +08:00
|
|
|
//
|
|
|
|
// This demo is split into two functions, the createScene() function which creates the scene graph
|
|
|
|
// with the various primitives in it, and the main() which sets up a basic viewer window and
|
|
|
|
// adds to the it the scene generated by createScene().
|
|
|
|
|
|
|
|
|
|
|
|
struct NormalPrint
|
|
|
|
{
|
2013-06-21 17:40:43 +08:00
|
|
|
void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
osg::Vec3 normal = (v2-v1)^(v3-v2);
|
|
|
|
normal.normalize();
|
|
|
|
std::cout << "\t("<<v1<<") ("<<v2<<") ("<<v3<<") "<<") normal ("<<normal<<")"<<std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// decompose Drawable primitives into triangles, print out these triangles and computed normals.
|
2003-03-15 04:35:45 +08:00
|
|
|
void printTriangles(const std::string& name, osg::Drawable& drawable)
|
|
|
|
{
|
|
|
|
std::cout<<name<<std::endl;
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::TriangleFunctor<NormalPrint> tf;
|
|
|
|
drawable.accept(tf);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
std::cout<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
/// Create a scene with examples of the different types of OpenGL primitives.
|
|
|
|
/// The primitives are the same as shown in the OpenGL diagram loaded in
|
|
|
|
/// createBackground() (Images/primitives.gif).
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Node* createScene()
|
|
|
|
{
|
|
|
|
// create the Geode (Geometry Node) to contain all our osg::Geometry objects.
|
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// following are separate blocks for creating POINTS, LINES, LINE_STRIP, LINE_LOOP, POLYGON, QUADS,
|
|
|
|
// QUAD_STRIP, TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN primitives. An image of these primitives
|
|
|
|
// is provided in the distribution: OpenSceneGraph-Data/Images/primitives.gif.
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// create POINTS
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and points primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* pointsGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// create a Vec3Array and add to it all my coordinates.
|
2013-06-21 17:40:43 +08:00
|
|
|
// Like all the *Array variants (see include/osg/Array) , Vec3Array is derived from both osg::Array
|
2003-03-15 04:35:45 +08:00
|
|
|
// and std::vector<>. osg::Array's are reference counted and hence sharable,
|
2007-12-11 01:30:18 +08:00
|
|
|
// which std::vector<> provides all the convenience, flexibility and robustness
|
2003-03-15 04:35:45 +08:00
|
|
|
// of the most popular of all STL containers.
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array;
|
|
|
|
vertices->push_back(osg::Vec3(-1.02168, -2.15188e-09, 0.885735));
|
|
|
|
vertices->push_back(osg::Vec3(-0.976368, -2.15188e-09, 0.832179));
|
|
|
|
vertices->push_back(osg::Vec3(-0.873376, 9.18133e-09, 0.832179));
|
|
|
|
vertices->push_back(osg::Vec3(-0.836299, -2.15188e-09, 0.885735));
|
|
|
|
vertices->push_back(osg::Vec3(-0.790982, 9.18133e-09, 0.959889));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
pointsGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// create the color of the geometry, one single for the whole geometry.
|
2007-12-11 01:30:18 +08:00
|
|
|
// for consistency of design even one single color must added as an element
|
2003-03-15 04:35:45 +08:00
|
|
|
// in a color array.
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
// add a white color, colors take the form r,g,b,a with 0.0 off, 1.0 full on.
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// pass the color array to points geometry, note the binding to tell the geometry
|
2003-03-15 04:35:45 +08:00
|
|
|
// that only use one color for the whole object.
|
2013-06-21 17:43:15 +08:00
|
|
|
pointsGeom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Set the normal in the same way as the color.
|
|
|
|
// (0,-1,0) points toward the viewer, in the default coordinate
|
|
|
|
// setup. Even for POINTS, the normal specified here
|
|
|
|
// is used to determine how the geometry appears under different
|
|
|
|
// lighting conditions.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
pointsGeom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// create and add a DrawArray Primitive (see include/osg/Primitive). The first
|
|
|
|
// parameter passed to the DrawArrays constructor is the Primitive::Mode which
|
2003-03-15 04:35:45 +08:00
|
|
|
// in this case is POINTS (which has the same value GL_POINTS), the second
|
|
|
|
// parameter is the index position into the vertex array of the first point
|
|
|
|
// to draw, and the third parameter is the number of points to draw.
|
|
|
|
pointsGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS,0,vertices->size()));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(pointsGeom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create LINES
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* linesGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// this time we'll preallocate the vertex array to the size we
|
2003-03-15 04:35:45 +08:00
|
|
|
// need and then simple set them as array elements, 8 points
|
|
|
|
// makes 4 line segments.
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(8);
|
|
|
|
(*vertices)[0].set(-1.13704, -2.15188e-09, 0.40373);
|
|
|
|
(*vertices)[1].set(-0.856897, -2.15188e-09, 0.531441);
|
|
|
|
(*vertices)[2].set(-0.889855, -2.15188e-09, 0.444927);
|
|
|
|
(*vertices)[3].set(-0.568518, -2.15188e-09, 0.40373);
|
|
|
|
(*vertices)[4].set(-1.00933, -2.15188e-09, 0.370773);
|
|
|
|
(*vertices)[5].set(-0.716827, -2.15188e-09, 0.292498);
|
|
|
|
(*vertices)[6].set(-1.07936, 9.18133e-09, 0.317217);
|
|
|
|
(*vertices)[7].set(-0.700348, 9.18133e-09, 0.362533);
|
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
linesGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// set the colors as before, plus using the above
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Set the normal in the same way as the color.
|
|
|
|
// As above for POINTS, this normal is used for the lighting
|
|
|
|
// calculations of the LINES.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,8));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(linesGeom);
|
|
|
|
}
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// create LINE_STRIP
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* linesGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
// this time we'll preallocate the vertex array to the size
|
2003-03-15 04:35:45 +08:00
|
|
|
// and then use an iterator to fill in the values, a bit perverse
|
|
|
|
// but does demonstrate that we have just a standard std::vector underneath.
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(5);
|
|
|
|
osg::Vec3Array::iterator vitr = vertices->begin();
|
|
|
|
(vitr++)->set(-0.0741545, -2.15188e-09, 0.416089);
|
|
|
|
(vitr++)->set(0.234823, -2.15188e-09, 0.259541);
|
|
|
|
(vitr++)->set(0.164788, -2.15188e-09, 0.366653);
|
|
|
|
(vitr++)->set(-0.0288379, -2.15188e-09, 0.333695);
|
|
|
|
(vitr++)->set(-0.0453167, -2.15188e-09, 0.280139);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
linesGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// set the colors as before, plus using the above
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Set the normal in the same way as the color (see note at POINTS, above).
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP,0,5));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(linesGeom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create LINE_LOOP
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* linesGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// this time we'll a C arrays to initialize the vertices.
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3 myCoords[] =
|
|
|
|
{
|
|
|
|
osg::Vec3(0.741546, -2.15188e-09, 0.453167),
|
|
|
|
osg::Vec3(0.840418, -2.15188e-09, 0.304858),
|
|
|
|
osg::Vec3(1.12468, -2.15188e-09, 0.300738),
|
|
|
|
osg::Vec3(1.03816, 9.18133e-09, 0.453167),
|
|
|
|
osg::Vec3(0.968129, -2.15188e-09, 0.337815),
|
|
|
|
osg::Vec3(0.869256, -2.15188e-09, 0.531441)
|
|
|
|
};
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
linesGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// set the colors as before, plus using the above
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
linesGeom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Set the normal in the same way as the color (see note at POINTS, above).
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
|
2013-06-21 17:43:15 +08:00
|
|
|
linesGeom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP,0,numCoords));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(linesGeom);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Now we'll stop creating separate normal and color arrays.
|
|
|
|
// Since we are using the same values all the time, we'll just
|
|
|
|
// share the same ColorArray and NormalArrays.
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Set the colors as before, use a ref_ptr rather than just
|
2003-03-15 04:35:45 +08:00
|
|
|
// standard C pointer, as that in the case of it not being
|
|
|
|
// assigned it will still be cleaned up automatically.
|
|
|
|
osg::ref_ptr<osg::Vec4Array> shared_colors = new osg::Vec4Array;
|
|
|
|
shared_colors->push_back(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Same trick for shared normal.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::ref_ptr<osg::Vec3Array> shared_normals = new osg::Vec3Array;
|
|
|
|
shared_normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
|
|
|
|
|
|
|
|
// Note on vertex ordering.
|
2010-09-15 20:00:12 +08:00
|
|
|
// According to the OpenGL diagram vertices should be specified in a clockwise direction.
|
|
|
|
// In reality you need to specify coords for polygons in a anticlockwise direction
|
|
|
|
// for their front face to be pointing towards you; get this wrong and you could
|
2013-06-21 17:40:43 +08:00
|
|
|
// find back face culling removing the wrong faces of your models. The OpenGL diagram
|
2010-09-15 20:00:12 +08:00
|
|
|
// is just plain wrong, but it's a nice diagram so we'll keep it for now!
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// create POLYGON
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* polyGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// this time we'll use C arrays to initialize the vertices.
|
2007-12-11 01:30:18 +08:00
|
|
|
// note, anticlockwise ordering.
|
2013-06-21 17:40:43 +08:00
|
|
|
// note II, OpenGL polygons must be convex, planar polygons, otherwise
|
2003-03-15 04:35:45 +08:00
|
|
|
// undefined results will occur. If you have concave polygons or ones
|
2007-01-08 19:23:52 +08:00
|
|
|
// that cross over themselves then use the osgUtil::Tessellator to fix
|
2003-03-15 04:35:45 +08:00
|
|
|
// the polygons into a set of valid polygons.
|
|
|
|
osg::Vec3 myCoords[] =
|
|
|
|
{
|
|
|
|
osg::Vec3(-1.0464, 0.0f, -0.193626),
|
|
|
|
osg::Vec3(-1.0258, 0.0f, -0.26778),
|
|
|
|
osg::Vec3(-0.807461, 0.0f, -0.181267),
|
|
|
|
osg::Vec3(-0.766264, 0.0f, -0.0576758),
|
|
|
|
osg::Vec3(-0.980488, 0.0f, -0.094753)
|
|
|
|
};
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
polyGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// use the shared color array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// use the shared normal array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,numCoords));
|
|
|
|
|
|
|
|
printTriangles("Polygon",*polyGeom);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(polyGeom);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create QUADS
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* polyGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// note, anticlockwise ordering.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3 myCoords[] =
|
|
|
|
{
|
|
|
|
osg::Vec3(0.0247182, 0.0f, -0.156548),
|
|
|
|
osg::Vec3(0.0247182, 0.0f, -0.00823939),
|
|
|
|
osg::Vec3(-0.160668, 0.0f, -0.0453167),
|
|
|
|
osg::Vec3(-0.222464, 0.0f, -0.13183),
|
|
|
|
|
|
|
|
osg::Vec3(0.238942, 0.0f, -0.251302),
|
|
|
|
osg::Vec3(0.333696, 0.0f, 0.0329576),
|
|
|
|
osg::Vec3(0.164788, 0.0f, -0.0453167),
|
|
|
|
osg::Vec3(0.13595, 0.0f, -0.255421)
|
|
|
|
};
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
polyGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// use the shared color array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// use the shared normal array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,numCoords));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
printTriangles("Quads",*polyGeom);
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(polyGeom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create QUAD_STRIP
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* polyGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// note, first coord at top, second at bottom, reverse to that buggy OpenGL image..
|
|
|
|
osg::Vec3 myCoords[] =
|
|
|
|
{
|
|
|
|
osg::Vec3(0.733306, -2.15188e-09, -0.0741545),
|
|
|
|
osg::Vec3(0.758024, -2.15188e-09, -0.205985),
|
|
|
|
|
|
|
|
osg::Vec3(0.885735, -2.15188e-09, -0.0576757),
|
|
|
|
osg::Vec3(0.885735, -2.15188e-09, -0.214224),
|
|
|
|
|
|
|
|
osg::Vec3(0.964009, 9.18133e-09, -0.0370773),
|
|
|
|
osg::Vec3(1.0464, 9.18133e-09, -0.173027),
|
|
|
|
|
|
|
|
osg::Vec3(1.11232, -2.15188e-09, 0.0123591),
|
|
|
|
osg::Vec3(1.12468, 9.18133e-09, -0.164788),
|
|
|
|
};
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
polyGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// use the shared color array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// use the shared normal array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
printTriangles("Quads strip",*polyGeom);
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(polyGeom);
|
|
|
|
}
|
|
|
|
|
|
|
|
// create TRIANGLES, TRIANGLE_STRIP and TRIANGLE_FAN all in one Geometry/
|
|
|
|
{
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* polyGeom = new osg::Geometry();
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// note, first coord at top, second at bottom, reverse to that buggy OpenGL image..
|
|
|
|
osg::Vec3 myCoords[] =
|
|
|
|
{
|
|
|
|
// TRIANGLES 6 vertices, v0..v5
|
2007-12-11 01:30:18 +08:00
|
|
|
// note in anticlockwise order.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3(-1.12056, -2.15188e-09, -0.840418),
|
|
|
|
osg::Vec3(-0.95165, -2.15188e-09, -0.840418),
|
|
|
|
osg::Vec3(-1.11644, 9.18133e-09, -0.716827),
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// note in anticlockwise order.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3(-0.840418, 9.18133e-09, -0.778623),
|
|
|
|
osg::Vec3(-0.622074, 9.18133e-09, -0.613835),
|
|
|
|
osg::Vec3(-1.067, 9.18133e-09, -0.609715),
|
|
|
|
|
|
|
|
// TRIANGLE STRIP 6 vertices, v6..v11
|
2013-06-21 17:40:43 +08:00
|
|
|
// note defined top point first,
|
2003-03-15 04:35:45 +08:00
|
|
|
// then anticlockwise for the next two points,
|
|
|
|
// then alternating to bottom there after.
|
|
|
|
osg::Vec3(-0.160668, -2.15188e-09, -0.531441),
|
|
|
|
osg::Vec3(-0.160668, -2.15188e-09, -0.749785),
|
|
|
|
osg::Vec3(0.0617955, 9.18133e-09, -0.531441),
|
|
|
|
osg::Vec3(0.168908, -2.15188e-09, -0.753905),
|
|
|
|
osg::Vec3(0.238942, -2.15188e-09, -0.531441),
|
|
|
|
osg::Vec3(0.280139, -2.15188e-09, -0.823939),
|
|
|
|
|
|
|
|
// TRIANGLE FAN 5 vertices, v12..v16
|
2007-12-11 01:30:18 +08:00
|
|
|
// note defined in anticlockwise order.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3(0.844538, 9.18133e-09, -0.712708),
|
|
|
|
osg::Vec3(1.0258, 9.18133e-09, -0.799221),
|
|
|
|
osg::Vec3(1.03816, -2.15188e-09, -0.692109),
|
|
|
|
osg::Vec3(0.988727, 9.18133e-09, -0.568518),
|
|
|
|
osg::Vec3(0.840418, -2.15188e-09, -0.506723),
|
|
|
|
|
|
|
|
};
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
polyGeom->setVertexArray(vertices);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// use the shared color array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setColorArray(shared_colors.get(), osg::Array::BIND_OVERALL);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// use the shared normal array.
|
2013-06-21 17:43:15 +08:00
|
|
|
polyGeom->setNormalArray(shared_normals.get(), osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
// This time we simply use primitive, and hardwire the number of coords to use
|
2003-03-15 04:35:45 +08:00
|
|
|
// since we know up front,
|
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,6));
|
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,6,6));
|
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_FAN,12,5));
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
// polygon stipple
|
|
|
|
osg::StateSet* stateSet = new osg::StateSet();
|
|
|
|
polyGeom->setStateSet(stateSet);
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && !defined(OSG_GL3_AVAILABLE)
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::PolygonStipple* polygonStipple = new osg::PolygonStipple;
|
|
|
|
stateSet->setAttributeAndModes(polygonStipple,osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON);
|
2009-11-12 20:07:13 +08:00
|
|
|
#endif
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
printTriangles("Triangles/Strip/Fan",*polyGeom);
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(polyGeom);
|
|
|
|
}
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Turn off the lighting on the geode. This is not required for setting up
|
|
|
|
// the geometry. However, by default, lighting is on, and so the normals
|
|
|
|
// above are used to light the geometry.
|
|
|
|
// - With lighting turned off, the geometry has the same color
|
|
|
|
// regardless of the angle you view it from.
|
|
|
|
// - With lighting turned on, the colors darken as the light moves
|
|
|
|
// away from the normal.
|
2017-04-24 21:44:59 +08:00
|
|
|
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,
|
|
|
|
osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
|
2013-06-21 17:40:43 +08:00
|
|
|
return geode;
|
2017-04-25 03:55:40 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2010-09-15 20:00:12 +08:00
|
|
|
// define a node callback to animate a transform as a cycle along the y axis, between 0 and 2.0.
|
2003-03-15 04:35:45 +08:00
|
|
|
class MyTransformCallback : public osg::NodeCallback
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
MyTransformCallback(float angularVelocity)
|
|
|
|
{
|
|
|
|
_angular_velocity = angularVelocity;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator() (osg::Node* node, osg::NodeVisitor* nv)
|
|
|
|
{
|
2013-06-21 17:40:43 +08:00
|
|
|
osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node);
|
2003-03-15 04:35:45 +08:00
|
|
|
if (nv && transform && nv->getFrameStamp())
|
|
|
|
{
|
2007-01-25 20:02:51 +08:00
|
|
|
double time = nv->getFrameStamp()->getSimulationTime();
|
2003-03-15 04:35:45 +08:00
|
|
|
transform->setMatrix(osg::Matrix::translate(0.0f,1.0f+cosf(time*_angular_velocity),0.0f));
|
|
|
|
}
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// must continue subgraph traversal.
|
2013-06-21 17:40:43 +08:00
|
|
|
traverse(node,nv);
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
protected:
|
2013-06-21 17:40:43 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
float _angular_velocity;
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
};
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2017-04-24 21:44:59 +08:00
|
|
|
/// Create a quad that sits behind the geometry from createScene()
|
|
|
|
/// and shows the image from the OpenGL docs that geometry duplicates.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Node* createBackground()
|
2013-06-21 17:40:43 +08:00
|
|
|
{
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
// we'll create a texture mapped quad to sit behind the Geometry
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile("Images/primitives.gif");
|
2003-03-15 04:35:45 +08:00
|
|
|
if (!image) return NULL;
|
2013-06-21 17:40:43 +08:00
|
|
|
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// create Geometry object to store all the vertices and lines primitive.
|
2013-06-18 19:18:28 +08:00
|
|
|
osg::Geometry* polyGeom = new osg::Geometry();
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// note, anticlockwise ordering.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3 myCoords[] =
|
|
|
|
{
|
|
|
|
osg::Vec3(-1.22908f,0.0f,1.0f),
|
|
|
|
osg::Vec3(-1.22908f,0.0f,-1.0f),
|
|
|
|
osg::Vec3(1.22908f,0.0f,-1.0f),
|
|
|
|
osg::Vec3(1.22908f,0.0f,1.0f)
|
|
|
|
};
|
|
|
|
|
|
|
|
int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
|
|
|
|
|
|
|
|
// pass the created vertex array to the points geometry object.
|
|
|
|
polyGeom->setVertexArray(new osg::Vec3Array(numCoords,myCoords));
|
|
|
|
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
|
2013-06-27 17:54:12 +08:00
|
|
|
polyGeom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Set the normal in the same way as the color.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
|
2013-06-27 17:54:12 +08:00
|
|
|
polyGeom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
osg::Vec2 myTexCoords[] =
|
|
|
|
{
|
|
|
|
osg::Vec2(0,1),
|
|
|
|
osg::Vec2(0,0),
|
|
|
|
osg::Vec2(1,0),
|
|
|
|
osg::Vec2(1,1)
|
|
|
|
};
|
|
|
|
|
2004-04-17 06:07:30 +08:00
|
|
|
int numTexCoords = sizeof(myTexCoords)/sizeof(osg::Vec2);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// pass the created tex coord array to the points geometry object,
|
|
|
|
// and use it to set texture unit 0.
|
|
|
|
polyGeom->setTexCoordArray(0,new osg::Vec2Array(numTexCoords,myTexCoords));
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// we'll use indices and DrawElements to define the primitive this time.
|
2003-03-15 04:35:45 +08:00
|
|
|
unsigned short myIndices[] =
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1,
|
2009-11-12 20:07:13 +08:00
|
|
|
3,
|
|
|
|
2
|
2003-03-15 04:35:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int numIndices = sizeof(myIndices)/sizeof(unsigned short);
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// There are three variants of the DrawElements osg::Primitive, UByteDrawElements which
|
|
|
|
// contains unsigned char indices, UShortDrawElements which contains unsigned short indices,
|
2013-06-21 17:40:43 +08:00
|
|
|
// and UIntDrawElements which contains ... unsigned int indices.
|
|
|
|
// The first parameter to DrawElements is
|
2009-11-12 20:07:13 +08:00
|
|
|
polyGeom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP,numIndices,myIndices));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2013-06-21 17:40:43 +08:00
|
|
|
// new we need to add the texture to the Drawable, we do so by creating a
|
2003-03-15 04:35:45 +08:00
|
|
|
// StateSet to contain the Texture2D StateAttribute.
|
|
|
|
osg::StateSet* stateset = new osg::StateSet;
|
|
|
|
|
|
|
|
// set up the texture.
|
|
|
|
osg::Texture2D* texture = new osg::Texture2D;
|
|
|
|
texture->setImage(image);
|
|
|
|
|
|
|
|
stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
polyGeom->setStateSet(stateset);
|
|
|
|
|
2017-04-24 21:44:59 +08:00
|
|
|
// create the Geode (Geometry Node) to contain the quad's osg::Geometry
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// add the points geometry to the geode.
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(polyGeom);
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
// Turn off the lighting (see note in createScene(), above).
|
2017-04-24 21:44:59 +08:00
|
|
|
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,
|
|
|
|
osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// create a transform to move the background back and forward with.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::MatrixTransform* transform = new osg::MatrixTransform();
|
|
|
|
transform->setUpdateCallback(new MyTransformCallback(1.0f));
|
|
|
|
transform->addChild(geode);
|
|
|
|
|
|
|
|
return transform;
|
2017-04-25 03:55:40 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
int main(int, char **)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
// create the model
|
|
|
|
osg::Group* root = new osg::Group;
|
|
|
|
root->addChild( createScene() );
|
|
|
|
root->addChild( createBackground() );
|
|
|
|
|
2017-04-25 03:55:40 +08:00
|
|
|
//osgDB::writeNodeFile(*root,"geometry.osgt");
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// add model to viewer.
|
|
|
|
viewer.setSceneData( root );
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|