【问题标题】:LWJGL rendering nothingLWJGL 无渲染
【发布时间】:2016-07-09 15:04:33
【问题描述】:

我一遍又一遍地查看以下程序,但找不到为什么运行它时没有任何内容呈现在屏幕上:

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() 以立即模式呈现,其中没有任何代码与缓冲区有关,它仍然没有呈现任何内容。我真的很困惑,我错过了什么?

【问题讨论】:

    标签: java opengl graphics rendering lwjgl


    【解决方案1】:

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

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

    glOrtho()的签名,基于man page,是:

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

    由于您为 left 和 right 都传递了0,因此结果将是无效的投影矩阵。

    您的案例的正确调用是:

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

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多