围绕其轴旋转形状,然后在OpenGL ES 2.0中平移形状

时间:2013-09-07 10:40:09

标签: opengl-es opengl-es-2.0

我正在尝试围绕其中心(0,0,0)旋转三角形,然后将其翻译为(0.5,0.5,0)。但是,无论我在 rotateM 命令之前还是之后放置 translateM 命令,我仍然得到相同的结果,即转换为所述位置的三角形,旋转(轨道运行)约(0,0,0)。我看过网上发现了类似的帖子,但他们没有帮助。这是我的代码:

注意:我的三角形都以(0,0,0)为中心开始。

    Matrix.setIdentityM(mModelMatrix, 0);

    // *** transform shape before rendering

    Matrix.setRotateM(mRotationMatrix, 0, rx, -1.0f, 0, 0);
    Matrix.multiplyMM(mModelMatrix, 0, mRotationMatrix, 0, mModelMatrix, 0);        // rotate

    Matrix.setRotateM(mRotationMatrix, 0, ry, 0, -1.0f, 0);
    Matrix.multiplyMM(mModelMatrix, 0, mRotationMatrix, 0, mModelMatrix, 0);        // rotate

    Matrix.setRotateM(mRotationMatrix, 0, rz, 0, 0, -1.0f);
    Matrix.multiplyMM(mModelMatrix, 0, mRotationMatrix, 0, mModelMatrix, 0);        // rotate

    Matrix.translateM(mModelMatrix, 0, tx, ty, tz);                         // translate

    Matrix.multiplyMM(mvpMatrix, 0, mModelMatrix, 0, mvpMatrix, 0);         // world view


    // *** render

    // 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");
    MyGL20Renderer.checkGlError("glGetUniformLocation");

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

    // Draw the triangle
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);

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

任何建议都将不胜感激。

0 个答案:

没有答案