Help! HelloKeyInput.java

I'm new to JME 2.01.

I just run HelloKeyInput.java,and find something wrong.



import java.net.URL;

import java.nio.FloatBuffer;



import com.jme.app.SimpleGame;

import com.jme.image.Texture;

import com.jme.input.KeyBindingManager;

import com.jme.input.KeyInput;

import com.jme.math.Vector2f;

import com.jme.math.Vector3f;

import com.jme.scene.TexCoords;

import com.jme.scene.TriMesh;

import com.jme.scene.state.TextureState;

import com.jme.util.TextureManager;

import com.jme.util.geom.BufferUtils;



/**

  • Started Date: Jul 21, 2004<br>
  • <br>

    *
  • This program demonstrates using key inputs to change things.

    *
  • @author Jack Lindamood

    */

    public class NewClass extends SimpleGame {

    // The TriMesh that I will change

    TriMesh square;

    // A scale of my current texture values

    float coordDelta;



    public static void main(String[] args) {

    NewClass app = new NewClass();

    app.setConfigShowMode(ConfigShowMode.AlwaysShow);

    app.start();

    }



    protected void simpleInitGame() {

    // Vertex positions for the mesh

    Vector3f[] vertexes = {

    new Vector3f(0, 0, 0),

    new Vector3f(1, 0, 0),

    new Vector3f(0, 1, 0),

    new Vector3f(1, 1, 0)

    };



    // Texture Coordinates for each position

    coordDelta = 1;

    Vector2f[] texCoords = {

      new Vector2f(0,0),

                new Vector2f(1,0),

                new Vector2f(0,1),

                new Vector2f(1,1)

    };



    // The indexes of Vertex/Normal/Color/TexCoord sets. Every 3 makes a

    // triangle.

    int[] indexes = { 0, 1, 2, 1, 2, 3 };



    // Create the square

    square = new TriMesh(“My Mesh”,

    BufferUtils.createFloatBuffer(vertexes),

    null,

    null,

    TexCoords.makeNew(texCoords),

    BufferUtils.createIntBuffer(indexes));



    // Point to the monkey image

    URL monkeyLoc = HelloKeyInput.class.getClassLoader().getResource(

    “jmetest/data/images/Monkey.jpg”);



    // Get my TextureState

    TextureState ts = display.getRenderer().createTextureState();



    // Get my Texture

    Texture t = TextureManager.loadTexture(monkeyLoc,

    Texture.MinificationFilter.BilinearNearestMipMap,

    Texture.MagnificationFilter.Bilinear);



    // Set a wrap for my texture so it repeats

    t.setWrap(Texture.WrapMode.Repeat);



    // Set the texture to the TextureState

    ts.setTexture(t);



    // Assign the TextureState to the square

    square.setRenderState(ts);



    // Scale my square 10x larger

    square.setLocalScale(10);



    // Attach my square to my rootNode

    rootNode.attachChild(square);



    // Assign the “+” key on the keypad to the command “coordsUp”

    KeyBindingManager.getKeyBindingManager().set(“coordsUp”,

    KeyInput.KEY_ADD);



    // Adds the “u” key to the command “coordsUp”

    KeyBindingManager.getKeyBindingManager().add(“coordsUp”,

    KeyInput.KEY_U);



    // Assign the “-” key on the keypad to the command “coordsDown”

    KeyBindingManager.getKeyBindingManager().set(“coordsDown”,

    KeyInput.KEY_SUBTRACT);

    }



    // Called every frame update

    protected void simpleUpdate() {

    boolean updateTex = false;



    // If the coordsDown command was activated

    if (KeyBindingManager.getKeyBindingManager().isValidCommand(

    “coordsDown”, true)) {

    // Scale my texture down

    coordDelta -= .01f;

    updateTex = true;

    }



    // if the coordsUp command was activated

    if (KeyBindingManager.getKeyBindingManager().isValidCommand(“coordsUp”,

    true)) {

    // Scale my texture up

    coordDelta += .01f;

    updateTex = true;

    }



    if (updateTex) {

    // Get my square’s texture array

    FloatBuffer texBuf = square.getTextureCoords((int)0.5).coords;

    texBuf.rewind().position(2); // start after the 1st texcoord (2

    // floats wide)

    // Change the values of the texture coords in the buffer

    texBuf.put(coordDelta).put(0);

    texBuf.put(0).put(coordDelta);

    texBuf.put(coordDelta).put(coordDelta);

    }

    }

    }









    This is the code.



    Instead of a complete square,I got a square half of which  is brighter.



    Another is when i changed the value in “square.getTextureCoords((int)0.5).coords;” from 0.5 to 0,nothing happened.

    If I change “int[] indexes = { 0, 1, 2, 1, 2, 3 };” into  “int[] indexes = { 0, 1, 2, 2, 1, 3 };” ,

    I’ll get a whole bright square.

    Anyone could explain this?








According to wiki,it should be like this.

Hi,



I'm also new to jme and saw the same problem.  I think it has to do with the ordering of the vertices in the mesh.  In the "int[] indexes = { 0, 1, 2, 1, 2, 3 };" line, if you look at the vertices that correspond to those indices, the ones making the first triangle (0, 1, 2) loop in a counter-clock-wise direction, so by the right-hand rule, the normal for that face would point out of the screen.  But the vertices for the other triangle go in the opposite direction, so the normal points away.  If you look at the back of the mesh, you will notice the dark triangle becomes the light one and vice-versa.  That's why it got fixed when the order was changed for the second triangle.



I hope that helps and is accurate, but as I said, I'm just getting started here myself.

Thank you for replying.

Maybe you are right.



Since you found the same problem,I think there is somethings wrong in wiki or in JME.

http://www.jmonkeyengine.com/wiki/doku.php/starter:hello_mouse_pick_jme2:hello_mousepick



I checked some of the HelloKeyInput.java code versions yesterday.It seems that the code "int[] indexes = { 0, 1, 2, 1, 2, 3 };"  never changed during the past 6 years.

Why not fix this during such a long period?

Thanks again.

Yeah, I think its just a simple matter where no one has cared enough to fix it, since it's not terribly important, especially with most of the focus right now on getting JME 3.0 put together.  After that all the documentation will need to be redone anyway.  I'm thinking I should probably look into figuring out how to submit some fixes myself, cause I've noticed a few problems in other tutorials as well…