Original Post
Hi, I've be learning opengl for a while, so forgive my noobness ;) I'm trying to create a grid of cubes. I've written a function that allows me to position a cube on a specific x,y,z The problem is that the z axis is wrong, some object are more in front then others. I just want a flat grid of cubes, that will allow me to eventually rotate the cubes. This is my cube code:
void glCube(float x, float y, float z, float scale){
glPushMatrix();
glTranslatef(x, y, z); // move 5 units into the screen.
// Rotate around the x- and y-axis.
//glRotatef(angle,s,c,0.0f);
for (int i=0;i<4;i++) {
// Push the current matrix stack (there is a stack of matrices for each of the matrix modes).
glPushMatrix();
// Rotate around the y-axis to create the front, back, left and right faces.
glRotatef(i*90.0f,0.0f,1.0f,0.0f);
// Move along the z-axis (towards the camera).
glTranslatef(0.0f,0.0f,1.0f);
// Use a named texture.
glBindTexture(GL_TEXTURE_2D,texture_list);
// Define the front face.
glBegin(GL_QUADS);
glTexCoord2f(1.0f,0.0f);
glVertex3f(+1.0f,+1.0f,0.0f); // Upper right vertex.
glTexCoord2f(0.0f,0.0f);
glVertex3f(-1.0f,+1.0f,0.0f); // Upper left vertex.
glTexCoord2f(0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f); // Lower left vertex.
glTexCoord2f(1.0f,1.0f);
glVertex3f(+1.0f,-1.0f,0.0f); // Lower right vertex.
glEnd();
// Pop the current matrix stack.
glPopMatrix();
}
// Push the current matrix stack (there is a stack of matrices for each of the matrix modes).
glPushMatrix();
// Rotate around the x-axis to create the bottom face.
glRotatef(90.0f,1.0f,0.0f,0.0f);
// Move along the z-axis (towards the camera).
glTranslatef(0.0f,0.0f,1.0f);
// Use a named texture.
glBindTexture(GL_TEXTURE_2D,texture_list[4]);
// Define the front face.
glBegin(GL_QUADS);
glTexCoord2f(1.0f,0.0f);
glVertex3f(+1.0f,+1.0f,0.0f); // Upper right vertex.
glTexCoord2f(0.0f,0.0f);
glVertex3f(-1.0f,+1.0f,0.0f); // Upper left vertex.
glTexCoord2f(0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f); // Lower left vertex.
glTexCoord2f(1.0f,1.0f);
glVertex3f(+1.0f,-1.0f,0.0f); // Lower right vertex.
glEnd();
// Pop the current matrix stack.
glPopMatrix();
// Rotate around the x-axis to create the top face.
glRotatef(-90.0f,1.0f,0.0f,0.0f);
// Move along the z-axis (towards the camera).
glTranslatef(0.0f,0.0f,1.0f);
// Use a named texture.
glBindTexture(GL_TEXTURE_2D,texture_list[5]);
// Define the front face.
glBegin(GL_QUADS);
glTexCoord2f(1.0f,0.0f);
glVertex3f(+1.0f,+1.0f,0.0f); // Upper right vertex.
glTexCoord2f(0.0f,0.0f);
glVertex3f(-1.0f,+1.0f,0.0f); // Upper left vertex.
glTexCoord2f(0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,0.0f); // Lower left vertex.
glTexCoord2f(1.0f,1.0f);
glVertex3f(+1.0f,-1.0f,0.0f); // Lower right vertex.
glEnd();
glPopMatrix();
}
Code to make the grid of cubes:
float xcount, ycount;
//glTranslatef(0.0f, 0.0f, ((unsigned int)angle)/100.0f);
cubecount=0;
for (xcount=-1*split; xcount<=1*split; xcount+=2){
for (ycount=-1*split; ycount<=1*split; ycount+=2){
cubecount++;
glCube(xcount, ycount, -timer, 1.0f);
fprintf(stderr, "x: %5f y: %5f z: %5f\n", xcount, ycount, -timer);
}
}
Does anybody know whats wrong with my code? Or does anybody have a better way of doing this? Btw I have to be able to give every side of the cube another texture. TIA.