Class mynodevisitor: Public OSG: nodevisitor
{
Pulic:
Mynodevisitor (): OSG: nodevisitor (OSG: nodevisitor: traverse_all_children)
{}
Void apply (OSG: geode & geode)
{
// Calculate the world transformation matrix corresponding to the current geode node, used to calculate the world coordinates corresponding to the vertex in geode
OSG: matrix geodematrix = OSG: computelocaltoworld (getnodepath ());
Unsigned int COUNT = geode. getnumdrawables ();
For (unsigned int geomidx = 0; geomidx <count; geomidx ++)
{
OSG: geometery * geometry = geode. getdrawable (geomidx)-> asgeometry ();
If (! Geometry) continue;
// Vertex data
OSG: vec3array * vertices = dynamic_cast <OSG: vec3array *> (geometry-> getvertexarray ());
// Normal Vector
OSG: vec3array * normals = dynamic_cast <OSG: vec3array *> (geometry-> getnormalarray ());
// Index the Array
For (unsigned int primitiveidx = 0; primitiveidx <geometry-> getnumprimitivesets (); ++ primitiveidx)
{
OSG: primitiveset * PS = geometry-> getprimitiveset (primitiveidx );
If (! PS) continue;
Switch (PS-> GetType ())
{
Case OSG: primitiveset: drawelementsushortprimitivetype:
{
OSG: drawelementsushort * Deus = dynamic_cast <OSG: drawelementsushort *> (PS );
Const unsigned int indexnum = Deus-> getnumindices ();
Switch (Deus-> getmode)
{
Case OSG: primitiveset: triangles:
// Suppose geometry-> getnormalbinding () = OSG: geometry: bind_per_vertex)
// Each vertex corresponds to a normal vector.
For (unsigned int I = 0; I <indexnum; I ++)
{
// Vertex Index
Unsigned int idx = Deus-> at (I );
// Normal vector. To convert it to world coordinates, multiply it by the inverse matrix transpose of geodematrix. For details, refer to the article on calculation of normal vectors.
Vec3 normalworld = Normals-> at (idx) * (transpose the inverse matrix of geodematrix );
// Vertex coordinates
Vec3 vertexworld = vertices-> at (idx) * geodematrix;
}
Break;
}
}
}
}
}
}
}
OSG: Calculate the world transformation matrix, normal vector, and vertex coordinates of a node in nodevisitor.