OSDN Git Service

7bdbf1d32a5e0c0b96b7e925aab280ff4ef1962b
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / input / action / TestFirstPersonController.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 jmetest.input.action;
34
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
38 import com.jme.app.BaseGame;
39 import com.jme.bounding.BoundingSphere;
40 import com.jme.input.FirstPersonHandler;
41 import com.jme.input.InputHandler;
42 import com.jme.input.KeyBindingManager;
43 import com.jme.input.KeyInput;
44 import com.jme.math.Vector3f;
45 import com.jme.renderer.Camera;
46 import com.jme.renderer.ColorRGBA;
47 import com.jme.scene.Line;
48 import com.jme.scene.Node;
49 import com.jme.scene.Point;
50 import com.jme.scene.TriMesh;
51 import com.jme.system.DisplaySystem;
52 import com.jme.system.JmeException;
53 import com.jme.util.Timer;
54 import com.jme.util.geom.BufferUtils;
55
56 /**
57  * <code>TestBackwardAction</code>
58  * @author Mark Powell
59  * @version
60  */
61 public class TestFirstPersonController extends BaseGame {
62     private static final Logger logger = Logger
63             .getLogger(TestFirstPersonController.class.getName());
64     
65     private Node scene;
66     private Camera cam;
67     private Line l;
68     private Point p;
69     private TriMesh t;
70     private TriMesh t2;
71     private InputHandler input;
72     /** High resolution timer for jME. */
73     protected Timer timer;
74
75     protected void update(float interpolation) {
76         /** Recalculate the framerate. */
77         timer.update();
78           /** Update tpf to time per frame according to the Timer. */
79         float tpf = timer.getTimePerFrame();
80         
81         if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit", false)) {
82             finish();
83         }
84
85         /** Check for key/mouse updates. */
86         input.update(tpf);
87     }
88
89     /**
90      * Render the scene
91      * @see com.jme.app.BaseGame#render(float)
92      */
93     protected void render(float interpolation) {
94         display.getRenderer().clearBuffers();
95         display.getRenderer().draw(scene);
96     }
97
98     /**
99      * set up the display system and camera.
100      * @see com.jme.app.BaseGame#initSystem()
101      */
102     protected void initSystem() {
103         try {
104             display = DisplaySystem.getDisplaySystem(settings.getRenderer());
105             display.createWindow(
106                 settings.getWidth(),
107                 settings.getHeight(),
108                 settings.getDepth(),
109                 settings.getFrequency(),
110                 settings.isFullscreen());
111             cam =
112                 display.getRenderer().createCamera(
113                     settings.getWidth(),
114                     settings.getHeight());
115
116         } catch (JmeException e) {
117             logger.log(Level.SEVERE, "Could not create displaySystem", e);
118             System.exit(1);
119         }
120         ColorRGBA blackColor = new ColorRGBA();
121         blackColor.r = 0;
122         blackColor.g = 0;
123         blackColor.b = 0;
124         display.getRenderer().setBackgroundColor(blackColor);
125         cam.setFrustum(1.0f, 1000.0f, -0.55f, 0.55f, 0.4125f, -0.4125f);
126         Vector3f loc = new Vector3f(4.0f, 0.0f, 0.0f);
127         Vector3f left = new Vector3f(0.0f, -1.0f, 0.0f);
128         Vector3f up = new Vector3f(0.0f, 0.0f, 1.0f);
129         Vector3f dir = new Vector3f(-1.0f, 0f, 0.0f);
130         cam.setFrame(loc, left, up, dir);
131
132         display.getRenderer().setCamera(cam);
133
134         input = new FirstPersonHandler(cam, 50, 1);
135
136         /** Get a high resolution timer for FPS updates. */
137       timer = Timer.getTimer();
138
139       KeyBindingManager.getKeyBindingManager().set(
140               "exit",
141               KeyInput.KEY_ESCAPE);
142
143     }
144
145     /**
146      * set up the scene
147      * @see com.jme.app.BaseGame#initGame()
148      */
149     protected void initGame() {
150         Vector3f[] vertex = new Vector3f[1000];
151         ColorRGBA[] color = new ColorRGBA[1000];
152         for (int i = 0; i < 1000; i++) {
153             vertex[i] = new Vector3f();
154             vertex[i].x = (float) Math.random() * 50;
155             vertex[i].y = (float) Math.random() * 50;
156             vertex[i].z = (float) Math.random() * 50;
157             color[i] = new ColorRGBA();
158             color[i].r = (float) Math.random();
159             color[i].g = (float) Math.random();
160             color[i].b = (float) Math.random();
161             color[i].a = 1.0f;
162         }
163
164         l = new Line("Line Group",vertex, null, color, null);
165         l.setLocalTranslation(new Vector3f(-200.0f, -25, -25));
166         l.setModelBound(new BoundingSphere());
167         l.updateModelBound();
168
169         Vector3f[] vertex2 = new Vector3f[1000];
170         ColorRGBA[] color2 = new ColorRGBA[1000];
171         for (int i = 0; i < 1000; i++) {
172             vertex2[i] = new Vector3f();
173             vertex2[i].x = (float) Math.random() * -100 - 50;
174             vertex2[i].y = (float) Math.random() * 50 - 25;
175             vertex2[i].z = (float) Math.random() * 50 - 25;
176
177             color2[i] = new ColorRGBA();
178             color2[i].r = (float) Math.random();
179             color2[i].g = (float) Math.random();
180             color2[i].b = (float) Math.random();
181             color2[i].a = 1.0f;
182         }
183
184         p = new Point("Point Group",vertex2, null, color2, null);
185         p.setLocalTranslation(new Vector3f(0.0f, 25, 0));
186         p.setModelBound(new BoundingSphere());
187         p.updateModelBound();
188         Node pointNode = new Node("Point Node");
189         pointNode.attachChild(p);
190
191         Vector3f[] verts = new Vector3f[3];
192         ColorRGBA[] color3 = new ColorRGBA[3];
193
194         verts[0] = new Vector3f();
195         verts[0].x = -50;
196         verts[0].y = 0;
197         verts[0].z = 0;
198         verts[1] = new Vector3f();
199         verts[1].x = -50;
200         verts[1].y = 25;
201         verts[1].z = 25;
202         verts[2] = new Vector3f();
203         verts[2].x = -50;
204         verts[2].y = 25;
205         verts[2].z = 0;
206
207         color3[0] = new ColorRGBA();
208         color3[0].r = 1;
209         color3[0].g = 0;
210         color3[0].b = 0;
211         color3[0].a = 1;
212         color3[1] = new ColorRGBA();
213         color3[1].r = 0;
214         color3[1].g = 1;
215         color3[1].b = 0;
216         color3[1].a = 1;
217         color3[2] = new ColorRGBA();
218         color3[2].r = 0;
219         color3[2].g = 0;
220         color3[2].b = 1;
221         color3[2].a = 1;
222         int[] indices = { 0, 1, 2 };
223
224         t = new TriMesh("Triangle 1", BufferUtils.createFloatBuffer(verts), null, BufferUtils.createFloatBuffer(color3), null, BufferUtils.createIntBuffer(indices));
225         t.setLocalTranslation(new Vector3f(-150, 0, 0));
226         t.setModelBound(new BoundingSphere());
227         t.updateModelBound();
228
229         pointNode.attachChild(t);
230         pointNode.setLocalTranslation(new Vector3f(0, -50, 0));
231
232         //should be culled:
233
234         Vector3f[] verts2 = new Vector3f[3];
235         ColorRGBA[] color4 = new ColorRGBA[3];
236
237         verts2[0] = new Vector3f();
238         verts2[0].x = -50;
239         verts2[0].y = 0;
240         verts2[0].z = 0;
241         verts2[1] = new Vector3f();
242         verts2[1].x = -50;
243         verts2[1].y = 25;
244         verts2[1].z = 25;
245         verts2[2] = new Vector3f();
246         verts2[2].x = -50;
247         verts2[2].y = 25;
248         verts2[2].z = 0;
249
250         color4[0] = new ColorRGBA();
251         color4[0].r = 1;
252         color4[0].g = 0;
253         color4[0].b = 0;
254         color4[0].a = 1;
255         color4[1] = new ColorRGBA();
256         color4[1].r = 0;
257         color4[1].g = 1;
258         color4[1].b = 0;
259         color4[1].a = 1;
260         color4[2] = new ColorRGBA();
261         color4[2].r = 0;
262         color4[2].g = 0;
263         color4[2].b = 1;
264         color4[2].a = 1;
265         int[] indices2 = { 0, 1, 2 };
266
267         t2 = new TriMesh("Triangle 2", BufferUtils.createFloatBuffer(verts2), null, BufferUtils.createFloatBuffer(color4), null, BufferUtils.createIntBuffer(indices2));
268         t2.setLocalTranslation(new Vector3f(150, 0, 0));
269         t2.setModelBound(new BoundingSphere());
270         t2.updateModelBound();
271
272         scene = new Node("Scene graph root");
273         scene.attachChild(l);
274         scene.attachChild(pointNode);
275         scene.attachChild(t2);
276         cam.update();
277
278         scene.updateGeometricState(0.0f, true);
279
280     }
281
282     /**
283      * not used.
284      * @see com.jme.app.BaseGame#reinit()
285      */
286     protected void reinit() {
287     }
288
289     /**
290      * not used.
291      * @see com.jme.app.BaseGame#cleanup()
292      */
293     protected void cleanup() {
294     }
295
296     public static void main(String[] args) {
297         TestFirstPersonController app = new TestFirstPersonController();
298         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
299         app.start();
300     }
301 }