Reputation: 137
Like the picture showed in the link below. I want to color my robot hand with white color(RGB: 0.9 0.9 0.9). When I use glColor3f, the white color display correctly. But when I store RGB data in GLfloat array and call glColorPointer, the color becomes so weird.
Here is code of right arm
static const GLfloat playerBodyColor[] = {0.9, 0.9, 0.9};
glEnableClientState(GL_COLOR_ARRAY);
//glColor3f(0.9, 0.9, 0.9);
glColorPointer(3, GL_FLOAT, 0, playerBodyColor);
glTranslatef(0.0, player_body_height/6, player_body_width/2+LENGTH_UNIT/2);
glRotatef(340.0, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(2*LENGTH_UNIT, 0.0, 0.0);
glScalef (4.0, 1.0, 1.0);
copyCodeSolidCube (LENGTH_UNIT);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
If I change code like this:
static const GLfloat playerBodyColor[] = {0.9, 0.9, 0.9};
//glEnableClientState(GL_COLOR_ARRAY);
glColor3f(0.9, 0.9, 0.9);
//glColorPointer(3, GL_FLOAT, 0, playerBodyColor);
glTranslatef(0.0, player_body_height/6, player_body_width/2+LENGTH_UNIT/2);
glRotatef(340.0, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(2*LENGTH_UNIT, 0.0, 0.0);
glScalef (4.0, 1.0, 1.0);
copyCodeSolidCube (LENGTH_UNIT);
//glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
The color shows correct.
The picture of correct-color code: https://i.sstatic.net/xknCC.jpg
The picture of wrong-color code: https://i.sstatic.net/l1SOc.jpg
In the code, I also called a function copyCodeSolidCube. Here is part of copyCodeSolidCube code:
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_NORMAL_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, vert);
glNormalPointer (GL_FLOAT, 0, norm);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, one);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, two);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, three);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, four);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, five);
glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_BYTE, six);
glDisableClientState (GL_VERTEX_ARRAY);
glDisableClientState (GL_NORMAL_ARRAY);
Where is the problem?
Upvotes: 1
Views: 4417
Reputation: 54562
With glColorPointer()
, you specify an array of colors, one for each vertex. For example, if your cube has 8 vertices, you need an array of 8 colors. Since you have 3 components per color, the total size of the array you need is 24 floats.
Or, since you currently have 4 vertices per draw call, you need 4 colors in the color array, for 4 * 3 = 12 float values.
The big advantage of using this is of course that you can color each vertex individually. If you really want the same color for each vertex, glColor3f()
was actually the right call to use. Or you can use a slight variation:
glColor3fv(playerBodyColor);
This is equivalent to the glColor3f()
call you had, except that it's more convenient if you already have the color in an array of 3 values.
Upvotes: 2