LWJGL什么也没渲染

时间:2016-07-09 15:04:34

标签: java opengl graphics rendering lwjgl

我一遍又一遍地查看下面的程序,无法找到为什么在运行时没有渲染到屏幕上的原因:

Game.java:

public class Game {

    private int fbid, fbid2;

    public void start(){
        DisplayManager.init();

        glViewport(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT, 1, -1);

        glMatrixMode(GL_MODELVIEW);

        FloatBuffer fb = BufferUtils.createFloatBuffer(6);
        fb.put(100).put(100).put(100).put(200).put(200).put(200);
        fb.flip();

        fbid = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, fbid);
        glBufferData(GL_ARRAY_BUFFER, fb, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        FloatBuffer fb2 = BufferUtils.createFloatBuffer(9);
        fb2.put(1).put(1).put(1).put(1).put(1).put(1).put(1).put(1).put(1);
        fb2.flip();

        fbid2 = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, fbid2);
        glBufferData(GL_ARRAY_BUFFER, fb2, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        while(!DisplayManager.shouldClose()){
            render();
        }

        DisplayManager.destroy();
    }

    public void render(){
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0, 0, 0, 0);

        glColor3f(1, 1, 1);

        glEnableClientState(GL_VERTEX_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, fbid);
        glVertexPointer(2, GL_FLOAT, 0, 0);

        glEnableClientState(GL_COLOR_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, fbid2);
        glColorPointer(3, GL_FLOAT, 0, 0);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);

        DisplayManager.update();
    }

}

DisplayManager.java:

public class DisplayManager {

    public static final int SCR_WIDTH = 640;
    public static final int SCR_HEIGHT = 480;

    public static final int FPS = 30;

    public static void init(){
        try {
            Display.setDisplayMode(new DisplayMode(SCR_WIDTH, SCR_HEIGHT));
            Display.setTitle("StepBattler");
            Display.setResizable(false);
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            Display.destroy();
            System.exit(1);
        }
    }

    public static void update(){
        Display.update();
        Display.sync(FPS);
    }

    public static void destroy(){
        Display.destroy();
        System.exit(0);
    }

    public static boolean shouldClose(){
        return Display.isCloseRequested();
    }

    public static boolean shouldResize(){
        return Display.wasResized();
    }

}

当我运行屏幕时,我只得到一个充满黑色的窗口,我无法理解为什么白色三角形不会渲染到窗口。我和glClearColor()混淆了,改变它的值确实成功地改变了窗口背景颜色,但是我不能在窗口中出现白色三角形。此外,我尝试使用glBegin()...glEnd()以立即模式呈现,没有任何代码与缓冲区相关,并且它仍然没有呈现任何内容。我真的很困惑,我错过了什么?

1 个答案:

答案 0 :(得分:2)

glOrtho()调用中的参数看起来顺序错误:

glOrtho(0, 0, DisplayManager.SCR_WIDTH, DisplayManager.SCR_HEIGHT, 1, -1);

基于man pageglOrtho()签名是:

void glOrtho(GLdouble left, GLdouble right,
             GLdouble bottom, GLdouble top,
             GLdouble nearVal,  GLdouble farVal);

由于您为传递0,结果将是无效的投影矩阵。

您的案件的正确电话是:

glOrtho(0, DisplayManager.SCR_WIDTH, 0, DisplayManager.SCR_HEIGHT, 1, -1);