Reputation: 21237
I have a struct that consists of 4 vectors that I am using to create a rectangle. One of the matrices defines the color of the rectangle. Each time I draw a rectangle, I need to update the color of it. Problem is, I don't know how to update this part of the struct. Can anyone help? Thanks!
typedef struct {
GLKVector3 positionCoordinates;
GLKVector2 textureCoordinates;
GLKVector3 normalCoordinates;
GLKVector4 colorCoordinates;
} VertexData;
VertexData unitSquare[] = {
{ {-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ { 0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ {-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ {-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ { 0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} },
{ { 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f}, { 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f} }
};
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
...
// lame attempt to update the color of one vertex
unitSquare->colorCoordinates[0] = {0.0f, 0.0f, 0.0f, 1.0f};
...
}
Upvotes: 0
Views: 44
Reputation: 4805
There's three things wrong with your code
Unitsquare is an array of structs. Use this
unitSquare[0].colorCoordinate = GLKVector4Make(.1, .1, .1, .1);
Even if you update the unitSquare variable on the CPU, you still need to update your Vertex Buffer Object with the new color by binding your buffer with glBindBuffer and using glBufferData, or the GPU won't . But don't do that yet because....
All your vertices have the same color in your sample -- if this is true throughout your program, its far more efficient to use a uniform instead -- look up the glUniform3fv function.
Hope this helps.
Upvotes: 1