ReadOBJ
Repository source: ReadOBJ
Description¶
This example demonstrates how to read a Wavefront OBJ file. The result is displayed.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ReadOBJ.java
import vtk.vtkActor;
import vtk.vtkNamedColors;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkOBJReader;
public class ReadOBJ
{
// -----------------------------------------------------------------
// Load VTK library and print which library was not properly loaded
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
{
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
// -----------------------------------------------------------------
public static void main(String args[])
{
//parse command line arguments
if (args.length != 1)
{
System.err.println("Usage: java -classpath ... Filename(.obj) e.g trumpet.obj");
return;
}
String inputFilename = args[0];
vtkNamedColors colors = new vtkNamedColors();
//For Actor Color
double actorColor[] = new double[4];
//Renderer Background Color
double Bgcolor[] = new double[4];
colors.GetColor("HoneyDew", actorColor);
colors.GetColor("SpringGreen", Bgcolor);
vtkOBJReader reader = new vtkOBJReader();
reader.SetFileName(inputFilename);
reader.Update();
vtkPolyDataMapper mapper = new vtkPolyDataMapper();
mapper.SetInputConnection(reader.GetOutputPort());
vtkActor actor = new vtkActor();
actor.SetMapper(mapper);
actor.GetProperty().SetDiffuseColor(actorColor);
// Create the renderer, render window and interactor.
vtkRenderer ren = new vtkRenderer();
vtkRenderWindow renWin = new vtkRenderWindow();
renWin.AddRenderer(ren);
vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
// Visualize
ren.AddActor(actor);
ren.SetBackground(Bgcolor);
ren.ResetCamera();
ren.GetActiveCamera().Azimuth(30);
ren.GetActiveCamera().Elevation(30);
ren.GetActiveCamera().Dolly(1.5);
ren.ResetCameraClippingRange();
renWin.SetSize(640, 480);
renWin.Render();
iren.Initialize();
iren.Start();
}
}