1 /*
2  * Copyright (C) 2009 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.gldual;
18 
19 import java.nio.ByteBuffer;
20 import java.nio.ByteOrder;
21 import java.nio.FloatBuffer;
22 import java.nio.ShortBuffer;
23 
24 import javax.microedition.khronos.egl.EGLConfig;
25 import javax.microedition.khronos.opengles.GL10;
26 
27 import android.opengl.GLSurfaceView;
28 import android.opengl.GLU;
29 import android.os.SystemClock;
30 
31 public class TriangleRenderer implements GLSurfaceView.Renderer{
32 
TriangleRenderer()33     public TriangleRenderer() {
34         mTriangle = new Triangle();
35     }
36 
onSurfaceCreated(GL10 gl, EGLConfig config)37     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
38         /*
39          * By default, OpenGL enables features that improve quality
40          * but reduce performance. One might want to tweak that
41          * especially on software renderer.
42          */
43         gl.glDisable(GL10.GL_DITHER);
44 
45         /*
46          * Some one-time OpenGL initialization can be made here
47          * probably based on features of this particular context
48          */
49         gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
50                 GL10.GL_FASTEST);
51 
52         gl.glClearColor(.5f, .5f, .5f, 1);
53         gl.glShadeModel(GL10.GL_SMOOTH);
54     }
55 
onDrawFrame(GL10 gl)56     public void onDrawFrame(GL10 gl) {
57         /*
58          * By default, OpenGL enables features that improve quality
59          * but reduce performance. One might want to tweak that
60          * especially on software renderer.
61          */
62         gl.glDisable(GL10.GL_DITHER);
63 
64         /*
65          * Usually, the first thing one might want to do is to clear
66          * the screen. The most efficient way of doing this is to use
67          * glClear().
68          */
69 
70         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
71 
72         /*
73          * Now we're ready to draw some 3D objects
74          */
75 
76         gl.glMatrixMode(GL10.GL_MODELVIEW);
77         gl.glLoadIdentity();
78 
79         GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
80 
81         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
82 
83         long time = SystemClock.uptimeMillis() % 4000L;
84         float angle = 0.090f * ((int) time);
85 
86         gl.glRotatef(angle, 0, 0, 1.0f);
87 
88         mTriangle.draw(gl);
89     }
90 
onSurfaceChanged(GL10 gl, int w, int h)91     public void onSurfaceChanged(GL10 gl, int w, int h) {
92         gl.glViewport(0, 0, w, h);
93 
94         /*
95         * Set our projection matrix. This doesn't have to be done
96         * each time we draw, but usually a new projection needs to
97         * be set when the viewport is resized.
98         */
99 
100         float ratio = (float) w / h;
101         gl.glMatrixMode(GL10.GL_PROJECTION);
102         gl.glLoadIdentity();
103         gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
104 
105     }
106 
107     private Triangle mTriangle;
108 }
109 
110 class Triangle {
Triangle()111     public Triangle() {
112 
113         // Buffers to be passed to gl*Pointer() functions
114         // must be direct, i.e., they must be placed on the
115         // native heap where the garbage collector cannot
116         // move them.
117         //
118         // Buffers with multi-byte datatypes (e.g., short, int, float)
119         // must have their byte order set to native order
120 
121         ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
122         vbb.order(ByteOrder.nativeOrder());
123         mFVertexBuffer = vbb.asFloatBuffer();
124 
125         ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
126         tbb.order(ByteOrder.nativeOrder());
127 
128         ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
129         ibb.order(ByteOrder.nativeOrder());
130         mIndexBuffer = ibb.asShortBuffer();
131 
132         // A unit-sided equalateral triangle centered on the origin.
133         float[] coords = {
134                 // X, Y, Z
135                 -0.5f, -0.25f, 0,
136                  0.5f, -0.25f, 0,
137                  0.0f,  0.559016994f, 0
138         };
139 
140         for (int i = 0; i < VERTS; i++) {
141             for(int j = 0; j < 3; j++) {
142                 mFVertexBuffer.put(coords[i*3+j] * 2.0f);
143             }
144         }
145 
146         for(int i = 0; i < VERTS; i++) {
147             mIndexBuffer.put((short) i);
148         }
149 
150         mFVertexBuffer.position(0);
151         mIndexBuffer.position(0);
152     }
153 
draw(GL10 gl)154     public void draw(GL10 gl) {
155         gl.glFrontFace(GL10.GL_CCW);
156         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mFVertexBuffer);
157         gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, VERTS,
158                 GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
159     }
160 
161     private final static int VERTS = 3;
162 
163     private FloatBuffer mFVertexBuffer;
164     private ShortBuffer mIndexBuffer;
165 }
166