-1

私は問題を修正しました。以下は私が行った方法です。

RenderEngine のバインディング コード:

public int bindTexture(String location)
{
    BufferedImage texture;
    File il = new File(location);

    if(textureMap.containsKey(location))
    {
        glBindTexture(GL_TEXTURE_2D, textureMap.get(location));
        return textureMap.get(location);
    }

    try 
    {
        texture = ImageIO.read(il); 
    }
    catch(Exception e)
    {
        texture = missingTexture;
    }

    try
    {
        int i = glGenTextures();
        ByteBuffer buffer = BufferUtils.createByteBuffer(texture.getWidth() * texture.getHeight() * 4);
        Decoder.decodePNGFileToBuffer(buffer, texture);
        glBindTexture(GL_TEXTURE_2D, i);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.getWidth(), texture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
        textureMap.put(location, i);
        return i;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    return 0;
}

PNG デコーダーの方法:

public static void decodePNGFileToBuffer(ByteBuffer buffer, BufferedImage image)
{
    int[] pixels = new int[image.getWidth() * image.getHeight()];
    image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

    for(int y = 0; y < image.getHeight(); y++)
    {
        for(int x = 0; x < image.getWidth(); x++)
        {
            int pixel = pixels[y * image.getWidth() + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF));
            buffer.put((byte) ((pixel >> 8) & 0xFF));
            buffer.put((byte) (pixel & 0xFF)); 
            buffer.put((byte) ((pixel >> 24) & 0xFF));
        }
    }

    buffer.flip();
}

これが同じ問題を抱えている人に役立つことを願ってtextureMapいます

4

1 に答える 1

2

完全に順番を間違えています。必要がある:

  1. glGenTextures でテクスチャ名/ID を生成します – その ID を変数に保存します
  2. glBindTexture を使用してその ID をバインドします
  3. その場合のみ、glTexImage でデータをアップロードできます

描画コードでは、テクスチャ ロード全体を呼び出していますが、これは非効率的です。また、毎回新しいテクスチャ名を再作成しています。マップを使用してテクスチャ ファイル名を ID にマップします。ID が割り当てられていない場合にのみ、テクスチャを Gen/Bind/TexImage します。それ以外の場合は、バインドするだけです。

于 2012-04-12T09:57:08.070 に答える