Search code examples
javaandroidopengl-esperspective

Close objects look stretched / changing vanishing points?


I have built a very nice Rubik's Cube in OpenGl for my android application. Unfortunately it looks a bit stretched when I bring it closer to the camera. It looks like this:

My Rubiks Cube 1

My Rubiks Cube 2

I'd like to have my Cube as big as it could possibly be on my screen, but without that stretching.

Here is a bit of code:

gl.glMatrixMode(GL10.GL_MODELVIEW);

    gl.glLoadIdentity();

    float z = 3;
    float x = 0;
    float y = 3;

    GLU.gluLookAt(gl, x, y, z, 0, 0, 0, 0, 1, 0);

I tried to work with GLU.gluPerspective() but it did not work.

EDIT: Here is more code from my renderer class:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glDisable(GL10.GL_DITHER);

    gl.glClearColor(0, 0, 0, 0);

    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glClearDepthf(1f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

@Override
public void onDrawFrame(GL10 gl) {

    gl.glDisable(GL10.GL_DITHER);

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW);

    gl.glLoadIdentity();

    float z = 3;
    float x = 0;
    float y = 3;

    GLU.gluLookAt(gl, x, y, z, 0, 0, 0, 0, 1, 0);


    gl.glRotatef(angleX, 1, 0, 0); //This is the rotation if surface is touched
    gl.glRotatef(angleY, 0, 1, 0); 

    f.draw(gl); //drawing the pieces of the cube
    r.draw(gl);
    l.draw(gl);
    u.draw(gl);
    d.draw(gl);
    b.draw(gl);

    for (int i = 0; i < corners.length; i++) {
        corners[i].draw(gl);
    }
    for (int i = 0; i < edges.length; i++) {
        edges[i].draw(gl);
    }
    display(); //this has nothing to do with opengl

}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);

    float ratio = (float) width / height;

    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
}

Solution

  • The camera is too near to the object. Try moving the camera backwards a bit, and the near plane a bit back. (5th parameter to glFrustumf).