Vertex colors in own shader

This is most likely a stupid beginner problem, but I somehow really can’t get vertex colors working in my own shader. I’m not doing anything fancy…but see for yourself.
Here’s what my test model looks like in the SDK model viewer and how it should look:

And here’s how it looks ingame:

Here’s my vertex shader:
[java]uniform mat4 g_WorldViewProjectionMatrix;

attribute vec4 inPosition;
attribute vec4 inColor;

varying vec4 v_Color;

void main()
{
v_Color = inColor;
gl_Position = g_WorldViewProjectionMatrix * inPosition;
}
[/java]

And my fragment shader:
[java]varying vec4 v_Color;

void main()
{
gl_FragColor = v_Color;
}
[/java]

And the Material Definition:
[java]MaterialDef Default {

MaterialParameters {
}

Technique {
    VertexShader GLSL100:   MatDefs/objects/Default.vert
    FragmentShader GLSL100: MatDefs/objects/Default.frag

    WorldParameters {
        WorldViewProjectionMatrix
    }
}

}
[/java]

And I load the model like this:
[java] Spatial obj = assetManager.loadModel(“Models/monkeytest.j3o”);
Material mat = new Material(assetManager, “MatDefs/objects/Default.j3md”);
obj.setMaterial(mat);
rootNode.attachChild(obj);[/java]

I also tried the provided materials with varying results, but never got the correct colors - except when not applying a material at all.
Any ideas?

The shader looks fine for me, i suspect a problem with the models vertexcolor buffer.

The model is made with Blender 2.66 (latest version). I tried converting to Ogre XML and then to j3o, with the same result. It loads fine in another engine (libgdx) and I think it’s also somewhat strange that it works in the jME Model Viewer…
Maybe it’s some problem with the j3o converter? Not quite sure which Blender version that supports… (I also tried the nightly builds, no difference).
I guess I’ll give Blender 2.5 a try and report how that works.

Doesn’t work in Blender 2.57 either. Maybe I’m using Blender wrong?
I simply set the material color like this:

Anything wrong about that?

Try loading the .blend file directly, it should work without ogrexml in current jme3 svn versions.

Just tried that - it does load the .blend in the SVN version (strangely, the BlenderLoader is missing in the nightlies), but the result is exactly the same.
In case anyone else would like to give it a try, I uploaded my test model here: monkeytest.blend.
Anything else I could try?

Setting the colour in blender doesn’t set the vertex colours afaik. It sets the material colour…

So, how would I access these material colors in my shader then? All I’d like to do is loading some models with solid colors (some have individually colored faces).

I don’t know if/how you can do vertex colours from blender. I’ve done them from custom meshes and that’s easy enough (see the custom mesh section on the wiki).

Yes, I also got some custom meshes and they work perfectly indeed… unfortunately that’s just not a suitable way to get all the objects I need into the game :slight_smile:

Hm blender has a vertex paint mode (where object mode is, maybee that does the trick?
Also why not use a simple 1x1 texture? would probably be simpler in blender.

Yes, I also tried vertex paint, but if I use that on my model the SDK can’t convert/view the model anymore… no error message, it just doesn’t do anything.
Textures seem unnecessarily circumstantial, I’d like to avoid that.
I think the problem is the (jME) material I assign after loading the model. As mentioned in the beginning, if I don’t set a material the colors appear as they should, so apparently jME handles the .blend file (and the Blender material) properly. Which shader is used to render my model in this case? Is there some way to use a different shader without changing the material?

@Sebioff said: Yes, I also tried vertex paint, but if I use that on my model the SDK can't convert/view the model anymore... no error message, it just doesn't do anything. Textures seem unnecessarily circumstantial, I'd like to avoid that. I think the problem is the (jME) material I assign after loading the model. As mentioned in the beginning, if I don't set a material the colors appear as they should, so apparently jME handles the .blend file (and the Blender material) properly. Which shader is used to render my model in this case? Is there some way to use a different shader without changing the material?

The material is the shader, basically. Just like you can’t have the same lunch if you switch out the sandwich. :slight_smile:

The fact that it works with the standard shaders implies to me that blender has already give your model a texture or something. You could traverse the scene graph and find out what material is used and then inspect its parameter to see… if you were really curious.

It sounds like what you want is vertex painting but I’m not sure that vertex painting is currently supported by the JME3 Blender loader. What is happening in your example is you are assigning different materials to different parts of your model in Blender. This is supported by the JME3 Blender loader, however, the result will be that it creates multiple mesh objects each with a different material in the JME3 scene graph. If you want to use your custom shader, you will need to go through the scene graph and make sure you change the material in all the objects that the Blender loader created.

However, I would not recommend you set up your objects this way because it will be very inefficient if your model has many materials. Either you should ask Kaelthas about vertex painting support in the Blender loader, write a patch to support it yourself, or use UV textures which is a very well supported use-case and can probably do everything you would want to do vs vertex painting.