0
votes

I'm trying to create a 3D chessboard that the user can look at at various angles. I'm using gluLookAt with converted spherical coordinates to deal with the camera, but for some reason this changes the apparent depth of the objects. I think its related to the fact that I'm generating the board as a sequence of cubes and I'm not understanding something about how gluLookAt and glTranslate play with each other, but as far as I can tell it should work. Here's the display method:

glu.gluLookAt(
    r * cos(phi) * sin(theta) - .5, r * sin(phi) - .5, r * cos(phi) * cos(theta) - .5,
    board.getSize()[0] / 2.0 - .5, board.getSize()[1] / 2.0 - .5, board.getSize()[2] / 2.0 - .5, //glutSolidCube centeres the cube at the origin so I have to shift back 1/2 the lengh of the cube
    sin(phi) * sin(theta), abs(cos(phi)), sin(phi) * cos(theta));

for(int i=0;i<board.getSize()[2];i++){
    for(int j=0;j<board.getSize()[1];j++){
        for(int k=0;k<board.getSize()[0];k++){
            glut.glutSolidCube(1f);
            gl.glTranslatef(1f, 0f, 0f);
        }
        gl.glTranslatef(-1 * board.getSize()[0], 1f, 0f);
    }
    gl.glTranslatef(0f,-1 * board.getSize()[1], 1f);
}

Note that I'm using theta as the angle in the x-z plane and phi as the angle between the x-z plane and the vector. This is pi/2 shifted from the usual formulation, which is why x=rcos(phi)sin(theta) instead of rsin(phi)sin(theta) and so on.

1
You should avoid legacy OpenGLelect

1 Answers

0
votes

glTranslatef() does not affect gluLookAt(). Here is the parameters of gluLookAt() function :

 gluLookat(Camera.x,Camera.y,Camera,z,Lookat.x,Lookat.y,Lookat.z,up.X,up.Y,up.Z);

For your case, I think you can fix your Lookat position to the center of the board and not touch it while rotating. (As a chess player this is where I look most of the time physically). For up vector, I would use fixed (0,1,0) vector.