Use an indexed face set factory
From JReality Wiki
Source file: Cube04
JavaDoc: IndexedFaceSetFactory
To obtain the unit cube we now naturally use the IndexedFaceSetFactory to produce an instance of IndexedFaceSet. Besides specifying another four vertices to complete the cube, we now can define faces by giving an array of face indices, again referring to the indices (0-based) of vertices[]. Before setting this array be sure to call setFaceCount(int n)
to the number of faces.
Note that the edges of the cube are not automically generated by the factory.
- The factory will generate the edges automatically from the face definitions by calling setGenerateEdgesFromFaces(true).
- You can also set the edges using the method
setEdgeIndices(...)
inherited from theIndexedLineSetFactory
.
The same goes for the normal vectors, needed in order to do lighting calculations when rendering the faces.
- Face normals are calculated automatically by calling setGenerateFaceNormals(boolean b).
- Vertex normals are calculated automatically (by averaging the face normals) by calling setGenerateVertexNormals(boolean b).
- There are also methods for explicitly setting the normals (
setFaceNormals(...)
, etc).
Other methods are available for:
- automatically generating face labels (
setGenerateFaceLabels(true)
).
package de.jreality.tutorial.geom; import de.jreality.geometry.IndexedFaceSetFactory; import de.jreality.plugin.JRViewer; public class Cube04 { public static void main(String[] args) { IndexedFaceSetFactory ifsf = new IndexedFaceSetFactory(); double [][] vertices = new double[][] { {0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1} }; int [][] faceIndices = new int [][] { {0, 1, 2, 3}, {7, 6, 5, 4}, {0, 1, 5, 4}, {1, 2, 6, 5}, {2, 3, 7, 6}, {3, 0, 4, 7} }; ifsf.setVertexCount( vertices.length ); ifsf.setVertexCoordinates( vertices ); ifsf.setFaceCount( faceIndices.length); ifsf.setFaceIndices( faceIndices ); ifsf.setGenerateEdgesFromFaces( true ); ifsf.setGenerateFaceNormals( true ); ifsf.update(); VIewer v = JRViewer.display(ifsf.getIndexedFaceSet()); } }
|