0

これは私の 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 バイトごとに float を出力したところ、0.00.00.00.0 が得られました。これは、テクスチャが正しく読み込まれていないか、情報が別の形式でバッファに格納されていることを意味しますか?

4

1 に答える 1

2

フラグメント シェーダーが正しくないように見えます。白色を設定し、テクスチャから値を追加すると、白色に固定されます。もっとこのようなことをしてください

void main() {
    outputColor.a = 1.0f;
    outputColor.rgb = texture2D(texture_diffuse, textureCoordinate);
}
于 2013-03-17T10:46:26.610 に答える