-1
\$\begingroup\$

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)));
            }
        }
    }
\$\endgroup\$
  • \$\begingroup\$ Try looping in ZYX order, otherwise we will need to see some more context: How are you rendering your cubes (i.e. code snippets) \$\endgroup\$ – Mokosha Apr 29 '13 at 17:55
  • \$\begingroup\$ Doesnt work and im adding snippets now \$\endgroup\$ – gopgop Apr 29 '13 at 18:13
  • 1
    \$\begingroup\$ I think this is actually a "how to fix my code" question. \$\endgroup\$ – MichaelHouse Apr 30 '13 at 1:14
5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Chris Condy Apr 30 '13 at 1:21
  • \$\begingroup\$ @ChrisCondy That can lead to accumulation of rounding error. \$\endgroup\$ – Kevin Reid Apr 30 '13 at 1:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.