The index specifies the position of the data in the stream.
So to explain using that sample that you linked...
- Code: Select all
vertexFormat:declareCoord ( 1, MOAIVertexFormat.GL_FLOAT, 2 )
vertexFormat:declareUV ( 2, MOAIVertexFormat.GL_FLOAT, 2 )
vertexFormat:declareColor ( 3, MOAIVertexFormat.GL_UNSIGNED_BYTE )
Is saying that the stream is going to be formatted in chunks of 3 numbers and they will be of the order float, float, byte.
We know that these numbers are going to be in the order position, uv, color, however the shader does not know yet. All the shader knows is it'll get float, float, byte, it has no idea yet what these numbers represent. We will tell the shader what they represent later.
Because we know what they represent, we can go ahead and put some data into the stream. That's what happens next in the sample, we add 4 vertices, and add the position, uv, and color for each one.
- Code: Select all
vbo = MOAIVertexBuffer.new ()
vbo:setFormat ( vertexFormat )
vbo:reserveVerts ( 4 )
vbo:writeFloat ( -64, -64 )
vbo:writeFloat ( 0, 1 )
vbo:writeColor32 ( 1, 0, 0 )
vbo:writeFloat ( 64, -64 )
vbo:writeFloat ( 1, 1 )
vbo:writeColor32 ( 1, 1, 0 )
vbo:writeFloat ( 64, 64 )
vbo:writeFloat ( 1, 0 )
vbo:writeColor32 ( 0, 1, 0 )
vbo:writeFloat ( -64, 64 )
vbo:writeFloat ( 0, 0 )
vbo:writeColor32 ( 0, 0, 1 )
The shader is happy with these because all the values correspond to the data types that it was expecting to see.
Then a little further down, after we load in the vertex and pixel shaders, you will see the code where we tell the shader what each value in the stream corresponds to.
- Code: Select all
shader:setVertexAttribute ( 1, 'position' )
shader:setVertexAttribute ( 2, 'uv' )
shader:setVertexAttribute ( 3, 'color' )
The index numbers themselves aren't so important. I could've done this whole process using index 2 for position, 3 for uv, and 5 for color if I wanted.
I hope this helps you understand a little better and answers your question.