package framework.test;

import java.awt.Component;
import java.util.ArrayList;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Clip;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.IndexedTriangleArray;
import javax.media.j3d.Material;
import javax.media.j3d.Node;
import javax.media.j3d.Transform3D;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import fight3D.Stage;
import fight3D.StageManager;
import framework.RWT.RWTFrame3D;
import framework.RWT.RWTCanvas3D;
import framework.model3D.BaseObject3D;
import framework.model3D.Model3D;
import framework.model3D.ModelFactory;
import framework.model3D.Object3D;
import framework.model3D.Position3D;
import framework.model3D.Quaternion3D;
import framework.model3D.Universe;
import framework.physics.AngularVelocity3D;
import framework.physics.Force3D;
import framework.physics.Ground;
import framework.physics.PhysicalSystem;
import framework.physics.Solid3D;
import framework.view3D.Camera3D;

public class TestObject {
	private Object3D object;

	public TestObject() {
		// フレームの構成
		RWTFrame3D frame = new RWTFrame3D();
		RWTCanvas3D canvas = new RWTCanvas3D();
		frame.add(canvas);

		// 世界の作成
		Universe universe = new Universe();
				
		// ライトの設定
		DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f,
				1.0f, 1.0f), new Vector3f(0.0f, -1.0f, -0.1f));
		light.setInfluencingBounds(new BoundingSphere(new Point3d(), 50000.0));
		universe.placeLight(light);

		frame.setSize(720, 480);
		frame.setVisible(true);

		// ステージの設定
		IndexedTriangleArray groundGeometry = new IndexedTriangleArray(4, 
				IndexedTriangleArray.COORDINATES | IndexedTriangleArray.NORMALS, 6);
		groundGeometry.setCoordinate(0, new Point3d(-10.0, 0.0, -10.0));
		groundGeometry.setCoordinate(1, new Point3d(10.0, 0.0, -10.0));
		groundGeometry.setCoordinate(2, new Point3d(10.0, 0.0, 10.0));
		groundGeometry.setCoordinate(3, new Point3d(-10.0, 0.0, 10.0));
		groundGeometry.setNormal(0, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setNormal(1, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setNormal(2, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setNormal(3, new Vector3f(0.0f, 1.0f, 0.0f));
		groundGeometry.setCoordinateIndices(0, new int[]{0, 3, 2});
		groundGeometry.setCoordinateIndices(3, new int[]{0, 2, 1});
		Appearance ap2 = new Appearance();
		Material m = new Material();
		m.setDiffuseColor(1.0f, 1.0f, 1.0f);
		m.setAmbientColor(0.5f, 0.5f, 0.5f);
		m.setSpecularColor(0.0f, 0.0f, 0.0f);
		m.setShininess(1.0f);
		ap2.setMaterial(m);
		ap2.setColoringAttributes(new ColoringAttributes(0.5f, 0.5f, 0.5f, ColoringAttributes.NICEST));
		BaseObject3D stageObject = new BaseObject3D(groundGeometry, ap2);
		Ground stageGround = new Ground(stageObject);
		universe.place(stageGround);
		
		// オブジェクトの設定
		Model3D model = ModelFactory.loadModel("data\\cubeBlue.3ds");	// 3Dモデルファイルの読み込み
		object = model.createObject();		// オブジェクトを1つ生成
		universe.place(object);				// 世界に配置
		
		// カメラの設定
		Camera3D camera = new Camera3D(universe);
		canvas.attachCamera(camera);
		camera.addTarget(object);
		Position3D viewPoint = new Position3D(0.0, 10.0, -50.0);
		camera.setViewPoint(viewPoint);
		camera.adjust(0);

		// 表示
		universe.compile();
	}

	public void start() {
		// オブジェクトの運動
		for (;;) {
			try {
				Thread.sleep(1);
				Position3D p = new Position3D(0.0, 0.0, 0.0);
				object.apply(p, false);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	

	}

	public static void main(String[] args) {
		TestObject test = new TestObject();
		test.start();
	}
}
