Source code file content

Revision: 1

First check in
» Project Revision History

» Checkout URL

source / src / org / pushingpixels / marble / MarbleFireworks3D.java

Size: 8485 bytes, 1 line
package org.pushingpixels.marble;

import java.awt.*;
import java.util.*;

import javax.media.j3d.*;
import javax.media.j3d.Locale;
import javax.swing.JFrame;
import javax.vecmath.Vector3d;

import jp.nyatla.nyartoolkit.core.NyARCode;
import jp.nyatla.nyartoolkit.java3d.utils.*;

import org.pushingpixels.trident.Timeline;
import org.pushingpixels.trident.TimelineScenario;
import org.pushingpixels.trident.Timeline.TimelineState;
import org.pushingpixels.trident.callback.TimelineCallbackAdapter;
import org.pushingpixels.trident.callback.TimelineScenarioCallback;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class MarbleFireworks3D extends JFrame implements
		NyARSingleMarkerBehaviorListener {
	private static final float SCALE = 0.005f;

	private static final int HEIGHT = 480;

	private static final int WIDTH = 640;

	private static final long serialVersionUID = -8472866262481865377L;

	private final String CARCODE_FILE = "C:/jprojects/marble/Data/patt.hiro";

	private final String PARAM_FILE = "C:/jprojects/marble/Data/camera_para.dat";

	private NyARSingleMarkerBehaviorHolder nya_behavior;

	private J3dNyARParam ar_param;

	private Canvas3D canvas;

	private Locale locale;

	private VirtualUniverse universe;

	private TransformGroup mainSceneGroup;

	private Map<TimelineScenario, Set<Explosion3D>> explosions;

	public static void main(String[] args) {
		try {
			MarbleFireworks3D frame = new MarbleFireworks3D();
			frame.setVisible(true);
			Insets ins = frame.getInsets();
			frame.setSize(WIDTH + ins.left + ins.right, HEIGHT + ins.top
					+ ins.bottom);
			frame.setLocationRelativeTo(null);
			frame.startCapture();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onUpdate(boolean i_is_marker_exist, Transform3D i_transform3d) {
	}

	public void startCapture() throws Exception {
		nya_behavior.start();
	}

	public MarbleFireworks3D() throws Exception {
		super("Java3D Example NyARToolkit");

		NyARCode ar_code = new NyARCode(16, 16);
		ar_code.loadARPattFromFile(CARCODE_FILE);
		ar_param = new J3dNyARParam();
		ar_param.loadARParamFromFile(PARAM_FILE);
		ar_param.changeScreenSize(WIDTH, HEIGHT);

		universe = new VirtualUniverse();
		locale = new Locale(universe);
		canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
		View view = new View();
		ViewPlatform viewPlatform = new ViewPlatform();
		view.attachViewPlatform(viewPlatform);
		view.addCanvas3D(canvas);
		view.setPhysicalBody(new PhysicalBody());
		view.setPhysicalEnvironment(new PhysicalEnvironment());
		// view.setSceneAntialiasingEnable(true);

		Transform3D camera_3d = ar_param.getCameraTransform();
		view.setCompatibilityModeEnable(true);
		view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
		view.setLeftProjection(camera_3d);

		TransformGroup viewGroup = new TransformGroup();
		Transform3D viewTransform = new Transform3D();
		viewTransform.rotY(Math.PI);
		viewTransform.setTranslation(new Vector3d(0.0, 0.0, 0.0));
		viewGroup.setTransform(viewTransform);
		viewGroup.addChild(viewPlatform);
		BranchGroup viewRoot = new BranchGroup();
		viewRoot.addChild(viewGroup);
		locale.addBranchGraph(viewRoot);

		Background background = new Background();
		BoundingSphere bounds = new BoundingSphere();
		bounds.setRadius(10.0);
		background.setApplicationBounds(bounds);
		background.setImageScaleMode(Background.SCALE_FIT_ALL);
		background.setCapability(Background.ALLOW_IMAGE_WRITE);
		BranchGroup root = new BranchGroup();
		root.addChild(background);

		// TransformGroup transform = new TransformGroup();
		// transform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		// transform.addChild(createSceneGraph());
		// root.addChild(transform);

		TransformGroup boxGroup = new TransformGroup();
		boxGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		Transform3D boxTransform = new Transform3D();
		boxTransform.setTranslation(new Vector3d(-0.006f, -0.006f, -0.01f));
		boxGroup.setTransform(boxTransform);
		Appearance boxAppearance = new Appearance();
		boxAppearance.setColoringAttributes(new ColoringAttributes(0, 0, 0,
				ColoringAttributes.SHADE_FLAT));
		Box box = new Box(0.035f, 0.035f, 0.0001f, boxAppearance);
		boxGroup.addChild(box);
		// root.addChild(boxGroup);

		mainSceneGroup = new TransformGroup();
		mainSceneGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		mainSceneGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
		mainSceneGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
		// mainSceneGroup.addChild(boxGroup);
		root.addChild(mainSceneGroup);

		nya_behavior = new NyARSingleMarkerBehaviorHolder(ar_param, 30f,
				ar_code, 0.08);
		nya_behavior.setTransformGroup(mainSceneGroup);
		nya_behavior.setBackGround(background);

		root.addChild(nya_behavior.getBehavior());
		nya_behavior.setUpdateListener(this);

		locale.addBranchGraph(root);

		setLayout(new BorderLayout());
		add(canvas, BorderLayout.CENTER);

		this.explosions = new HashMap<TimelineScenario, Set<Explosion3D>>();

		// start adding random explosions
		new Thread() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(500 + (int) (Math.random() * 1000));
						float x = -1.0f + 2.0f * (float) Math.random();
						float y = -1.0f + 2.0f * (float) Math.random();
						float z = (float) Math.random();
						addFireworkNew(x * 5.0f, y * 5.0f, 5.0f + z * 12.0f);
					} catch (Exception exc) {
					}
				}
			}
		}.start();
	}

	private void addFireworkNew(float x, final float y, final float z) {
		final TimelineScenario scenario = new TimelineScenario.Parallel();
		Set<Explosion3D> scenarioExplosions = new HashSet<Explosion3D>();

		float R = 6;
		int NUMBER = 20;
		int r = (int) (255 * Math.random());
		int g = (int) (100 + 155 * Math.random());
		int b = (int) (50 + 205 * Math.random());
		Color color = new Color(r, g, b);
		for (double alpha = -Math.PI / 2; alpha <= Math.PI / 2; alpha += 2
				* Math.PI / NUMBER) {
			final float dy = (float) (R * Math.sin(alpha));
			final float yFinal = y + dy;
			float rSection = (float) Math.abs(R * Math.cos(alpha));
			int expCount = Math.max(0, (int) (NUMBER * rSection / R));
			for (int i = 0; i < expCount; i++) {
				float xFinal = (float) (x + rSection
						* Math.cos(i * 2.0 * Math.PI / expCount));
				final float dz = (float)(rSection
				* Math.sin(i * 2.0 * Math.PI / expCount));
				float zFinal = z + dz;

				final Explosion3D explosion = new Explosion3D(x * SCALE, y * SCALE,
						z * SCALE, color);
				scenarioExplosions.add(explosion);

				final Timeline expTimeline = new Timeline(explosion);
				expTimeline.addPropertyToInterpolate("alpha", 1.0f, 0.0f);
				expTimeline.addPropertyToInterpolate("x", x * SCALE, xFinal
						* SCALE);
				expTimeline.addPropertyToInterpolate("y", y * SCALE, yFinal
						* SCALE);
//				expTimeline.addPropertyToInterpolate("z", z * SCALE, zFinal
//						* SCALE);
				expTimeline.addCallback(new TimelineCallbackAdapter() {
					@Override
					public void onTimelinePulse(float durationFraction,
							float timelinePosition) {
						float t = expTimeline.getTimelinePosition();
						float zCurr = (z + dz * t - 10 * t * t) * SCALE;
						explosion.setZ(zCurr);
					}
				});
				expTimeline.setDuration(3000);

				expTimeline.addCallback(new TimelineCallbackAdapter() {
					@Override
					public void onTimelineStateChanged(TimelineState oldState,
							TimelineState newState, float durationFraction,
							float timelinePosition) {
						if (newState == TimelineState.PLAYING_FORWARD) {
							mainSceneGroup.addChild(explosion
									.getSphere3DBranchGroup());
						}
					}
				});

				scenario.addScenarioActor(expTimeline);
			}
		}

		synchronized (explosions) {
			explosions.put(scenario, scenarioExplosions);
		}
		scenario.addCallback(new TimelineScenarioCallback() {
			@Override
			public void onTimelineScenarioDone() {
				synchronized (explosions) {
					Set<Explosion3D> ended = explosions.remove(scenario);
					for (Explosion3D end : ended) {
						mainSceneGroup
								.removeChild(end.getSphere3DBranchGroup());
					}
				}
			}
		});
		scenario.play();
	}
}
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20100521.d19488a)
 
 
loading
Please Confirm