Establish scene graphics - 2 leaf nodes (Geode) and geometric information

Posted by gravedig2 on Fri, 11 Feb 2022 05:43:44 +0100

OSG Quick Start Guide Download address

1, Geometry overview

osg::ref_ptr<osg::Node> createSceneGraph() 
 // Create an object to hold geometric information
 osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; 
 // Creates an array of four vertices
 osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array; 
 geom->setVertexArray( v.get() ); 
 v->push_back( osg::Vec3( -1.f, 0.f, -1.f ) ); 
 v->push_back( osg::Vec3( 1.f, 0.f, -1.f ) ); 
 v->push_back( osg::Vec3( 1.f, 0.f, 1.f ) ); 
 v->push_back( osg::Vec3( -1.f, 0.f, 1.f ) ); 
 // Create an array of four colors
 osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array; 
 geom->setColorArray( c.get() ); 
 geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX ); 
 c->push_back( osg::Vec4( 1.f, 0.f, 0.f, 1.f ) ); 
 c->push_back( osg::Vec4( 0.f, 1.f, 0.f, 1.f ) ); 
 c->push_back( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) ); 
 c->push_back( osg::Vec4( 1.f, 1.f, 1.f, 1.f ) ); 
 // Creates an array of unique normals
 osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array; 
 geom->setNormalArray( n.get() ); 
 geom->setNormalBinding( osg::Geometry::BIND_OVERALL ); 
 n->push_back( osg::Vec3( 0.f, -1.f, 0.f ) ); 
 // Draws a polygon with four vertices from the saved data
 new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) ); 
 // Add geometry (Drawable) to the Geode class and return Geode 
 osg::ref_ptr<osg::Geode> geode = new osg::Geode; 
 geode->addDrawable( geom.get() ); 
 return geode.get(); 

1. Operation method

  1. Creates an array of vertices, colors, and normals
  2. Create osg::Geometry, a. put vertices, colors and normals into geometry, B. OSG:: drawarrays to specify the drawing method
  3. osg::Geode scene instantiation, geometric information into the node

2. Vector and array classes

osg::Vec2Two dimensional floating point array2D texture
osg::Vec33D floating point arrayVertex and normal data
The following is the STD:: vector < > version
osg::Vec2ArrayArray of vec2Texture coordinate array
osg::Vec3ArrayArray of vec3vertex array
osg::Vec4ArrayArray of vec4Color array

Tip: the use of vector resize() operator [] can reduce the reallocation of memory

3.Drawable class - Geometry

  1. Used to save rendering data
  2. Drawable is a virtual base class that cannot be instantiated directly
    Drawable subclass
  1. Geometry
Geometry functionOpenGL corresponding functionexplain
Set VBO data (equivalent to)
setVertexArrayglVertextPointer()To put it bluntly, it is the core VAO+VBO
setColorArrayglColorPointer()Save color
setNormalArrayglNormalPointer()Save vector
branchSpecifies whether other variables and vertices are on the same line
setColorBindingsetColorBinding(osg::Geometry::BIND_PER_VERTEX)Equivalent to in Core mode, in one line
setNormalBindingsetColorBinding(osg::Geometry::OVERALL)It is equivalent to that in Core mode, there are in each line
addPrimitiveSet()addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );Set rendering mode
  1. addPrimitiveSet(osg::PrimitiveSet *) and osg::DrawArrays
geom->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) ); 

a. osg::DrawArrays (QUADS) – draw squares in order
b. osg::DrawElementsUByte (QUADS) – draw squares in the specified order
c. osg::DrawElementsUByte (POINTS) – draw POINTS in the specified order
This is the most important sentence - painting

4.PrimitiveSet class diagram

DrawArraysDraw in orderDrawArrays(mode)
DrawElementsUIntDraw according to the specified points

DrawArrays mode type


2, Leaf node (Geode)

  1. A leaf node (Geode) no longer has children
  2. Save Geometry for rendering
  3. Geode::addDrawable(Drawable–Geometry)

3, Group node (osg::Group)

Summary: Group defines the interface for child nodes, and all nodes derived from group support child nodes (Geode is an exception, he has no group attribute)

1 sub interface – from osg::Group

Internal data: STD:: vector < ref_ ptr > child

class Group:public Node
	//internal data 
	std::vector< ref_ptr<Node> > child;
	//Add, delete, modify and check
	bool addChild(Node*);
	bool removeChild(Node*);
	bool replaceChild(Node*);
	bool containChild(Node*);
	unsigned int getNumChildren();

2 parent interface – from osg::Node

Internal list: STD:: vector < group * > parentlist;
Because it's just a reference, there's no need to manage the destructor, all of which are ordinary pointers

class Node: public Object
	//Define type
	typedef std::vector<Group*> ParentList;

	ParentList& getParents();
	Group* getParent(unsigned int I);



typeParent interface - from NodeSub interface - from Group
storagestd::vector<Group*> ParentList
ParentList parents
std::vector< ref_ptr<Node> > child
nothingbool addChild(Node*);
bool removeChild(Node*);
bool replaceChild(Node*);
checkbool containChild(Node*)ParentList& getParents();
Group* getParent(unsigned int I);
typedef std::vector<Group*> ParentList;

a. The child node does not need to manage the memory of the parent node, but directly saves the ordinary pointer, because only the parent node can release the child node
b. The child node holds multiple parent nodes
c. This pointer is automatically cleared
d. Usually: a node has a parent node (getNumParents() = = 0), get getParent(0)

3. Transform node

Class nameosg::Transform
osg:: MatrixTransform
functionNodes: recording all transformsMatrix - calculation tool

3.1 Matrix calculation tool

osg::Matrix = 4 × 4 matrix
Matrix itself is a 4 × 4 matrices (16 float s) are used for pure calculation

  1. Matrix assignment
osg::Matrix m;
m(0,1) = 0.f;  //Row 0, column 1
m(1,2)= 0.f;   //Row 1, column 2
  1. vector × Matrix matrix × matrix
osg::Matrix T;

osg:Matrix R;

Vec3 vPrime= v*R*T;

Comparison of matrix multiplication between OSG and OpenGL

Left multiplicationRight multiplication
v2= v1×R×T;v2 = T×R×v1
OSG right multiplyMultiply according to mathematics
  1. The right multiplication of matrix leads to the change of node connection mode
First, a MatrixTransform of mobile MT is created to generate mobile nodes

Then, create a rotating MR MatrixTransform, generate a rotating node, and hang this node under the mobile node

Finally, hang Geode under the rotation node

3.2 matrix transform node

Class nametypeexplain
MatrixTransformnodeReference - natural support ref_ ptr<>Just to catch the matrix
Matrixcomputing equipmentOrdinary class, does not support smart pointerJust to calculate
osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
osg::Matrix m;

Matrix::set function, passed in by column;

osg::Matrix m;
m.set( 1.f, 0.f, 0.f, 0.f, 
 0.f, 1.f, 0.f, 0.f, 
 0.f, 0.f, 1.f, 0.f, 
 10.f, 0.f, 0.f, 1.f );

osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;

3.3 position attribute transformation node


Matrix matrix in AutoCADThis is the way of yaw pitch roll in Bentley
  1. Quaternion osg::Quat
    How many degrees? About which axis

  2. Around an axis
    OSG:: quat Q0 (radian, axis);

Float angle(M_PI*0.5f);
osg::Vec3 axis(0.7f,0.7f,0.f);//Inclined axis

osg::Quat q0(angle,axis);
  1. Go around yaw pitch roll
    OSG:: quat Q0 (radian yaw, radian pitch, radian roll, axis yaw, axis pitch, axis roll);
osg::Vec3 yawAxis(0.f,0.f,1.f);
osg::Vec3 pitchAxis(1.f,0.f,0.f);
osg::Vec3 rollAxis(0.f,1.f,0.f);

osg::Quat q1(yawRad,yawAxis,pitchRad,pitchAxis,rollRad,rollAxis);

IV LOD level of detail node

  1. LOD can add child nodes
  1. LOD can control the display of child nodes

    Above: LOD has three child nodes, which are displayed only when the distance between observation points meets the effective range of the first child node

  2. code

osg::ref_ptr<osg::Geode> geode1;
osg::ref_ptr<osg::Geode> geode2;

osg::ref_ptr<osg::LOD> lod = new osg::LOD;

a. Displayed when the distance between the viewpoint and this child node is less than 1000 units
b.LOD is a range that can be specified for each child node

Five switch nodes (osg::Switch)

osg::ref_ptr<osg::Switch> sw= new osg::Switch;