/*
 *	@(#)InterleavedTest.java 1.4 00/02/10 13:14:01
 *
 * Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import com.sun.j3d.utils.behaviors.mouse.*;

public class InterleavedTest extends JApplet implements ActionListener {

    RenderingAttributes ra;
    ColoringAttributes ca;
    Material mat;			   
    Appearance app;			   
    JComboBox geomType;
    JCheckBox transparency;
    Shape3D shape;			   
    TransparencyAttributes transp;

    GeometryArray tetraRegular, tetraStrip, tetraIndexed, tetraIndexedStrip;
    GeometryArray[] geoArrays = new GeometryArray[4];

   // Globally used colors
   Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
   Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
   Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
   Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
   Color3f[] colors = {white, red, green, blue};			   

    private static final float sqrt3 = (float) Math.sqrt(3.0);
    private static final float sqrt3_3 = sqrt3 / 3.0f;
    private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;

    private static final float ycenter = 0.5f * sqrt24_3;
    private static final float zcenter = -sqrt3_3;

    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 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(0.0f, 1.0f, 1.0f);
    private static final Color3f c4 = new Color3f(1.0f, 1.0f, 0.0f);
    

    private static final float[] interleaved = {
	c1.x, c1.y, c1.z, // front face
	p1.x, p1.y, p1.z, // front face
	c2.x, c2.y, c2.z,
	p2.x, p2.y, p2.z,
	c4.x, c4.y, c4.z,
	p4.x, p4.y, p4.z,
	
	c1.x, c1.y, c1.z,// left, back face
	p1.x, p1.y, p1.z,// left, back face
	c4.x, c4.y, c4.z,
	p4.x, p4.y, p4.z,
	c3.x, c3.y, c3.z,
	p3.x, p3.y, p3.z,
	
	c2.x, c2.y, c2.z,// right, back face
	p2.x, p2.y, p2.z,// right, back face
	c3.x, c3.y, c3.z,
	p3.x, p3.y, p3.z,
	c4.x, c4.y, c4.z,
	p4.x, p4.y, p4.z,
	
	c1.x, c1.y, c1.z,// bottom face
	p1.x, p1.y, p1.z,// bottom face
	c3.x, c3.y, c3.z,
	p3.x, p3.y, p3.z,
	c2.x, c2.y, c2.z,
	p2.x, p2.y, p2.z,
    };
 
    private static final float[] indexedInterleaved = {
	c1.x,c1.y,c1.z,
	p1.x,p1.y,p1.z,
	c2.x,c2.y,c2.z,
	p2.x,p2.y,p2.z,
	c3.x,c3.y,c3.z,
	p3.x,p3.y,p3.z,
	c4.x,c4.y,c4.z,
	p4.x,p4.y,p4.z,
    };
    

    private static final int[] indices = {0,1,3,0,3,2,1,2,3,0,2,1};
    private int[] stripVertexCounts = {3,3,3,3};


   BranchGroup createSceneGraph() {
	BranchGroup objRoot = new BranchGroup();

	// Set up attributes to render lines
        app = new Appearance();
	
	transp = new TransparencyAttributes();
	transp.setTransparency(0.5f);
	transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
	transp.setTransparencyMode(TransparencyAttributes.NONE);
	app.setTransparencyAttributes(transp);

	tetraRegular = createGeometry(1);
	tetraStrip =createGeometry(2);
	tetraIndexed = createGeometry(3);
	tetraIndexedStrip = createGeometry(4);

	geoArrays[0] = tetraRegular;
	geoArrays[1] = tetraStrip;
	geoArrays[2] = tetraIndexed;
	geoArrays[3] = tetraIndexedStrip;
	
	shape = new Shape3D(tetraRegular, app);
	shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);

	Transform3D t = new Transform3D();
	// move the object upwards
	t.set(new Vector3f(0.0f, 0.3f, 0.0f));

	// rotate the shape
	Transform3D temp = new Transform3D();
        temp.rotX(Math.PI/4.0d);
	t.mul(temp);
        temp.rotY(Math.PI/4.0d);
        t.mul(temp);
	
	// Shrink the object 
	t.setScale(0.6);

	TransformGroup trans = new TransformGroup(t);
	trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

	objRoot.addChild(trans);
	trans.addChild(shape);

	BoundingSphere bounds =
	    new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

	// Create the drag behavior node
	MouseRotate behavior = new MouseRotate();
	behavior.setTransformGroup(trans);
	trans.addChild(behavior);
	behavior.setSchedulingBounds(bounds);
	
	// Create the zoom behavior node
	MouseZoom behavior2 = new MouseZoom();
	behavior2.setTransformGroup(trans);
	trans.addChild(behavior2);
	behavior2.setSchedulingBounds(bounds);
	
	// Create the zoom behavior node
	MouseTranslate behavior3 = new MouseTranslate();
	behavior3.setTransformGroup(trans);
	trans.addChild(behavior3);
	behavior3.setSchedulingBounds(bounds);



	// Set up the global lights
	Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
	Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
	Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);

	AmbientLight aLgt = new AmbientLight(alColor);
	aLgt.setInfluencingBounds(bounds);
	DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
	lgt1.setInfluencingBounds(bounds);
	objRoot.addChild(aLgt);
	objRoot.addChild(lgt1);
	
	// Let Java 3D perform optimizations on this scene graph.
        objRoot.compile();

	return objRoot;
    }

    JPanel createGeometryByReferencePanel() {
	JPanel panel = new JPanel();
	panel.setBorder(new TitledBorder("Geometry Type"));

	String values[] = {"Array", "Strip", "Indexed", "IndexedStrip"};
	geomType = new JComboBox(values);
	geomType.setLightWeightPopupEnabled(false);
	geomType.addActionListener(this);
	geomType.setSelectedIndex(0);
	panel.add(new JLabel("Geometry Type")); 	
	panel.add(geomType);

	transparency = new JCheckBox("EnableTransparency", 
				      false);
	transparency.addActionListener(this);
	panel.add(transparency);

	return panel;
    }


    public InterleavedTest() {
	Container contentPane = getContentPane();
	
        Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        contentPane.add("Center", c);

        BranchGroup scene = createSceneGraph();
        // SimpleUniverse is a Convenience Utility class
        SimpleUniverse u = new SimpleUniverse(c);

        // This will move the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);


	// Create GUI
	JPanel p = new JPanel();
	BoxLayout boxlayout = new BoxLayout(p, 
					    BoxLayout.Y_AXIS);
	p.add(createGeometryByReferencePanel());
	p.setLayout(boxlayout);

	contentPane.add("South", p);
	


    }
    public void actionPerformed(ActionEvent e) {
	Object target = e.getSource();
	if (target == geomType) {
	    shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);

	} 
	else if (target == transparency) {
	    if (transparency.isSelected()) {
		transp.setTransparencyMode(TransparencyAttributes.BLENDED);
	    }
	    else {
		transp.setTransparencyMode(TransparencyAttributes.NONE);
	    }


	} 


    }


			   
    public static void main(String[] args) {
	Frame frame = new MainFrame(new InterleavedTest(), 800, 800);
    }
			   
    public GeometryArray createGeometry (int type) {
	GeometryArray tetra = null;
	if (type == 1) {
	    tetra =new TriangleArray(12, 
				     TriangleArray.COORDINATES|
				     TriangleArray.COLOR_3|
				     /*
				       TriangleArray.NORMAL_3|
				       TriangleArray.TEXTURE_2 |
				       */
				     TriangleArray.INTERLEAVED|
				     TriangleArray.BY_REFERENCE);
	    
	    tetra.setInterleavedVertices(interleaved);

	}
	else if (type == 2) {
	    tetra = new TriangleStripArray(12,
					   TriangleStripArray.COORDINATES|
					   TriangleStripArray.COLOR_3|
					   /*
					     TriangleArray.NORMAL_3|
					     TriangleArray.TEXTURE_2 |
					     */
					   TriangleStripArray.INTERLEAVED|
					   TriangleStripArray.BY_REFERENCE,
					   stripVertexCounts);
	    tetra.setInterleavedVertices(interleaved);

	    
	}
	else if (type == 3) { // Indexed Geometry
	    tetra = new IndexedTriangleArray(4,
					     IndexedTriangleArray.COORDINATES|
					     IndexedTriangleArray.COLOR_3|
					     /*
					       IndexedTriangleArray.NORMAL_3|
					       IndexedTriangleArray.TEXTURE_2 |
					       */
					     IndexedTriangleArray.INTERLEAVED|
					     IndexedTriangleArray.BY_REFERENCE,
					     12);
	    tetra.setInterleavedVertices(indexedInterleaved);
	    ((IndexedTriangleArray)tetra).setCoordinateIndices(0, indices);
	    ((IndexedTriangleArray)tetra).setColorIndices(0, indices);
	}
	else if (type == 4) { // Indexed strip geometry
	    tetra = new IndexedTriangleStripArray(4,
						  IndexedTriangleStripArray.COORDINATES|
						  IndexedTriangleStripArray.COLOR_3|
						  /*
						    IndexedTriangleArray.NORMAL_3|
						    IndexedTriangleArray.TEXTURE_2 |
						    */
						  IndexedTriangleStripArray.INTERLEAVED|
						  IndexedTriangleStripArray.BY_REFERENCE,
						  12,
						  stripVertexCounts);
	    tetra.setInterleavedVertices(indexedInterleaved);
	    ((IndexedTriangleStripArray)tetra).setCoordinateIndices(0, indices);
	    ((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
	}
	else if (type == 5) { // Interleaved array
	}
	return tetra;
    }
}
