Home | History | Annotate | Download | only in renderers
      1 /*
      2  * Copyright (C) 2014 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 
     17 package com.android.cts.verifier.sensors.renderers;
     18 
     19 import android.opengl.GLSurfaceView;
     20 import android.opengl.GLU;
     21 
     22 import java.util.concurrent.atomic.AtomicInteger;
     23 
     24 import javax.microedition.khronos.egl.EGLConfig;
     25 import javax.microedition.khronos.opengles.GL10;
     26 
     27 /**
     28  * Renders a spinning block to indicate how the device should be rotated in the test.
     29  */
     30 public class GLRotationGuideRenderer implements GLSurfaceView.Renderer {
     31     public static final int BACKGROUND_BLACK = 0;
     32     public static final int BACKGROUND_RED = 1;
     33     public static final int BACKGROUND_GREEN = 2;
     34     private static final double ANGLE_INCREMENT = 1.0;
     35 
     36     private final Monolith mMonolith = new Monolith();
     37     private final AtomicInteger mBackgroundColor = new AtomicInteger(BACKGROUND_BLACK);
     38 
     39     private float mAngle;
     40     private float mRotateX;
     41     private float mRotateY;
     42     private float mRotateZ;
     43 
     44     /**
     45      * Sets the rotation of the monolith.
     46      */
     47     public void setRotation(float rotateX, float rotateY, float rotateZ) {
     48         mRotateX = rotateX;
     49         mRotateY = rotateY;
     50         mRotateZ = rotateZ;
     51     }
     52 
     53     /**
     54      * Sets the background color.
     55      */
     56     public void setBackgroundColor(int value) {
     57         mBackgroundColor.set(value);
     58     }
     59 
     60     /**
     61      * {@inheritDoc}
     62      */
     63     @Override
     64     public void onDrawFrame(GL10 gl) {
     65         clearBackground(gl);
     66         gl.glMatrixMode(GL10.GL_MODELVIEW);
     67         gl.glLoadIdentity();
     68         gl.glRotatef(mAngle, mRotateX, mRotateY, mRotateZ);
     69         mMonolith.draw(gl);
     70         mAngle += ANGLE_INCREMENT;
     71     }
     72 
     73     /**
     74      * {@inheritDoc}
     75      */
     76     @Override
     77     public void onSurfaceChanged(GL10 gl, int width, int height) {
     78         gl.glViewport(0, 0, width, height);
     79         gl.glMatrixMode(GL10.GL_PROJECTION);
     80         gl.glLoadIdentity();
     81         float ratio = (float) width / height;
     82         gl.glFrustumf(-ratio, ratio, -1, 1, 3, 15);
     83         GLU.gluLookAt(gl, 0, 0, 10, 0, 0, 0, 0, 1, 0);
     84     }
     85 
     86     /**
     87      * {@inheritDoc}
     88      */
     89     @Override
     90     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     91         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
     92         gl.glEnable(GL10.GL_LIGHTING);
     93         gl.glEnable(GL10.GL_LIGHT0);
     94         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, new float[] {0.75f, 0.75f, 0.75f, 1f}, 0);
     95     }
     96 
     97     private void clearBackground(GL10 gl) {
     98         switch (mBackgroundColor.get()) {
     99             case BACKGROUND_GREEN:
    100                 gl.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
    101                 break;
    102 
    103             case BACKGROUND_RED:
    104                 gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    105                 break;
    106 
    107             default:
    108                 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    109                 break;
    110         }
    111         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    112     }
    113 }
    114