package java3d.textures;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import javax.media.j3d.*;
import javax.vecmath.*;


/**
 * A class that demonstrates how to construct
 * textured objects (specifically, a cube) in Java3D
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class TexturedCubeFactory
{
    // Vertices
    private static final float vertices[][]   = {{-0.5f,-0.5f,-0.5f},
                                                 { 0.5f,-0.5f,-0.5f},
                                                 { 0.5f, 0.5f,-0.5f}, 
                                                 {-0.5f, 0.5f,-0.5f}, 
                                                 {-0.5f,-0.5f, 0.5f}, 
                                                 { 0.5f,-0.5f, 0.5f}, 
                                                 { 0.5f, 0.5f, 0.5f}, 
                                                 {-0.5f, 0.5f, 0.5f}
                                                };

    // Faces
    private static final int faces[][]  = {{4,5,6,7},
                                           {1,2,6,5},
                                           {0,1,5,4},
                                           {0,3,2,1},
                                           {0,4,7,3},
                                           {2,3,7,6}
                                          };

    // Face Colors
    private static final float colors[][]  = {{1.0f,0.0f,0.0f},
                                              {1.0f,1.0f,0.0f}, 
                                              {0.0f,1.0f,0.0f}, 
                                              {0.0f,0.0f,1.0f}, 
                                              {1.0f,0.0f,1.0f}, 
                                              {0.0f,1.0f,1.0f}
                                             };
    //[data
    // Texture Coordinates
    private static final float texcoords[][]  = {{0.0f, 1.0f},
                                                 {0.0f, 0.0f},
                                                 {1.0f, 0.0f}, 
                                                 {1.0f, 1.0f}
                                                };

    // Textures
    private static final String textures[] = {
                                              "abstract.gif",
                                              "brick.gif",
                                              "brownpaper.gif",
                                              "flagstone.gif",
                                              "frostedglass.gif",
                                              "oak.gif"
                                             };
    //]data


    //[createCube
    /**
     * Create a cube with white faces
     *
     * @return  The BranchGroup containing the cube
     */
    public static BranchGroup createCube()
    {
       Appearance        appearance;       
       BranchGroup       branchGroup;
       QuadArray         face;
       Shape3D           node;
       
       
       // Create an empty BranchGroup
       branchGroup = new BranchGroup();       

       // Create each face
       for (int f=0; f<faces.length; f++)
       {
          
          // Create a QuadArray to hold each face
          face = new QuadArray(4, GeometryArray.COORDINATES | 
                                  GeometryArray.TEXTURE_COORDINATE_2);

          // Add the vertices to the QuadArray
          for (int v=0; v<faces[f].length; v++)
          {
             face.setCoordinates(v, vertices[faces[f][v]]);

             // Set the texture coordinates
             for (int i=0; i<4; i++)
             {
                face.setTextureCoordinate(i, texcoords[i]);             
             }             
          }
          
          // Create a Node to hold the face
          node = new Shape3D(face);

          // Add an Appearance
          appearance = createTexturedAppearance(textures[f]);         
          node.setAppearance(appearance);          
       
          // Add the Node to the BranchGroup
          branchGroup.addChild(node);          

       }
       
       return branchGroup;       
    }
    //]createCube



    //[createTexture
    /**
     * Create a textured Appearance
     *
     * @param  name  The name of the file containg the image
     * @return       The Appearance
     */
    private static Appearance createTexturedAppearance(String name)
    {
       Appearance          appearance;       
       ImageComponent2D    image;       
       Texture2D           texture;       
       TextureLoader       textureLoader;
       

       // Load the image
       textureLoader = new TextureLoader(name, null);
       image         = textureLoader.getImage();


       // Create the Texture
       texture       = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
                                     image.getWidth(), image.getHeight());
       texture.setImage(0, image);  // Level 0 indicates no mip-mapping


       
       // Create the Appearance
       appearance    = new Appearance();
       appearance.setTexture(texture);
       

       return appearance;       
    }
    //]createTexture

}
