OpenGL perspective distorting scene much more than

2020-07-24 20:54发布

问题:

I'm working in LWJGL and have been struggling for a while to write a program that allows for simple 3d viewing of a scene. The major issue that I'm running into is that whenever I apply perspective, my scene is stretched very, very far in the z axis.

The square in the middle is a cube drawn with orthographic projection (here cube means "all sides are equal"). The shape trailing from the bottom left to the center is also a cube, but drawn with perspective projection!

Obviously, this does not not look like a cube. Here is my code (it is a bit of aa wall, so I've broken it up):

public class OpenGL
{
    //width and height of the screen, plus the depth of the 3d space
    public static final int WIDTH = 800, HEIGHT = 600, DEPTH = 1000;

    //called once at startup
    private static void initGl()
    {
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glEnable(GL11.GL_DEPTH_TEST);

        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glViewport(0,0,WIDTH,HEIGHT);
        GL11.glDepthFunc(GL11.GL_LESS);
        GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
        GL11.glClearColor(0,0,0,0);
        GL11.glShadeModel(GL11.GL_SMOOTH);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, WIDTH, HEIGHT, 0, DEPTH, -DEPTH);

        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        DEFAULT_FONT.addAsciiGlyphs();
        DEFAULT_FONT.addGlyphs(400, 600);
        DEFAULT_FONT.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
        try
        {
            DEFAULT_FONT.loadGlyphs();
        }
        catch (SlickException e)
        {
            e.printStackTrace();
        }
    }
}

These are all in another class and are called 30 times a second.

//main rendering function.
public void render()
{   
        //these two just input values for different variables, allowing for 
        //movement of the camera and testing different values of znear and zfar.  
        //nothing OpenGL-related is changed.
        update();
        updatePerspectiveTest();

        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        p.render(); //draws the normal cube, the "player"

        if (perspective)
        {
            perspective();
        }

        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        loadRotationalMatrix(yaw,pitch,roll);
        loadPositionalMatrix(-y,-x,-z);

        env.render(); //draws the distorted cube, the "environment"

        GL11.glLoadIdentity();

        ortho();

        //render debug data
}


//resets to orthographic projection
public static void ortho()
{
        int i = GL11.glGetInteger(GL11.GL_MATRIX_MODE);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(-OpenGL.WIDTH/2, OpenGL.WIDTH/2, 
                OpenGL.HEIGHT/2, -OpenGL.HEIGHT/2, OpenGL.DEPTH/2, -OpenGL.DEPTH/2);
        GL11.glMatrixMode(i);
}

This is the function that switches perspective on. This is probably where the problem is.

//activate perspective projection. Maybe something wrong here?
public static void perspective()
{
    int i = GL11.glGetInteger(GL11.GL_MATRIX_MODE);
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glFrustum(-OpenGL.WIDTH/2, OpenGL.WIDTH/2, 
                OpenGL.HEIGHT/2, -OpenGL.HEIGHT/2, 
                pzNear, pzFar);
    GL11.glMatrixMode(i);
}

These functions are used for transformations. They may not be relevant, but I figured it was good to include them.

//translates the camera via matrix multiplication.
public static void loadPositionalMatrix(float x, float y, float z)
{
    FloatBuffer m = BufferUtils.createFloatBuffer(16);

    m.put(new float[]
            {
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            x, y, z, 1
            });

    m.flip();

    GL11.glMultMatrix(m);
}

    //sets yaw, pitch and roll using rotational matrices. Not really being used now.
public static void loadRotationalMatrix(double pitch, double yaw, double roll)
{
        FloatBuffer Ry = BufferUtils.createFloatBuffer(16);
        FloatBuffer Rx = BufferUtils.createFloatBuffer(16);
        FloatBuffer Rz = BufferUtils.createFloatBuffer(16);

        Rx.put(new float[]
                {
                1, 0, 0, 0,
                0, (float) cos(pitch), (float) sin(pitch), 0,
                0, (float) -sin(pitch), (float) cos(pitch), 0,
                0, 0, 0, 1
                });
        Ry.put(new float[]
                {
                (float) cos(yaw), 0, (float) -sin(yaw),  0,
                0, 1, 0, 0,
                (float) sin(yaw), 0, (float) cos(yaw), 0,
                0, 0, 0, 1
                });
        Rz.put(new float[]
                {
                (float) cos(roll), (float) sin(roll), 0, 0,
                (float) -sin(roll), (float) cos(roll), 0, 0, 
                0, 0, 1, 0,
                0, 0, 0, 1
                });

        Rx.flip();
        Ry.flip();
        Rz.flip();

        GL11.glMultMatrix(Rz);
        GL11.glMultMatrix(Ry);
        GL11.glMultMatrix(Rx);
    }

I've played around with zNear and zFar, but no values that I have tried so far result in correct projection.

The specific question is this: why does the second cube appear so hideously distorted, and what can I do to correct it?

回答1:

The clipping planes are used by the matrix to project from the 3D environment to your screen. So the x and y clipping planes need to account for the aspect ratio of your window, and the FOV. So you should have something like this:

...
double fovyInDegrees = 50; // Change this if you want.
double ymax, xmax, aspectRatio;
aspectRatio = OpenGL.WIDTH / OpenGL.HEIGHT;
ymax = znear * Math.tan(fovyInDegrees * Math.PI / 360);
xmax = ymax * aspectRatio;
GL11.glFrustum(-xmax, xmax, -ymax, ymax, znear, zfar);
...

Note that I've just changed some of my c++ code to Java, so this isn't tested. It works in my code though, so should be fine.