【问题标题】:OpenGL ES 2 Textures rendering blackOpenGL ES 2 纹理呈现黑色
【发布时间】:2014-08-10 17:05:34
【问题描述】:

我正在努力在由 2 个三角形组成的四边形上渲染 2d 纹理。但是,即使我将纹理和纹理坐标传递给着色器,它也会渲染为黑色。非常感谢任何帮助!

渲染函数:

public void render(float[] mvpMatrix) {
    // Add program to OpenGL environment.
    GLES20.glUseProgram(mGLProgram);

    // Get handle to vertex shader's aPosition member
    // and enable a handle to the triangle vertices.
    int mPositionHandle = GLES20.glGetAttribLocation(mGLProgram, "aPosition");
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the vertex coordinate data.
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false, vertexStride, vertexBuffer
    );

    // Pass the texture coordinates.
    int mTextureCoordinateHandle = GLES20.glGetAttribLocation(mGLProgram, "aTexCoordinate");
    GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);

    // Prepare the uv coordinate data.
    GLES20.glVertexAttribPointer(mTextureCoordinateHandle, UV_SIZE,
            GLES20.GL_FLOAT, false, 0, uvBuffer
    );

    // Get handle to fragment shader's aColor member and set color for drawing the triangle.
    int mColorHandle = GLES20.glGetUniformLocation(mGLProgram, "uColor");
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // Tell the texture uniform sampler to use this texture
    // in the shader by binding to texture unit 0.
    int mTextureUniformHandle = GLES20.glGetUniformLocation(mGLProgram, "uTexture");
    GLES20.glUniform1i(mTextureUniformHandle, 0);

    // Get handle to shape's transformation matrix.
    int mMVPMatrixHandle = GLES20.glGetUniformLocation(mGLProgram, "uMVPMatrix");
    GLError.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation.
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    GLError.checkGlError("glUniformMatrix4fv");

    // Set the active texture unit to texture unit 0.
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

    // Bind the texture to this unit.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);

    // Render the sprite.
    GLES20.glDrawElements(
            GLES20.GL_TRIANGLES, indices.length,
            GLES20.GL_UNSIGNED_SHORT, indiceBuffer
    );

    // Disable vertex array.
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mTextureCoordinateHandle);
}

UV 和索引:

private float uvs[] = {
        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f
};

private short indices[] = {
        0, 1, 2, 0, 2, 3
};

创建 UV 字节缓冲区:

// Initialize byte buffer for the uvs.
ByteBuffer ub = ByteBuffer.allocateDirect(
        // Number of uv values * 4 bytes per float.
        uvs.length * 4
);

ub.order(ByteOrder.nativeOrder());
uvBuffer = ub.asFloatBuffer();
uvBuffer.put(uvs);
uvBuffer.position(0);

纹理加载器:

public static int loadTexture(Context context, final int id, final int resource) {
    final int[] texture = new int[1];

    GLES20.glGenTextures(id, texture, 0);

    if (texture[0] == 0) {
        throw new RuntimeException("Error loading texture bro. " + texture[0]);
    }

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;

    // Decode the bitmap automagically.
    final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resource, options);

    // Bind the texture as texture 2d.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[0]);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle();

    return texture[0];
}

顶点着色器:

uniform mat4 uMVPMatrix;

attribute vec2 aTexCoordinate;
attribute vec4 aPosition;

varying vec2 vTexCoordinate;

void main() {
    // Pass the texture coordinate.
    vTexCoordinate = aTexCoordinate;

    // Determine the position of the render.
    gl_Position = uMVPMatrix * aPosition;
}

片段着色器:

precision mediump float;

uniform sampler2D uTexture;
uniform vec4 uColor;

varying vec2 vTexCoordinate;

void main() {
    gl_FragColor = texture2D(uTexture, vTexCoordinate);
}

【问题讨论】:

    标签: java android opengl-es opengl-es-2.0 texture2d


    【解决方案1】:

    哎呀,我的错误!由于某种原因,UV_COORD 常数是 1 而不是 2。花 4 小时的好方法!

    【讨论】:

    • 您发布的代码中的任何地方都没有使用UV_COORD 常量。
    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多