splunk -4 years ago 157
C++ Question

# drawing a cube with six different colored faces in opengl

I have drawn a cube in OpenGL, but I'd like each face of the cube to be filled with a color.

This is what I've done, I don't know why only 2 faces are filled with color.

``````void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength)
{
GLfloat halfSideLength = edgeLength * 0.5f;

GLfloat vertices[] =
{
// front face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left

// back face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top left
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom left

// left face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left

// right face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left

// top face
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX + halfSideLength, centerPosY + halfSideLength, centerPosZ + halfSideLength, // bottom left

// bottom face                                                                                                                                                                           // right face
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // top left
centerPosX - halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // top right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right
centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength, // bottom left                                                                                                                                                                                                                                                                                                                                                                                                                                                  centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ - halfSideLength, // bottom right                                                                                                                                                                                                                                                                                                                                                                                                                                                  centerPosX + halfSideLength, centerPosY - halfSideLength, centerPosZ + halfSideLength  // bottom left
};

GLfloat colour[] = {
255, 0, 0,
255, 0, 0,
255, 0, 0,
255, 0, 0,
0, 255, 0,
0, 255, 0,
0, 255, 0,
0, 255, 0,
0, 0, 255,
0, 0, 255,
0, 0, 255,
0, 0, 255,
100,100,100,
100,100,100,
100,100,100,
100,100,100,
200,200,200,
200,200,200,
200,200,200,
200,200,200,
40,220,75,
40,220,75,
40,220,75,
40,220,75,
};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colour);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
``````

The output of my program is this:

As you can see I have 1 red face, 1 green face and 4 not colored faces

You either use `GLfloat (GL_FLOAT)` but then you have to use the range of [0.0, 1.0].
Or you use `GLubyte (GL_UNSIGNED_BYTE)` then you will have to use the range [0, 255].