/*
* Copyright 2010 Ian Bollinger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.worcester.subter;
import java.util.List;
import javax.media.j3d.Geometry;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;
import javax.vecmath.Vector3f;
import edu.worcester.subter.j3d.Appearances;
public class TexturedPlane extends Shape3D {
private static Geometry createGeometry(List<Point3f> coordinates,
Vector3f normal) {
final int numberOfPoints = coordinates.size();
final QuadArray plane = new QuadArray(numberOfPoints,
GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2
| GeometryArray.NORMALS);
plane.setCoordinates(0, coordinates.toArray(new Point3f[] {}));
final TexCoord2f[] corners = { new TexCoord2f(0f, 0f),
new TexCoord2f(1f, 0f), new TexCoord2f(1f, 1f),
new TexCoord2f(0f, 1f) };
final TexCoord2f[] textureCoordinates = new TexCoord2f[numberOfPoints];
for (int i = 0; i < numberOfPoints; ++i) {
textureCoordinates[i] = new TexCoord2f(corners[i % 4]);
}
plane.setTextureCoordinates(0, 0, textureCoordinates);
for (int i = 0; i < numberOfPoints; ++i) {
plane.setNormal(i, normal);
}
return plane;
}
public TexturedPlane(List<Point3f> coordinates, String fileName,
Vector3f normal) {
super();
setGeometry(createGeometry(coordinates, normal));
setAppearance(Appearances.fromFile(fileName));
}
}