How can I render a mesh (made up of triangles) where each triangle has same color without specifying that color 3 times per triangle in the vertex arrays.
Suppose I want to draw 2 triangles. Triangle 1 :
Vertex 1, position:(x1,y1) , color: (r1,g1,b1,a1)
Vertex 2, position:(x2,y2) , color: (r1,g1,b1,a1)
Vertex 3, position:(x3,y3) , color: (r1,g1,b1,a1)
Triangle 2 :
Vertex 4, position:(x4,y4) , color: (r2,g2,b2,a2)
Vertex 5, position:(x5,y5) , color: (r2,g2,b2,a2)
Vertex 6, position:(x6,y6) , color: (r2,g2,b2,a2)
I know this can be done by creating 2 vertex buffers:
Vertex Buffer 1:
[x1, y1]
[x2, y2]
[x3, y3]
[x4, y4]
[x5, y5]
[x6, y6]
Vertex Buffer 2:
[r1, g1, b1, a1]
[r1, g1, b1, a1]
[r1, g1, b1, a1]
[r2, g2, b2, a2]
[r2, g2, b2, a2]
[r2, g2, b2, a2]
I can now bind them to different attribute locations and do the usual draw call.
Vertex buffer 1 is fine because all data is unique, but my question is, isnt vertex buffer 2 such a waste of space!? What if I have thousands of triangles and with this approach i am duplicating color twice per each triangle. There has to be a smarter way of storing redundant attributes. I am not aware of it. Can anybody please tell?