• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.projection.cube;
18 
19 import android.opengl.GLSurfaceView;
20 
21 import javax.microedition.khronos.egl.EGLConfig;
22 import javax.microedition.khronos.opengles.GL10;
23 
24 /**
25  * Render a pair of tumbling cubes.
26  */
27 public class CubeRenderer implements GLSurfaceView.Renderer {
28 
29     private final boolean mTranslucentBackground;
30 
31     private final Cube mCube;
32     private float mAngle;
33     private float mScale = 1.0f;
34     private boolean mExploding;
35 
CubeRenderer(boolean useTranslucentBackground)36     public CubeRenderer(boolean useTranslucentBackground) {
37         mTranslucentBackground = useTranslucentBackground;
38         mCube = new Cube();
39     }
40 
explode()41     public void explode() {
42         mExploding = true;
43     }
44 
45     @Override
onDrawFrame(GL10 gl)46     public void onDrawFrame(GL10 gl) {
47         /*
48          * Usually, the first thing one might want to do is to clear
49          * the screen. The most efficient way of doing this is to use
50          * glClear().
51          */
52 
53         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
54 
55         /*
56          * Now we're ready to draw some 3D objects
57          */
58 
59         gl.glMatrixMode(GL10.GL_MODELVIEW);
60         gl.glLoadIdentity();
61         gl.glTranslatef(0, 0, -3.0f);
62         gl.glRotatef(mAngle,        0, 1, 0);
63         gl.glRotatef(mAngle*0.25f,  1, 0, 0);
64 
65         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
66         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
67 
68         gl.glScalef(mScale, mScale, mScale);
69         mCube.draw(gl);
70 
71         gl.glRotatef(mAngle*2.0f, 0, 1, 1);
72         gl.glTranslatef(0.5f, 0.5f, 0.5f);
73 
74         mCube.draw(gl);
75 
76         mAngle += 1.2f;
77 
78         if (mExploding) {
79             mScale *= 1.02f;
80             if (mScale > 4.0f) {
81                 mScale = 1.0f;
82                 mExploding = false;
83             }
84         }
85     }
86 
87     @Override
onSurfaceChanged(GL10 gl, int width, int height)88     public void onSurfaceChanged(GL10 gl, int width, int height) {
89         gl.glViewport(0, 0, width, height);
90 
91         /*
92          * Set our projection matrix. This doesn't have to be done
93          * each time we draw, but usually a new projection needs to
94          * be set when the viewport is resized.
95          */
96 
97         float ratio = (float) width / height;
98         gl.glMatrixMode(GL10.GL_PROJECTION);
99         gl.glLoadIdentity();
100         gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
101     }
102 
103     @Override
onSurfaceCreated(GL10 gl, EGLConfig config)104     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
105         /*
106          * By default, OpenGL enables features that improve quality
107          * but reduce performance. One might want to tweak that
108          * especially on software renderer.
109          */
110         gl.glDisable(GL10.GL_DITHER);
111 
112         /*
113          * Some one-time OpenGL initialization can be made here
114          * probably based on features of this particular context
115          */
116         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
117                 GL10.GL_FASTEST);
118 
119         if (mTranslucentBackground) {
120             gl.glClearColor(0,0,0,0);
121         } else {
122             gl.glClearColor(1,1,1,1);
123         }
124         gl.glEnable(GL10.GL_CULL_FACE);
125         gl.glShadeModel(GL10.GL_SMOOTH);
126         gl.glEnable(GL10.GL_DEPTH_TEST);
127     }
128 }