/*
//*****************************************************************************
/*
*       @(#) AppearanceTest.java
*
*       Project:                Java 3D Programming
*       Client:         Manning Publications
*
*       Copyright (c) 2000 Daniel Selman 
*       All Rights Reserved.
*
*       @author Daniel Selman: dselman@tornadolabs.com
*/
//*****************************************************************************

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.*;
import javax.vecmath.*;

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;

class AppearanceTest extends Applet
{
        private static int                              m_kWidth = 400;
        private static int                              m_kHeight = 400;
	
        private Appearance                              m_Appearance = null;
                	
        public AppearanceTest()
        {
                setLayout(new BorderLayout());
        	
                GraphicsConfiguration config =
                SimpleUniverse.getPreferredConfiguration();

                Canvas3D c = new Canvas3D(config);
                add("Center", c);
        	
                m_Appearance = new Appearance();
        	
                // THIS WILL BE IGNORED...
                m_Appearance.setColoringAttributes( new ColoringAttributes( 1,0,0, ColoringAttributes.NICEST ) );
        	
                // Create a simple scene and attach it to the virtual universe
                BranchGroup scene = createSceneBranchGroup();
        	
                Background back = new Background( new Color3f( 0.8f, 0.8f, 0.8f ) );
                back.setApplicationBounds( new BoundingSphere( new Point3d(0,0,0), 100 ) );
                scene.addChild( back );
                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);
        }
                                        	
        protected BranchGroup createSceneBranchGroup()
        {
                BranchGroup objRoot = new BranchGroup();
                Bounds bounds = new BoundingSphere( new Point3d(0,0,0), 100 );
        	
                TransformGroup zoomTg = new TransformGroup();
                zoomTg.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
                zoomTg.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );

                // attach a navigation behavior to the position of the viewer
                KeyNavigatorBehavior key = new KeyNavigatorBehavior( zoomTg );
                key.setSchedulingBounds( bounds );
                key.setEnable( true );
                objRoot.addChild( key );
        	
                Transform3D t3d = new Transform3D();
                t3d.rotY( 2 );
                TransformGroup objTrans1 = new TransformGroup( t3d );
                        	
                // 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);
                        	
                float nScale = 0.5f;
        	
                Box box = new Box(nScale,nScale,nScale, Primitive.GENERATE_NORMALS | Primitive.GENERATE_TEXTURE_COORDS, m_Appearance );

                // BEGINNING OF COMMENT...
        	
                Shape3D frontFace = box.getShape( Box.LEFT );
        	
                // create a new left face so we can 
                // assign per-vertex colors
        	
                GeometryArray geometry = new QuadArray( 4,    GeometryArray.COORDINATES | 
                                                                                                                                        GeometryArray.NORMALS | 
                                                                                                                                        GeometryArray.COLOR_4 | 
                                                                                                                                        GeometryArray.TEXTURE_COORDINATE_2 );
        	
                nScale = 0.4f;
        	
                final float[] verts = 
                {
                        // left face
                        -1.0f * nScale, -1.0f * nScale,  1.0f * nScale,
                        -1.0f * nScale,  1.0f * nScale,  1.0f * nScale,
                        -1.0f * nScale,  1.0f * nScale, -1.0f * nScale,
                        -1.0f * nScale, -1.0f * nScale, -1.0f * nScale
                };
        	
                final float[] colors = 
                {
                        // left face
                        1,0,0,0,
                        0,1,0,0.2f,
                        0,0,1,0.8f,
                        0,0,0,1,
                };
        	
          float[] tcoords = 
                {
                        // left
                        1, 0,
                        1, 1,
                        0, 1,
                        0, 0
                };
    
                Vector3f normalVector = new Vector3f(-1.0f,  0.0f,  0.0f);

                geometry.setColors( 0, colors, 0, 4 );
        	
                for( int n = 0; n < 4; n++ )
                        geometry.setNormal( n, normalVector );

                geometry.setTextureCoordinates( 0, 0, tcoords );        	
                geometry.setCoordinates( 0, verts );
        	
                frontFace.setGeometry( geometry );
        	
                // END OF COMMENT
        	
                // connect the scenegraph
                objTrans1.addChild( box );
                zoomTg.addChild( objTrans1 );
                objRoot.addChild( zoomTg );
                        	
                return objRoot;
        }	

        public static void main(String[] args)
        {
                AppearanceTest appearanceTest = new AppearanceTest(); 	
                new MainFrame( appearanceTest, 256, 256);
        }
}