OSDN Git Service

4d8e6ffa9c827272ebdf364400a2f5a17b2bc978
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / system / canvas / SimpleCanvasImpl.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 package com.jme.system.canvas;
34
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;
43
44 /**
45  * <code>SimpleCanvasImpl</code>
46  * 
47  * @author Joshua Slack
48  * @version $Id: SimpleCanvasImpl.java,v 1.8 2007/09/21 15:45:30 nca Exp $
49  */
50
51 public class SimpleCanvasImpl extends JMECanvasImplementor {
52
53     // Items for scene
54     protected Node rootNode;
55
56     protected Timer timer;
57
58     protected float tpf;
59
60     protected Camera cam;
61
62     /**
63      * This class should be subclasses - not directly instantiated.
64      * @param width canvas width
65      * @param height canvas height
66      */
67     protected SimpleCanvasImpl(int width, int height) {
68         this.width = width;
69         this.height = height;
70     }
71     
72     public void doSetup() {
73         
74         DisplaySystem display = DisplaySystem.getDisplaySystem();
75         renderer = display.getRenderer();
76
77         /**
78          * Create a camera specific to the DisplaySystem that works with the
79          * width and height
80          */
81         cam = renderer.createCamera(width, height);
82
83         /** Set up how our camera sees. */
84         cam.setFrustumPerspective(45.0f, (float) width / (float) height, 1,
85                 1000);
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. */
93         cam.update();
94         /** Assign the camera to this renderer. */
95         renderer.setCamera(cam);
96
97         /** Set a black background. */
98         renderer.setBackgroundColor(ColorRGBA.black.clone());
99
100         /** Get a high resolution timer for FPS updates. */
101         timer = new NanoTimer();
102
103         /** Create rootNode */
104         rootNode = new Node("rootNode");
105
106         /**
107          * Create a ZBuffer to display pixels closest to the camera above
108          * farther ones.
109          */
110         ZBufferState buf = renderer.createZBufferState();
111         buf.setEnabled(true);
112         buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
113
114         rootNode.setRenderState(buf);
115
116         simpleSetup();
117         
118         /**
119          * Update geometric and rendering information for both the rootNode and
120          * fpsNode.
121          */
122         rootNode.updateGeometricState(0.0f, true);
123         rootNode.updateRenderState();
124
125         setup = true;
126     }
127
128     public void doUpdate() {
129         timer.update();
130         /** Update tpf to time per frame according to the Timer. */
131         tpf = timer.getTimePerFrame();
132
133         simpleUpdate();
134         
135         getRootNode().updateGeometricState(tpf, true);
136         getRootNode().updateWorldBound();
137     }
138
139     public void doRender() {
140         renderer.clearBuffers();
141         renderer.draw(rootNode);
142         simpleRender();
143         renderer.displayBackBuffer();
144     }
145
146     public void simpleSetup() {        
147     }
148
149     public void simpleUpdate() {
150     }
151     
152     public void simpleRender() {
153     }
154
155     public Camera getCamera() {
156         return cam;
157     }
158
159     public Node getRootNode() {
160         return rootNode;
161     }
162
163     public float getTimePerFrame() {
164         return tpf;
165     }
166 }