Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to export triangle data in OpenSceneGraph

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

OpenSceneGraph how to export triangle data, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Today, I wrote a class to export triangles, which can export all triangle data of a Group (including all child of Group), mainly for collision detection. For example, there is a Group "bike", this Group contains a child Group "front wheel" and a child Group "rear wheel", and the child object is connected to the parent object through MatrixTransform. Then this class can export the triangle data of Group "bicycle" including "front wheel" and "rear wheel" to a vector, which is used for collision detection.

The code snippet is as follows:

Struct GetVertex {void operator () (const osg::Vec3& v1gramme Const osg::Vec3& v2je Const osg::Vec3& v3, bool) const {osg::Vec3 v1Newswire v1* (* matrix); osg::Vec3 v2Newcastle v2* (* matrix); osg::Vec3 v3Newcastle v3* (* matrix); vertexList- > push_back (v1New); vertexList- > push_back (v2New); vertexList- > push_back (v3New);} osg::Vec3Array* vertexList;osg::Matrix* matrix;} Class VertexVisitor:public osg::NodeVisitor {public:VertexVisitor (): osg::NodeVisitor (osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}; virtual void apply (osg::Geode& geode) {osg::NodePathList nodePathList=geode.getParentalNodePaths (stopAt); osg::NodePath firstNodePath=* (nodePathList.begin ()); osg::Matrix matrix=osg::computeLocalToWorld (firstNodePath); osg::Geode::DrawableList drawableList=geode.getDrawableList (); osg::TriangleFunctor tf;tf.vertexList=vertexList;tf.matrix=&matrix;for (osg::Geode::DrawableList::iterator itr=drawableList.begin () Itritr++) {(* itr)-> accept (tf);} traverse (geode);} osg::Vec3Array* vertexList;osg::Group* stopAt;}; osg::Vec3Array* TriangleConvertor::Convert (osg::Group* group) {osg::Vec3Array* vertexList=new osg::Vec3Array;VertexVisitor vertexVisitor;vertexVisitor.vertexList=vertexList;vertexVisitor.stopAt=group;group- > accept (vertexVisitor); return vertexList;}

The idea is to use a visitor to traverse all the Geode under the Group, and then use osg::TriangleFunctor to get all the triangular slices. The matrix transformation of each triangle is carried out during the acquisition, in order to transform the triangle data of local coordinate system into the triangle of world coordinate system. The way to get the matrix is using the osg::computeLocalToWorld method. It is worth noting that when looking for ParentPath, set the parameter haltTraversalAtNode to the Group at the beginning of the query. This is because if group still has a parent, it will get all the objects instead of returning triangular data based on the coordinate system in which the group is located. There may be some twists and turns in what you say.)

Let's put it this way, for example, the hierarchy is as follows:

Group RootMatrixTransform PositionMatrixTransform BikeMatrixTransform FrontWheelGeode 1Geode 2MatrixTransform RearWheelGeode 3Geode 4

Note that a MatrixTransform is also a Group. Here we need to get the triangle piece of the whole Bike model, and the whole triangle patch is affected by the Position matrix. So when traversing to Geode 1, if you call geode.getParentalNodePaths (), the NodeParentList will be returned as follows:

RootPositionBikeFrontWhell

This is equivalent to converting all triangles into values in the world coordinate system. It does not meet our requirements. So if haltTraversalAtNode=Bike is set, that is to say, call geode.getParentalNodePaths (Bike), then the NodeParentList is as follows:

BikeFrontWheel

This will meet our requirements. Then the calculated matrix is relative to the matrix of Bike, and the transformed triangle slice is the triangle slice of the whole Bike. Finally, the whole triangle piece is returned through Vec3Array.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report