DATAKIT SDK  V2026.4
testlibgltfwrite.cpp File Reference

Functions

Dtk_ErrorStatus GltfwInstances (const Dtk_string &inOutputFileName)
 Sample 2: writes 10 re-instanced cubes and a wireframe mesh into a GLTF file, demonstrating geometry instancing. More...
 
DtkErrorStatus GltfWriteSample (const Dtk_string &inResultDirectory)
 
Dtk_ErrorStatus GltfwTwoMeshes (const Dtk_string &inOutputFileName)
 Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a GLTF file. More...
 

Function Documentation

◆ GltfwInstances()

Dtk_ErrorStatus GltfwInstances ( const Dtk_string &  inOutputFileName)

Sample 2: writes 10 re-instanced cubes and a wireframe mesh into a GLTF file, demonstrating geometry instancing.

58 { // SAMPLE 2
59  Dtk_MeshPtr CubeMesh;
60  Dtk_MeshPtr WireMesh;
61  // Cube Sample
62  CubeMesh = sampleWriter::CreateMeshCube();
63  WireMesh = sampleWriter::CreateMeshWire();
64  Dtk_transfo cubetransfo;
65  Dtk_bool useless, isNewNode; // unused in this sample
66 
67  // For this sample, we write 10 instances of cube.
68  // the matrix given is shifted in all iterations
69  // Inside the for below, we give an ID to OpenInstance. Then we add the cube, and close the child.
70  // For all others instances, we give the same ID to OpenInstance, and just close the child. Gltf Writer will recognize the ID and automatic reinstance the cube with another matrix.
72  // First we initialize writing with name of files and protection fonction if needed (3rd parameter)
73  Dtk_GltfWriter gltfWriter( inOutputFileName, err );
74  if( err != dtkNoError )
75  return err;
76  gltfWriter.OpenInstance( "RootNode", -1, useless );
77  int instanceIdx;
78  for( instanceIdx = 0; instanceIdx < 10; instanceIdx++ )
79  {
80  gltfWriter.OpenInstance( "Cube", 1, isNewNode, cubetransfo ); // Giving ID 1
81  if( isNewNode )
82  {
83  gltfWriter.WriteMesh( CubeMesh );
84  }
85  gltfWriter.CloseLastInstance();
86  cubetransfo.addTranslate( Dtk_dir( 10, 0, 0 ) ); // shift matrix.
87  }
88  gltfWriter.OpenInstance("WireMesh", 2, isNewNode, Dtk_transfo()); // Giving ID 2
89  if (isNewNode)
90  {
91  gltfWriter.WriteMesh(WireMesh);
92  }
93  gltfWriter.CloseLastInstance();
94 
95  gltfWriter.CloseLastInstance();
96  return dtkNoError;
97 }

◆ GltfWriteSample()

DtkErrorStatus GltfWriteSample ( const Dtk_string &  inResultDirectory)
101 {
102  Dtk_string outputDirectory, fileName;
103  std::cout << std::endl << "----------------------------------------------" << std::endl;
104 
105  // Choosing output directory and file name
106  outputDirectory = inResultDirectory + L"Gltf/";
107  outputDirectory.FixPathSeparator();
108  outputDirectory.mkdir();
109 
110  std::cout << "Gltf Write start" << std::endl;
111  std::cout << "sample 1" << std::endl;
112  // sample 1 : just write 2 meshes
113  fileName = outputDirectory + L"twomeshes.gltf";
114  Dtk_ErrorStatus errorStatus = GltfwTwoMeshes( fileName );
115  if( errorStatus == dtkNoError )
116  std::cout << "=> " << fileName.c_str() << std::endl;
117  else
118  std::cout << "error : " << dtkTypeError( errorStatus ).c_str() << std::endl;
119 
120  std::cout << "sample 2" << std::endl;
121  // sample 2 : write instances
122  fileName = outputDirectory + L"instances.gltf";
123  errorStatus = GltfwInstances( fileName );
124  if( errorStatus == dtkNoError )
125  std::cout << "=> " << fileName.c_str() << std::endl;
126  else
127  std::cout << "error : " << dtkTypeError( errorStatus ).c_str() << std::endl;
128 
129  std::cout << "Gltf Write end" << std::endl;
130 
131  return errorStatus;
132 }

◆ GltfwTwoMeshes()

Dtk_ErrorStatus GltfwTwoMeshes ( const Dtk_string &  inOutputFileName)

Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a GLTF file.

17 { // SAMPLE 1
18  Dtk_MeshPtr CubeMesh, CylinderMesh;
19  Dtk_bool useless; // useless for this sample
20  // Cube Sample
21  CubeMesh = sampleWriter::CreateMeshCube();
22  // Cylinder Sample
23  CylinderMesh = sampleWriter::CreateMeshCylinder( 30 );
24 
26  // First we initialize writing with name of files and protection fonction if needed (3rd parameter)
27  Dtk_GltfWriter gltfWriter( inOutputFileName, err );
28  if( err != dtkNoError )
29  return err;
30 
31  // Rootnode creation
32  gltfWriter.OpenInstance( "RootNode", -1, useless );
33 
34  // from this node, create a Child for the cube, and naming it "Cube"
35  gltfWriter.OpenInstance( "Cube", -1, useless );
36  // write the cube
37  gltfWriter.WriteMesh( CubeMesh );
38 
39  // close the cube node
40  gltfWriter.CloseLastInstance();
41 
42  // from root node again, create a Child for the cylinder, and naming it "Cylinder"
43  gltfWriter.OpenInstance( "Cylinder", -1, useless );
44  // write the cube
45  gltfWriter.WriteMesh( CylinderMesh );
46  // close the cube node
47  gltfWriter.CloseLastInstance();
48 
49  // Rootnode closing.
50  gltfWriter.CloseLastInstance();
51 
52  return dtkNoError;
53 }
Dtk_transfo
This is the Transformation dedicated class.
Definition: dtk_transfo.hpp:19
sampleWriter::CreateMeshCube
Dtk_MeshPtr CreateMeshCube()
Mesh Cube sample.
Definition: testcreatemesh.cpp:207
Dtk_GltfWriter
GLTF Writer main class.
Definition: gltfw.hpp:22
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:53
GltfwInstances
Dtk_ErrorStatus GltfwInstances(const Dtk_string &inOutputFileName)
Sample 2: writes 10 re-instanced cubes and a wireframe mesh into a GLTF file, demonstrating geometry ...
Definition: testlibgltfwrite.cpp:57
Dtk_bool
char Dtk_bool
Definition: define.h:724
sampleWriter::CreateMeshWire
Dtk_MeshPtr CreateMeshWire()
Wire mesh sample.
Definition: testcreatemesh.cpp:389
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_SmartPtr
Definition: util_ptr_dtk.hpp:37
dtkTypeError
Dtk_string dtkTypeError(Dtk_Int32 errNumero)
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
sampleWriter::CreateMeshCylinder
Dtk_MeshPtr CreateMeshCylinder(int nbpoints)
Mesh Cylinder sample.
Definition: testcreatemesh.cpp:338
Dtk_string::mkdir
int mkdir() const
File Utility : Create a Directory.
Dtk_string::FixPathSeparator
void FixPathSeparator()
File Utility : Fixes path separator consistency. It lets you replace the '\' or '/' by the OS needed ...
GltfwTwoMeshes
Dtk_ErrorStatus GltfwTwoMeshes(const Dtk_string &inOutputFileName)
Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a GLTF file.
Definition: testlibgltfwrite.cpp:16
Dtk_transfo::addTranslate
void addTranslate(const Dtk_dir &V)
Translate the Dtk_transfo.
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:151
Dtk_dir
This is a mathematical direction class.
Definition: dtk_dir.hpp:14