在LWJGL中渲染纹理四边形渲染白色?

时间:2016-08-28 07:51:05

标签: java textures lwjgl

我正在尝试将纹理四边形渲染到屏幕上,作为我的新游戏的开始菜单按钮。但是当渲染它只是渲染为白色四边形时,我已经在互联网上搜索了几天,而我还没有找到解决问题的单一答案。

我的纹理是wood.png,它位于" res"项目中资源源文件夹内的文件夹。它是一个128 * 128像素的图像。

渲染纹理的代码如下:

public static void renderTexture(Texture texture, float width, float height, float x, float y) {

    texture.bind();

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    texture.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();

    glDisable(GL_BLEND);
}

我用来加载纹理的代码是:

    public static Texture loadTexture(String fileName){
    try {
        Texture texture = TextureLoader.getTexture("PNG",Class.class.getResourceAsStream("/res/"+fileName+".png"));
        return texture;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

我有一个静态纹理存储private static Texture startTex = Loader.loadTexture("wood");,我通过执行以下每帧渲染:

RenderSystem.renderTexture(startTex, 200, 200, 0, 0);

1 个答案:

答案 0 :(得分:1)

答案是我做glBegin(GL_TEXTURE_2D);而不是glEnable(GL_TEXTURE_2D); 抱歉,该部分的代码未显示。