Open Scene Graph based on materials from http
Open. Scene. Graph based on materials from http: //www. openscenegraph. org/ Katia Oleinik: koleinik@bu. edu
Agenda: • • • Introduction to Open. Scene. Graph Hardware requirements Overview of OSG structure First example – displaying a model Building primitives Transformations Using Open. GL primitives Texture Special nodes
ü Introduction to Open. Scene. Graph • an open source 3 D graphics API (application programming interface) • used for - visual simulation, - computer games, - scientific visualization, - modeling, - training, etc.
ü Introduction to Open. Scene. Graph • written in C++ (encourages object oriented programming); • runs on a number of operating systems, including: - MS Windows - Max OS X - Linux - IRIX - Solaris - Sony Playstation uses Open. GL for rendering (allows for high performance graphics); supports the standard template library (STL);
ü Introduction to Open. Scene. Graph 3. 0 Features: • Support for performance increasing features o View frustum, small feature and occlusion culling o Level of detail (LOD) o State sorting and lazy state updating o Open. GL latest extensions o Multi-threading and database optimization • Support for Open. GL, from 1. 1 through 2. 0 including the latest extensions • Support for Open. GL Shading Language • Support for a wide range of 2 D image and 3 D database formats • Loaders available for Open. Flight, Terra. Page, OBJ, 3 DS, JPEG, PNG and Geo. TIFF • Particle effects • Support for anti-aliased True. Type text • Multi-threaded and configurable support for multiple CPU/multiple GPU machines
ü Introduction to Open. Scene. Graph 3. 0 latest updates: • • Support for Windows MS Visual Studio Support for Android on tablets and phones Support for IOS (i. Phone OS) on tablets and phones Improvements to osg. Volume class enabling high quality volume rendering
Fight. Gear Flight Simulator
Fight. Gear Flight Simulator
Fight. Gear Flight Simulator
ü Hardware requirements Processor • OSG runs on most contemporary CPUs. • OSG is thread-safe and can take advantage of multi-processor and dual core architectures. • OSG runs on both 32 - and 64 -bit processors. Graphics • OSG requires graphics hardware with robust Open. GL support • 256 MB of graphics RAM – good starting point RAM • 1 GB – good enough for many application, but you might need more, depending on your dataset Disc • Depends on your data requirements
ü Overview of OSG structure
ü Overview of OSG structure Root Matrix Geode Drawable Transformation Geode Drawable Matrix Drawable
ü First example – displaying a model • ex_simple_viewer. cpp // load the nodes from the command line arguments. osg: : Node* model = osg. DB: : read. Node. File(argv[1]); // initialize the viewer and set the scene to render osg. Viewer: : Viewer viewer; viewer. set. Scene. Data(model); // run viewer return viewer. run(); Root Node
ü First example – displaying a model • ex_simple_viewer. cpp: compiling, linking and running To compile and link % make ex_simple_viewer To run the viewer % ex_simple_viewer cow. obj First button – rotate the model Second button – translate Third button – scale Press “q” (“Esc” for Windows) button to exit
ü First example – displaying a model • ex_viewer_args. cpp // call argument parser osg: : Argument. Parser arguments (&argc, argv); std: : string filename; // define the argument line option arguments. read("--model", filename); // load the nodes from the command line arguments osg: : Node* model = osg. DB: : read. Node. File(filename);
ü First example – displaying a model • ex_viewer_args. cpp: running To compile and link % make ex_viewer_args To run the viewer % ex_viewer_args –-model cow. obj Try a few different models: dumptruck. osg teapot. osg
ü First example – displaying a model • Input OSG model file structure Geode { name "teapot. osg" node. Mask 0 xffff culling. Active TRUE num_drawables 1 Geometry { Data. Variance STATIC use. Display. List FALSE use. Vertex. Buffer. Objects TRUE Primitive. Sets 1 { Draw. Arrays TRIANGLES 0 9744 } Vertex. Array Vec 3 Array 9744 { 0. 367875 -0 0. 237053 0. 375 -0 0. 225 0. 365248 0. 086895 0. 225. . . } Color. Binding OVERALL Color. Array Vec 4 Array 1 { 1 1 } } }
ü Building geometric primitives • ex_simple_cone. cpp // Create a vector to represent the "center of the cone" Vec 3 vcen(xcen, ycen, zcen); osg: : Cone* cone = new Cone(vcen, radius, height); cone // Create a drawable object based on the cone osg: : Shape. Drawable *drawable = new Shape. Drawable(cone); // create a new geode (root node) osg: : Geode* geode = new Geode(); geode->add. Drawable(drawable); Root Node Drawable Geode
ü Building geometric primitives • Improving ex_simple_cone. cpp // Create a vector to represent the "center of the cone" osg: : Vec 3 vcen(xcen, ycen, zcen); osg: : Cone* cone = new Cone(vcen, radius, height); cone // Create a drawable object based on the cone osg: : Shape. Drawable *drawable = new Shape. Drawable(cone); drawable->set. Color(osg: : Vec 4(1. 0 f, 0. 0 f, 1. 0 f)); Drawable // create a new geode osg: : Geode* geode = new Geode(); geode->add. Drawable(drawable); Geode // create a root node osg: : Group *root = new osg: : Group(); root->add. Child(geode); Root Node
ü Building geometric primitives • ex_simple_cone. cpp To compile and link % make ex_simple_cone To run the viewer % ex_simple_cone
ü Building geometric primitives • ex_simple_cone. cpp OSG comes with a number of primitives § § § Box Sphere Cone Cylinder Capsule Special shapes (e. g. Infinite. Plane)
• ex_arrow. cpp // Create a cone and a cylinder Geode *make_cone( float xcen, …){} Geode *make_cylinder( float xcen, …){} ü Building geometric primitives cone cylinder // create an arrow, as a transform node Matrix. Transform* arrow = new Matrix. Transform; arrow->set. Matrix(Matrix: : scale(1. 0, 1. 0)); arrow->add. Child(cone); arrow->add. Child(cylinder); Transform Matrix // add the arrow to the upper transform Matrix. Transform* mt = new Matrix. Transform(); mt->set. Matrix( Matrix: : rotate(in. Degrees(30. 0), 1. 0, 0. 0)); Transform Matrix mt->add. Child(arrow); // create a root node osg: : Group *root = new osg: : Group(); root->add. Child(mt); Root Node
• Exercise Building 3 arrows ü Building geometric primitives cone cylinder Transform Matrix Root Node Transform Matrix
• Exercise: Building 3 arrows ü Building geometric primitives Group *make_vec_arrow(float shaft_radius, float total_length, float r, float g, float b) { float cone_radius = 2*shaft_radius; float cone_height = cone_radius; float shaft_length = total_length - cone_height; osg: : Geode *cylinder = make_cylinder(0. 0, shaft_length/2. 0, shaft_radius, shaft_length, r, g, b, 1. 0); osg: : Geode *cone = make_cone(0. 0, shaft_length + cone_height/4. 0, cone_radius, cone_height, r, g, b, 1. 0); osg: : Group* vec_arrow = new Group; vec_arrow->add. Child(cylinder); vec_arrow->add. Child(cone); return vec_arrow; } osg: : Group *red_arrow = make_vec_arrow(…); osg: : Matrix. Transform* xaxis = new Matrix. Transform; xaxis->add. Child(red_arrow); xaxis->set. Matrix(…);
• Reusing the geometry ü Building geometric primitives Faces Vertices Colors Geometry Geode 1 Transform Matrix Geode 2 Transform Matrix Root Node Geode 3 Transform Matrix
• Primitive. Set Class 1 3 4 3 2 Lines 2 1 2 0 1 Line. Strip 1 2 3 4 0 3 Points 0 4 1 2 ü Using Open. GL primitives 3 2 4 0 1 Line. Loop 5 6 0 1 Triangles Polygon 2 3 0 1 Quads 0 2 4 Triangle. Strip 1 0 4 0 5 7 2 4 Quad. Strip 6 3 5 3 1 2 Triangle. Fan
• Primitive. Set Class ü Using Open. GL primitives osg: : Group *root = new osg: : Group(); Group … osg: : Geode* prim. Geode = new osg: : Geode(); root->add. Child(prim. Geode); Geode … osg: : Geometry* prim. Geom = new osg: : Geometry(); prim. Geode->add. Drawable(prim. Geom); Geometry … viewer. set. Scene. Data(root) Vertices Faces Colors Texture
• Primitive. Set Class ü Using Open. GL primitives osg: : Vec 3 Array* pyramid. Vertices = new osg: : Vec 3 Array; pyramid. Vertices->push_back( osg: : Vec 3( 0, 0, 0) ); // pyramid. Vertices->push_back( osg: : Vec 3(10, 10, 0) ); // pyramid. Vertices->push_back( osg: : Vec 3( 5, 5, 10) ); // front left front right back left peak // create primitives: quad for the base osg: : Draw. Elements. UInt* pyramid. Base = new osg: : Draw. Elements. UInt(osg: : Primitive. Set: : QUADS, 0); pyramid. Base->push_back(3); pyramid. Base->push_back(2); pyramid. Base->push_back(1); pyramid. Base->push_back(0); // create primitives: triangles for the sides osg: : Draw. Elements. UInt* pyramid. Face. One = new osg: : Draw. Elements. UInt(osg: : Primitive. Set: : TRIANGLES, 0); pyramid. Face. One->push_back(1); pyramid. Face. One->push_back(4);
• Primitive. Set Class ü Using Open. GL primitives // assign all primitives to the Geometry node osg: : Geometry* pyramid. Geometry = new osg: : Geometry(); pyramid. Geometry->set. Vertex. Array( pyramid. Vertices ); pyramid. Geometry->add. Primitive. Set(pyramid. Base); pyramid. Geometry->add. Primitive. Set(pyramid. Face. One); pyramid. Geometry->add. Primitive. Set(pyramid. Face. Two); pyramid. Geometry->add. Primitive. Set(pyramid. Face. Three); pyramid. Geometry->add. Primitive. Set(pyramid. Face. Four); geometry // create a geode and add the geometry to the geode osg: : Geode* pyramid. Geode = new osg: : Geode(); pyramid. Geode->add. Drawable(pyramid. Geometry); Geode // Create a root node and add the geode osg: : Group* root = new osg: : Group(); root->add. Child(pyramid. Geode); Root Node
• Primitive. Set Class // create an array of colors osg: : Vec 4 Array* colors = new osg: : Vec 4 Array; colors->push_back(osg: : Vec 4(1. 0 f, 0. 0 f, colors->push_back(osg: : Vec 4(0. 0 f, 1. 0 f, colors->push_back(osg: : Vec 4(1. 0 f, ü Using Open. GL primitives 1. 0 f) ); ); //index 0 1 2 3 red green blue white // create an index array osg: : Template. Index. Array <unsigned int, osg: : Array: : UInt. Array. Type, 4, 4> * color. Index. Array; color. Index. Array = new osg: : Template. Index. Array<unsigned int, osg: : Array: : UInt. Array. Type, 4, 4>; color. Index. Array->push_back(0); // vertex 0 assigned color array element 0 color. Index. Array->push_back(1); // vertex 1 assigned color array element 1 color. Index. Array->push_back(2); // vertex 2 assigned color array element 2 color. Index. Array->push_back(3); // vertex 3 assigned color array element 3 color. Index. Array->push_back(0); // vertex 4 assigned color array element 0 // assign the arrays to the geometry pyramid. Geometry->set. Color. Array(colors); pyramid. Geometry->set. Color. Indices(color. Index. Array); pyramid. Geometry->set. Color. Binding(osg: : Geometry: : BIND_PER_VERTEX);
ü Transformations OSG allows for hierarchies of transformation node. Such structure makes it much easier to control the motions of each limb, part or the whole body. transform de transform geode o ge geode rm fo ns transform geode rm fo ns tra rm transform geode fo ns tra geode e od ge de o ge
ü Transformations OSG Matrix. Transform Class: // direct transformation specification transform->get. Matrix(); transform->set. Matrix(); // set identity Identity(); // navigation osg: : Matrix mt 1 = osg: : Matrix: : Translate(x, y, z); osg: : Matrix mt 2 = osg: : Matrix: : Rotate(angle, x, y, z); osg: : Matrix mt 3 = osg: : Matrix: : Scale(x, y, z); // for multiplying matrices osg: : Matrix result. Mat = mt 1 * t 2 * mt 3; // invert matrix osg: : Matrix: : Invert();
ü Adding. Texture // initialize texture class osg: : Texture 2 D* texture = new osg: : Texture 2 D; texture->set. Data. Variance(osg: : Object: : DYNAMIC); Any file format supported by the plugins // load the texture image from the file : osg: : Image* tex. Image = osg. DB: : read. Image. File(texture_file); if (! tex. Image){ std: : cout << " couldn't find texture, quiting. " << std: : endl; return -1; } // Assign the texture to the image we read from file: texture->set. Image(tex. Image); // Create a new State. Set with default settings: osg: : State. Set* state. Tex = new osg: : State. Set(); // Assign texture unit 0 of our new State. Set to the texture // enable the texture. state. Tex->set. Texture. Attribute. And. Modes(0, texture, osg: : State. Attribute: : ON); // Associate this state set with the Geode that contains // the primitive: geode->set. State. Set(state. Tex);
• Exercise: Reading texture from the command line // Add parsing texture option from a command line // call argument parser osg: : Argument. Parser arguments (&argc, argv); std: : string filename; // define the argument line option arguments. read("--texture", texfilename); osg: : Texture 2 D* texture = new osg: : Texture 2 D; texture->set. Data. Variance(osg: : Object: : DYNAMIC); // load the texture image from the file : osg: : Image* tex. Image = osg. DB: : read. Image. File(texture_file); if (! tex. Image){ std: : cout << " couldn't find texture, quiting. " << std: : endl; return -1; } … ü Adding. Texture
ü Special Nodes Switch node - Node for switching between different states of an object LOD node - Rendering Optimization node Billboard node – rendering optimization node Text node – node for presenting text on the screen
ü Special Nodes LOD (“level of detail” node - Rendering Optimization node This node “switches” based on the distance from the viewer to the object. It works like a regular group node: load. add. Child(detailed. Node); Set the visible range from the viewer to the object: load. set. Range(child. Number, near, far);
ü Special Nodes LOD lod = new LOD(); Lod. add. Child(detailed. Node); Lod. set. Range(0, 0, 10); Lod. add. Child(Not. Sodetailed. Node); Lod. set. Range(1, 100); Lod. add. Child(Corse. Node); Lod. set. Range(2, 1000); Lod. add. Child(No. Detail. Node); Lod. set. Range(2, 10000);
For up-to-date information on the project, in-depth details on how to compile and run libraries and examples, see the documentation on the Open. Scene. Graph website: http: //www. openscenegraph. org For support subscribe to OSG public mailing list: http: //www. openscenegraph. org/projects/osg/wiki/Mailing. Lists or forum: http: //forum. openscenegraph. org
Contact me: Katia Oleinik: koleinik@bu. edu Tutorial presentations and examples online: www. bu. edu/tech/research/training/presentations/list/ Online evaluation: http: //scv. bu. edu/survey/tutorial_evaluation. html
Resources • BU Scientific Computing and Visualization: http: //www. bu. edu/tech/research/scv/ • Open. Scene. Graph: http: //www. openscenegraph. org/ Autodesk Maya
- Slides: 42