Home | History | Annotate | Download | only in lightingtest
      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.android.lightingtest;
     18 
     19 import java.nio.ByteBuffer;
     20 import java.nio.ByteOrder;
     21 import java.nio.FloatBuffer;
     22 
     23 import javax.microedition.khronos.egl.EGL10;
     24 import javax.microedition.khronos.egl.EGLConfig;
     25 import javax.microedition.khronos.opengles.GL10;
     26 
     27 import android.app.Activity;
     28 import android.content.Context;
     29 import android.opengl.GLSurfaceView;
     30 import android.os.Bundle;
     31 import android.util.Log;
     32 import android.view.MotionEvent;
     33 
     34 public class ClearActivity extends Activity {
     35     @Override
     36     protected void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38         mGLView = new ClearGLSurfaceView(this);
     39         setContentView(mGLView);
     40     }
     41 
     42     @Override
     43     protected void onPause() {
     44         super.onPause();
     45         mGLView.onPause();
     46     }
     47 
     48     @Override
     49     protected void onResume() {
     50         super.onResume();
     51         mGLView.onResume();
     52     }
     53     private GLSurfaceView mGLView;
     54 }
     55 
     56 class ClearGLSurfaceView extends GLSurfaceView {
     57     public ClearGLSurfaceView(Context context) {
     58         super(context);
     59         mRenderer = new ClearRenderer();
     60         setRenderer(mRenderer);
     61     }
     62 
     63     ClearRenderer mRenderer;
     64 }
     65 
     66 class ClearRenderer implements GLSurfaceView.Renderer {
     67     public ClearRenderer() {
     68     }
     69 
     70     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
     71         // Do nothing special.
     72     }
     73 
     74     public void onSurfaceChanged(GL10 gl, int w, int h) {
     75         // 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) {
     95         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
     96 
     97         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};
    101 
    102         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                 };
    107 
    108         final float v[] = new float[9];
    109         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 
    115         gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
    116         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);
    119         gl.glEnable(GL10.GL_LIGHT0);
    120 
    121         gl.glEnable(GL10.GL_LIGHTING);
    122 
    123 
    124         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    125         gl.glNormal3f(0, 0, 1);
    126 
    127 
    128         // 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;
    147         vb.put(v).position(0);
    148         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vb);
    149 
    150         // draw lower left triangle
    151         gl.glPushMatrix();
    152         gl.glTranslatef(pos[0], pos[1], pos[2]);
    153         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
    154         gl.glPopMatrix();
    155 
    156         // draw lower middle triangle
    157         gl.glPushMatrix();
    158         gl.glTranslatef(pos[3], pos[4], pos[5]);
    159         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
    160         gl.glPopMatrix();
    161 
    162         // draw lower right triangle
    163         gl.glPushMatrix();
    164         gl.glTranslatef(pos[6], pos[7], pos[8]);
    165         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
    166         gl.glPopMatrix();
    167     }
    168 
    169     public int[] getConfigSpec() {
    170         int[] configSpec = { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE };
    171         return configSpec;
    172     }
    173 }
    174 
    175