2015-10-07 120 views
1

我有使用PNG圖片紋理的草地模型。並且我得到白色背景顏色而不是黑色,我想要。爲何如此,我該如何解決這個問題?我使用LWJGL 3PNGDecoder.jarLWJGL png紋理透明(textureColour.a白色而不是黑色)

紋理加載器代碼:

public int loadTexture(String fileName) { 
    ByteBuffer buf = null; 
    int tWidth = 0; 
    int tHeight = 0; 

    try { 
     // Open the PNG file as an InputStream 
     InputStream in = new FileInputStream("res/" + fileName + ".png"); 
     // Link the PNG decoder to this stream 
     PNGDecoder decoder = new PNGDecoder(in); 

     // Get the width and height of the texture 
     tWidth = decoder.getWidth(); 
     tHeight = decoder.getHeight(); 

     // Decode the PNG file in a ByteBuffer 
     buf = ByteBuffer.allocateDirect(
       4 * decoder.getWidth() * decoder.getHeight()); 
     decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA); 
     buf.flip(); 

     in.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
    } 

    // Create a new texture object in memory and bind it 
    int textureId = GL11.glGenTextures(); 
    GL13.glActiveTexture(textureId); 
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); 

    // All RGB bytes are aligned to each other and each component is 1 byte 
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); 

    // Upload the texture data and generate mip maps (for scaling) 
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
      GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); 
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); 

    // Setup the ST coordinate system 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); 

    // Setup what to do when the texture has to be scaled 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, 
      GL11.GL_NEAREST); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, 
      GL11.GL_LINEAR_MIPMAP_LINEAR); 

    return textureId; 
} 
+1

我們可以看一下你的着色器,我們可以有一個下載的圖片?請記住,您從網站上下載的ThinMatrix圖像實際上並不透明。此外,你是否嘗試過檢查R G和B值是否是100%白色並使其透明? – Joehot200

回答

1

如果你想透明質感看起來透明,您必須啓用第一混合。把下面的代碼放在你的OpenGL初始化代碼中。

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

你也在內部存儲你的紋理在RGB,而不是RGBA格式。

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); 

應該成爲

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, tWidth, tHeight, 0, 
    GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); 
+0

它不工作..我想在stder上設置shader。如: vec4 textureColour = texture(modelTexture,pass_textureCoords);如果(textureColour.a <0.5){ }丟棄; } 但我真的不知道爲什麼背景是白色的,而不是黑色。 –

+1

爲什麼你想要草地有黑色的背景?或者你的意思是透明的,所以你可以通過它看到其他物體? – javac

+0

我在下面的教程(https://www.youtube.com/watch?v=ZyzXBYVvjsg&index=15&list=PLRIWtICgwaX0u7Rf9zkZhLoLuZVfUksDP)。他正在使用lwjgl2,我是lwjgl3。所以我在代碼上有一些區別。這是問題,因爲他有背景黑色和白色。賴利不知道爲什麼。我認爲這可能是由我自己編寫的loadTexture方法造成的。在2:05你可以看到我想要的。 –

相關問題