简体   繁体   中英

OpenGL Android Translate object on y Axis 2D

I cannot seem to get my object to move on the y axis, it just stays at its current location.

To my current knowledge I know to use

Matrix.translateM()

However it does not seem to work and I am not sure what step I am missing.

In my renderer the OnDrawFrame Method is as follows:

@Override
public void onDrawFrame(GL10 glUnused) {
    // Clear the rendering surface.
    glClear(GL_COLOR_BUFFER_BIT);
    // Apply transformation, start with translation

    Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    // Calculate the projection and view transformation
    Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
    // Create a rotation transformation for the triangle
    long time = SystemClock.uptimeMillis() % 4000L;
    float mAngle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);



    Matrix.orthoM(mMatrix, 0, -1, 1, -1, 1, -1, 1);

    // Combine Rotation and Translation matrices
    mTempMatrix = mModelMatrix.clone();
    Matrix.multiplyMM(mModelMatrix, 0, mTempMatrix, 0, mRotationMatrix, 0);

    // Combine the model matrix with the projection and camera view
    mTempMatrix = mMVPMatrix.clone();
    Matrix.multiplyMM(mMVPMatrix, 0, mTempMatrix, 0, mModelMatrix, 0);
    Matrix.orthoM(mMatrix, 0, -1, 1, -1, 1, -1, 1);
   mCircle.draw(mMatrix);

    Matrix.setIdentityM(mCircle.mModelMatrix, 0);
    Matrix.translateM(mCircle.mModelMatrix, 0, 50.5f, 5.7f, 5.7f);

    //mTriangle.draw(mMatrix);

}

My Circle class is as follows

public class Circle{
public float[] mModelMatrix = new float[16];
public FloatBuffer mVertexBuffer;
public float vertices[] = new float[364 * 3];
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
public float ofset = 0;
private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;" +
                "attribute vec4 vPosition;" +
                "void main() {" +
                // the matrix must be included as a modifier of gl_Position
                // Note that the uMVPMatrix factor *must be first* in order
                // for the matrix multiplication product to be correct.
                "  gl_Position = uMVPMatrix * vPosition;" +
                "}";

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

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

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = {
        // in counterclockwise order:
        0.0f,  0.622008459f, 0.0f,   // top
        -0.5f, -0.311004243f, 0.0f,   // bottom left
        0.5f, -0.311004243f, 0.0f    // bottom right
};
private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

    public Circle(){
        vertices[0] = 0;
        vertices[1] = 0;
        vertices[2] = 0;

        for(int i =1; i < 364; i++){
            vertices[(i * 3)+ 0] = (float) (0.1 * Math.cos((3.14/180) * (float)i )+ofset);

            vertices[(i * 3)+ 1] = (float) (0.059 * Math.sin((3.14/180) * (float)i )+ofset);
            vertices[(i * 3)+ 2] = 0;
        }
        // initialize vertex byte buffer for shape coordinates
        ByteBuffer bb = ByteBuffer.allocateDirect(
                // (number of coordinate values * 4 bytes per float)
                vertices.length * 4);
        // use the device hardware's native byte order
        bb.order(ByteOrder.nativeOrder());

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

        // prepare shaders and OpenGL program
        int vertexShader = ImpulseRushRenderer.loadShader(
                GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = ImpulseRushRenderer.loadShader(
                GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.glLinkProgram(mProgram);                  // create OpenGL program executables
    }

public void draw(float[] mvpMatrix) {
    // Add program to OpenGL 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);

    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_FAN, 0, 364);

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

}

}

I can't seem to find a step by step guide for getting a translation to work so I am assuming I am missing something important.I have gotten Rotate to work before but I am having no luck trying to get Translation to work.

You need to be consistent with your matrices and know when to use which. In the code you posted you actually translated the matrix that is not in use at all so no change was made.

Usually an MVP matrix system is used which means a product of 3 matrices which are Model, View and Projection. Each of these represents a part of the transformation of the drawn object and until you get to some optimizations you are best to use all 3 of them both in your application and in the shaders. For some systems such as lighting you will need to separate them for the shader anyway.

So a model matrix is the one that represents the model in the scene. An identity should be used so it is placed in the center with no rotations or scale. Then modify it to change the model position in the scene such as translating it. It makes sense for each model to have its own model matrix (but not projection and view).

A view matrix represents the user or camera position in the scene. In most cases you will use look-at procedure to give the position, the location you are looking toward and the up vector. In most cases you will have only one of these but there may be more in cases such as split screen.

A projection matrix is used to define the coordinates system and or projection of your scene. The most common here is to use ortho or frustum . They are very similar to look at and basically one is used for 2D and another for 3D. This matrix is used closely with "view" matrix.

So if you use all 3 of them then it gets pretty simple. Once the view is generated or the view frame changes you will need to setup the projection matrix. The vie matrix must change when you want to look around or move around the scene. And the model matrix should be set for every draw call. Do make sure all of them are set at least to identity from the beginning or none of the operations will work on them.

I hope this clears a few things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM