Hi,
I was messing around with openGL and was able to get a simple cube drawn in immediate mode with quads. But I tried moving the cube to a vertex array and drawing the quads and I seem to be having a bit of trouble. If someone could take a look and see if they can spot something obvious I am doing wrong I would really appreciate it.
I am using SDL and openGL to do my drawing. The following code is what I am trying to use to render the cube:
//Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0f, 0.0f, -10.0f);
glRotatef(rotation, 1.0f, 1.0f, 0.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glColorPointer(4, GL_FLOAT, 0, &myCube->colors);
glVertexPointer(3, GL_FLOAT, 0, &myCube->vertices);
glDrawArrays(GL_QUADS, 0, myCube->vertices.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glLoadIdentity();
rotation -= 0.5f;
//Update Screen
SDL_GL_SwapBuffers();
The code I am using to generate the cube vertices and colors are as follows:
void model::createCube(float size, float x, float y, float z, float r, float g, float b, float a)
{
//find half distance
float half = size / 2.0f;
//create the top face vertices
this->vertices.push_back(x-half); this->vertices.push_back(y+half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y+half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y+half); this->vertices.push_back(z-half);
this->vertices.push_back(x-half); this->vertices.push_back(y+half); this->vertices.push_back(z-half);
//Create the top & bottom face vertex colors
float tr, tb, tg;
tr = r / 3.0f;
tg = g / 3.0f;
tb = b / 3.0f;
//Add top face vertex colors
for(int i=0; i < 4; i++){
this->colors.push_back(tr); this->colors.push_back(tg); this->colors.push_back(tb); this->colors.push_back(a);
}
//create the bottom face vertices
this->vertices.push_back(x-half); this->vertices.push_back(y-half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y-half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y-half); this->vertices.push_back(z-half);
this->vertices.push_back(x-half); this->vertices.push_back(y-half); this->vertices.push_back(z-half);
//Add bottom face vertex colors
for(int i=0; i < 4; i++){
this->colors.push_back(tr); this->colors.push_back(tg); this->colors.push_back(tb); this->colors.push_back(a);
}
//create the front face vertices
this->vertices.push_back(x-half); this->vertices.push_back(y+half); this->vertices.push_back(z+half);
this->vertices.push_back(x-half); this->vertices.push_back(y-half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y-half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y+half); this->vertices.push_back(z+half);
//Add front face vertex colors
for(int i=0; i < 4; i++){
this->colors.push_back(r); this->colors.push_back(g); this->colors.push_back(b); this->colors.push_back(a);
}
//create the back face vertices
this->vertices.push_back(x-half); this->vertices.push_back(y+half); this->vertices.push_back(z-half);
this->vertices.push_back(x-half); this->vertices.push_back(y-half); this->vertices.push_back(z-half);
this->vertices.push_back(x+half); this->vertices.push_back(y-half); this->vertices.push_back(z-half);
this->vertices.push_back(x+half); this->vertices.push_back(y+half); this->vertices.push_back(z-half);
//Add back face vertex colors
for(int i=0; i < 4; i++){
this->colors.push_back(r); this->colors.push_back(g); this->colors.push_back(b); this->colors.push_back(a);
}
//create the left face vertices
this->vertices.push_back(x-half); this->vertices.push_back(y+half); this->vertices.push_back(z-half);
this->vertices.push_back(x-half); this->vertices.push_back(y-half); this->vertices.push_back(z-half);
this->vertices.push_back(x-half); this->vertices.push_back(y-half); this->vertices.push_back(z+half);
this->vertices.push_back(x-half); this->vertices.push_back(y+half); this->vertices.push_back(z+half);
//Create the left & right face vertex colors
tr = r / 2.0f;
tg = g / 2.0f;
tb = b / 2.0f;
//Add left face vertex colors
for(int i=0; i < 4; i++){
this->colors.push_back(tr); this->colors.push_back(tg); this->colors.push_back(tb); this->colors.push_back(a);
}
//create the right face vertices
this->vertices.push_back(x+half); this->vertices.push_back(y+half); this->vertices.push_back(z-half);
this->vertices.push_back(x+half); this->vertices.push_back(y+half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y-half); this->vertices.push_back(z+half);
this->vertices.push_back(x+half); this->vertices.push_back(y-half); this->vertices.push_back(z-half);
//Add left face vertex colors
for(int i=0; i < 4; i++){
this->colors.push_back(tr); this->colors.push_back(tg); this->colors.push_back(tb); this->colors.push_back(a);
}
}
I have attached some images that show what I am getting rendered. When I try to use the color pointer it appears that only the top face is getting rendered correctly (Aside from the colors not being correct).
When I don't use a pointer to the color array and just use one color to render the cube I get the image in red.
Does anyone know where I am going wrong on this?
Thanks in advance!






