/** * Copyright (c) 2008-2014 Ardor Labs, Inc. * * This file is part of Ardor3D. * * Ardor3D is free software: you can redistribute it and/or modify it * under the terms of its license which may be found in the accompanying * LICENSE file or at . */ package com.ardor3d.example.canvas; import java.util.Random; import com.ardor3d.annotation.MainThread; import com.ardor3d.framework.Canvas; import com.ardor3d.framework.Updater; import com.ardor3d.image.Texture; import com.ardor3d.input.InputState; import com.ardor3d.input.Key; import com.ardor3d.input.control.FirstPersonControl; import com.ardor3d.input.logical.AnyKeyCondition; import com.ardor3d.input.logical.InputTrigger; import com.ardor3d.input.logical.KeyPressedCondition; import com.ardor3d.input.logical.KeyReleasedCondition; import com.ardor3d.input.logical.LogicalLayer; import com.ardor3d.input.logical.TriggerAction; import com.ardor3d.input.logical.TwoInputStates; import com.ardor3d.light.PointLight; import com.ardor3d.math.ColorRGBA; import com.ardor3d.math.Matrix3; import com.ardor3d.math.Vector3; import com.ardor3d.renderer.state.LightState; import com.ardor3d.renderer.state.RenderState; import com.ardor3d.renderer.state.TextureState; import com.ardor3d.renderer.state.ZBufferState; import com.ardor3d.scenegraph.Mesh; import com.ardor3d.scenegraph.shape.Box; import com.ardor3d.ui.text.BasicText; import com.ardor3d.util.GameTaskQueue; import com.ardor3d.util.GameTaskQueueManager; import com.ardor3d.util.ReadOnlyTimer; import com.ardor3d.util.TextureManager; import java.util.concurrent.Callable; public class RotatingCube implements Updater { // private final Canvas view; private final MyScene scene; private final Exit exit; private final LogicalLayer logicalLayer; private final Key toggleRotationKey; private final static float CUBE_ROTATE_SPEED = 1; private final Vector3 rotationAxis = new Vector3(1, 1, 0); private double angle = 0; private Mesh box; private final Matrix3 rotation = new Matrix3(); private static final int MOVE_SPEED = 4; private int rotationSign = 1; private GameTaskQueueManager manager; public RotatingCube(final MyScene scene, final Exit exit, final LogicalLayer logicalLayer, final Key toggleRotationKey) { this.scene = scene; this.exit = exit; this.logicalLayer = logicalLayer; this.toggleRotationKey = toggleRotationKey; } public RotatingCube(final MyScene scene, final Exit exit, final LogicalLayer logicalLayer, final Key toggleRotationKey, GameTaskQueueManager manager) { this.scene = scene; this.exit = exit; this.logicalLayer = logicalLayer; this.toggleRotationKey = toggleRotationKey; this.manager = manager; } @Override public void init() { } public void addCube() { // add a cube to the scene // add a rotating controller to the cube // add a light box = new Box("The cube", new Vector3(-.01, -.01, -.01), new Vector3(.01, .01, .01)); final ZBufferState buf = new ZBufferState(); buf.setEnabled(true); buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo); // Add a texture to the box. final TextureState ts = new TextureState(); ts.setTexture(TextureManager.load("images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear, true)); box.setRenderState(ts); final PointLight light = new PointLight(); final Random random = new Random(); final float r = 0.9f; final float g = 0.2f; final float b = 1f; final float a = random.nextFloat(); light.setDiffuse(new ColorRGBA(r, g, b, 1f)); light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f)); light.setLocation(new Vector3(MOVE_SPEED, MOVE_SPEED, MOVE_SPEED)); light.setEnabled(true); /** Attach the light to a lightState and the lightState to rootNode. */ final LightState lightState = new LightState(); lightState.setEnabled(true); lightState.attach(light); registerInputTriggers(); Callable updateCallable = new Callable() { public Object call() { scene.getRoot().setRenderState(buf); scene.getRoot().setRenderState(lightState); scene.getRoot().attachChild(box); return null; } }; //manager.getQueue(GameTaskQueue.UPDATE).enqueue(updateCallable); scene.getRoot().setRenderState(buf); scene.getRoot().setRenderState(lightState); scene.getRoot().attachChild(box); // Skip for now // final BasicText text = BasicText.createDefaultTextLabel("test", "Hello World!"); // scene.getRoot().attachChild(text); } public void addCube2() { // add a cube to the scene // add a rotating controller to the cube // add a light box = new Box("The cube", new Vector3(-1, -1, -1), new Vector3(1, 1, 1)); final ZBufferState buf = new ZBufferState(); buf.setEnabled(true); buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo); // Add a texture to the box. final TextureState ts = new TextureState(); ts.setTexture(TextureManager.load("images/ardor3d_white_256.jpg", Texture.MinificationFilter.Trilinear, true)); box.setRenderState(ts); final PointLight light = new PointLight(); final Random random = new Random(); final float r = 0.9f; final float g = 0.2f; final float b = 1f; final float a = random.nextFloat(); light.setDiffuse(new ColorRGBA(r, g, b, 1f)); light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f)); light.setLocation(new Vector3(MOVE_SPEED, MOVE_SPEED, MOVE_SPEED)); light.setEnabled(true); /** Attach the light to a lightState and the lightState to rootNode. */ final LightState lightState = new LightState(); lightState.setEnabled(true); lightState.attach(light); //registerInputTriggers(); Callable updateCallable = new Callable() { public Object call() { scene.getRoot().setRenderState(buf); scene.getRoot().setRenderState(lightState); scene.getRoot().attachChild(box); return null; } }; manager.getQueue(GameTaskQueue.UPDATE).enqueue(updateCallable); // scene.getRoot().setRenderState(buf); // scene.getRoot().setRenderState(lightState); // scene.getRoot().attachChild(box); } private void registerInputTriggers() { final FirstPersonControl control = FirstPersonControl.setupTriggers(logicalLayer, Vector3.UNIT_Y, true); control.setMoveSpeed(MOVE_SPEED); logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ESCAPE), new TriggerAction() { @Override public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { exit.exit(); } })); logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(toggleRotationKey), new TriggerAction() { @Override public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { toggleRotation(); } })); logicalLayer.registerTrigger(new InputTrigger(new KeyReleasedCondition(Key.U), new TriggerAction() { @Override public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { toggleRotation(); } })); logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ZERO), new TriggerAction() { @Override public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { resetCamera(source); } })); logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NINE), new TriggerAction() { @Override public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { lookAtZero(source); } })); logicalLayer.registerTrigger(new InputTrigger(new AnyKeyCondition(), new TriggerAction() { @Override public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { final InputState current = inputStates.getCurrent(); System.out.println("Key character pressed: " + current.getKeyboardState().getKeyEvent().getKeyChar()); } })); } private void lookAtZero(final Canvas source) { source.getCanvasRenderer().getCamera().lookAt(Vector3.ZERO, Vector3.UNIT_Y); } private void resetCamera(final Canvas source) { final Vector3 loc = new Vector3(0.0f, 0.0f, 10.0f); final Vector3 left = new Vector3(-1.0f, 0.0f, 0.0f); final Vector3 up = new Vector3(0.0f, 1.0f, 0.0f); final Vector3 dir = new Vector3(0.0f, 0f, -1.0f); source.getCanvasRenderer().getCamera().setFrame(loc, left, up, dir); } private void toggleRotation() { rotationSign *= -1; } @Override @MainThread public void update(final ReadOnlyTimer timer) { final double tpf = timer.getTimePerFrame(); logicalLayer.checkTriggers(tpf); // rotate away angle += tpf * CUBE_ROTATE_SPEED * rotationSign; rotation.fromAngleAxis(angle, rotationAxis); if (box != null) { box.setRotation(rotation); } scene.getRoot().updateGeometricState(tpf, true); } }