1.使用OpenCasCade创建好brep后,对边界表达的模型进行 网格化处理.
2.occ 网格化处理 参照了 OSG plugin下 occ的

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
osg::Node* BuildShapeMesh(const TopoDS_Shape& aShape) { osg::ref_ptr<osg::Vec3Array> vertexList = new osg::Vec3Array(); osg::ref_ptr<osg::Group> root = new osg::Group(); osg::ref_ptr<osg::Geode> geode = new osg::Geode(); //osg::Array::Binding colorBinding = osg::Array::BIND_OVERALL; // vector to save _colorTool //osg::ref_ptr<osg::Vec3Array> colorList = new osg::Vec3Array(); // create one osg primitive set osg::ref_ptr<osg::DrawElementsUInt> triangleStrip = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0); unsigned int noOfTriangles = 0; osg::ref_ptr<osg::Geometry> geom = new osg::Geometry; gim::EntityPoints1 ep; // large vertex datasets work best with VBO. geom->setUseVertexBufferObjects(true); if (!aShape.IsNull()) { // clean any previous triangulation BRepTools::Clean(aShape); //_healShape(shape); /// call to incremental mesh on this shape /// \todo not sure why this 1 is passed. Its called deflection BTW /// need to find a way to calculate it double linearDeflection = 1.0; BRepMesh_IncrementalMesh(aShape, linearDeflection); ///iterate faces // this variable will help in keeping track of face indices unsigned int index = 0; for (TopExp_Explorer ex(aShape, TopAbs_FACE); ex.More(); ex.Next()) { TopoDS_Face face = TopoDS::Face(ex.Current()); TopLoc_Location location; /// triangulate current face Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation(face, location); if (!triangulation.IsNull()) { int noOfNodes = triangulation->NbNodes(); // Store vertices. Build vertex array here for (int j = 1; j <= triangulation->NbNodes(); j++) { // populate vertex list // Ref: http://www.opencascade.org/org/forum/thread_16694/?forum=3 gp_Pnt pt = (triangulation->Nodes())(j).Transformed(location.Transformation()); vertexList->push_back(osg::Vec3(pt.X(), pt.Y(), pt.Z())); } /// now we need to get face indices for triangles // get list of triangle first const Poly_Array1OfTriangle& triangles = triangulation->Triangles(); //No of triangles in this triangulation noOfTriangles = triangulation->NbTriangles(); Standard_Integer v1, v2, v3; for (unsigned int j = 1; j <= noOfTriangles; j++) { /// If face direction is reversed then we add verticews in reverse order /// order of vertices is important for normal calculation later if (face.Orientation() == TopAbs_REVERSED) { triangles(j).Get(v1, v3, v2); } else { triangles(j).Get(v1, v2, v3); } triangleStrip->push_back(index + v1 - 1); triangleStrip->push_back(index + v2 - 1); triangleStrip->push_back(index + v3 - 1); } index = index + noOfNodes; } } geom->setVertexArray(vertexList.get()); geom->addPrimitiveSet(triangleStrip.get()); } osg::Vec3Array* vs = dynamic_cast<osg::Vec3Array*>(vertexList->asArray()); vertex = new float[vs->size() * 3]; indices = new int[triangleStrip->size()]; vertexcount = vs->size(); for (int i = 0; i < vs->size(); i++) { vertex[i * 3] = vs->at(i).x(); vertex[i * 3 + 1] = vs->at(i).y(); vertex[i * 3 + 2] = vs->at(i).z(); } for (int i = 0; i < triangleStrip->size(); i++) { indices[i] = triangleStrip->getElement(i); } geode->addDrawable(geom); root->addChild(geode); //osgDB::writeNodeFile(*root, "saved.osg"); return root.release(); } |
3. 最后 将网格化后的数据进行到Unity
C++端
1 2 3 |
[DllImport("brep")] public static extern void GetVertex(out IntPtr vertex_data,out IntPtr indices_data, out int size); |
Unity端
1 2 3 4 5 6 7 |
UnityExport void GetVertex(float** vertex_data, int** indices_data, int* size) { BuildShapeMesh(test()); *vertex_data = vertex; *indices_data = indices; *size = vertexcount; } |
4. 注意 在OSG中查看 occ 模型 要注意开启
1 |
state->setMode(GL_CULL_FACE, osg::StateAttribute::ON); |
如果不开启,将进行双面材质的渲染, 不容易区分索引到底对不对.
转载请注明:veyvin » OCC网格模型导入unity