}
// Create a triangle array using NIO.
// Unfortunately, Sun picking utilities don't handle NIO very well, thus
// we need to overload a method
TriangleArray ta = new TriangleArray(triangles.length, GeometryArray.BY_REFERENCE|GeometryArray.USE_NIO_BUFFER|GeometryArray.COORDINATES|GeometryArray.NORMALS|GeometryArray.COLOR_3) {
public double[] getCoordRefDouble() {
// When picking, tests have shown that NIO buffer copy has no noticeable impact
// on performance. So, it is actually better to copy the array each time the picking is
// done than to hog memory with a permanent copy.
// It is also still better to copy the buffer only when picking rather than at each rendering
double[] array = new double[getVertexCount()*3];
DoubleBuffer db = (DoubleBuffer)super.getCoordRefBuffer().getBuffer();
db.rewind();
db.get(array); // optimized get
return array;
}
};
ta.setCoordRefBuffer(new J3DBuffer(nioCoords));
// Use Java3D utilities to compute normals
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
double[] coords = new double[data[0].length*3];
nioCoords.position(0);
nioCoords.get(coords);
gi.setCoordinates(coords);
// generate normals
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
Vector3f[] n3f = gi.getNormals();
int[] ni = gi.getNormalIndices();
float[] tmp = new float[3];
for (int i=0; i<ni.length; ++i) {
n3f[ni[i]].get(tmp);
nioNormals.put(tmp);
}
ta.setNormalRefBuffer(new J3DBuffer(nioNormals));
// Setup color buffer
ta.setColorRefBuffer(new J3DBuffer(nioColors));
ta.setCapability(TriangleArray.ALLOW_COLOR_WRITE);
ta.setCapabilityIsFrequent(TriangleArray.ALLOW_COLOR_WRITE);
Shape3D shape3d = new Shape3D(ta);
// The appearance of a shape has many interesting features.
// In particular, in case one wants to display lines (edges) over the shape,