2011-02-09 165 views
1

我遇到了一个问题,我的应用程序在我的模拟器上看起来很正常,但在我的手机上它只显示我的场景片段。Android模拟器vs手机opengl不一致

图片here(仿真器是右边的一个。

我的渲​​染器的代码是在这里看到。(这是一个抽象类,但所有的实现类正在做的是绘制多边形)

public abstract class AbstractRenderer implements Renderer { 
float x = 0.5f; 
float y = 1f; 
float z = 3; 

boolean displayCoordinateSystem = true; 

public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) { 
    gl.glDisable(GL10.GL_DITHER); 
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
    gl.glClearColor(.5f, .5f, .5f, 1); 
    gl.glShadeModel(GL10.GL_SMOOTH); 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
} 

public void onSurfaceChanged(GL10 gl, int w, int h) { 
    gl.glViewport(0, 0, w, h); 
    float ratio = (float) w/h; 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 
    gl.glFrustumf(-ratio, ratio, -1, 1, 0, 10); 
} 

public void onDrawFrame(GL10 gl) { 
    gl.glDisable(GL10.GL_DITHER); 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); 
    GLU.gluLookAt(gl, x, y, z, 0f, 0, 0f, 0f, 1f, 0f); 
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

    if(displayCoordinateSystem) { 
     drawCoordinateSystem(gl); 
    } 

    draw(gl); 

//  gl.glFlush(); 
} 

private void drawCoordinateSystem(GL10 gl) { 
    ByteBuffer vbb = ByteBuffer.allocateDirect(6*3*4); 
    vbb.order(ByteOrder.nativeOrder()); 
    FloatBuffer vertices = vbb.asFloatBuffer(); 

    ByteBuffer ibb = ByteBuffer.allocateDirect(6*2); 
    ibb.order(ByteOrder.nativeOrder()); 
    ShortBuffer indexes = ibb.asShortBuffer(); 

    final float coordLength = 27f; 

    //add point (-1, 0, 0) 
    vertices.put(-coordLength); 
    vertices.put(0); 
    vertices.put(0); 

    //add point (1, 0, 0) 
    vertices.put(coordLength); 
    vertices.put(0); 
    vertices.put(0); 

    //add point (0, -1, 0) 
    vertices.put(0); 
    vertices.put(-coordLength); 
    vertices.put(0); 

    //add point (0, 1, 0) 
    vertices.put(0); 
    vertices.put(coordLength); 
    vertices.put(0); 

    //add point (0, 0, -1) 
    vertices.put(0); 
    vertices.put(0); 
    vertices.put(-coordLength); 

    //add point (0, 0, 1) 
    vertices.put(0); 
    vertices.put(0); 
    vertices.put(coordLength); 

    for(int i = 0; i < 6; i++) { 
     indexes.put((short)i); 
    } 

    vertices.position(0); 
    indexes.position(0); 

    gl.glColor4f(1, 1, 0, 0.5f); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices); 
    gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, indexes); 

    indexes.position(2); 
    gl.glColor4f(0, 1, 0, 0.5f); 
    gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, indexes); 

    indexes.position(4); 
    gl.glColor4f(0, 0, 1, 0.5f); 
    gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, indexes); 
} 

protected abstract void draw(GL10 gl); 
} 

我的猜测是,我没有设置默认情况下由仿真器实现设置的一些值,只有一点是我不知道这件事可能是什么,

希望听到你的帅哥和伙计们!

回答

1

这是一个深度缓冲问题:从在glFrustum手册页的“注意事项”部分:

附近绝不能设置为0。

您应该计算在不久的价值尽可能远离相机,尽可能远离相机,同时还包括您想要绘制的东西。

+1

另外,看起来您的顶点距离相机27个单位,您的远距离只有10个单位。好的起点是gl.glFrustumf(....,0.1f,100.0f);然后你可以按照ryanm的建议调整值。 – Gilead 2011-02-09 23:20:26