如何使用透视投影矩阵?

时间:2013-04-09 08:49:43

标签: java opengl lwjgl

我编写了这个程序,但是我想让正方形随着它移动得更远而改变尺寸。

package com.ncom.src;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.util.glu.GLU.*;

import static org.lwjgl.opengl.GL11.*;

public class Main {
    public void start() {
        float y = 0;
        try {
            Display.setDisplayMode(new DisplayMode(800,600));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
        while (!Display.isCloseRequested()) {
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(0, 800, 600, 0, 500000000, -500000000);
            glMatrixMode(GL_MODELVIEW);
            glClear(GL_COLOR_BUFFER_BIT);
            glBegin(GL_QUADS);
                glVertex3f(350, 250, y);
                glVertex3f(350, 270, y);
                glVertex3f(370, 270, y);
                glVertex3f(370, 250, y);
            glEnd();
            Display.update();
            y -= 20;
        }

        Display.destroy();
        System.exit(0);
    }
    public static void main(String[] argv) {
        Main quadExample = new Main();
        quadExample.start();
    }
 }

2 个答案:

答案 0 :(得分:4)

代码glOrtho(0, 800, 600, 0, 500000000, -500000000);用于正交视图,请尝试使用GLU.gluPerspective()。例如:

GL11.glMatrixMode(GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(field_of_vision, Display.getWidth()/Display.getHeight(), zNear, zFar);
GL11.glMatrixMode(GL_MODELVIEW);
GL11.glLoadIdentity();

答案 1 :(得分:3)

如果你想看透视图,那么我首先要使用透视投影。

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        near = 1; // near should be chosen as far into the scene as possible
        far  = 100;
        fov  = 1; // 1 gives you a 90° field of view. It's tan(fov_angle)/2.
        glFrustum(-aspect*near*fov, aspect*near*fov, -fov, fov, near, far);
相关问题