0

我正在尝试了解顶点缓冲区对象。我已经能够在 2D 空间中成功渲染四边形(glOrtho在初始化期间使用),但是在使用gluPerspective.

我正在使用 Java 和 LWJGL,并在下面附上了我的代码。目前,只有黑色透明渲染到窗口。

public class GameWindow {
    // Height and width of the viewport
    private final int WIDTH = 800;
    private final int HEIGHT = 600;

    private final float zNear = 1f;
    private final float zFar = 1000f;

    long lastFrame, lastFps;
    int fps;

    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90, WIDTH / HEIGHT, zNear, zFar);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        float[] quadCoords = {
                100, 100, -1,
                300, 100, -1,
                300, 300, -1,
                100, 300, -1
        };

        float[] colorCoords = {
                1, 0, 0,
                0, 1, 0,
                0, 0, 1,
                1, 0, 1
        };

        FloatBuffer vertexData = BufferUtils.createFloatBuffer(4 * 3);
        vertexData.put(quadCoords);
        vertexData.flip();

        FloatBuffer colorData = BufferUtils.createFloatBuffer(4 * 3);
        colorData.put(colorCoords);
        colorData.flip();

        int vboVertex = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
        glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        int vboColor = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboColor);
        glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        getDelta();
        lastFps = TimeUtil.getTime();

        while (!Display.isCloseRequested()) {
            int delta = getDelta();
            updateFPS();
            glClear(GL_COLOR_BUFFER_BIT);

            glBindBuffer(GL_ARRAY_BUFFER, vboVertex);
            glVertexPointer(3, GL_FLOAT, 0, 0L);

            glBindBuffer(GL_ARRAY_BUFFER, vboColor);
            glColorPointer(3, GL_FLOAT, 0, 0L);

            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_COLOR_ARRAY);
            glDrawArrays(GL_QUADS, 0, 4);
            glDisableClientState(GL_COLOR_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);

            Display.update();
            Display.sync(60);
        }
        Display.destroy();
    }

    private int getDelta() {
        long time = TimeUtil.getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;

        return delta;
    }

    public void updateFPS() {
        if (TimeUtil.getTime() - lastFps > 1000) {
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFps += 1000;
        }
        fps++;
    }

    public static void main(String[] args) {
        GameWindow window = new GameWindow();
        window.start();
    }
}
4

2 回答 2

1

我认为您的四边形的顶点不在视野范围内。在不设置视图矩阵的情况下(例如通过 gluLookAt),相机指向 -Z 轴。您要求 zNear 和 zFar 为 1.0 和 1000.0,因此将绘制 z 坐标 -1.0 和 -1000.0 之间的所有内容(zNear 和 zFar 类似于到相机的“距离”)。您还必须考虑到透视投影与正交投影确实不同。我会说,尝试将您的顶点移回 Z 轴,如下所示:

float[] quadCoords = {
        100, 100, -500,
        300, 100, -500,
        300, 300, -500,
        100, 300, -500
};
于 2013-07-08T04:22:24.210 回答
0

您的近剪裁平面为 1,但您将所有 z 坐标渲染为 -1

于 2013-12-17T15:08:11.557 回答