为什么我不能从OpenGL中的图像文件加载矩形?

时间:2012-06-28 03:24:30

标签: java opengl lwjgl

每次我的代码在我的关卡图像中遇到白色像素时,我都会制作一些代码来生成方框。但是,它似乎不起作用。它只有在我注释掉图像部分的实际加载时才有效。

        GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(-5, 5, -5, 5, -20, 20);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    float camPosX=0, camPosY=0;
    for(int i=0;i<level.getWidth();i++){
        for(int j=0;j<level.getHeight();j++){
            if(level.getRGB(i, j)==Color.red.getRGB()){camPosX=i;camPosY=j;}
        }
    }
    System.out.println("Camera position is "+camPosX+", "+camPosY);
    int x=0;
    while (!Display.isCloseRequested()) {
        Display.sync(60);
        //poll for keypresses first, default key is 'forward'
        if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD8));



        GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT|GL11.GL_COLOR_BUFFER_BIT);
        //GL11.glEnable(GL11.GL_CULL_FACE);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glColor3f(0, 0, 0);
        for(int i=0;i<level.getWidth();i++){
            for(int j=0;j<level.getHeight();j++){
                if(level.getRGB(i, j)==Color.WHITE.getRGB()){
                    GL11.glTranslatef(-i, 0, -j);
                    GL11.glRotatef(45, 1, 1, 1);
                    GL11.glBegin(GL11.GL_QUADS);
                    GL11.glColor3f(1.0f,0.0f,0.0f);          // Set The Color To Red
                    GL11.glVertex3f( 0.5f, 0.5f, 0.5f);          // Top Right Of The Quad (Front)
                    GL11.glVertex3f(-0.5f, 0.5f, 0.5f);          // Top Left Of The Quad (Front)
                    GL11.glVertex3f(-0.5f,-0.5f, 0.5f);          // Bottom Left Of The Quad (Front)
                    GL11.glVertex3f( 0.5f,-0.5f, 0.5f);          // Bottom Right Of The Quad (Front)
                    GL11.glVertex3f( 0.5f,-0.5f,-0.5f);          // Bottom Left Of The Quad (Back)
                    GL11.glVertex3f(-0.5f,-0.5f,-0.5f);          // Bottom Right Of The Quad (Back)
                    GL11.glVertex3f(-0.5f, 0.5f,-0.5f);          // Top Right Of The Quad (Back)
                    GL11.glVertex3f( 0.5f, 0.5f,-0.5f);          // Top Left Of The Quad (Back)
                    GL11.glVertex3f(-0.5f, 0.5f, 0.5f);          // Top Right Of The Quad (Left)
                    GL11.glVertex3f(-0.5f, 0.5f,-0.5f);          // Top Left Of The Quad (Left)
                    GL11.glVertex3f(-0.5f,-0.5f,-0.5f);          // Bottom Left Of The Quad (Left)
                    GL11.glVertex3f(-0.5f,-0.5f, 0.5f);          // Bottom Right Of The Quad (Left)
                    GL11.glVertex3f( 0.5f, 0.5f,-0.5f);          // Top Right Of The Quad (Right)
                    GL11.glVertex3f( 0.5f, 0.5f, 0.5f);          // Top Left Of The Quad (Right)
                    GL11.glVertex3f( 0.5f,-0.5f, 0.5f);          // Bottom Left Of The Quad (Right)
                    GL11.glVertex3f( 0.5f,-0.5f,-0.5f);          // Bottom Right Of The Quad (Right)
                    GL11.glEnd();
                    GL11.glLoadIdentity();
                }
            }
        }
        GL11.glTranslatef(-camPosX, 0, -camPosY);
        Display.update();
    }

1 个答案:

答案 0 :(得分:0)

在生成框的主循环中,您可能想要

...
if(level.GetRGB(i, j)==Color.WHITE.getRGB()) {
    GL11.glPushMatrix(); // store current matrix
    GL11.glTranslatef(-i, 0, -j);
    ...
    GL11.glPopMatrix(); // instead of GL11.glLoadIdentity()
}

该行

GL11.glTranslatef(-camPosX, 0, -camPosY);

可能不是你认为的那样。它对以下调用Display.update()没有任何影响。但是, 会影响主循环的下一次运行,因为您不会在主循环开始时调用GL11.glLoadIdentity()。我建议将它移到主循环的开头并在之前调用glLoadIdentity()