上四

2013-03-17 43 views
0

這Opengl的紋理是我的Texture類的內容:上四

public int id; 

public Texture(InputStream inputStream) { 
    ByteBuffer buf = null; 
    int tWidth = 0; 
    int tHeight = 0; 

    try { 
     PNGDecoder decoder = new PNGDecoder(inputStream); 
     buf = ByteBuffer.allocateDirect(4*decoder.getWidth()*decoder.getHeight()); 
     decoder.decode(buf, decoder.getWidth()*4, PNGDecoder.TextureFormat.RGBA); 
     buf.rewind(); 
     inputStream.close(); 
    } catch (IOException exception) { 
     ErrorHandler.handleError("Failed to load image", exception); 
    } 

    id = glGenTextures(); 
    glActiveTexture(id); 
    glBindTexture(GL_TEXTURE_2D, id); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tWidth, tHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); 

    glBindTexture(GL_TEXTURE_2D, 0); 
} 

這是我如何渲染:

glActiveTexture(background.id); 
    glBindTexture(GL_TEXTURE_2D, background.id); 

    glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle); 
    glEnableVertexAttribArray(0); 
    glEnableVertexAttribArray(1); 

    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); 
    glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 4*18); 

    glDrawArrays(GL_TRIANGLES, 0, 6); 

    glDisableVertexAttribArray(0); 
    glDisableVertexAttribArray(1); 

    glBindTexture(GL_TEXTURE_2D, 0); 

,這是片段着色器:

#version 330 

in vec2 textureCoordinate; 

out vec4 outputColor; 

uniform sampler2D texture_diffuse; 

void main() { 
    outputColor.rgb = vec3(1.0f, 1.0f, 1.0f); 
    outputColor += texture2D(texture_diffuse, textureCoordinate); 
} 

我做錯了什麼?傳遞給着色器程序的紋理座標是100%正確的(我檢查過)。但我仍然得到一個白色的四。

注:我使用this png解碼器。

編輯: 我打印出浮筒,每4個字節到控制檯,並且我得到0.00.00.00.0 ....合乎理是否意味着紋理incorectly加載,或者所述信息被存儲到緩衝區採用不同的格式?

回答

2

你的片段着色器看起來不對 - 你設置一個白色並從紋理中添加值,所以它會鉗制成白色。只是做更多這樣的事情

void main() { 
    outputColor.a = 1.0f; 
    outputColor.rgb = texture2D(texture_diffuse, textureCoordinate); 
} 
+0

我不這麼認爲。我的片段着色器將所有三角形的顏色設置爲白色,然後在其上添加紋理。多次嘗試,一切都很好。無論如何,我試着確保你的代碼,並且我有一個後臺屏幕。這是因爲四核是絕對透明的。 – Qualphey 2013-03-17 10:52:17

+0

我猜紋理初始化代碼是錯誤的 – Qualphey 2013-03-17 10:53:21

+0

請參閱編輯的代碼 - 更好嗎? – 2013-03-17 10:54:08