# Generating a grid of cubes in 3D space [closed]

I am trying to generate a grid of cubes in 3D space and It aint workin...

All im doing is for looping YXZ (in that order) (nested for loops) then im doing cubes[x][y][z] = ... (new Location(x,y,z))

But when I render them they are in this weird line that curves every X cubes... What am I doing wrong?

RENDERING:

public void render() {
Cube[][][] temp = testchunk.getCubes();
for (int z = 0; z<temp[0].length;z++) {
for (int y = 0; y<temp[0][0].length; y++) {
for (int x = 0; x<temp.length; x++) {
Cube b = temp[x][z][y];
if (b != null) {
GL11.glTranslatef(b.getPosition().x, b.getPosition().y, b.getPosition().z);
GL11.glCallList(cubes.get(b.getCubeId()));
}
}
}
}

}


GENERATING:

    for (int z = 0; z<cubes[0].length;z++) {
for (int y = 0; y<cubes[0][0].length; y++) {
for (int x = 0; x<cubes.length; x++) {
cubes[x][z][y] = (Cube.dirt.getInstance()).setPosition(new Vector3f(position.x+(x*Cube.CUBE_SIZE),position.z+(z*Cube.CUBE_SIZE),position.y+(y*Cube.CUBE_SIZE)));
}
}
}

• Try looping in ZYX order, otherwise we will need to see some more context: How are you rendering your cubes (i.e. code snippets) – Mokosha Apr 29 '13 at 17:55
• Doesnt work and im adding snippets now – Amit Assaraf Apr 29 '13 at 18:13
• I think this is actually a "how to fix my code" question. – MichaelHouse Apr 30 '13 at 1:14

## 1 Answer

You aren't resetting the modelview matrix, so each cube's translation is added to all further cubes. Use glPushMatrix and glPopMatrix around each translate-and-call.

• To add to this question, understand you are not doing this so instead of pushing and popping just add to your current location is a valid solution also. – Chris Condy Apr 30 '13 at 1:21
• @ChrisCondy That can lead to accumulation of rounding error. – Kevin Reid Apr 30 '13 at 1:32