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