• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils.X;
19 import static com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils.Y;
20 import static com.android.cts.verifier.sensors.sixdof.Utils.MathsUtils.Z;
21 
22 import android.opengl.GLES20;
23 import android.opengl.Matrix;
24 
25 import com.android.cts.verifier.sensors.sixdof.Renderer.RenderUtils.Colour;
26 import com.android.cts.verifier.sensors.sixdof.Renderer.RenderUtils.DrawParameters;
27 import com.android.cts.verifier.sensors.sixdof.Renderer.RenderUtils.ShaderHelper;
28 
29 import java.nio.ByteBuffer;
30 import java.nio.ByteOrder;
31 import java.nio.FloatBuffer;
32 
33 /**
34  * Rotating Rectangle for the robustness test.
35  */
36 public class RectangleRenderable extends Renderable {
37     private static final int LINE_WIDTH = 8;
38     protected static final float[] RECTANGLE_POSITION = {0.0f, 0.0f, -2.99f};
39 
40     private FloatBuffer mPositionBuffer;
41     private FloatBuffer mColorBuffer;
42     private int mColorHandle;
43 
44     private float[] mRectanglePositionData;
45 
RectangleRenderable()46     public RectangleRenderable() {
47         // Reset the model matrix to the identity and move it infront of the camera preview
48         Matrix.setIdentityM(getModelMatrix(), 0);
49         Matrix.translateM(getModelMatrix(), 0,
50                 RECTANGLE_POSITION[X], RECTANGLE_POSITION[Y], RECTANGLE_POSITION[Z]);
51     }
52 
initialiseRectangle(float[] rectanglePositionData)53     public void initialiseRectangle(float[] rectanglePositionData) {
54         mRectanglePositionData = rectanglePositionData;
55 
56         // Initialize the buffers.
57         mPositionBuffer = ByteBuffer.allocateDirect(mRectanglePositionData.length * BYTES_PER_FLOAT)
58                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
59         mPositionBuffer.put(mRectanglePositionData).position(0);
60 
61         // float count / floats per vertex
62         mVertexCount = mRectanglePositionData.length / POSITION_DATA_SIZE;
63         int colourCount = mVertexCount * COLOUR_DATA_SIZE; // Vertex count * rgba
64         float[] colours = new float[colourCount];
65 
66         for (int i = 0; i < colourCount; i++) {
67             int index = i % COLOUR_DATA_SIZE;
68             colours[i] = Colour.GREEN[index];
69         }
70 
71         mColorBuffer = ByteBuffer.allocateDirect(colours.length * BYTES_PER_FLOAT)
72                 .order(ByteOrder.nativeOrder()).asFloatBuffer();
73         mColorBuffer.put(colours).position(0);
74 
75         final String vertexShader = ShaderHelper.getRectangleVertexShader();
76         final String fragmentShader = ShaderHelper.getRectangleFragmentShader();
77 
78         final int vertexShaderHandle =
79                 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
80         final int fragmentShaderHandle =
81                 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
82 
83         mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
84                 new String[]{"a_Position", "a_Color"});
85     }
86 
87     @Override
draw(DrawParameters drawParameters)88     public void draw(DrawParameters drawParameters) {
89         GLES20.glUseProgram(mProgramHandle);
90 
91         // Set program handles for camera preview drawing.
92         mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
93         mMVMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVMatrix");
94         mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
95         mColorHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Color");
96 
97         updateMvpMatrix(drawParameters.getViewMatrix(), drawParameters.getProjectionMatrix());
98         drawRectangle();
99     }
100 
drawRectangle()101     private void drawRectangle() {
102         // Pass in the position information
103         mPositionBuffer.position(0);
104         GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false,
105                 0, mPositionBuffer);
106 
107         GLES20.glEnableVertexAttribArray(mPositionHandle);
108 
109         // Pass in the modelview matrix.
110         GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, getMvMatrix(), 0);
111 
112         // Pass in the combined matrix.
113         GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, getMvpMatrix(), 0);
114 
115         synchronized (this) {
116             // Pass in the color information
117             mColorBuffer.position(0);
118             GLES20.glVertexAttribPointer(mColorHandle, COLOUR_DATA_SIZE, GLES20.GL_FLOAT, false,
119                     0, mColorBuffer);
120 
121             GLES20.glEnableVertexAttribArray(mColorHandle);
122         }
123 
124         // Draw the rectangle.
125         GLES20.glLineWidth(LINE_WIDTH);
126         GLES20.glDrawArrays(GLES20.GL_LINES, 0, mVertexCount); // 2 points per line * 4 lines = 8
127     }
128 
setLineColor(float[] newColor)129     public void setLineColor(float[] newColor) {
130         synchronized (this) {
131             // float count / floats per vertex
132             int vertexCount = mRectanglePositionData.length / POSITION_DATA_SIZE;
133             int colourCount = vertexCount * COLOUR_DATA_SIZE; // Vertex count * rgba
134             float[] colours = new float[colourCount];
135 
136             for (int i = 0; i < colourCount; i++) {
137                 int index = i % COLOUR_DATA_SIZE;
138                 colours[i] = newColor[index];
139             }
140 
141             mColorBuffer.put(colours).position(0);
142         }
143     }
144 
145     @Override
destroy()146     public void destroy() {
147         mPositionBuffer = null;
148         mColorBuffer = null;
149     }
150 }
151