2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 package com.jme.system.canvas;
35 import com.jme.math.Vector3f;
36 import com.jme.renderer.Camera;
37 import com.jme.renderer.ColorRGBA;
38 import com.jme.scene.Node;
39 import com.jme.scene.state.ZBufferState;
40 import com.jme.system.DisplaySystem;
41 import com.jme.util.NanoTimer;
42 import com.jme.util.Timer;
45 * <code>SimpleCanvasImpl</code>
47 * @author Joshua Slack
48 * @version $Id: SimpleCanvasImpl.java,v 1.8 2007/09/21 15:45:30 nca Exp $
51 public class SimpleCanvasImpl extends JMECanvasImplementor {
54 protected Node rootNode;
56 protected Timer timer;
63 * This class should be subclasses - not directly instantiated.
64 * @param width canvas width
65 * @param height canvas height
67 protected SimpleCanvasImpl(int width, int height) {
72 public void doSetup() {
74 DisplaySystem display = DisplaySystem.getDisplaySystem();
75 renderer = display.getRenderer();
78 * Create a camera specific to the DisplaySystem that works with the
81 cam = renderer.createCamera(width, height);
83 /** Set up how our camera sees. */
84 cam.setFrustumPerspective(45.0f, (float) width / (float) height, 1,
86 Vector3f loc = new Vector3f(0.0f, 0.0f, 25.0f);
87 Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
88 Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
89 Vector3f dir = new Vector3f(0.0f, 0f, -1.0f);
90 /** Move our camera to a correct place and orientation. */
91 cam.setFrame(loc, left, up, dir);
92 /** Signal that we've changed our camera's location/frustum. */
94 /** Assign the camera to this renderer. */
95 renderer.setCamera(cam);
97 /** Set a black background. */
98 renderer.setBackgroundColor(ColorRGBA.black.clone());
100 /** Get a high resolution timer for FPS updates. */
101 timer = new NanoTimer();
103 /** Create rootNode */
104 rootNode = new Node("rootNode");
107 * Create a ZBuffer to display pixels closest to the camera above
110 ZBufferState buf = renderer.createZBufferState();
111 buf.setEnabled(true);
112 buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
114 rootNode.setRenderState(buf);
119 * Update geometric and rendering information for both the rootNode and
122 rootNode.updateGeometricState(0.0f, true);
123 rootNode.updateRenderState();
128 public void doUpdate() {
130 /** Update tpf to time per frame according to the Timer. */
131 tpf = timer.getTimePerFrame();
135 getRootNode().updateGeometricState(tpf, true);
136 getRootNode().updateWorldBound();
139 public void doRender() {
140 renderer.clearBuffers();
141 renderer.draw(rootNode);
143 renderer.displayBackBuffer();
146 public void simpleSetup() {
149 public void simpleUpdate() {
152 public void simpleRender() {
155 public Camera getCamera() {
159 public Node getRootNode() {
163 public float getTimePerFrame() {