2012-04-12 11 views
-1

を結合しない、私は問題を解決し、以下のいる私はそれをやった方法です、OpenGLは私のテクスチャ

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(); 
} 

私はこれが同じ問題の誰かに役立つことを願っています PS textureMapは、文字列をキーとし、整数を値として持つHas​​hMapです。

答えて

2

完全に間違っています。以下を行う必要があります。

  1. がglGenTexturesとテクスチャ名/ IDを生成 - 任意のだけ、あなたは
glTexImageにデータをアップロードすることができglBindTexture
  • 使用して変数
  • バインドでそのIDをそのIDを保存します

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

  • +0

    いいえ、それは動作しませんでした –

    +0

    まあ、テクスチャリングも有効にする必要があります。 glEnable(GL_TEXTURE_2D) - glBeginの直前に呼び出します – datenwolf

    関連する問題