0

How can I set color for vertex in opengl es 2.0?

Now I use color array:

float[] TriangleColors = new float[]{
                           1.0f, 1.0f, 0.0f,
                           1.0f, 1.0f, 0.0f,
                           1.0f, 1.0f, 0.0f,
                         };

GL.EnableVertexAttribArray((int)GLKVertexAttrib.Color);
GL.VertexAttribPointer((int)GLKVertexAttrib.Color,
                       3, VertexAttribPointerType.Float,
                       false, 0, 0);

GL.GenBuffers(1, out colorBuffer);
GL.BindBuffer(BufferTarget.ArrayBuffer, colorBuffer);

GL.BufferData (BufferTarget.ArrayBuffer,
               (IntPtr)(TriangleColors.Length * sizeof(float)),
               TriangleColors,
               BufferUsage.StaticDraw);

This code doesn't work for me.

John Willemse
  • 6,608
  • 7
  • 31
  • 45

1 Answers1

0

you should generate the VBO storing the vertex colors & bind it to the array buffer target before calling the glVertexAttribPointer function.

the function glVertexAttribPointer can work in 2 ways:

  • you can either use the last argument to directly provide a pointer to the vertex attribute data ( TriangleColors in your case ) - then you don't need any VBOs at all, but you also have to make sure that there's no buffer bound to the array buffer when calling the function.

  • on the other hand, if you wish to use vertex buffer objects, you'll have to make sure that the correct VBO is bound to the array buffer target when calling the function - this changes the meaning of the last argument; it now becomes a byte offset into the currently bound buffer.

in your case

GL.VertexAttribPointer((int)GLKVertexAttrib.Color,
                           3, VertexAttribPointerType.Float,
                           false, 0, 0);

you don't provide a pointer, so the last argument is treated as byte offset into the array buffer target ( i.e., no offset ); but your code doesn't show if any VBO is even bound at that point - only that you bind the color VBO afterwards.

gemse
  • 1,240
  • 9
  • 10