渲染纹理四边形不工作

时间:2015-08-08 17:17:01

标签: java opengl 2d lwjgl texture2d

所以,我正在使用lwjgl制作2D游戏,并且渲染纹理四边形不会起作用。 我有3个纹理,名称是:

DirtTexture.png, GrassTexture.png, WaterTexture.png

所有这些都位于" res"封装

我的代码是:

    public static void DrawQuadTex(Texture tex, float x, float y, float width, float height) {

    tex.bind();
    glTranslatef(x, y, 0);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(width, 0);
    glTexCoord2f(1, 1);
    glVertex2f(width, height);
    glTexCoord2f(0, 1);
    glVertex2f(0, height);
    glLoadIdentity();
    glEnd();

}

public static Texture LoadTexture(String path, String fileType) {

    Texture tex = null;
    InputStream in = ResourceLoader.getResourceAsStream(path);
    try {
        tex = TextureLoader.getTexture(fileType, in);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return tex;

}

它的名字是这样的:

public class Boot {

public Boot() {

    BeginSession();

    Texture t = LoadTexture("res/GrassTexture.png", "PNG");
    while(!Display.isCloseRequested()) {

        DrawQuadTex(t, 0, 0, 64, 64);

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

    }

    Display.destroy();

}

    public static void main(String[] args) {
        new Boot();
    }

}

我的问题是它呈现白色纹理,即使我选择的纹理不是白色。 谁知道为什么?谢谢:))

2 个答案:

答案 0 :(得分:0)

我不知道为什么,但同时显示2张图片解决了问题...

答案 1 :(得分:0)

您可能希望在使用前清除屏幕:

glClear(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT);

tex.bind();
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
...