OSG: Calculate the world transformation matrix, normal vector, and vertex coordinates of a node in nodevisitor.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.