Setting color

Well, I’ve searched the forum, read the Javadoc, had a look at the example files: I still don’t understand a bit or two. So I am going to bother all of you here in the forum…



I looked at TestBoxColor.java and the thing I don’t understand is that you need to switch the light OFF in order to see the color of the box.



What I tried was to create a simple scene that has a little colored box along the x, y and z axis with a distance of 1 to each other. X should be red, Y green and Z blue.



So I came up with something like:

private void createDot(Vector3f position, ColorRGBA c, Node scene) {

Box box = new Box(“my box” + boxCount, position, 0.05f, 0.05f, 0.05f);

box.setModelBound(new BoundingSphere());

box.updateModelBound();

scene.attachChild(box);

box.setSolidColor((ColorRGBA)c.clone());

boxCount++;

}

but then I too need to switch off lighting by disabling lightState, or else I don’t see the colors.

So I started looking for a way to set the ambient and diffuse colors of the object faces and thought of something like:



private void createDot(Vector3f position, ColorRGBA c, Node scene) {

Box box = new Box(“my box” + boxCount, position, 0.05f, 0.05f, 0.05f);

box.setModelBound(new BoundingSphere());

box.updateModelBound();

scene.attachChild(box);

MaterialState ms = display.getRenderer().getMaterialState();

ms.setAmbient((ColorRGBA)c.clone());

ms.setDiffuse((ColorRGBA)c.clone());

box.setRenderState(ms);

boxCount++;

}



but that don’t seem to work at all. I tried updateRenderState() as well, btw.



Can anyone help?

The problem is that gl lighting overrides vertex color settings. That is why it only works when the lights are off.



If you want the color to show when the lights are on, you need to use materials. You were going the right way with MaterialState, you just need to enable the state with ms.setEnabled(true);

First of all, thanks for the response.



Well, in fact I enabled the material, I forgot to copy this into the listing since I fiddled around with the source.



It still does not work. I keep getting plain white boxes, no matter whether lights are on or off. This may not be the correct place for this, but here’s my (fairly short) listing:


import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;

public class JmeEval extends SimpleGame {
    static int boxCount = 0;
    long startTime = 0l;

    public static void main(String[] args) {
        JmeEval app = new JmeEval();
        app.setDialogBehaviour(AbstractGame.FIRSTRUN_OR_NOCONFIGFILE_SHOW_PROPS_DIALOG);
        app.start();
    }

    protected void simpleInitGame() {
        display.setTitle("JME evaluation");

        createXAxis(rootNode);
        createYAxis(rootNode);
        createZAxis(rootNode);

        cam.setLocation(new Vector3f(10f, 10f, 10f));
        cam.setDirection(new Vector3f(-10f, -10f, -10f));
        cam.setUp(new Vector3f(0f, 1f, 0f));
       
        rootNode.updateRenderState();
    }

    private void createXAxis(Node scene) {
        for (float x = -10.0f; x < 10.0f; x += 1.0f) {
            ColorRGBA xColor = new ColorRGBA(0.5f + x / 20f, 0f, 0f, 1f);
            Vector3f current = new Vector3f(x, 0f, 0f);
            createDot(current, xColor, scene);
        }
    }

    private void createYAxis(Node scene) {
        for (float y = -10.0f; y < 10.0f; y += 1.0f) {
            ColorRGBA yColor = new ColorRGBA(0f, 0.5f + y / 20f, 0f, 1f);
            Vector3f current = new Vector3f(0f, y, 0f);
            createDot(current, yColor, scene);
        }
    }

    private void createZAxis(Node scene) {
        for (float z = -10.0f; z < 10.0f; z += 1.0f) {
            ColorRGBA zColor = new ColorRGBA(0f, 0f, 0.5f + z / 20f, 1f);
            Vector3f current = new Vector3f(0f, 0f, z);
            createDot(current, zColor, scene);
        }
    }

    private void createDot(Vector3f position, ColorRGBA c, Node scene) {
        Box box = new Box("my box" + boxCount, position, 0.05f, 0.05f, 0.05f);
        MaterialState ms = display.getRenderer().getMaterialState();
        ms.setAmbient((ColorRGBA)c.clone());
        ms.setDiffuse((ColorRGBA)c.clone());
        ms.setSpecular((ColorRGBA)ColorRGBA.white.clone());
        ms.setEnabled(true);
        box.setRenderState(ms);
        boxCount++;
        scene.attachChild(box);
    }
}



Any ideas?!?

yep, the problem was that emissive is set to white by default… bad. Also, shininess is set to 0 by default, also fairly bad. You could have fixed the issue by adding in calls to set both of these properties.



That said, in the state itself, I’ve changed these to the following defaults which would be more reasonable in most cases:


emissive = new ColorRGBA(0,0,0,1);
ambient = new ColorRGBA(0,0,0,1);
diffuse = new ColorRGBA(1,1,1,1);
specular = new ColorRGBA(1,1,1,1);
shininess = 64f;



Now your program runs ok as is (assuming you check out the latest from cvs.) A few notes though: you don't need to call rootNode.updateRenderState() in your code when using SimpleGame as it is done for you. Also, you should call cam.update(); after making changes to the camera. Finally, you shouldn't need to clone the colors since you create them new before passing them into the createDot method. Heh, but I imagine most of what I'm referring to was added to try to solve this material state issue. :)

Thanks alot, this really solved the problem. :smiley:



Although I might be a seasoned java developer, starting to learn development of 3d apps, especially in a “bleeding edge” area, is obviously challenging. It’s quite a bit of theory I need to learn, lots of experimenting and most of the time if something doesn’t work, it must be because I didn’t understand something correctly.

:wink:

I clearly understand that there is some sort of lack of tutorials and such things, since most ppl which are involved in jme are implementing features (and every developer I know hates writing documentation). So maybe I collect all the pits while I run into them and finally write some sort of “Learning by stumbling” tutorial lesson…

That would be appreciated I’m sure. Mojo is writing up some documentation over time and hopefully that will help too. Also, the demos in jmetest are meant to serve as mini-tutorials on each feature… That said, any suggestions on what might help new users would be helpful.

I’m always willing people to write stuff for me. I’ll be writing the official user’s guide again now, so any insight into what needs to be covered in detail would be great. A lot of those “gotchas” that you guys are running into need to be documented.

I think one of the things I’d find helpful to be documented is the various update calls and when and why they need to be called.



updateRenderState()

cam.update()

updateModelBound()

updateGeometricState()



etc.



For any particular call like these, we can check the javadocs and hope for a good explanation. The bigger problem is knowing that they need to be there in the first place.



-Mike