opengl问题适用于机器人,但不适用于droid eris和其他人

时间:2010-05-16 12:29:48

标签: android opengl-es

这个GlRenderer在moto机器人上工作得很好,但在droid eris或其他Android手机上根本不能正常工作有人知道为什么吗?

package com.ntu.way2fungames.spacehockeybase;
import java.io.DataInputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.ntu.way2fungames.LoadFloatArray;
import com.ntu.way2fungames.OGLTriReader;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Handler;
import android.os.Message;

public class GlRenderer extends Thread implements Renderer {
        private float drawArray[];
        private float yoff;
        private float yoff2;
        private long lastRenderTime;
        private float[] yoffs= new float[10];
        int Width;
        int Height;

        private float[] pixelVerts = new float[] {

                +.0f,+.0f,2,
                +.5f,+.5f,0,
                +.5f,-.5f,0,

                +.0f,+.0f,2,
                +.5f,-.5f,0,
                -.5f,-.5f,0,

                +.0f,+.0f,2,
                -.5f,-.5f,0,
                -.5f,+.5f,0,

                +.0f,+.0f,2,
                -.5f,+.5f,0,
                +.5f,+.5f,0,
        };


        @Override
        public void run() {

}
        private float[] arenaWalls = new float[] {


                8.00f,2.00f,1f,2f,2f,1f,2.00f,8.00f,1f,8.00f,2.00f,1f,2.00f,8.00f,1f,8.00f,8.00f,1f,
                2.00f,8.00f,1f,2f,2f,1f,0.00f,0.00f,0f,2.00f,8.00f,1f,0.00f,0.00f,0f,0.00f,10.00f,0f,
                8.00f,8.00f,1f,2.00f,8.00f,1f,0.00f,10.00f,0f,8.00f,8.00f,1f,0.00f,10.00f,0f,10.00f,10.00f,0f,
                2f,2f,1f,8.00f,2.00f,1f,10.00f,0.00f,0f,2f,2f,1f,10.00f,0.00f,0f,0.00f,0.00f,0f,
                8.00f,2.00f,1f,8.00f,8.00f,1f,10.00f,10.00f,0f,8.00f,2.00f,1f,10.00f,10.00f,0f,10.00f,0.00f,0f,
                10.00f,10.00f,0f,0.00f,10.00f,0f,0.00f,0.00f,0f,10.00f,10.00f,0f,0.00f,0.00f,0f,10.00f,0.00f,0f,
                8.00f,6.00f,1f,8.00f,4.00f,1f,122f,4.00f,1f,8.00f,6.00f,1f,122f,4.00f,1f,122f,6.00f,1f,
                8.00f,6.00f,1f,122f,6.00f,1f,120f,7.00f,0f,8.00f,6.00f,1f,120f,7.00f,0f,10.00f,7.00f,0f,
                122f,4.00f,1f,8.00f,4.00f,1f,10.00f,3.00f,0f,122f,4.00f,1f,10.00f,3.00f,0f,120f,3.00f,0f,
                480f,10.00f,0f,470f,10.00f,0f,470f,0.00f,0f,480f,10.00f,0f,470f,0.00f,0f,480f,0.00f,0f,
                478f,2.00f,1f,478f,8.00f,1f,480f,10.00f,0f,478f,2.00f,1f,480f,10.00f,0f,480f,0.00f,0f,
                472f,2f,1f,478f,2.00f,1f,480f,0.00f,0f,472f,2f,1f,480f,0.00f,0f,470f,0.00f,0f,
                478f,8.00f,1f,472f,8.00f,1f,470f,10.00f,0f,478f,8.00f,1f,470f,10.00f,0f,480f,10.00f,0f,
                472f,8.00f,1f,472f,2f,1f,470f,0.00f,0f,472f,8.00f,1f,470f,0.00f,0f,470f,10.00f,0f,
                478f,2.00f,1f,472f,2f,1f,472f,8.00f,1f,478f,2.00f,1f,472f,8.00f,1f,478f,8.00f,1f,
                478f,846f,1f,472f,846f,1f,472f,852f,1f,478f,846f,1f,472f,852f,1f,478f,852f,1f,
                472f,852f,1f,472f,846f,1f,470f,844f,0f,472f,852f,1f,470f,844f,0f,470f,854f,0f,
                478f,852f,1f,472f,852f,1f,470f,854f,0f,478f,852f,1f,470f,854f,0f,480f,854f,0f,
                472f,846f,1f,478f,846f,1f,480f,844f,0f,472f,846f,1f,480f,844f,0f,470f,844f,0f,
                478f,846f,1f,478f,852f,1f,480f,854f,0f,478f,846f,1f,480f,854f,0f,480f,844f,0f,
                480f,854f,0f,470f,854f,0f,470f,844f,0f,480f,854f,0f,470f,844f,0f,480f,844f,0f,
                10.00f,854f,0f,0.00f,854f,0f,0.00f,844f,0f,10.00f,854f,0f,0.00f,844f,0f,10.00f,844f,0f,
                8.00f,846f,1f,8.00f,852f,1f,10.00f,854f,0f,8.00f,846f,1f,10.00f,854f,0f,10.00f,844f,0f,
                2f,846f,1f,8.00f,846f,1f,10.00f,844f,0f,2f,846f,1f,10.00f,844f,0f,0.00f,844f,0f,
                8.00f,852f,1f,2.00f,852f,1f,0.00f,854f,0f,8.00f,852f,1f,0.00f,854f,0f,10.00f,854f,0f,
                2.00f,852f,1f,2f,846f,1f,0.00f,844f,0f,2.00f,852f,1f,0.00f,844f,0f,0.00f,854f,0f,
                8.00f,846f,1f,2f,846f,1f,2.00f,852f,1f,8.00f,846f,1f,2.00f,852f,1f,8.00f,852f,1f,
                6f,846f,1f,4f,846f,1f,4f,8f,1f,6f,846f,1f,4f,8f,1f,6f,8f,1f,
                6f,846f,1f,6f,8f,1f,7f,10f,0f,6f,846f,1f,7f,10f,0f,7f,844f,0f,
                4f,8f,1f,4f,846f,1f,3f,844f,0f,4f,8f,1f,3f,844f,0f,3f,10f,0f,
                474f,8f,1f,474f,846f,1f,473f,844f,0f,474f,8f,1f,473f,844f,0f,473f,10f,0f,
                476f,846f,1f,476f,8f,1f,477f,10f,0f,476f,846f,1f,477f,10f,0f,477f,844f,0f,
                476f,846f,1f,474f,846f,1f,474f,8f,1f,476f,846f,1f,474f,8f,1f,476f,8f,1f,
                130f,10.00f,0f,120f,10.00f,0f,120f,0.00f,0f,130f,10.00f,0f,120f,0.00f,0f,130f,0.00f,0f,
                128f,2.00f,1f,128f,8.00f,1f,130f,10.00f,0f,128f,2.00f,1f,130f,10.00f,0f,130f,0.00f,0f,
                122f,2f,1f,128f,2.00f,1f,130f,0.00f,0f,122f,2f,1f,130f,0.00f,0f,120f,0.00f,0f,
                128f,8.00f,1f,122f,8.00f,1f,120f,10.00f,0f,128f,8.00f,1f,120f,10.00f,0f,130f,10.00f,0f,
                122f,8.00f,1f,122f,2f,1f,120f,0.00f,0f,122f,8.00f,1f,120f,0.00f,0f,120f,10.00f,0f,
                128f,2.00f,1f,122f,2f,1f,122f,8.00f,1f,128f,2.00f,1f,122f,8.00f,1f,128f,8.00f,1f,
                352f,8.00f,1f,358f,8.00f,1f,358f,2.00f,1f,352f,8.00f,1f,358f,2.00f,1f,352f,2.00f,1f,
                358f,2.00f,1f,358f,8.00f,1f,360f,10.00f,0f,358f,2.00f,1f,360f,10.00f,0f,360f,0.00f,0f,
                352f,2.00f,1f,358f,2.00f,1f,360f,0.00f,0f,352f,2.00f,1f,360f,0.00f,0f,350f,0.00f,0f,
                358f,8.00f,1f,352f,8.00f,1f,350f,10.00f,0f,358f,8.00f,1f,350f,10.00f,0f,360f,10.00f,0f,
                352f,8.00f,1f,352f,2.00f,1f,350f,0.00f,0f,352f,8.00f,1f,350f,0.00f,0f,350f,10.00f,0f,
                350f,0.00f,0f,360f,0.00f,0f,360f,10.00f,0f,350f,0.00f,0f,360f,10.00f,0f,350f,10.00f,0f,
                358f,6.00f,1f,472f,6.00f,1f,470f,7.00f,0f,358f,6.00f,1f,470f,7.00f,0f,360f,7.00f,0f,
                472f,4.00f,1f,358f,4.00f,1f,360f,3.00f,0f,472f,4.00f,1f,360f,3.00f,0f,470f,3.00f,0f,
                472f,4.00f,1f,472f,6.00f,1f,358f,6.00f,1f,472f,4.00f,1f,358f,6.00f,1f,358f,4.00f,1f,
                472f,848f,1f,472f,850f,1f,358f,850f,1f,472f,848f,1f,358f,850f,1f,358f,848f,1f,
                472f,848f,1f,358f,848f,1f,360f,847f,0f,472f,848f,1f,360f,847f,0f,470f,847f,0f,
                358f,850f,1f,472f,850f,1f,470f,851f,0f,358f,850f,1f,470f,851f,0f,360f,851f,0f,
                350f,844f,0f,360f,844f,0f,360f,854f,0f,350f,844f,0f,360f,854f,0f,350f,854f,0f,
                352f,852f,1f,352f,846f,1f,350f,844f,0f,352f,852f,1f,350f,844f,0f,350f,854f,0f,
                358f,852f,1f,352f,852f,1f,350f,854f,0f,358f,852f,1f,350f,854f,0f,360f,854f,0f,
                352f,846f,1f,358f,846f,1f,360f,844f,0f,352f,846f,1f,360f,844f,0f,350f,844f,0f,
                358f,846f,1f,358f,852f,1f,360f,854f,0f,358f,846f,1f,360f,854f,0f,360f,844f,0f,
                352f,852f,1f,358f,852f,1f,358f,846f,1f,352f,852f,1f,358f,846f,1f,352f,846f,1f,
                128f,846f,1f,122f,846f,1f,122f,852f,1f,128f,846f,1f,122f,852f,1f,128f,852f,1f,
                122f,852f,1f,122f,846f,1f,120f,844f,0f,122f,852f,1f,120f,844f,0f,120f,854f,0f,
                128f,852f,1f,122f,852f,1f,120f,854f,0f,128f,852f,1f,120f,854f,0f,130f,854f,0f,
                122f,846f,1f,128f,846f,1f,130f,844f,0f,122f,846f,1f,130f,844f,0f,120f,844f,0f,
                128f,846f,1f,128f,852f,1f,130f,854f,0f,128f,846f,1f,130f,854f,0f,130f,844f,0f,
                130f,854f,0f,120f,854f,0f,120f,844f,0f,130f,854f,0f,120f,844f,0f,130f,844f,0f,
                122f,848f,1f,8f,848f,1f,10f,847f,0f,122f,848f,1f,10f,847f,0f,120f,847f,0f,
                8f,850f,1f,122f,850f,1f,120f,851f,0f,8f,850f,1f,120f,851f,0f,10f,851f,0f,
                8f,850f,1f,8f,848f,1f,122f,848f,1f,8f,850f,1f,122f,848f,1f,122f,850f,1f,
                10f,847f,0f,120f,847f,0f,124.96f,829.63f,-0.50f,10f,847f,0f,124.96f,829.63f,-0.50f,19.51f,829.63f,-0.50f,
                130f,844f,0f,130f,854f,0f,134.55f,836.34f,-0.50f,130f,844f,0f,134.55f,836.34f,-0.50f,134.55f,826.76f,-0.50f,
                350f,844f,0f,350f,854f,0f,345.45f,836.34f,-0.50f,350f,844f,0f,345.45f,836.34f,-0.50f,345.45f,826.76f,-0.50f,
                360f,847f,0f,470f,847f,0f,460.49f,829.63f,-0.50f,360f,847f,0f,460.49f,829.63f,-0.50f,355.04f,829.63f,-0.50f,
                470f,7.00f,0f,360f,7.00f,0f,355.04f,24.37f,-0.50f,470f,7.00f,0f,355.04f,24.37f,-0.50f,460.49f,24.37f,-0.50f,
                350f,10.00f,0f,350f,0.00f,0f,345.45f,17.66f,-0.50f,350f,10.00f,0f,345.45f,17.66f,-0.50f,345.45f,27.24f,-0.50f,
                130f,10.00f,0f,130f,0.00f,0f,134.55f,17.66f,-0.50f,130f,10.00f,0f,134.55f,17.66f,-0.50f,134.55f,27.24f,-0.50f,
                473f,844f,0f,473f,10f,0f,463.36f,27.24f,-0.50f,473f,844f,0f,463.36f,27.24f,-0.50f,463.36f,826.76f,-0.50f,
                7f,10f,0f,7f,844f,0f,16.64f,826.76f,-0.50f,7f,10f,0f,16.64f,826.76f,-0.50f,16.64f,27.24f,-0.50f,
                120f,7.00f,0f,10.00f,7.00f,0f,19.51f,24.37f,-0.50f,120f,7.00f,0f,19.51f,24.37f,-0.50f,124.96f,24.37f,-0.50f,
                120f,7.00f,0f,130f,10.00f,0f,134.55f,27.24f,-0.50f,120f,7.00f,0f,134.55f,27.24f,-0.50f,124.96f,24.37f,-0.50f,
                10.00f,7.00f,0f,7f,10f,0f,16.64f,27.24f,-0.50f,10.00f,7.00f,0f,16.64f,27.24f,-0.50f,19.51f,24.37f,-0.50f,
                350f,10.00f,0f,360f,7.00f,0f,355.04f,24.37f,-0.50f,350f,10.00f,0f,355.04f,24.37f,-0.50f,345.45f,27.24f,-0.50f,
                473f,10f,0f,470f,7.00f,0f,460.49f,24.37f,-0.50f,473f,10f,0f,460.49f,24.37f,-0.50f,463.36f,27.24f,-0.50f,
                473f,844f,0f,470f,847f,0f,460.49f,829.63f,-0.50f,473f,844f,0f,460.49f,829.63f,-0.50f,463.36f,826.76f,-0.50f,
                360f,847f,0f,350f,844f,0f,345.45f,826.76f,-0.50f,360f,847f,0f,345.45f,826.76f,-0.50f,355.04f,829.63f,-0.50f,
                130f,844f,0f,120f,847f,0f,124.96f,829.63f,-0.50f,130f,844f,0f,124.96f,829.63f,-0.50f,134.55f,826.76f,-0.50f,
                7f,844f,0f,10f,847f,0f,19.51f,829.63f,-0.50f,7f,844f,0f,19.51f,829.63f,-0.50f,16.64f,826.76f,-0.50f,
                19.51f,829.63f,-0.50f,124.96f,829.63f,-0.50f,136.47f,789.37f,-2f,19.51f,829.63f,-0.50f,136.47f,789.37f,-2f,41.56f,789.37f,-2f,
                134.55f,826.76f,-0.50f,134.55f,836.34f,-0.50f,145.09f,795.41f,-2f,134.55f,826.76f,-0.50f,145.09f,795.41f,-2f,145.09f,786.78f,-2f,
                345.45f,826.76f,-0.50f,345.45f,836.34f,-0.50f,334.91f,795.41f,-2f,345.45f,826.76f,-0.50f,334.91f,795.41f,-2f,334.91f,786.78f,-2f,
                355.04f,829.63f,-0.50f,460.49f,829.63f,-0.50f,438.44f,789.37f,-2f,355.04f,829.63f,-0.50f,438.44f,789.37f,-2f,343.53f,789.37f,-2f,
                460.49f,24.37f,-0.50f,355.04f,24.37f,-0.50f,343.53f,64.63f,-2f,460.49f,24.37f,-0.50f,343.53f,64.63f,-2f,438.44f,64.63f,-2f,
                345.45f,27.24f,-0.50f,345.45f,17.66f,-0.50f,334.91f,58.59f,-2f,345.45f,27.24f,-0.50f,334.91f,58.59f,-2f,334.91f,67.22f,-2f,
                134.55f,27.24f,-0.50f,134.55f,17.66f,-0.50f,145.09f,58.59f,-2f,134.55f,27.24f,-0.50f,145.09f,58.59f,-2f,145.09f,67.22f,-2f,
                463.36f,826.76f,-0.50f,463.36f,27.24f,-0.50f,441.03f,67.22f,-2f,463.36f,826.76f,-0.50f,441.03f,67.22f,-2f,441.03f,786.78f,-2f,
                16.64f,27.24f,-0.50f,16.64f,826.76f,-0.50f,38.97f,786.78f,-2f,16.64f,27.24f,-0.50f,38.97f,786.78f,-2f,38.97f,67.22f,-2f,
                124.96f,24.37f,-0.50f,19.51f,24.37f,-0.50f,41.56f,64.63f,-2f,124.96f,24.37f,-0.50f,41.56f,64.63f,-2f,136.47f,64.63f,-2f,
                124.96f,24.37f,-0.50f,134.55f,27.24f,-0.50f,145.09f,67.22f,-2f,124.96f,24.37f,-0.50f,145.09f,67.22f,-2f,136.47f,64.63f,-2f,
                19.51f,24.37f,-0.50f,16.64f,27.24f,-0.50f,38.97f,67.22f,-2f,19.51f,24.37f,-0.50f,38.97f,67.22f,-2f,41.56f,64.63f,-2f,
                345.45f,27.24f,-0.50f,355.04f,24.37f,-0.50f,343.53f,64.63f,-2f,345.45f,27.24f,-0.50f,343.53f,64.63f,-2f,334.91f,67.22f,-2f,
                463.36f,27.24f,-0.50f,460.49f,24.37f,-0.50f,438.44f,64.63f,-2f,463.36f,27.24f,-0.50f,438.44f,64.63f,-2f,441.03f,67.22f,-2f,
                463.36f,826.76f,-0.50f,460.49f,829.63f,-0.50f,438.44f,789.37f,-2f,463.36f,826.76f,-0.50f,438.44f,789.37f,-2f,441.03f,786.78f,-2f,
                355.04f,829.63f,-0.50f,345.45f,826.76f,-0.50f,334.91f,786.78f,-2f,355.04f,829.63f,-0.50f,334.91f,786.78f,-2f,343.53f,789.37f,-2f,
                134.55f,826.76f,-0.50f,124.96f,829.63f,-0.50f,136.47f,789.37f,-2f,134.55f,826.76f,-0.50f,136.47f,789.37f,-2f,145.09f,786.78f,-2f,
                16.64f,826.76f,-0.50f,19.51f,829.63f,-0.50f,41.56f,789.37f,-2f,16.64f,826.76f,-0.50f,41.56f,789.37f,-2f,38.97f,786.78f,-2f,



        };


        private float[] backgroundData = new float[] {
//                # ,Scale, Speed,
                300 , 1.05f, .001f,
                150 , 1.07f, .002f,
                075 , 1.10f, .003f,
                040 , 1.12f, .006f,
                 20 , 1.15f, .012f,
                 10 , 1.25f, .025f,
                 05 , 1.50f, .050f,
                  3 , 2.00f, .100f,
                  2 , 3.00f, .200f,             
                };

        private float[] triangleCoords = new float[] {
                 0,  -25, 0,
             -.75f,   -1, 0,
             +.75f,   -1, 0,

                 0, +2, 0,
              -.99f, -1, 0,
               .99f, -1, 0,

        };

        private float[] triangleColors = new float[] {
                1.0f,  1.0f, 1.0f, 0.05f,
                1.0f,  1.0f, 1.0f, 0.5f,
                1.0f,  1.0f, 1.0f, 0.5f,

                1.0f,  1.0f, 1.0f, 1.0f,
                1.0f,  1.0f, 1.0f, 0.5f,
                1.0f,  1.0f, 1.0f, 0.5f,

       };




        private float[] drawArray2;
        private FloatBuffer drawBuffer2;
        private float[] colorArray2;

        private static FloatBuffer colorBuffer;
        private static FloatBuffer triangleBuffer;
        private static FloatBuffer quadBuffer;
        private static FloatBuffer drawBuffer;
        private float[] backgroundVerts;
        private FloatBuffer backgroundVertsWrapped;
        private float[] backgroundColors;
        private Buffer backgroundColorsWraped;
        private FloatBuffer backgroundColorsWrapped;

        private FloatBuffer arenaWallsWrapped;
        private FloatBuffer arenaColorsWrapped;
        private FloatBuffer arena2VertsWrapped;
        private FloatBuffer arena2ColorsWrapped;
        private long wallHitStartTime;
        private int wallHitDrawTime;
        private FloatBuffer pixelVertsWrapped;
        private float[] wallHit;
        private FloatBuffer pixelColorsWrapped;
        //private float[] pitVerts;
        private Resources lResources;
        private FloatBuffer pitVertsWrapped;
        private FloatBuffer pitColorsWrapped;
        private boolean arena2;
        private long lastStartTime;
        private long startTime;

        private int state=1;
        private long introEndTime;
        protected long introTotalTime =8000;
        protected long introStartTime;
        private boolean initDone= false;
        private static int stateIntro = 0;
        private static int stateGame  = 1;

        public GlRenderer(spacehockey nspacehockey) {
            lResources = nspacehockey.getResources();

            nspacehockey.SetHandlerToGLRenderer(new Handler() {

                @Override
                public void handleMessage(Message m) {
                    if (m.what ==0){
                        wallHit = m.getData().getFloatArray("wall hit");
                        wallHitStartTime =System.currentTimeMillis();
                        wallHitDrawTime = 1000;
                    }else if (m.what ==1){
                        //state = stateIntro;
                        introEndTime= System.currentTimeMillis()+introTotalTime ;
                        introStartTime = System.currentTimeMillis();
                    }
            }});
        }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {

                gl.glShadeModel(GL10.GL_SMOOTH);
                gl.glClearColor(.01f, .01f, .01f, .1f);

                gl.glClearDepthf(1.0f);
                gl.glEnable(GL10.GL_DEPTH_TEST);
                gl.glDepthFunc(GL10.GL_LEQUAL);

                gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);


        }

        private float SumOfStrideI(float[] data, int offset, int stride) {
            int sum= 0;
            for (int i=offset;i<data.length-1;i=i+stride){
                sum = (int) (data[i]+sum);
            }
            return sum;
        }

    public void onDrawFrame(GL10 gl) {
        if (state== stateIntro){DrawIntro(gl);}
        if (state== stateGame){DrawGame(gl);}
    }

    private void DrawIntro(GL10 gl) {
        startTime = System.currentTimeMillis();
        if (startTime< introEndTime){
            float ptd = (float)(startTime- introStartTime)/(float)introTotalTime;
            float ptl = 1-ptd;
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//dont move        
            gl.glMatrixMode(GL10.GL_MODELVIEW);

                int setVertOff = 0;
                gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
                gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
                gl.glColorPointer(4, GL10.GL_FLOAT, 0, backgroundColorsWrapped);
                for (int i = 0; i < backgroundData.length / 3; i = i + 1) {
                    int setoff = i * 3;
                    int setVertLen = (int) backgroundData[setoff];

                    yoffs[i] = (backgroundData[setoff + 2]*(90+(ptl*250))) + yoffs[i];
                    if (yoffs[i] > Height) {yoffs[i] = 0;}

                    gl.glPushMatrix();
                    //gl.glTranslatef(0, -(Height/2), 0);
                    //gl.glScalef(1f, 1f+(ptl*2), 1f);
                    //gl.glTranslatef(0, +(Height/2), 0);

                    gl.glTranslatef(0, yoffs[i], i+60);
                    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, backgroundVertsWrapped);
                    gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 2 * 3) - 0, (setVertLen * 2 * 3) - 1);
                    gl.glTranslatef(0, -Height, 0);
                    gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 2 * 3) - 0, (setVertLen * 2 * 3) - 1);

                    setVertOff = (int) (setVertOff + setVertLen);
                    gl.glPopMatrix();
                }
                gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
                gl.glDisableClientState(GL10.GL_COLOR_ARRAY);




        }else{state = stateGame;}
    }

        private void DrawGame(GL10 gl) {
            lastStartTime = startTime;
            startTime = System.currentTimeMillis();
            long moveTime = startTime-lastStartTime;

            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);//dont move        
            gl.glMatrixMode(GL10.GL_MODELVIEW);

                int setVertOff = 0;
                gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
                gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
                gl.glColorPointer(4, GL10.GL_FLOAT, 0, backgroundColorsWrapped);
                for (int i = 0; i < backgroundData.length / 3; i = i + 1) {
                    int setoff = i * 3;
                    int setVertLen = (int) backgroundData[setoff];

                    yoffs[i] = (backgroundData[setoff + 2]*moveTime) + yoffs[i];
                    if (yoffs[i] > Height) {yoffs[i] = 0;}

                    gl.glPushMatrix();
                    gl.glTranslatef(0, yoffs[i], i+60);
                    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, backgroundVertsWrapped);
                    gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 6) - 0, (setVertLen *6) - 1);
                    gl.glTranslatef(0, -Height, 0);
                    gl.glDrawArrays(GL10.GL_TRIANGLES, (setVertOff * 6) - 0, (setVertLen *6) - 1);

                    setVertOff = (int) (setVertOff + setVertLen);
                    gl.glPopMatrix();
                }       


                //arena frame
                gl.glPushMatrix();
                gl.glVertexPointer(3, GL10.GL_FLOAT, 0, arenaWallsWrapped);
                gl.glColorPointer(4, GL10.GL_FLOAT, 0, arenaColorsWrapped);
                gl.glColor4f(.1f, .5f, 1f, 1f);
                gl.glTranslatef(0, 0, 50);
                gl.glDrawArrays(GL10.GL_TRIANGLES, 0, (int)(arenaWalls.length / 3));
                gl.glPopMatrix();

                //arena2 frame
                if (arena2 == true){
                    gl.glLoadIdentity();
                    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, pitVertsWrapped);
                    gl.glColorPointer(4, GL10.GL_FLOAT, 0, pitColorsWrapped);
                    gl.glTranslatef(0, -Height, 40);
                    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, (int)(pitVertsWrapped.capacity() / 3));
                }

                if (wallHitStartTime != 0) {
                    float timeRemaining = (wallHitStartTime + wallHitDrawTime)-System.currentTimeMillis();
                    if (timeRemaining>0) {
                        gl.glPushMatrix();
                        float percentDone = 1-(timeRemaining/wallHitDrawTime);
                        gl.glLoadIdentity();
                        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, pixelVertsWrapped);
                        gl.glColorPointer(4, GL10.GL_FLOAT, 0, pixelColorsWrapped);
                        gl.glTranslatef(wallHit[0], wallHit[1], 0);
                        gl.glScalef(8, Height*percentDone, 0);
                        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 12);
                        gl.glPopMatrix();
                    } else {
                        wallHitStartTime = 0;
                    }
                }

                gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
                gl.glDisableClientState(GL10.GL_COLOR_ARRAY);


    }

        public void init(GL10 gl) {
            if (arena2 == true) {
                AssetManager assetManager = lResources.getAssets();

                try {
                    // byte[] ba = {111,111};
                    DataInputStream Dis = new DataInputStream(assetManager
                            .open("arena2.ogl"));
                    pitVertsWrapped = LoadFloatArray.FromDataInputStream(Dis);
                    pitColorsWrapped = MakeFakeLighting(pitVertsWrapped.array(),
                            .25f, .50f, 1f, 200, .5f);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if ((Height != 854) || (Width != 480)) {
                arenaWalls = ScaleFloats(arenaWalls, Width / 480f, Height / 854f);
            }
            arenaWallsWrapped = FloatBuffer.wrap(arenaWalls);
            arenaColorsWrapped = MakeFakeLighting(arenaWalls, .03f, .16f, .33f,
                    .33f, 3);
            pixelVertsWrapped = FloatBuffer.wrap(pixelVerts);
            pixelColorsWrapped = MakeFakeLighting(pixelVerts, .03f, .16f, .33f,
                    .10f, 20);

            initDone=true;
        }

        public void onSurfaceChanged(GL10 gl, int nwidth, int nheight) {
                Width= nwidth;
                Height = nheight;

             // avoid division by zero
                if (Height == 0)
                    Height = 1;
                // draw on the entire screen
                gl.glViewport(0, 0, Width, Height);
                // setup projection matrix
                gl.glMatrixMode(GL10.GL_PROJECTION);
                gl.glLoadIdentity();
                gl.glOrthof(0, Width, Height, 0, 100, -100);
                // gl.glOrthof(-nwidth*2, nwidth*2, nheight*2,-nheight*2, 100, -100);
                // GLU.gluPerspective(gl, 180.0f, (float)nwidth / (float)nheight,
                // 1000.0f, -1000.0f);
                gl.glEnable(GL10.GL_BLEND);
                gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
                System.gc();

                if (initDone == false){
                    SetupStars();
                    init(gl);
                    }

        }

        public void SetupStars(){
            backgroundVerts = new float[(int) SumOfStrideI(backgroundData,0,3)*triangleCoords.length];
            backgroundColors = new float[(int) SumOfStrideI(backgroundData,0,3)*triangleColors.length];

            int iii=0;
            int vc=0;
            float ascale=1;
            for (int i=0;i<backgroundColors.length-1;i=i+1){
                if (iii==0){ascale = (float) Math.random();}

                if (vc==3){
                    backgroundColors[i]= (float) (triangleColors[iii]*(ascale));
                }else if(vc==2){
                    backgroundColors[i]= (float) (triangleColors[iii]-(Math.random()*.2));
                }else{
                    backgroundColors[i]= (float) (triangleColors[iii]-(Math.random()*.3));

                }
                iii=iii+1;if (iii> triangleColors.length-1){iii=0;}                 
                vc=vc+1;  if (vc>3){vc=0;}
            }

            int ii=0;
            int i =0;
            int set =0;                
            while(ii<backgroundVerts.length-1){
                float scale = (float) backgroundData[(set*3)+1];
                int   length= (int) backgroundData[(set*3)];

                for (i=0;i<length;i=i+1){
                    if (set ==0){
                        AddVertsToArray(ScaleFloats(triangleCoords, scale,scale*.25f), backgroundVerts, (float)(Math.random()*Width),(float) (Math.random()*Height), ii);   
                    }else{
                        AddVertsToArray(ScaleFloats(triangleCoords, scale), backgroundVerts, (float)(Math.random()*Width),(float) (Math.random()*Height), ii);}
                    ii=ii+triangleCoords.length;
                }

                set=set+1;
            }
            backgroundVertsWrapped = FloatBuffer.wrap(backgroundVerts);
            backgroundColorsWrapped = FloatBuffer.wrap(backgroundColors);
        }
        public void AddVertsToArray(float[] sva,float[]dva,float ox,float oy,int start){
            //x         
            for (int i=0;i<sva.length;i=i+3){
                if((start+i)<dva.length){dva[start+i]= sva[i]+ox;}
                }
            //y         
            for (int i=1;i<sva.length;i=i+3){
                if((start+i)<dva.length){dva[start+i]= sva[i]+oy;}
                }
            //z
            for (int i=2;i<sva.length;i=i+3){
                if((start+i)<dva.length){dva[start+i]= sva[i];}
                }
        }
        public FloatBuffer MakeFakeLighting(float[] sa,float r, float g,float b,float a,float multby){
            float[] da = new float[((sa.length/3)*4)];
            int vertex=0;
            for (int i=0;i<sa.length;i=i+3){
                if (sa[i+2]>=1){
                    da[(vertex*4)+0]= r*multby*sa[i+2];
                    da[(vertex*4)+1]= g*multby*sa[i+2];
                    da[(vertex*4)+2]= b*multby*sa[i+2];
                    da[(vertex*4)+3]= a*multby*sa[i+2];
                }else if (sa[i+2]<=-1){
                    float divisor = (multby*(-sa[i+2]));
                    da[(vertex*4)+0]= r / divisor;
                    da[(vertex*4)+1]= g / divisor;
                    da[(vertex*4)+2]= b / divisor;
                    da[(vertex*4)+3]= a / divisor;

                }else{
                    da[(vertex*4)+0]= r;
                    da[(vertex*4)+1]= g;
                    da[(vertex*4)+2]= b;
                    da[(vertex*4)+3]= a;                    
                }
                vertex = vertex+1;
            }
            return FloatBuffer.wrap(da);
        }
        public float[] ScaleFloats(float[] va,float s){
            float[] reta= new float[va.length];

            for (int i=0;i<va.length;i=i+1){
                reta[i]=va[i]*s;
            }
            return reta;
        }
        public float[] ScaleFloats(float[] va,float sx,float sy){
            float[] reta= new float[va.length];
            int  cnt = 0;
            for (int i=0;i<va.length;i=i+1){
                if      (cnt==0){reta[i]=va[i]*sx;}
                else if (cnt==1){reta[i]=va[i]*sy;}
                else if (cnt==2){reta[i]=va[i];}

                cnt = cnt +1;if (cnt>2){cnt=0;}
            }
            return reta;
        }


}

0 个答案:

没有答案