Home | History | Annotate | Download | only in RenderUtils
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.cts.verifier.sensors.sixdof.Renderer.RenderUtils;
     17 
     18 import com.android.cts.verifier.sensors.sixdof.Renderer.Renderable.ConeRenderable;
     19 import com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils;
     20 
     21 import static com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils.X;
     22 import static com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils.Y;
     23 import static com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils.Z;
     24 
     25 import android.opengl.Matrix;
     26 
     27 /**
     28  * Manages the model matrix of the direction cone.
     29  */
     30 public class ConeModelMatrixCalculator extends ModelMatrixCalculator {
     31     float[] mUpVector;
     32 
     33     public ConeModelMatrixCalculator(int toRotate, float[] upVector) {
     34         super(toRotate);
     35         mUpVector = upVector;
     36     }
     37 
     38     public void updateModelMatrix(float[] translation, float[] quaternion, float[] lookAtPosition) {
     39         float[] convertedTranslation = MathsUtils.convertToOpenGlCoordinates(translation, mToRotate);
     40         // Calculate the extrinsics based model matrix with current pose data.
     41         float[] newModelMatrix = calculateModelMatrix(convertedTranslation, quaternion);
     42 
     43         // Extract the information we need from calculated model matrix. (Just the translation).
     44         float[] translationMatrix = new float[MathsUtils.MATRIX_4X4];
     45         Matrix.setIdentityM(translationMatrix, 0);
     46         Matrix.translateM(translationMatrix, 0, newModelMatrix[MATRIX_4X4_TRANSLATION_X],
     47                 newModelMatrix[MATRIX_4X4_TRANSLATION_Y], newModelMatrix[MATRIX_4X4_TRANSLATION_Z]);
     48 
     49         float[] openGlRingPosition = MathsUtils.convertToOpenGlCoordinates(lookAtPosition, mToRotate);
     50         float[] rotationTransformation = new float[MathsUtils.MATRIX_4X4];
     51         // Calculate direction vector.
     52         float[] relativeVector = new float[MathsUtils.VECTOR_3D];
     53         for (int i = 0; i < relativeVector.length; i++) {
     54             relativeVector[i] = openGlRingPosition[i] - convertedTranslation[i];
     55         }
     56         Matrix.setIdentityM(rotationTransformation, 0);
     57         // Calculate look at rotation transformation.
     58         // Has to be relative to the origin otherwise we get some warping of the cone.
     59         MathsUtils.setLookAtM(rotationTransformation,
     60                 // Where we are.
     61                 0.0f, 0.0f, 0.0f,
     62                 // What we want to look at.
     63                 relativeVector[X], relativeVector[Y], relativeVector[Z],
     64                 // Up direction.
     65                 mUpVector[X], mUpVector[Y], mUpVector[Z]);
     66 
     67         // Apply translation to the look at matrix.
     68         Matrix.multiplyMM(mModelMatrix, 0, translationMatrix, 0, rotationTransformation, 0);
     69     }
     70 
     71     /**
     72      * Rotations that need to be done before rotating. Used for calculating the CONE_OFFSET.
     73      *
     74      * @return The offset that the cone needs to be at.
     75      */
     76     @Override
     77     protected float[] getRequiredTranslations() {
     78         return ConeRenderable.CONE_OFFSET;
     79     }
     80 }
     81