blob: 3ae8c5ca29d778de513b20404fa6aea640a54c7c [file] [log] [blame]
Mathias Agopian46d7ccb2009-06-01 18:59:44 -07001/*
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
17package com.android.lightingtest;
18
19import java.nio.ByteBuffer;
20import java.nio.ByteOrder;
21import java.nio.FloatBuffer;
22
23import javax.microedition.khronos.egl.EGL10;
24import javax.microedition.khronos.egl.EGLConfig;
25import javax.microedition.khronos.opengles.GL10;
26
27import android.app.Activity;
28import android.content.Context;
29import android.opengl.GLSurfaceView;
30import android.os.Bundle;
31import android.util.Log;
32import android.view.MotionEvent;
33
34public class ClearActivity extends Activity {
35 @Override
36 protected void onCreate(Bundle savedInstanceState) {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070037 super.onCreate(savedInstanceState);
38 mGLView = new ClearGLSurfaceView(this);
39 setContentView(mGLView);
40 }
41
42 @Override
43 protected void onPause() {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070044 super.onPause();
45 mGLView.onPause();
46 }
47
48 @Override
49 protected void onResume() {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070050 super.onResume();
51 mGLView.onResume();
52 }
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070053 private GLSurfaceView mGLView;
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070054}
55
56class ClearGLSurfaceView extends GLSurfaceView {
57 public ClearGLSurfaceView(Context context) {
58 super(context);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070059 mRenderer = new ClearRenderer();
60 setRenderer(mRenderer);
61 }
62
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070063 ClearRenderer mRenderer;
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070064}
65
66class ClearRenderer implements GLSurfaceView.Renderer {
67 public ClearRenderer() {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070068 }
69
70 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
71 // Do nothing special.
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070072 }
73
74 public void onSurfaceChanged(GL10 gl, int w, int h) {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070075 // Compute the projection matrix
76 gl.glMatrixMode(GL10.GL_PROJECTION);
77 gl.glLoadIdentity();
78
79 // Compute the boundaries of the frustum
80 float fl = (float) (-(w / 2)) / 288;
81 float fr = (float) (w / 2) / 288;
82 float ft = (float) (h / 2) / 288;
83 float fb = (float) (-(h / 2)) / 288;
84
85 // Set the view frustum
86 gl.glFrustumf(fl, fr, fb, ft, 1.0f, 2000.0f);
87
88 // Set the viewport dimensions
89 gl.glMatrixMode(GL10.GL_MODELVIEW);
90 gl.glLoadIdentity();
91 gl.glViewport(0, 0, w, h);
92 }
93
94 public void onDrawFrame(GL10 gl) {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -070095 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
96
Mathias Agopiande15ddc2009-06-02 18:10:08 -070097 final float lightOff[] = {0.0f, 0.0f, 0.0f, 1.0f};
98 final float lightAmbient[] = {5.0f, 0.0f, 0.0f, 1.0f};
99 final float lightDiffuse[] = {0.0f, 2.0f, 0.0f, 0.0f};
100 final float lightPosSpot[] = {0.0f, 0.0f, -8.0f, 1.0f};
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700101
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700102 final float pos[] = {
103 -5.0f, -1.5f, 0.0f,
104 0.0f, -1.5f, 0.0f,
105 5.0f, -1.5f, 0.0f,
106 };
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700107
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700108 final float v[] = new float[9];
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700109 ByteBuffer vbb = ByteBuffer.allocateDirect(v.length*4);
110 vbb.order(ByteOrder.nativeOrder());
111 FloatBuffer vb = vbb.asFloatBuffer();
112
113 gl.glDisable(GL10.GL_DITHER);
114
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700115 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700116 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
117 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightOff, 0);
118 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosSpot, 0);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700119 gl.glEnable(GL10.GL_LIGHT0);
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700120
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700121 gl.glEnable(GL10.GL_LIGHTING);
122
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700123
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700124 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700125 gl.glNormal3f(0, 0, 1);
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700126
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700127
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700128 // draw first 3 triangles, without using transforms
129 for (int i=0 ; i<3 ; i++) {
130 v[0] = -1; v[1] =-1; v[2] = -10;
131 v[3] = 0; v[4] = 1; v[5] = -10;
132 v[6] = 1; v[7] =-1; v[8] = -10;
133 for (int j=0 ; j<3 ; j++) {
134 v[j*3+0] -= pos[i*3+0];
135 v[j*3+1] -= pos[i*3+1];
136 v[j*3+2] -= pos[i*3+2];
137 }
138 vb.put(v).position(0);
139 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb);
140 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
141 }
142
143 // draw the 2nd batch this time with transforms
144 v[0] = -1; v[1] =-1; v[2] = -10;
145 v[3] = 0; v[4] = 1; v[5] = -10;
146 v[6] = 1; v[7] =-1; v[8] = -10;
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700147 vb.put(v).position(0);
148 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700149
150 // draw lower left triangle
151 gl.glPushMatrix();
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700152 gl.glTranslatef(pos[0], pos[1], pos[2]);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700153 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
154 gl.glPopMatrix();
155
156 // draw lower middle triangle
157 gl.glPushMatrix();
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700158 gl.glTranslatef(pos[3], pos[4], pos[5]);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700159 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
160 gl.glPopMatrix();
161
162 // draw lower right triangle
163 gl.glPushMatrix();
Mathias Agopiande15ddc2009-06-02 18:10:08 -0700164 gl.glTranslatef(pos[6], pos[7], pos[8]);
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700165 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
166 gl.glPopMatrix();
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700167 }
168
169 public int[] getConfigSpec() {
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700170 int[] configSpec = { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
171 return configSpec;
172 }
Mathias Agopian46d7ccb2009-06-01 18:59:44 -0700173}
174