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