Phil Barr
Phil Barr

Reputation: 161

Open GLES 1.1 - GLColorPointer creates a rainbow of colours when I just want red

I'm rendering an object like this:

for (int i = 0; i < COLOR_ARRAY_SIZE; i += 4) {
    colors[i] = 1.0f;
    colors[i + 1] = 0.0f;
    colors[i + 2] = 0.0f;
    colors[i + 3] = 1.0f;
}

// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Set GL11 flags:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glEnable(GL_DEPTH_TEST);

// make sure nothing messes with the colour
glDisable(GL_BLEND);
glDisable(GL_DITHER);
glDisable(GL_FOG);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glShadeModel(GL_FLAT);

// Load projection matrix:
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projectionMatrix);

// Load model view matrix and scale appropriately
int kObjectScale = 300f;
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(modelViewMatrix);
glTranslatef(0.5f, 0.5f, 0.5f);
glScalef(kObjectScale, kObjectScale, kObjectScale);

// Draw object
glVertexPointer(3, GL_FLOAT, 0, (const GLvoid*) &vertexPositions[0]);
glNormalPointer(GL_FLOAT, 0, (const GLvoid*) &vertexNormals[0]);
glColorPointer(4, GL_FLOAT, 0, (const GLvoid*) &colors[0]);
glDrawElements(GL_TRIANGLES, 11733, GL_UNSIGNED_SHORT,
        (const GLvoid*) &indices[0]);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

I would expect that this would render my object all in red, but instead it's a rainbow of different colours. Does anyone know why? I would assume that there is something wrong with my "colors" array buffer but I can't for the life of me see what it is. The actual vertices seem to rendered just fine.

Upvotes: 2

Views: 2283

Answers (1)

James Bedford
James Bedford

Reputation: 28962

Your for loop is very confused. You're incrementing your value of i by 4 each time. What's more is that you're indexing with an offset of 1, 2 and 3 in lines 3-5. I presume that your define of COLOR_ARRAY_SIZE is 4? Try initializing your color array as follows:

float colors[] = {1.0f, 0.0f, 0.0f, 1.0f};

And then calling glColorPointer as follows:

glColorPointer(4, GL_FLOAT, 0, colors);

Notice that I've set the stride to be 0. If your color array only contains colors then I don't see any reason why you should be using a stride (stride is used to jump over interwoven information in an array).

Upvotes: 1

Related Questions