【问题标题】:Rendering a textured quad not working渲染带纹理的四边形不起作用
【发布时间】:2015-11-01 00:14:24
【问题描述】:

所以,我正在使用 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();
    }

}

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

【问题讨论】:

    标签: java opengl 2d lwjgl texture2d


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可能想在绘图前清除屏幕:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 1970-01-01
        • 2013-08-14
        • 1970-01-01
        相关资源
        最近更新 更多