JCarnac3D-OGL comes with a set of readers which provide import capabilities for the following formats :
STL (ascii and binary)
OFF
OBJ
X3D (not full support)
VRML (not full support)
The readers all share the same interface : cg3DFileReader
A utility class is provided to simple convert a file into a scenegraph node : cg3DFileIO
This class defines a static method to read a file. It will iterate over the register readers to find the correct one (depending on the file extension).
Sample code
//open a file chooser to select 3D files JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new FileNameExtensionFilter("3D files", cg3DFileIO.getReaderFileSuffixes())); chooser.setMultiSelectionEnabled(true); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { try { cgAABoundingBox3D bbox = null; //iterate over the files to open 3D objects. for (File file : chooser.getSelectedFiles()) { cgAABoundingBox3D fbbox = new cgAABoundingBox3D(); //read one file and convert it into a scenegraph node cgNode fNode = cg3DFileIO.readFile(file, fbbox); if (fNode != null) { //add the node to the scenegraph parentNdde.addNode(fNode); //update the global bounding box from the 3D object bounding box (returned by the reader) if (bbox == null) { bbox = fbbox; } else { bbox.setBoundingBox(
Math.min(bbox.getMinX(), fbbox.getMinX()), Math.max(bbox.getMaxX(), fbbox.getMaxX()), Math.min(bbox.getMinY(), fbbox.getMinY()), Math.max(bbox.getMaxY(), fbbox.getMaxY()), Math.min(bbox.getMinZ(), fbbox.getMinZ()), Math.max(bbox.getMaxZ(), fbbox.getMaxZ())); } } } } catch (cg3DFileReadingException e) { LOG.log(Level.SEVERE, e.getMessage()); } }
It is also possible to directly use a dedicated reader.
The following example shows how to use the STL file reader directly.
Sample code
JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new FileNameExtensionFilter("STL files", cgSTLFileReader.EXT)); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { cgSTLFileReader reader = new cgSTLFileReader(); try { cgAABoundingBox3D bbox = new cgAABoundingBox3D(); cgNode node = reader.readFile(chooser.getSelectedFile(), bbox);