import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.picking.*;

public class Octahedron extends myShape 
{    
    private Point3f vertex0=new Point3f(0.0f,0.0f,-1.5f);
        private Point3f vertex1=new Point3f(0.0f,0.0f,1.5f);
    private Point3f vertex2=new Point3f(0.0f,-1.0f,0.0f);
    private Point3f vertex3=new Point3f(0.0f,1.0f,0.0f);
    private Point3f vertex4=new Point3f(1.0f,0.0f,0.0f);
    private Point3f vertex5=new Point3f(-1.0f,0.0f,0.0f);
    private int noFaces=8;
    private int noVertices=24;
    private Point3f[] vertices= 
    {
            vertex4, vertex2, vertex0, // front 4 faces
            vertex4, vertex0, vertex3,
            vertex4, vertex3, vertex1,  
            vertex4, vertex1, vertex2,  
            vertex5, vertex1, vertex3, // back 4 faces
            vertex5, vertex3, vertex0,
            vertex5, vertex0, vertex2,
            vertex5, vertex2, vertex1,
    };

    private Point2f texCoord[] = 
    {
        new Point2f(0.0f, 0.0f),
            new Point2f(1.0f, 0.0f),
        new Point2f(0.5f,((float)Math.sqrt(3.0))/2.0f),
    };

    public Octahedron() 
    {
            int i;

            TriangleArray tetra=new TriangleArray(noVertices,TriangleArray.COORDINATES|
                TriangleArray.NORMALS|TriangleArray.TEXTURE_COORDINATE_2|TriangleArray.COLOR_3);
                   
                // set the vertex points                
            tetra.setCoordinates(0,vertices);
            
            // set the texture coordintaes
        for (i = 0;i<noVertices;i++) 
        {
            tetra.setTextureCoordinate(i,texCoord[i%3]);
        }
        
        // set the colours
        Color3f colour[]=new Color3f[6];
        colour[0]=new Color3f(1.0f, 0.0f, 0.0f);
        colour[1]=new Color3f(0.0f, 1.0f, 0.0f);
        colour[2]=new Color3f(0.0f, 0.0f, 1.0f);
        colour[3]=new Color3f(1.0f, 1.0f, 0.0f);
        colour[4]=new Color3f(0.0f, 1.0f, 1.0f);
        colour[5]=new Color3f(1.0f, 0.0f, 1.0f);
         
        for(i=0;i<noVertices;i++)
        {
            tetra.setColor(i,colour[i%6]);
        }
         
        // set the normals
            int face;
            Vector3f normal=new Vector3f();
            Vector3f vector1=new Vector3f();
            Vector3f vector2=new Vector3f();
            Point3f [] points=new Point3f[3];
            for(i=0;i<3;i++)points[i]=new Point3f();

            for(face=0;face<noVertices/3;face++) 
            {
                tetra.getCoordinates(face*3,points);
                vector1.sub(points[1],points[0]);
                vector2.sub(points[2],points[0]);
                normal.cross(vector1,vector2);
                normal.normalize();
                for (i=0;i<3;i++) 
                {
                        tetra.setNormal((face*3+i),normal);
                }
            }
            
            // create the shape
            Appearance thisAppearance=new Appearance();
            this.setGeometry(tetra);
            this.setAppearance(thisAppearance);
    }
    
    public Point3f getTriangleCoordinates(int i)
    {
        return vertices[i];
    }   
    
    public String getName()
    {
        return "octahedron";
    }
    
    public int getNumberTriangles()
    {
        return vertices.length/3;
    }
}
