Android OpenGL设置立方体的旋转速度

时间:2013-09-05 02:52:38

标签: android opengl-es rotation

我有一个围绕坐标系中心旋转的立方体。但问题是它旋转得非常慢。那么在我的情况下如何设置转速?

以下三种方法使用给定的模型转换更新mCurrentModelMatrix。这些是有状态的累积方法。

    public void trnslate(float x, float y, float z)
    {
       float[] tempModelMatrix = new float[16];       
       Matrix.setIdentityM(tempModelMatrix, 0);
       Matrix.translateM(tempModelMatrix,0,x,y,z);
        Matrix.multiplyMM(this.mCurrentModelMatrix, 0, 
              tempModelMatrix, 0, this.mCurrentModelMatrix, 0);
    }
    public void rotate(float angle, float x, float y, float z)
    {
       float[] tempModelMatrix = new float[16];       
       Matrix.setIdentityM(tempModelMatrix, 0);
       Matrix.rotateM(tempModelMatrix,0,angle,x,y,z);
        Matrix.multiplyMM(this.mCurrentModelMatrix, 0, 
              tempModelMatrix, 0, this.mCurrentModelMatrix, 0);
    }
    public void scale(float xFactor, float yFactor, float zFactor)
    {
       float[] tempModelMatrix = new float[16];       
       Matrix.setIdentityM(tempModelMatrix, 0);
       Matrix.scaleM(tempModelMatrix,0,xFactor,yFactor,zFactor);
        Matrix.multiplyMM(this.mCurrentModelMatrix, 0, 
              tempModelMatrix, 0, this.mCurrentModelMatrix, 0);
    }

    /*
     * Calculaute the final model view matrix
     * 1. Order of matrix multiplication is important
     * 2. MVPmatrix = proj * view * model;
     * 3. Setup the MVP matrix in the vertex shader memory
     */
    protected void setupMatrices()
    {
       float[] tempModelMatrix = new float[16];
       Matrix.setIdentityM(tempModelMatrix, 0);

        //translate the model combo next
        Matrix.multiplyMM(mMVPMatrix, 0, //matrix and offset 
              mCurrentModelMatrix, 0, 
              tempModelMatrix, 0);

       //translate eye coordinates first
        Matrix.multiplyMM(mMVPMatrix, 0, 
              this.mVMatrix, 0, 
              mMVPMatrix, 0);

        //Project it: screen coordinates
        Matrix.multiplyMM(mMVPMatrix, 0, 
              mProjMatrix, 0, 
              mMVPMatrix, 0);

        //Set the vertex uniform handler representing the MVP matrix
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, //uniform handle 
              1, //number of uniforms. 1 if it is not an array
              false, //transpose: must be false
              mMVPMatrix, //client matrix memory pointer
              0); //offset
    }

绘制方法

   // Drawing operation
@Override
protected void draw(GL10 gl, int positionHandle) {
    // Hide the hidden surfaces using these APIs
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDepthFunc(GLES20.GL_LESS);

    // Transfer vertices to the shader
    transferVertexPoints(positionHandle);
    // Transfer texture points to the shader
    transferTexturePoints(getTextureHandle());

    // Implement rotation from 0 to 360 degrees
    // Stop when asked and restart when the stopFlag
    // is set to false.
    // Decide what the current angle to apply
    // for rotation is.
    if (stopFlag == true) {
        // stop rotation
        curAngle = stoppedAtAngle;
    } else {
        curAngle += 1.0f;
    }
    if (curAngle > 360) {
        curAngle = 0;
    }

    // Tell the base class to start their
    // matrices to unit matrices.
    this.initializeMatrices();

    // The order of these model transformations matter
    // Each model transformation is specified with
    // respect to the last one, and not the very first.

    // Center the cube
    this.trnslate(0, 0, -1);
    // Rotate it around y axis
    this.rotate(curAngle, 0, -1, 0);
    // Decenter it to where ever you want
    this.trnslate(0, -2, 2);

    // Go ahead calculate the ModelViewMatrix as
    // we are done with ALL of our model transformations
    this.setupMatrices();

    // Call glDrawArrays to use the vertices and draw
    int vertexCount = mTriangleVerticesData.length / 3;
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, // what primitives to use
            0, // at what point to start
            vertexCount); // Starting there how many points to use
    // Check if there are errors
    checkGlError("glDrawArrays");
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

您每帧旋转1度,因此需要360帧才能完成旋转。

如果您希望它在2秒内旋转,并且您以每秒30帧的速度运行,则需要通过更改此部分每帧旋转6度:

if (stopFlag == true) {
    // stop rotation
    curAngle = stoppedAtAngle;
} else {
    curAngle += 6.0f;
}
if (curAngle > 360) {
    curAngle = 0;
}