import javax.media.j3d.*; import javax.vecmath.*; import java.awt.image.*; import com.sun.j3d.utils.image.TextureLoader; import java.applet.Applet; // Applet // Origin at center of solid. Sides of length 2. public class TexTetra extends Shape3D { private static final float ycenter = (float)Math.sqrt(1.0/6.0); private static final float zcenter = (float)Math.sqrt(1.0/3.0); private static final float sqrt3 = (float)Math.sqrt(3.0); private static final float sqrt24_3 = (float)Math.sqrt ( 8.0/3.0); private static final Point3f p1 = new Point3f ( -1.0f, -ycenter, zcenter ); private static final Point3f p2 = new Point3f ( 1.0f, -ycenter, zcenter ); private static final Point3f p3 = new Point3f ( 0.0f, -ycenter, -sqrt3 + zcenter ); private static final Point3f p4 = new Point3f ( 0.0f, sqrt24_3 -ycenter, 0.0f ); private static final Point3f[] verts = { p1, p2, p4, // front face p1, p4, p3, // left back face p2, p3, p4, // right back face p1, p3, p2, // bottom face }; //private static final TexCoord2f t1 = new TexCoord2f ( 0.0f, 0.0f ); //private static final TexCoord2f t2 = new TexCoord2f ( 1.0f, 0.0f ); //private static final TexCoord2f t3 = new TexCoord2f ( 0.0f, 0.5f ); private static final Point2f t1 = new Point2f ( 0.0f, 0.0f ); private static final Point2f t2 = new Point2f ( 1.0f, 0.0f ); private static final Point2f t3 = new Point2f ( 0.0f, 1.0f ); //private static final TexCoord2f[] texs = { private static final Point2f[] texs = { t1, t2, t3, t1, t2, t3, t1, t2, t3, t1, t2, t3, }; /* assumes it is a triangle */ private Vector3f normalizeTriangle ( Point3f p1, Point3f p2, Point3f p3 ) { //Point3f p1 = triangle[0]; //Point3f p2 = triangle[1]; //Point3f p3 = triangle[2]; Vector3f v1 = new Vector3f ( p2.x - p1.x, p2.y - p1.y, p2.z - p1.z ); Vector3f v2 = new Vector3f ( p3.x - p1.x, p3.y - p1.y, p3.z - p1.z ); Vector3f v3 = new Vector3f ( v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x ); float norm = (float) Math.sqrt ( v3.x*v3.x + v3.y*v3.y + v3.z*v3.z ); v3.x /= norm; v3.y /= norm; v3.z /= norm; return v3; } /* from Lit.java private void createMaterials ( Appearance[] mats ) { Color3f black = new Color3f ( 0.0f, 0.0f, 0.0f ); Color3f deepRed = new Color3f ( 0.9f, 0.2f, 0.1f ); Color3f royalBlue = new Color3f ( 0.1f, 0.3f, 0.9f ); Color3f white = new Color3f ( 1.0f, 1.0f, 1.0f ); for ( int i = 0; i < 4; i++ ) mats[i] = new Appearance(); mats[0].setMaterial ( new Material ( deepRed, black, deepRed, black, 1.0f ) ); mats[1].setMaterial ( new Material ( royalBlue, black, royalBlue, black, 1.0f ) ); mats[2].setMaterial ( new Material ( deepRed, black, deepRed, white, 1.0f ) ); mats[3].setMaterial ( new Material ( royalBlue, black, royalBlue, white, 1.0f ) ); } */ /* From ColorTetra -- private static final Color3f c1 = new Color3f ( 1.0f, 0.0f, 0.0f ); private static final Color3f c2 = new Color3f ( 0.0f, 1.0f, 0.0f ); private static final Color3f c3 = new Color3f ( 1.0f, 1.0f, 1.0f ); private static final Color3f c4 = new Color3f ( 0.0f, 0.0f, 1.0f ); private static final Color3f[] colors = { c1, c2, c4, c1, c4, c3, c2, c3, c4, c1, c3, c2, }; */ private Texture createTexture() { //int height = 64; //int width = 64; int height = 8; int width = 8; int type = BufferedImage.TYPE_INT_RGB; BufferedImage bufferedImage = new BufferedImage( width, height, type ); WritableRaster writableRaster = bufferedImage.getRaster(); //System.out.println ( "raster is " + writableRaster ); int[] red = {0xff, 0, 0 }; int[] green = {0, 0xff, 0 }; int[] blue = {0,0, 0xff}; int[] color = blue; /* float[] red = {1,0,0}; float[] green = {0,1,0 }; float[] blue = {0,0,1}; float[] color = blue; */ for ( int i = 0; i < width; i++ ) for ( int j = 0; j < width; j++ ) { if ( color == blue ) color = red; else if ( color == red ) color = green; else if ( color == green ) color = blue; writableRaster.setPixel ( i, j, color ); } //System.out.println ( "raster is " + writableRaster ); int imageFormat = ImageComponent.FORMAT_RGB8; ImageComponent2D image = new ImageComponent2D ( imageFormat, bufferedImage ); int texFormat = Texture.RGB; int mode = Texture.BASE_LEVEL; Texture2D tex = new Texture2D ( mode, texFormat, width, height ); int level = 0; tex.setImage ( level, image ); return tex; } public TexTetra( Applet a ) { TriangleArray tetra = new TriangleArray ( 12, TriangleArray.COORDINATES | TriangleArray.TEXTURE_COORDINATE_2 ); // TriangleArray.COORDINATES | TriangleArray.NORMALS tetra.setCoordinates (0, verts ); //tetra.setColors (0, colors ); Vector3f[] norms = new Vector3f[12]; norms[0] = norms[1] = norms[2] = normalizeTriangle ( p1, p2, p4 ); norms[3] = norms[4] = norms[5] = normalizeTriangle ( p1, p4, p3 ); norms[6] = norms[7] = norms[8] = normalizeTriangle ( p2, p3, p4 ); norms[9] = norms[10] = norms[11] = normalizeTriangle ( p1, p3, p2 ); //tetra.setNormals ( 0, norms ); tetra.setTextureCoordinates ( 0, texs ); Texture tex = createTexture(); //Texture tex = new TextureLoader ( "earth.jpg", a ).getTexture(); Appearance mat = new Appearance(); mat.setTexture(tex); this.setGeometry ( tetra ); this.setAppearance ( mat ); //this.setAppearance ( mats[3] ); } }