Help with Rotating Colors

I have written a class that draws a triangle with TriMesh and is then supposed to put red in 1 corner and green in the other two corners. I then added key bindings so that when you press the G key it is supposed to move the red color to a different corner and then the greens to the other two corners. Here is my code:


/**
 *
 * @author Josh Branchaud
 * @version 12/3/2008
 */

// imported classes
import com.jme.app.SimpleGame;
import com.jme.scene.TriMesh;
import com.jme.math.Vector3f;
import com.jme.math.Vector2f;
import com.jme.renderer.ColorRGBA;
import com.jme.bounding.BoundingBox;
import com.jme.util.geom.BufferUtils;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;

public class rotateColor1 extends SimpleGame
{
    KeyBindingManager KBM = KeyBindingManager.getKeyBindingManager();
    TriMesh triMesh1;

    // create the colors here
    ColorRGBA[] colors =
        {
            new ColorRGBA(1,0,0,1),
            new ColorRGBA(1,0,0,1),
            new ColorRGBA(0,1,0,1)
        };

    protected void simpleInitGame()
    {
        // create an instance of the TriMesh
        triMesh1 = new TriMesh("TriMesh1");

        // create vectors for the TriMesh
        Vector3f[] vectorArray =
        {
            new Vector3f(0,0,0),
            new Vector3f(2,2,0),
            new Vector3f(0,4,0)
        };

        // create texture Coordinates for the bufferUtil
        Vector2f[] texCoords =
        {
           new Vector2f(0,0),
           new Vector2f(2,2),
           new Vector2f(0,4)
        };

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

        triMesh1.reconstruct
               (BufferUtils.createFloatBuffer(vectorArray),
                null,
                BufferUtils.createFloatBuffer(colors),
                BufferUtils.createFloatBuffer(texCoords),
                BufferUtils.createIntBuffer(indexes));

        // create and update model bound for triMesh1
        triMesh1.setModelBound(new BoundingBox());
        triMesh1.updateModelBound();

        // add triMesh1 to the rootNode
        rootNode.attachChild(triMesh1);

        // get rid of default lighting
        lightState.setEnabled(false);

        // set an action to KEY_G
        KBM.set("rotate", KeyInput.KEY_G);
    }

    protected void simpleUpdate()
    {
        if(KBM.isValidCommand("rotate", false))
        {
            ColorRGBA color1, color2, color3;

            color1 = colors[0];
            color2 = colors[1];
            color3 = colors[2];

            colors[0] = color3;
            colors[1] = color1;
            colors[2] = color2;
        }
    }

    public static void main(String[] args)
    {
        rotateColor1 thisWorld = new rotateColor1();
        thisWorld.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
        thisWorld.start();
    }
}



Any ideas of what I am doing wrong?

You'll have to reconstruct your TriMesh's buffers after changing values in the arrays. (Just copy the reconstruct call over to the update method where you change the color array).

Or you could change them in the buffers and call updateGeometricData()

I did the first suggestion and it now reconstructs the TriMesh like I wanted, but I am not sure it is displaying the colors like I expected. I thought that one corner would be distinctly red while the others were green and so on as I changed which colors affect which corners, but it doesn't seem to work quite right… here is my code:


/**
 *
 * @author Josh Branchaud
 * @version 12/3/2008
 */

// imported classes
import com.jme.app.SimpleGame;
import com.jme.scene.TriMesh;
import com.jme.math.Vector3f;
import com.jme.math.Vector2f;
import com.jme.renderer.ColorRGBA;
import com.jme.bounding.BoundingBox;
import com.jme.util.geom.BufferUtils;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;

public class rotateColor1 extends SimpleGame
{
    KeyBindingManager KBM = KeyBindingManager.getKeyBindingManager();
    TriMesh triMesh1;

    // create the colors here
    ColorRGBA[] colors =
        {
            new ColorRGBA(1,0,0,1),
            new ColorRGBA(1,0,0,1),
            new ColorRGBA(0,1,0,1)
        };

    // create vectors for the TriMesh
    Vector3f[] vectorArray =
        {
            new Vector3f(0,0,0),
            new Vector3f(2,2,0),
            new Vector3f(0,4,0)
        };

    // create texture Coordinates for the bufferUtil
    Vector2f[] texCoords =
        {
           new Vector2f(0,0),
           new Vector2f(2,2),
           new Vector2f(0,4)
        };

    // the indexes, every 3 define the points of a triangle
    int[] indexes =
        {
            1,2,3
        };

    protected void simpleInitGame()
    {
        // create an instance of the TriMesh
        triMesh1 = new TriMesh("TriMesh1");

        triMesh1.reconstruct
               (BufferUtils.createFloatBuffer(vectorArray),
                null,
                BufferUtils.createFloatBuffer(colors),
                BufferUtils.createFloatBuffer(texCoords),
                BufferUtils.createIntBuffer(indexes));

        // create and update model bound for triMesh1
        triMesh1.setModelBound(new BoundingBox());
        triMesh1.updateModelBound();

        // add triMesh1 to the rootNode
        rootNode.attachChild(triMesh1);

        // get rid of default lighting
        lightState.setEnabled(false);

        // set an action to KEY_G
        KBM.set("rotate", KeyInput.KEY_G);
    }

    protected void simpleUpdate()
    {
        if(KBM.isValidCommand("rotate", false))
        {
            ColorRGBA color1, color2, color3;

            color1 = colors[0];
            color2 = colors[1];
            color3 = colors[2];

            colors[0] = color3;
            colors[1] = color1;
            colors[2] = color2;

            triMesh1.reconstruct
               (BufferUtils.createFloatBuffer(vectorArray),
                null,
                BufferUtils.createFloatBuffer(colors),
                BufferUtils.createFloatBuffer(texCoords),
                BufferUtils.createIntBuffer(indexes));
        }
    }

    public static void main(String[] args)
    {
        rotateColor1 thisWorld = new rotateColor1();
        thisWorld.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
        thisWorld.start();
    }
}



any Ideas?

(Question) Is this JME1?

I guess your problem stems from your indexes being 1-based, instead of 0-based.

Try 0,1,2 instead.

@jjmonte I followed the instructions found at this link: http://www.jmonkeyengine.com/wiki/doku.php?id=setting_up_netbeans_5.0_to_build_jme_and_jme-physics_2

So, I guess it is jME1. Would I want to update it to jME2? How would I go about doing that?

@hevee That did the trick, thanks so much for taking the time to look at my code!