Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OpenGL设置立方体的旋转速度_Android_Opengl Es_Rotation - Fatal编程技术网

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

Android OpenGL设置立方体的旋转速度,android,opengl-es,rotation,Android,Opengl Es,Rotation,我有一个立方体,它绕着坐标系的中心旋转。但问题是它旋转得很慢。那么在我的例子中,如何设置旋转速度呢 以下三种方法使用给定的模型转换更新mCurrentModelMatrix。这些是有状态的累积方法 public void trnslate(float x, float y, float z) { float[] tempModelMatrix = new float[16]; Matrix.setIdentityM(tempModelMa

我有一个立方体,它绕着坐标系的中心旋转。但问题是它旋转得很慢。那么在我的例子中,如何设置旋转速度呢

以下三种方法使用给定的模型转换更新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度,因此需要360帧才能完成完整旋转

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

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