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.TutorialGuide;
36 import java.nio.FloatBuffer;
38 import com.jme.app.SimpleGame;
39 import com.jme.image.Texture;
40 import com.jme.input.KeyBindingManager;
41 import com.jme.input.KeyInput;
42 import com.jme.math.Vector2f;
43 import com.jme.math.Vector3f;
44 import com.jme.scene.TexCoords;
45 import com.jme.scene.TriMesh;
46 import com.jme.scene.state.TextureState;
47 import com.jme.util.TextureManager;
48 import com.jme.util.geom.BufferUtils;
51 * Started Date: Jul 21, 2004<br><br>
53 * This program demonstrates using key inputs to change things.
55 * @author Jack Lindamood
57 public class HelloKeyInput extends SimpleGame {
58 // The TriMesh that I will change
60 // A scale of my current texture values
62 public static void main(String[] args) {
63 HelloKeyInput app = new HelloKeyInput();
64 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
68 protected void simpleInitGame() {
69 // Vertex positions for the mesh
77 // Texture Coordinates for each position
79 Vector2f[] texCoords={
81 new Vector2f(coordDelta,0),
82 new Vector2f(0,coordDelta),
83 new Vector2f(coordDelta,coordDelta)
86 // The indexes of Vertex/Normal/Color/TexCoord sets. Every 3 makes a triangle.
91 square=new TriMesh("My Mesh",BufferUtils.createFloatBuffer(vertexes),null, null, TexCoords.makeNew(texCoords), BufferUtils.createIntBuffer(indexes));
92 // Point to the monkey image
93 URL monkeyLoc=HelloKeyInput.class.getClassLoader().getResource("jmetest/data/images/Monkey.jpg");
94 // Get my TextureState
95 TextureState ts=display.getRenderer().createTextureState();
97 Texture t=TextureManager.loadTexture(monkeyLoc,Texture.MinificationFilter.BilinearNearestMipMap, Texture.MagnificationFilter.Bilinear);
98 // Set a wrap for my texture so it repeats
99 t.setWrap(Texture.WrapMode.Repeat);
100 // Set the texture to the TextureState
103 // Assign the TextureState to the square
104 square.setRenderState(ts);
105 // Scale my square 10x larger
106 square.setLocalScale(10);
107 // Attach my square to my rootNode
108 rootNode.attachChild(square);
110 // Assign the "+" key on the keypad to the command "coordsUp"
111 KeyBindingManager.getKeyBindingManager().set(
115 // Adds the "u" key to the command "coordsUp"
116 KeyBindingManager.getKeyBindingManager().add(
120 // Assign the "-" key on the keypad to the command "coordsDown"
121 KeyBindingManager.getKeyBindingManager().set(
123 KeyInput.KEY_SUBTRACT);
126 // Called every frame update
127 protected void simpleUpdate(){
129 boolean updateTex = false;
130 // If the coordsDown command was activated
131 if (KeyBindingManager.getKeyBindingManager().isValidCommand("coordsDown",true)){
132 // Scale my texture down
136 // if the coordsUp command was activated
137 if (KeyBindingManager.getKeyBindingManager().isValidCommand("coordsUp",true)){
138 // Scale my texture up
144 // Get my square's texture array
145 FloatBuffer texBuf = square.getTextureCoords(0).coords;
146 texBuf.rewind().position(2); // start after the 1st texcoord (2 floats wide)
147 // Change the values of the texture coords in the buffer
148 texBuf.put(coordDelta).put(0);
149 texBuf.put(0).put(coordDelta);
150 texBuf.put(coordDelta).put(coordDelta);