不能很好地翻译 - Android - OpenGL ES 2

时间:2015-06-29 17:40:23

标签: android matrix opengl-es opengl-es-2.0

我有这个代码应该移动我的圆圈(方形):

float[] scratch = new float[16];

float[] move = new float[16];
Matrix.setIdentityM(move, 0);
Matrix.translateM(move, 0, 100, 100, 0);

Matrix.multiplyMM(scratch, 0, projectionMatrix, 0, move, 0);

mCircle.draw(scratch); 

projectionMatrix是相机:

Matrix.orthoM(projectionMatrix, 0, 0, width, height, 0, -1f, 1f);

但是当我执行代码时,我得到了这个: Image

我遵循了Android Developer的代码。

precision highp float;

uniform float uRadius;
vec2 center = vec2(uRadius, uRadius);
vec2 coord = vec2(gl_FragCoord.x, 1080. - gl_FragCoord.y);
vec2 position = coord - center;
uniform vec4 uColor;

void main()
{
    if (length(position) > uRadius) {
    discard;
    }

    gl_FragColor = uColor;
}
--------------------------------
uniform mat4 uMatrix;

attribute vec4 aPosition;

void main()
{
    gl_Position = uMatrix * aPosition;
}

我的主循环:

public void onDrawFrame(GL10 unused) {
    // Draw background color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    float[] scratch = new float[16];

    float[] move = new float[16];
    Matrix.setIdentityM(move, 0);
    Matrix.translateM(move, 0, 50, 50, 0);

    Matrix.multiplyMM(scratch, 0, projectionMatrix, 0, move, 0);

    mCircle.draw(scratch);
}

圆圈绘制功能是:

public void draw(float[] projectionMatrix) {
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    int mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");

    // Enable a handle to the triangle vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

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

    // get handle to fragment shader's vColor member
    int mColorHandle = GLES20.glGetUniformLocation(mProgram, "uColor");

    // Set color for drawing the triangle
    GLES20.glUniform4fv(mColorHandle, 1, mColor, 0);

    int radiusHandle = GLES20.glGetUniformLocation(mProgram, "uRadius");
    MyGLRenderer.checkGlError("glGetUniformLocation");
    GLES20.glUniform1f(radiusHandle, mRadius);

    // get handle to shape's transformation matrix
    int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMatrix");
    MyGLRenderer.checkGlError("glGetUniformLocation");

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

    // Draw the square
    GLES20.glDrawElements(
            GLES20.GL_TRIANGLES, drawOrder.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

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

1 个答案:

答案 0 :(得分:0)

几个小时后我终于在片段着色器中找到了逻辑错误:圆圈的中心保持不变,现在我为偏移/位置增加了2个制服并且它正在工作。