我的所有矩阵函数都不起作用? OpenGL ES 2.0

时间:2013-01-30 04:31:36

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

我正在尝试制作投影矩阵来缩放屏幕并制作协调系统。出于某种原因,我认为我的矩阵调用没有任何工作......我正在使用的3函数是

Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0,  1000, -1, 10);
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

他们互相取消了吗?有什么不对吗? (完整的渲染类代码在最后)

我这样做的主要目标是最终达到这样一种情况:当我制作一个正方形时,我可以提供(200, 100,0) //x, y, z之类的坐标,这些坐标不仅介于-1和1之间。

这是我的完整渲染类:

public class MyRenderer implements Renderer {

private static final String TAG = "MyRenderer";

Square square;

private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];

private int camWidth,camHeight;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.5f);
    camWidth=480;camHeight=320;
    // initialize a square
    square = new Square();
}

@Override
public void onDrawFrame(GL10 nope) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    //set camera position

    GLES20.glViewport(0, 0, camWidth, camHeight);

    Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0,  1000, -10, 999999);
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);


    square.draw(mMVPMatrix);
}

@Override
public void onSurfaceChanged(GL10 nope, int width, int height) {




    GLES20.glViewport(0, 0, camWidth, camHeight);

    Matrix.orthoM(mProjMatrix, 0, 0, 1520, 0,  1000, -10, 999999);
    Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);
}

public static int loadShader(int type, String shaderCode) {

    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}

 }

My Square课程(不确定是否需要,但只是为了安全:)) -

public class Square {
private final String vertexShaderCode =
    "attribute vec4 vPosition;" +
    "void main() {" +
    "  gl_Position = vPosition;" +
    "}";

private final String fragmentShaderCode = "precision mediump float;"
        + "uniform vec4 vColor;" + "void main() {"
        + "  gl_FragColor = vColor;" + "}";

static final int COORDS_PER_VERTEX = 3;

static float triangleCoords[] = { // in counterclockwise order:
-0.5f, 0.5f, 0.0f, // top left
        -0.5f, -0.5f, 0.0f, // bottom left
        0.5f, -0.5f, 0.0f, // bottom right
        0.5f, 0.5f, 0.0f

};
private short drawOrder[] = { 0, 1, 2, 0, 2, 3 };
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;

private final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex

// red-green-blue-alpha
float color[] = { 0.63f, 0.76f, 0.22f, 1.0f };

private final int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;

public Square() {
    ByteBuffer bb = ByteBuffer.allocateDirect(
    // # of coords values * 4 bytes per float
            triangleCoords.length * 4);

    // use native byte order
    bb.order(ByteOrder.nativeOrder());

    // create a floating point buffer from the ByteBuffer
    vertexBuffer = bb.asFloatBuffer();
    // add coordination to FloatBuffer
    vertexBuffer.put(triangleCoords);
    // set the buffer to read first coordinate
    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);

    dlb.order(ByteOrder.nativeOrder());

    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    int vertexShader = ChizRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = ChizRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);

}

public void draw(float[] mvpMatrix) {
    // Add program to OpenGL ES environment
    GLES20.glUseProgram(mProgram);

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

    // 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
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

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

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

    // apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

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

    // dispable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);

}
}

最后,你可以有一些视觉效果:

with and without metrix functions

这就是我的手机在有和没有前面提到的三种metrix功能的情况下的外观,似乎唯一能够对宽度和高度做出任何改变的是GLES20.glViewport(0, 0, camWidth, camHeight);

似乎该矩阵似乎无所作为。

1 个答案:

答案 0 :(得分:2)

看起来您已将sample改编为正方形。这里有几个问题:

  1. 仅在glViewPort中仅使用给定的参数调用onSurfaceChanged
  2. 顶点着色器代码不使用uMVPMatrix。您已经通过检查mMVPMatrixHandle-1对于不存在的制服的值来看到了这一点,请参阅here)。
  3. 在链接程序后,着色器变量的位置是固定的,因此代码可以获取一次,而不是每次绘制调用。
  4. 然后,你需要调整方形的坐标...