0

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);
}
awaelchli
  • 796
  • 7
  • 23
  • Please show the projection matrix used. – Stefan Hanke Apr 12 '12 at 14:41
  • Hey Stefan, I am not sure if the code I just added is what you were looking for. I couldn't find any projection matrix you asked for. I got most of this code from a tutorial and I don't understand every little detail of it. Hope this helps. And thank you to look at it. – awaelchli Apr 12 '12 at 19:09
  • 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`). – Stefan Hanke Apr 13 '12 at 09:51
  • That's it! I changed `gl.glFrustumf(-ratio, ratio, -1, 1, 2, 25);` and the camera distance. Very clever, Stefen. Thanks a lot. – awaelchli Apr 13 '12 at 11:08
  • I have copied the text into an answer. Please accept. – Stefan Hanke Apr 13 '12 at 11:34

1 Answers1

2

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).

Stefan Hanke
  • 3,458
  • 2
  • 30
  • 34