How can I set Colors for each Vertex?

I want to color each Vertex myself, is this somehow possible? (without shaders, I mean…)

Then I want to combine it with a WireframeState, is this also possible?



Thanks in advance…

Tim

rickard said:

why do you vertexColor.rewind()?


I don't think it matters in this example, but as you can see, put(i) increases the index by one. So, as i understand it, .rewind() is a safety measure to make sure you begin from 0.

See the section "Clearing, flipping, and rewinding" in the API Spec for Buffer.


Do the vertices have an order?

Not sure


The different nio Buffers for a Geometry (other than the index buffer) require strict parallel ordering.  Obviously, if you changed the order that color buffer elements are written in Rickard's sample code, they would effect different vertexes.

Hi.

Basically, using the vertex buffer of the mesh, you can grab a specific vertex by knowing its position(they're stored in the buffer as: x,y,z,x,y,z,x,y,z), so a new vertex position starts every 3 values.

The color buffer contains the color codes for each vertex, each vertex has four color values (R,G,B,A), so a new vertexs' color starts every 4 values.



Below is an example where i use this to color a mesh using the terrain color map:


FloatBuffer vertices =  meshes.getVertexBuffer();
                    FloatBuffer vertexColor = meshes.getColorBuffer();
                    vertexColor.rewind();
                    for (int i = 0; i < vertices.capacity(); i+= 3)
                    {
                        Point pos = new Point((int)(vertices.get(i)), (int)(vertices.get(i+2)));
                        ColorRGBA color = GameWorld.pickTextureColor(colorMap, pos, 1);

                        vertexColor.put(color.r/1.5f);
                        vertexColor.put(color.g/1.5f);
                        vertexColor.put(color.b/1.5f);
                        vertexColor.put(1);
                    }



Hope it helps a bit.
Basically, using the vertex buffer of the mesh, you can grab a specific vertex by knowing its position(they're stored in the buffer as: x,y,z,x,y,z,x,y,z), so a new vertex position starts every 3 values.
The color buffer contains the color codes for each vertex, each vertex has four color values (R,G,B,A), so a new vertexs' color starts every 4 values.

Okay.. basically as I thought, thank you!
Do the vertices have an order?


Below is an example where i use this to color a mesh using the terrain color map:

Code:

FloatBuffer vertices =  meshes.getVertexBuffer();
                   FloatBuffer vertexColor = meshes.getColorBuffer();
                   vertexColor.rewind();
                   for (int i = 0; i < vertices.capacity(); i+= 3)
                   {
                       Point pos = new Point((int)(vertices.get(i)), (int)(vertices.get(i+2)));
                       ColorRGBA color = GameWorld.pickTextureColor(colorMap, pos, 1);

                       vertexColor.put(color.r/1.5f);
                       vertexColor.put(color.g/1.5f);
                       vertexColor.put(color.b/1.5f);
                       vertexColor.put(1);
                   }


Hope it helps a bit.

Not a bit! That's very helpful!
But one question:
why do you vertexColor.rewind()?
why do you vertexColor.rewind()?


I don't think it matters in this example, but as you can see, put(i) increases the index by one. So, as i understand it, .rewind() is a safety measure to make sure you begin from 0.

Do the vertices have an order?


Not sure