How to set texture coordinate for a shape when exporting gltf file

I have a bunch of triangles, and all the information about the triangles including texture coordinates.

I want to convert these triangular faces into shapes and export them by RWGltf_CafWriter, so I want to know how to add texture coordinate information to the shape, so that the texture can be displayed correctly in gltf.

Dmitrii Pasukhin's picture

Hello,

To create a mesh face "Poly_Triangulation" should be used.

You can set Triangle's Vertexes (Node)

You can set Triangle's 'UV Vertexes (UVNode, 2d nodes, the same with TCoord, the number should be same with Node)

You can set Normals (if exists, the number is the same with Node) or can be generated by Poly::ComputeNormals

You can set Triangles (simple triangles only)

Best regards, Dmitrii.

jack-wangss's picture

Hi Dmitry,

Thank you very much for your reply. I have seen "Poly_Triangulation" before, but I am not sure it can work with "RWGltf_CafWriter"(it seems only support "TopoDS_Shape").

Do you know some way to convert "Poly_Triangulation" to "TopoDS_Shape" and keep the texture infomation for gltf?

Thanks with best regards.

Dmitrii Pasukhin's picture

Poly_Triangulation is the main part of the shape.

For example, to create a TopoDS_Face you can do the next one:

Handle(Poly_Triangulation) aResMesh;
// Some code to generate or create a valid mesh object
// aResMesh = transferTriangleWithPointNormalsUVIndices(aTriangleWithPointNormalsUVIndices, aPoints, aPointsSize, anUVPoints, aPreparedNorms, myScaleUnit);
BRep_Builder aBuilder; // Tool to work with shapes
TopoDS_Face aFace; // New TopoDS_Shape object
aBuilder.MakeFace(aFace, aResMesh);
// Now we have a TopoDS_Shape, that can be used to set for a XBF document
// The next code to set a shape to the Document
  Handle(TDocStd_Document) aDoc;
  Handle(TDocStd_Application) anApp = new TDocStd_Application;
  // Initialize standard document formats at creation - they should
  // be available even if this DRAW plugin is not loaded by pload command
  StdLDrivers::DefineFormat(anApp);
  BinLDrivers::DefineFormat(anApp);
  XmlLDrivers::DefineFormat(anApp);
  StdDrivers::DefineFormat(anApp);
  BinDrivers::DefineFormat(anApp);
  XmlDrivers::DefineFormat(anApp);
anApp->NewDoc(aDoc);
Handle(XCAFDoc_ShapeTool) aShTool = XCAFDoc_DocumetTool(aDoc->Main());
aShTool->AddShape(aFace);
// Additionally you can link a color and another attributes.
// Write a Gltf(glb)
RWGltf_CafReader aReader;
aReader.Perform("d:/test.glb");
// Done

Best regards, Dmitrii

jack-wangss's picture

I wrote a test based the code you provided and it worked well. Thanks again.

Attachments: