Android - OpenGL不能在真正的手机上工作,但在模拟器上一切都很好

时间:2014-03-02 21:00:27

标签: android opengl-es cube

我尝试在我的Android应用程序中显示一个多维数据集,但我似乎无法在我的HTC One SV上正常工作,因为屏幕只保持白色(我认为是因为glClearColor()) 这是两个相关类的代码:

public class ClassicByteRenderer implements GLSurfaceView.Renderer {

private Square square = new Square();
public int width = 0;
public int height = 0;

public void onDrawFrame(GL10 gl) {
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
    gl.glMatrixMode(GLES10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluPerspective(gl, 45.0F, (float) this.width / (float) this.height,0.1F,100.0F);
    GLU.gluLookAt(gl, 5.0F, 10.0F, 5.0F, 1.0F, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F);
    gl.glMatrixMode(GLES10.GL_MODELVIEW);
    gl.glLoadIdentity();
    this.drawCube(gl);
}

public void drawCube(GL10 gl) {
    gl.glColor4f(1.0F,1.0F,0.0F,1.0F);
    this.square.draw(gl);

    gl.glTranslatef(0.0F,0.0F,-2.0F);
    gl.glColor4f(1.0F,0.0F,0.0F,1.0F);
    this.square.draw(gl);

    gl.glTranslatef(1.0F,0.0F,1.0F);
    gl.glRotatef(90.0F,0.0F,1.0F,0.0F);
    gl.glColor4f(0.0F,0.0F,1.0F,1.0F);
    this.square.draw(gl);

    gl.glTranslatef(0.0F,0.0F,-2.0F);
    gl.glColor4f(0.0F,1.0F,0.0F,1.0F);
    this.square.draw(gl);

    gl.glTranslatef(0.0F,-1.0F,1.0F);
    gl.glRotatef(90.0F,-1.0F,0.0F,0.0F);
    gl.glColor4f(0.0F,1.0F,1.0F,1.0F);
    this.square.draw(gl);

    gl.glTranslatef(0.0F,0.0F,2.0F);
    gl.glColor4f(1.0F,0.0F,1.0F,1.0F);
    this.square.draw(gl);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
    this.width = width;
    this.height = height;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    GLES20.glClearColor(1.0F, 1.0F, 1.0F, 1.0F);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glEnable(GLES20.GL_TEXTURE_2D);
    GLES20.glDisable(GLES20.GL_CULL_FACE);
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    gl.glHint(GLES10.GL_PERSPECTIVE_CORRECTION_HINT, GLES10.GL_NICEST);
    GLES20.glDepthMask(true);
}

}

这是另一个可能必要的:

public class ClassicByteView extends GLSurfaceView {

public ClassicByteView(Context context) {
    super(context);
    super.setEGLConfigChooser(true);
    this.setEGLContextClientVersion(2);
    this.setRenderer(new ClassicByteRenderer());
    this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
}

一切应该如何(这就是它在模拟器中的呈现方式):

1 个答案:

答案 0 :(得分:0)

我能够自己解决问题。解决方案是删除

super.setEGLConfigChooser(true);
this.setEGLContextClientVersion(2);
ClassicByteView类中的

。现在一切正常。