GLTFImporter
Repository source: GLTFImporter
Description¶
The example uses vtkGLTFImporter to import a scene from a gltf file. glTF (derivative short form of GL Transmission Format) is a file format for 3D scenes and models using the JSON standard. It is an API-neutral runtime asset delivery format developed by the Khronos Group 3D Formats Working Group. It was announced at HTML5DevConf 2016.
There are many sources of glTF file including:
- SketchLab. To download you will need to signup. There are also non-free models offered here.
- Khronos Group
usage
GLTFImporter FlightHelmet.gltf
Warning
When you run the example, be sure to specify the full pathname of the .glTF file. There is currently a bug in the vtkGLTFImporter. For example, if your home directry is /home/janedoe the the full pathname of the FlightHelmet data is /home/janedoe/VTKExamples/src/Testing/Data/gltf/FlightHelmet/FlightHelmet.gltf.
Info
The original data can be found here: FlightHelmet Data). Be sure to download all of the files.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
GLTFImporter.cxx
#include <vtkCamera.h>
#include <vtkGLTFImporter.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkLight.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
int main(int argc, char* argv[])
{
if (argc <= 1)
{
std::cout << "Usage: " << argv[0] << " <gltf file> e.g. FlightHelmet.gltf"
<< std::endl;
return EXIT_FAILURE;
}
vtkNew<vtkNamedColors> colors;
vtkColor3d backgroundColor = colors->GetColor3d("SlateGray");
vtkNew<vtkGLTFImporter> importer;
importer->SetFileName(argv[1]);
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(backgroundColor.GetData());
renderer->UseHiddenLineRemovalOn();
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(640, 512);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("GLTFImporter");
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkNew<vtkInteractorStyleTrackballCamera> style;
renderWindowInteractor->SetInteractorStyle(style);
importer->SetRenderWindow(renderWindow);
importer->Update();
vtkNew<vtkLight> headLight;
headLight->SetLightTypeToHeadlight();
headLight->SwitchOn();
renderer->AddLight(headLight);
renderWindow->Render();
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(20);
renderer->GetActiveCamera()->Elevation(30);
renderer->ResetCameraClippingRange();
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(GLTFImporter)
find_package(VTK COMPONENTS
CommonColor
CommonCore
IOImport
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "GLTFImporter: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(GLTFImporter MACOSX_BUNDLE GLTFImporter.cxx )
target_link_libraries(GLTFImporter PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS GLTFImporter
MODULES ${VTK_LIBRARIES}
)
Download and Build GLTFImporter¶
Click here to download GLTFImporter and its CMakeLists.txt file. Once the tarball GLTFImporter.tar has been downloaded and extracted,
cd GLTFImporter/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./GLTFImporter
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.