【问题标题】:LWJGL texture renders plain whiteLWJGL 纹理呈现纯白色
【发布时间】:2013-12-27 23:50:00
【问题描述】:

嘿嘿,

这个问题真的很烦……

我正在尝试使用 LWJGL 创建一个窗口并加载和渲染纹理。

Window-Creation 完美运行,应该正确加载纹理(经过测试,它始终是我想要渲染的纹理),但它会呈现纯白色。

初始化 OpenGL,加载纹理并渲染它:

    private void initGL(int width, int height)
{
    try
    {
        searchDisplayMode(width, height, true);
        Display.create();
        Display.setVSyncEnabled(true);
    } catch (LWJGLException e)
    {
        e.printStackTrace();
        System.exit(0);
    }      
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glViewport(0,0,width,height);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, width, height, 0, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    GL11.glEnable(GL11.GL_TEXTURE_2D); 
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

public void loadTextures()
{
    try
    {
        DirtID = textureManager.registerTexture("Dirt.png");
        System.out.println(DirtID);
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

public void render()
{
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    Color.white.bind();

    // This was the bug! [Fixed it now]
    textureManager.bindTexture(DirtID);

    GL11.glBegin(GL11.GL_QUADS);

    GL11.glTexCoord2f(0, 0);
    GL11.glVertex2f(0, 0);
    GL11.glTexCoord2f(1, 0);
    GL11.glVertex2f(0 + textureManager.getWidth(DirtID), 0);
    GL11.glTexCoord2f(1, 1);
    GL11.glVertex2f(0 + textureManager.getWidth(DirtID), 0 + textureManager.getHeight(DirtID));
    GL11.glTexCoord2f(0, 1);
    GL11.glVertex2f(0, 0 + textureManager.getHeight(DirtID));
    GL11.glEnd();

}

TextureManager.java 中的加载功能 + 其他内容:

public TextureManager()
{
    textures = new ArrayList<TextureIO>();
}

public int registerTexture(String file) throws IOException
{
    return registerTexture(file, false);
}

public int registerTexture(String file, boolean isFlipped) throws IOException
{
    for (TextureIO t : textures)
    {
        if (t.getName().equals(file))
        {
            return t.getId();
        }
    }
    if (isFlipped)
    {
        System.out.println("Register flipped Texture ~* " + file);
    } else
    {
        System.out.println("Register Texture ~* " + file);
    }
    TextureIO texture = new TextureIO(file, isFlipped);
    texture.init();
    textures.add(texture);
    return texture.getId();
}

public void bindTexture(int textureId)
{
    if (boundedTexture != textureId)
    {
        glBindTexture(GL_TEXTURE_2D, textureId);
        boundedTexture = textureId;
        System.out.println("The texture " + boundedTexture + " was bound");
    }
}

public void unbindTexture()
{
    glBindTexture(GL_TEXTURE_2D, 0);
    boundedTexture = 0;
}

public TextureIO getTexture(int textureId)
{
    for (TextureIO tex: textures)
    {
        if (tex.getId() == textureId)
        {
            return tex;
        }
    }
    return null;
}  

以及TextureManager使用的TextureIO.java:

public TextureIO(String ref, boolean flipped) throws IOException
{
    BufferedImage image = ImageIO.read(TextureIO.class.getResourceAsStream("graphics/" + ref));
    width = image.getWidth();
    height = image.getHeight();
    name = ref;

    isFlipped = flipped;

    if (flipped)
    {
            final AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
            tx.translate(0, -height);

            final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            image = op.filter(image, null);
    }

    final int[] pixels = image.getRGB(0, 0, width, height, null, 0, width);
    texture = BufferUtils.createIntBuffer(pixels.length);
    texture.put(pixels);
    texture.rewind();
}

public String getName()
{
    return name;
}

public void init()
{
    final IntBuffer buffer = BufferUtils.createIntBuffer(1);
    glGenTextures(buffer);
    id = buffer.get(0);

    glBindTexture(GL_TEXTURE_2D, id);
    /*
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);*/
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL12.GL_BGRA, GL_UNSIGNED_BYTE /*GL_UNSIGNED_INT_8_8_8_8_REV*/, texture);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDisable(GL_TEXTURE_2D);
}

public void bind()
{
    glBindTexture(GL_TEXTURE_2D, id);
}

public void unbind()
{
    glBindTexture(GL_TEXTURE_2D, 0);
}

结果:

也许你可以帮我解决这个问题。

【问题讨论】:

    标签: java opengl lwjgl deprecated opengl-1.x


    【解决方案1】:

    您在绘图时忘记启用GL_TEXTURE_2D。由于您在纹理加载完成后禁用它。

    而且你必须在开始渲染四边形之前绑定纹理。

    【讨论】:

    • 在渲染函数中添加了它,但它仍然渲染白色而不是纹理。 :我
    • 这是导致我的问题的原因:我在开始渲染四边形后绑定了纹理。我上面发布的代码现在是固定的工作版本。感谢 Martijn Courteaux 的帮助!
    • 同样的问题,修复得很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 2013-07-03
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多