因此,我尝试学习openGLES 2.0并创建带纹理的矩形。显然,我并没有按照所有说明进行操作,但是现在我得到的只是一个奇怪的颜色方块。

这是我的着色器。

final String vertexShader =
        "uniform mat4 u_MVPMatrix;      \n"     // A constant representing the combined model/view/projection matrix.
        + "attribute vec2 a_TexCoordinate;\n" // Per-vertex texture coordinate information we will pass in.
        + "attribute vec4 a_Position;     \n"     // Per-vertex position information we will pass in.
        //          + "attribute vec4 a_Color;        \n"     // Per-vertex color information we will pass in.

        //          + "varying vec4 v_Color;          \n"     // This will be passed into the fragment shader.
        + "varying vec2 v_TexCoordinate;     \n"   // This will be passed into the fragment shader.
        + "void main()                    \n"     // The entry point for our vertex shader.
        + "{                              \n"
        + "   v_TexCoordinate = a_TexCoordinate;\n"     // Pass the texture coordinate through to the fragment shader.
        // It will be interpolated across the triangle.
        + "   gl_Position = u_MVPMatrix   \n"     // gl_Position is a special variable used to store the final position.
        + "               * a_Position;   \n"     // Multiply the vertex by the matrix to get the final point in
        + "}                                \n";

final String fragmentShader =
        "precision mediump float;       \n"     // Set the default precision to medium. We don't need as high of a
        // precision in the fragment shader.
        + "uniform sampler2D u_Texture;   \n"    // The input texture.

        + "uniform vec4 u_Color;          \n"     // This is the color from the vertex shader interpolated across the
        // triangle per fragment.
        + "varying vec2 v_TexCoordinate;  \n" // Interpolated texture coordinate per fragment.

        + "void main()                    \n"     // The entry point for our fragment shader.
        + "{                              \n"
        + "   gl_FragColor = texture2D(u_Texture, v_TexCoordinate); \n"
        + "}                              \n";


这是加载纹理函数

public static int loadTexture(final Context context, final int resourceId)
{
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0)
    {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;   // No pre-scaling

        // Read in the resource
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        // Set filtering
        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);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();
    }

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

    return textureHandle[0];
}


这是drawFrame

        @Override
public void onDrawFrame(GL10 gl) {
    // Set the background clear color to gray.
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
    GLES20.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
    mTextureUniformHandle = GLES20.glGetUniformLocation(programHandle, "u_Texture");
    mTextureCoordinateHandle = GLES20.glGetAttribLocation(programHandle, "a_TexCoordinate");

    // 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, mTextureDataHandle);

    // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
    GLES20.glUniform1i(mTextureUniformHandle, 0);
    // Draw the triangle facing straight on.
    Matrix.setIdentityM(mModelMatrix, 0);
    Matrix.translateM(mModelMatrix, 0, Fields.screen_width / 2, Fields.screen_height / 2, 0);
    Matrix.scaleM(mModelMatrix, 0, Fields.screen_width / 4, Fields.screen_width / 4, 0);
    drawTriangle(mTriangle1Vertices);
}


对于摆脱奇怪的棕色正方形并实际显示纹理有什么建议吗?

编辑:这是纹理坐标

public GL20Renderer(Context context) {
    final float[] triangle1VerticesData = {
            // X, Y, Z,
            // R, G, B, A
            -1f, 1f, 0.0f,
            -1, -1, 0.0f,
            1f, 1f, 0.0f,
            -1f, -1f, 0.0f,
            1f, -1f, 0.0f,
            1f, 1f, 0.0f,
    };
    // Initialize the buffers.
    mTriangle1Vertices = ByteBuffer.allocateDirect(triangle1VerticesData.length * mBytesPerFloat)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTriangle1Vertices.put(triangle1VerticesData).position(0);

    final float[] triangle1TextureCoordinateData =
        {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f,
            1.0f, 0.0f
        };
    mTriangleTextureCoordinates = ByteBuffer.allocateDirect(triangle1TextureCoordinateData.length * mBytesPerFloat)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    mTriangleTextureCoordinates.put(triangle1TextureCoordinateData).position(0);
    this.context = context;
}


这是drawtriangle函数

private void drawTriangle(final FloatBuffer aTriangleBuffer)
{
    // Pass in the position information
    aTriangleBuffer.position(mPositionOffset);
    GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
            mStrideBytes, aTriangleBuffer);

    GLES20.glEnableVertexAttribArray(mPositionHandle);

    int colorHandle = GLES20.glGetUniformLocation(programHandle, "u_Color");

    // Set color for drawing the triangle
    GLES20.glUniform4f(colorHandle, 0.0f, 0.8f, 1.0f, 1.0f);

    // This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
    // (which currently contains model * view).
    Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);

    // This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
    // (which now contains model * view * projection).
    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
}

最佳答案

您的drawTriangle代码没有在任何地方设置纹理坐标。添加如下代码:

triangle1TextureCoordinateData.position(mPositionOffset * 2 / 3);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, 2, GLES20.GL_FLOAT, false,
        0, triangle1TextureCoordinateData);

GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);


请注意,如果您的数据打包在缓冲区中,则可以将跨步设置为零。

10-05 20:23