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 jmetest.input.action;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
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;
57 * <code>TestBackwardAction</code>
61 public class TestFirstPersonController extends BaseGame {
62 private static final Logger logger = Logger
63 .getLogger(TestFirstPersonController.class.getName());
71 private InputHandler input;
72 /** High resolution timer for jME. */
73 protected Timer timer;
75 protected void update(float interpolation) {
76 /** Recalculate the framerate. */
78 /** Update tpf to time per frame according to the Timer. */
79 float tpf = timer.getTimePerFrame();
81 if (KeyBindingManager.getKeyBindingManager().isValidCommand("exit", false)) {
85 /** Check for key/mouse updates. */
91 * @see com.jme.app.BaseGame#render(float)
93 protected void render(float interpolation) {
94 display.getRenderer().clearBuffers();
95 display.getRenderer().draw(scene);
99 * set up the display system and camera.
100 * @see com.jme.app.BaseGame#initSystem()
102 protected void initSystem() {
104 display = DisplaySystem.getDisplaySystem(settings.getRenderer());
105 display.createWindow(
107 settings.getHeight(),
109 settings.getFrequency(),
110 settings.isFullscreen());
112 display.getRenderer().createCamera(
114 settings.getHeight());
116 } catch (JmeException e) {
117 logger.log(Level.SEVERE, "Could not create displaySystem", e);
120 ColorRGBA blackColor = new ColorRGBA();
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);
132 display.getRenderer().setCamera(cam);
134 input = new FirstPersonHandler(cam, 50, 1);
136 /** Get a high resolution timer for FPS updates. */
137 timer = Timer.getTimer();
139 KeyBindingManager.getKeyBindingManager().set(
141 KeyInput.KEY_ESCAPE);
147 * @see com.jme.app.BaseGame#initGame()
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();
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();
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;
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();
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);
191 Vector3f[] verts = new Vector3f[3];
192 ColorRGBA[] color3 = new ColorRGBA[3];
194 verts[0] = new Vector3f();
198 verts[1] = new Vector3f();
202 verts[2] = new Vector3f();
207 color3[0] = new ColorRGBA();
212 color3[1] = new ColorRGBA();
217 color3[2] = new ColorRGBA();
222 int[] indices = { 0, 1, 2 };
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();
229 pointNode.attachChild(t);
230 pointNode.setLocalTranslation(new Vector3f(0, -50, 0));
234 Vector3f[] verts2 = new Vector3f[3];
235 ColorRGBA[] color4 = new ColorRGBA[3];
237 verts2[0] = new Vector3f();
241 verts2[1] = new Vector3f();
245 verts2[2] = new Vector3f();
250 color4[0] = new ColorRGBA();
255 color4[1] = new ColorRGBA();
260 color4[2] = new ColorRGBA();
265 int[] indices2 = { 0, 1, 2 };
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();
272 scene = new Node("Scene graph root");
273 scene.attachChild(l);
274 scene.attachChild(pointNode);
275 scene.attachChild(t2);
278 scene.updateGeometricState(0.0f, true);
284 * @see com.jme.app.BaseGame#reinit()
286 protected void reinit() {
291 * @see com.jme.app.BaseGame#cleanup()
293 protected void cleanup() {
296 public static void main(String[] args) {
297 TestFirstPersonController app = new TestFirstPersonController();
298 app.setConfigShowMode(ConfigShowMode.AlwaysShow);