• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.kube;
18 
19 import android.opengl.GLSurfaceView;
20 
21 import javax.microedition.khronos.egl.EGLConfig;
22 import javax.microedition.khronos.opengles.GL10;
23 
24 
25 /**
26  * Example of how to use OpenGL|ES in a custom view
27  *
28  */
29 class KubeRenderer implements GLSurfaceView.Renderer {
30     public interface AnimationCallback {
animate()31         void animate();
32     }
33 
KubeRenderer(GLWorld world, AnimationCallback callback)34     public KubeRenderer(GLWorld world, AnimationCallback callback) {
35         mWorld = world;
36         mCallback = callback;
37     }
38 
onDrawFrame(GL10 gl)39     public void onDrawFrame(GL10 gl) {
40          if (mCallback != null) {
41              mCallback.animate();
42          }
43 
44         /*
45          * Usually, the first thing one might want to do is to clear
46          * the screen. The most efficient way of doing this is to use
47          * glClear(). However we must make sure to set the scissor
48          * correctly first. The scissor is always specified in window
49          * coordinates:
50          */
51 
52         gl.glClearColor(0.5f,0.5f,0.5f,1);
53         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
54 
55         /*
56          * Now we're ready to draw some 3D object
57          */
58 
59         gl.glMatrixMode(GL10.GL_MODELVIEW);
60         gl.glLoadIdentity();
61         gl.glTranslatef(0, 0, -3.0f);
62         gl.glScalef(0.5f, 0.5f, 0.5f);
63         gl.glRotatef(mAngle,        0, 1, 0);
64         gl.glRotatef(mAngle*0.25f,  1, 0, 0);
65 
66         gl.glColor4f(0.7f, 0.7f, 0.7f, 1.0f);
67         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
68         gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
69         gl.glEnable(GL10.GL_CULL_FACE);
70         gl.glShadeModel(GL10.GL_SMOOTH);
71         gl.glEnable(GL10.GL_DEPTH_TEST);
72 
73         mWorld.draw(gl);
74     }
75 
onSurfaceChanged(GL10 gl, int width, int height)76     public void onSurfaceChanged(GL10 gl, int width, int height) {
77         gl.glViewport(0, 0, width, height);
78 
79         /*
80          * Set our projection matrix. This doesn't have to be done
81          * each time we draw, but usually a new projection needs to be set
82          * when the viewport is resized.
83          */
84 
85         float ratio = (float)width / height;
86         gl.glMatrixMode(GL10.GL_PROJECTION);
87         gl.glLoadIdentity();
88         gl.glFrustumf(-ratio, ratio, -1, 1, 2, 12);
89 
90         /*
91          * By default, OpenGL enables features that improve quality
92          * but reduce performance. One might want to tweak that
93          * especially on software renderer.
94          */
95         gl.glDisable(GL10.GL_DITHER);
96         gl.glActiveTexture(GL10.GL_TEXTURE0);
97     }
98 
onSurfaceCreated(GL10 gl, EGLConfig config)99     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
100         // Nothing special, don't have any textures we need to recreate.
101     }
102 
setAngle(float angle)103     public void setAngle(float angle) {
104         mAngle = angle;
105     }
106 
getAngle()107     public float getAngle() {
108         return mAngle;
109     }
110 
111     private GLWorld mWorld;
112     private AnimationCallback mCallback;
113     private float mAngle;
114 }
115 
116 
117