0

我的渲染器有以下代码:

package hello.project;

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

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

public class HelloOpenGLES10Renderer implements Renderer {

    private Square      square;
    private Square2     square2;
    private Square3     square3;
    private SquareAccesories        squareAcc;
    private SquareEyes      squareEyes;
    private SquareLips      squareLips;
    private SquarePants     squarePants;
    private SquareShoes     squareShoes;
    private Context     context;
    //public static int w,h;


    /** Constructor to set the handed over context */
    public HelloOpenGLES10Renderer(Context context) {
        this.square     = new Square();
        this.square2        = new Square2();
        this.square3        = new Square3();
        this.squareAcc      = new SquareAccesories();
        this.squareEyes     = new SquareEyes();
        this.squareLips     = new SquareLips();
        this.squarePants        = new SquarePants();
        this.squareShoes        = new SquareShoes();
        this.context=context;
    }

    public void onDrawFrame(GL10 gl) {

        if (Project.ifDraw){
            Square.loadGLTexture(gl, this.context,Square.getSex()); 
            Square2.loadGLTexture(gl, this.context,Square2.getHair());
            Square3.loadGLTexture(gl, this.context,Square3.getDress());
            SquareAccesories.loadGLTexture(gl, this.context,SquareAccesories.getAcc());
            SquareEyes.loadGLTexture(gl, this.context,SquareEyes.getEyes());
            SquareLips.loadGLTexture(gl, this.context,SquareLips.getLips());
            SquarePants.loadGLTexture(gl, this.context,SquarePants.getPants());
            SquareShoes.loadGLTexture(gl, this.context,SquareShoes.getShoes());
            Project.ifDraw=false;
        }
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity(); 
        //GLU.gluPerspective(gl, 45.0f, (float)w / (float)h, 0.1f, 100.0f);
        GLU.gluLookAt(gl, 0, 1, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        gl.glColor4f(1.0f, 1.0f, 1.0f, 2.0f);
        square.draw(gl);
        square2.draw(gl);
        square3.draw(gl);
        squareEyes.draw(gl);
        squareAcc.draw(gl);
        squareLips.draw(gl);
        squareShoes.draw(gl);
        squarePants.draw(gl);

        /*// clear Screen and Depth Buffer
        Square.loadGLTexture(gl, this.context,Square.getSex()); 
        Square2.loadGLTexture(gl, this.context,Square2.getHair()); 
        gl.glColor4f(1.0f, 1.0f, 1.0f, 2.0f);   
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        // Reset the Modelview Matrix
        gl.glLoadIdentity();
        // GLU.gluLookAt(gl, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        // Drawing
        gl.glTranslatef(0.0f, 0.0f, -5.0f);  // move 5 units INTO the screen
        square.draw(gl);
        square2.draw(gl);*/ 
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if(height == 0) {                       //Prevent A Divide By Zero By
            height = 1;                         //Making Height Equal One
        }

        //w=width;
        //h=height;
        //Square.loadGLTexture(gl, this.context,Square.getSex()); 
        //Square2.loadGLTexture(gl, this.context,Square2.getHair()); 
        gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
        gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
        gl.glLoadIdentity();                    //Reset The Projection Matrix

        //Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
        gl.glLoadIdentity();                    //Reset The Mode   lview Matrix
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Load the texture for the square
        gl.glEnable(GL10.GL_BLEND);
        gl.glEnable(GL10.GL_TEXTURE_2D); 
        gl.glShadeModel(GL10.GL_FLAT);    //Enable Smooth Shading
        //gl.glEnable(GL10.GL_ALPHA_TEST);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        //gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        //Square.loadGLTexture(gl, this.context,Square.getSex()); 
        //Square2.loadGLTexture(gl, this.context,Square2.getHair());
        //Square3.loadGLTexture(gl, this.context,Square3.getDress());
        //SquareAccesories.loadGLTexture(gl, this.context,SquareAccesories.getAcc());
        //SquareEyes.loadGLTexture(gl, this.context,SquareEyes.getEyes());
        //SquareLips.loadGLTexture(gl, this.context,SquareLips.getLips());
        //SquarePants.loadGLTexture(gl, this.context,SquarePants.getPants());
        //SquareShoes.loadGLTexture(gl, this.context,SquareShoes.getShoes());
        //gl.glAlphaFunc(GL10.GL_GREATER, 0.5f);

        gl.glDisable(GL10.GL_DEPTH_TEST);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  //Black Background
        gl.glClearDepthf(1.0f);      //Depth Buffer Setup
        gl.glDepthFunc(GL10.GL_NEVER);    //The Type Of Depth Testing To Do

        //Really Nice Perspective Calculations
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 
    }
}

在模拟器上,它运行完美。我使用 Xperia Play 进行调试,并在 HTC xplorer 和 Galaxy Nexus 上进行了尝试,并且成功了。2 天,我在三星 Galaxy 和 HTC 手机上试用了它,但 SurfaceView 没有显示任何内容,它是空白的,知道为什么会发生这种情况吗?

4

1 回答 1

1

使图片尺寸为 2 的幂并知道它有效

于 2012-07-04T12:13:35.597 回答