簡體   English   中英

在quad的Opengl紋理

[英]Opengl texture on quad

這是我的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%正確的(我檢查過)。 但我仍然得到一個白色四邊形。

注意:我使用這個 png解碼器。

編輯:我打印出每4個字節的浮動到控制台,我得到0.00.00.00.0 .... Doest意味着紋理加載不明確,或信息以不同的格式存儲到緩沖區?

您的片段着色器看起來不對 - 您設置了白色並從紋理中添加了值,因此它會夾到白色。 只是做更像這樣的事情

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM