AndroidDev
AndroidDev

Reputation: 21237

Modifying a Struct of OpenGL Matrices

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

Answers (1)

Ben Pious
Ben Pious

Reputation: 4805

There's three things wrong with your code

  1. Unitsquare is an array of structs. Use this

    unitSquare[0].colorCoordinate = GLKVector4Make(.1, .1, .1, .1);

  2. 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....

  3. 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

Related Questions