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.Renderable;
17 
18 import com.android.cts.verifier.sensors.sixdof.Renderer.RenderUtils.DrawParameters;
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  * Base class for all Renderables
29  */
30 public abstract class Renderable {
31     protected static final int BYTES_PER_FLOAT = 4;
32     protected static final int POSITION_DATA_SIZE = 3;
33     protected static final int COLOUR_DATA_SIZE = 4;
34     protected static final int NORMAL_DATA_SIZE = 3;
35 
36     protected int mVertexCount;
37 
38     protected int mProgramHandle;
39     protected int mPositionHandle;
40     protected int mMVPMatrixHandle;
41     protected int mMVMatrixHandle;
42 
43     private float[] mModelMatrix = new float[MathsUtils.MATRIX_4X4];
44     private float[] mMvMatrix = new float[MathsUtils.MATRIX_4X4];
45     private float[] mMvpMatrix = new float[MathsUtils.MATRIX_4X4];
46 
47     /**
48      * Applies the view and projection matrices and draws the Renderable.
49      *
50      * @param drawParameters parameters needed for drawing objects.
51      */
draw(DrawParameters drawParameters)52     public abstract void draw(DrawParameters drawParameters);
53 
updateMvpMatrix(float[] viewMatrix, float[] projectionMatrix)54     public synchronized void updateMvpMatrix(float[] viewMatrix,
55                                              float[] projectionMatrix) {
56         // Compose the model, view, and projection matrices into a single mvp
57         // matrix
58         Matrix.setIdentityM(mMvMatrix, 0);
59         Matrix.setIdentityM(mMvpMatrix, 0);
60         Matrix.multiplyMM(mMvMatrix, 0, viewMatrix, 0, getModelMatrix(), 0);
61         Matrix.multiplyMM(mMvpMatrix, 0, projectionMatrix, 0, mMvMatrix, 0);
62     }
63 
getModelMatrix()64     public float[] getModelMatrix() {
65         return mModelMatrix;
66     }
67 
setModelMatrix(float[] modelMatrix)68     public void setModelMatrix(float[] modelMatrix) {
69         mModelMatrix = modelMatrix;
70     }
71 
getMvMatrix()72     public float[] getMvMatrix() {
73         return mMvMatrix;
74     }
75 
getMvpMatrix()76     public float[] getMvpMatrix() {
77         return mMvpMatrix;
78     }
79 
setRotationAngle(float newAngle)80     public synchronized void setRotationAngle(float newAngle) {
81         // Rotate around the Z axis. (only used in robustness test).
82         float[] translations = new float[]
83                 {getModelMatrix()[12], getModelMatrix()[13],getModelMatrix()[14]};
84         synchronized (this) {
85             Matrix.setIdentityM(getModelMatrix(), 0);
86             Matrix.rotateM(getModelMatrix(), 0, newAngle, 0.0f, 0.0f, 1.0f);
87             Matrix.translateM(getModelMatrix(), 0,
88                     translations[X], translations[Y], translations[Z]);
89         }
90     }
91 
destroy()92     public abstract void destroy();
93 }
94