Android Opengl渲染器在短时间内关闭

时间:2012-02-28 05:47:02

标签: android opengl-es

好的,我现在已经在这个opengl渲染上工作了一段时间。但是如果我渲染大背景,我就无法提高帧速率。但主要问题是应用程序在短时间内关闭。

我会感激任何帮助。

我的渲染类:

    package opengl.renderer;

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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.Build;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

public class GlRenderer implements Renderer {


    float Scwidth;
    float Scheight;

    float framecount;
    Context context;

    public LevelBackVisual square=new LevelBackVisual();

    public sprite text;

    long starttime;

    sprite babytest=null;

    float density;

    Bitmap image2;
    float tempy=0;

    // constructor
    public GlRenderer(Context context , float screenwidth, float screenheight){

        this.Scwidth = screenwidth;
        this.Scheight = screenheight;


        this.context = context;






    }

    /* (non-Javadoc)
     * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
     */

    float xfocus=0;
    float yfocus= 0;


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        //this.text.loadGLTexture(gl, this.context);
        // Load the texture for the square 

        gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
        gl.glShadeModel(GL10.GL_FLAT);          //Enable Smooth Shading
        //gl.glClearColor(0f, 100.0f, 0.0f, 100f);  //Black Background
        //gl.glClearDepthf(24f);                    //Depth Buffer Setup
        gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

        //Really Nice Perspective Calculations
        //gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        // TODO Auto-generated method stub 



    }
    @Override
    public void onDrawFrame(GL10 gl) {








        // TODO Auto-generated method stub
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        // Reset the Modelview Matrix
        gl.glLoadIdentity();


        float version=0;
        try{
        version= Float.valueOf(Build.VERSION.RELEASE);
        }catch(Exception e){}

        Log.d("matrics", ""+version); 


        if (version==1.5){
            gl.glTranslatef(xfocus, yfocus, -581);

        }else{
            gl.glTranslatef(xfocus, yfocus, -966);  // move 5 unit INTO the screen 
        }
        // Drawing

        //ADDJUST HERE FOR BABYGAME SIZE CORRENCTION // LAS GOOD SETTINGS: gl.glTranslatef(-240.0f, 400f, -966.0f);
    //  gl.glTranslatef(-2.5f, 4.2f, -10.0f);  // move 5 unit INTO the screen
    //  gl.glTranslatef(xfocus, yfocus, -966f);  // move 5 unit INTO the screen

                                             // is the same as moving the camera 5 units away
        //square.draw(gl);                   // Draw the square

        /// Move x and y ont both squares
        //this.square2.x+=.02f; 
        //this.square.y+=.01f; 
        //this.square3.y-=.03f;  
        square = new LevelBackVisual(1, this.context);



    //  square3.draw(gl);                    // Draw the square
    //  square2.draw(gl);                    // Draw the square
        square.width=this.Scwidth;
        square.height=this.Scheight;


        xfocus=-square.width/2;

        yfocus= (square.height/2); 

        square.loadGLTexture(gl); 

        square.draw(gl);                     // Draw the square


        if (this.starttime==0){this.starttime=System.currentTimeMillis();}
        framecount++;
        String fpstext = ""+this.framecount/((System.currentTimeMillis()-starttime)/1000);

        Log.d("fps", "fps: "+fpstext); 


        Paint paint= new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(25);
        //paint.setAntiAlias(true);

        this.image2=Bitmap.createBitmap(128,64, Bitmap.Config.ARGB_8888);
        this.image2.eraseColor(Color.TRANSPARENT);
        Canvas canvas2= new Canvas(this.image2);
        canvas2.drawARGB(50, 0, 0, 0);
        //canvas.setViewport(100, 50);
        canvas2.drawText(fpstext, 10, 20, paint);

        //text= new sprite(image);



        //sprite tempsprite=new sprite(image2);

        text= new sprite(this.image2);
        text.width=120f;
        text.height= 64f;
        //this.text.loadGLTexture(gl, this.context);
        text.loadGLTexture(gl);

        text.draw(gl);  


        this.babytest= new sprite(BitmapFactory.decodeResource(context.getResources(), R.drawable.jimmy) );

        this.babytest.y= tempy;
        babytest.width=50f; 
        babytest.height=50f;
        babytest.y-=3;
        tempy= babytest.y;
        babytest.loadGLTexture(gl);
        babytest.draw(gl);


    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {



        // Drawing

        //ADDJUST HERE FOR BABYGAME SIZE CORRENCTION // LAS GOOD SETTINGS: gl.glTranslatef(-240.0f, 400f, -966.0f);
    //  gl.glTranslatef(-2.5f, 4.2f, -10.0f);  // move 5 unit INTO the screen

        if(height == 0){                //Prevent A Divide By Zero By
            height = 1;

        }
           // gl.glOrthof(width/2, height/2, 480f, 0, 0, 1000);
        gl.glViewport(0, 0, width, height);      // Reset The Current Viewport
        gl.glMatrixMode(GL10.GL_PROJECTION);     // Select The Projection Matrix
        gl.glLoadIdentity();                    // Reset The Modelview Matrix

        //Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 2000.0f);
        //GLU.gluOrtho2D(gl, 0, width, height, 0); 
        gl.glMatrixMode(GL10.GL_MODELVIEW);     // Select The Modelview Matrix
        gl.glLoadIdentity();                    // Reset The Modelview Matrix

       // GLU.gluLookAt(gl, width/2, height/2, -5, 100f, 100f, 0f, 0f, 1.0f, 0.0f);        



    }





}

我的精灵课程:

 package opengl.renderer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//import android.opengl.GLES10;
import android.opengl.GLUtils;
import android.util.DisplayMetrics;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11Ext;


public class sprite {

    float y = 0;
    float x = 0;
    float xHolder = 0;

    float yHolder = 0;

    float height = 10;

    float width = 10;

    int Level = 1;

    int rotate;

    boolean visible = false;

    private FloatBuffer vertexBuffer; // buffer holding the verteces
    private FloatBuffer textureBuffer;
    private float vertices[];
    float[] texture;

    public Bitmap bitmap = null;

    public sprite(Bitmap image) {
        this.bitmap = image;



    }
    public sprite() {



    }

    public void update() {

        float[] anArray = {
                // 0.0f + x, (0.0f +height)+ y, 0.0f, // V1 - first vertex
                // (x,y,z)
                // // (0.10f+(width)) + x, (0.0f -height) + y, 0.0f, // V2 -
                // second vertex
                // (0.5f+(width/2)) + x, (0.10f) + y, 0.0f, // V3 - third
                // vertex]

                0f, 0f - height, 0, // bottom left
                0f, 0, 0f, // top left
                0f + width, 0f - height, 0f, // bottom right

                0f + width, 0f, 0f, // top right 

        };

        vertices = anArray;

        float[] texturearray = {

        /*
         * 0f+x, -0f-height+y, 0, // bottom left 0f+x, 0+y, 0f, // top left
         * 0f+width+x, 0f-height+y, 0f, // bottom right
         * 
         * 0f+width+x, 0f+y, 0f, // top right
         */
        0.0f, 1.0f, // top left (V2)
                0.0f, 0.0f, // bottom left (V1)
                1.0f, 1.0f, // top right (V4)
                1.0f, 0.0f // bottom right (V3)

        };

        texture = texturearray;

        // a float has 4 bytes so we allocate for each coordinate 4 bytes
        ByteBuffer vertexByteBuffer = ByteBuffer
                .allocateDirect(vertices.length * 4);
        vertexByteBuffer.order(ByteOrder.nativeOrder());

        // allocates the memory from the byte buffer
        vertexBuffer = vertexByteBuffer.asFloatBuffer();

        // fill the vertexBuffer with the vertices
        vertexBuffer.put(vertices);

        // set the cursor position to the beginning to the buffer
        vertexBuffer.position(0);



        vertexByteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
        vertexByteBuffer.order(ByteOrder.nativeOrder());
        textureBuffer = vertexByteBuffer.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);

        textureBuffer.clear();
        vertexByteBuffer.clear();
        vertexBuffer.clear();


    }

    public void draw(GL10 gl) {

        gl.glTranslatef(x, y, 0);


        // this.loadGLTexture(gl, context);
        // Update variables and position

            update();

            gl.glEnable(GL10.GL_BLEND);
            // gl.glBlendFunc(GLES10.GL_ONE, GLES10.GL_ONE);
            gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);



            // Set the face rotation
            // gl.glFrontFace(GL10.GL_CW);

            // Point to our vertex buffer
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                    GL10.GL_CLAMP_TO_EDGE);

            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            // Draw the vertices as triangle strip
            gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

            // Disable the client state before leaving
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

            x = xHolder;
            y = yHolder;
            textureBuffer.clear();
            vertexBuffer.clear();


    }

    /** The texture pointer */
    private int[] textures = new int[1];


    // private Bitmap[] backimagesec =new Bitmap[5];

    int bacSecCount = 0;

    public void loadGLTexture(GL10 gl) {
    //  gl.glDeleteTextures(1, textures, 0);

        // loading texture

        // generate one texture pointer
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // create nearest filtered texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_LINEAR);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR);

        // gl.glEnable(GL10.GL_ALPHA_BITS);
        // draw...
        // gl.glDisable(GL10.GL_ALPHA_BITS);
        // gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
        // /*GL10.GL_REPLACE*/ GL10.GL_MODULATE);
        // Use Android GLUtils to specify a two-dimensional texture image from
        // our bitmap

        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, this.bitmap, 0);
        // Clean up
        bitmap.recycle();



    }

}

我的LevelBackVisual:

package opengl.renderer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.opengl.GLUtils;
import android.util.DisplayMetrics;

public class LevelBackVisual {

    float x = 0;

    float y = 0;

    float height = 10;

    float width = 10;

    int Level = 1;

    int rotate;

    boolean visible = false;

    private FloatBuffer vertexBuffer; // buffer holding the verteces
    private FloatBuffer textureBuffer;
    private float vertices[];
    float[] texture;

    sprite Sprite;
    boolean spriteset=false;
    public LevelBackVisual(){} 

    public LevelBackVisual(int level, Context context) {
        if (!spriteset){
        Bitmap bitmap=null;

        this.Level = level;
        // this updates the triangle

        switch (this.Level) {
        case 1:

            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.backlevel1animation0001);

            break;

        case 2:

            break;

        default:
            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.backgroundlevel_1);

            break;

        }
        Bitmap bitmaptemp;
        //bitmaptemp.eraseColor(Color.TRANSPARENT);

        bitmaptemp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);

        Canvas canvas = new Canvas(bitmaptemp);

        canvas.drawBitmap(bitmap, 0, 0, null);


        this.Sprite = new sprite(bitmaptemp);

        //bitmaptemp.recycle();

        bitmap.recycle();
        this.spriteset=true;

        }

    }




    public void draw(GL10 gl) {

        Sprite.width =this.width;

        Sprite.height = this.height;

        Sprite.x = this.x;
        Sprite.y = this.y;

        Sprite.draw(gl);
    }

    /** The texture pointer */

    public void loadGLTexture(GL10 gl) {

        Sprite.loadGLTexture(gl);






    }

}

1 个答案:

答案 0 :(得分:0)

如果它在短时间后关闭,可能是你的内存不足。检查你的onDrawFrame()方法。你似乎在它里面一次又一次地创建某些对象(精灵等)。