Android OGL 1.0:显示对象问题

时间:2013-06-24 11:16:11

标签: android opengl-es

我是OGL(1.0)世界的新手,我遇到了一个小问题。所以我绘制两个面板并旋转它们。面板彼此成90度角。旋转面板时,其中一个总是显示在正面。但在这一刻它应该落后于对方!

enter image description here

以下是代码TexturedCube

public class TexturedCube implements IFigure {
    private static final int VERTEX_SIZE = (3 + 2) * 4;
    private FloatBuffer vertexBuffer;
    private int textureId;
    private float vertices[] = {
         1.0f, 0.0f, 1.0f, 1, 1, 
        -1.0f, 0.0f, 1.0f, 0, 1, 
        -1.0f, 1.0f, 1.0f, 0, 0,

         1.0f, 0.0f, 1.0f, 1, 1,
         1.0f, 1.0f, 1.0f, 1, 0,
        -1.0f, 1.0f, 1.0f, 0, 0,

        1.0f, 1.0f, -1.0f, 1, 0, 
         1.0f, 1.0f, 1.0f, 0, 0,
         1.0f, 0.0f, 1.0f, 0, 1,

         1.0f, 0.0f, -1.0f, 1, 1,
         1.0f, 1.0f, -1.0f, 1, 0,
         1.0f, 0.0f, 1.0f, 0, 1,};
private float rotation = 0.1f;

public TexturedCube(int textureId) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertexBuffer = byteBuffer.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.flip();
    this.textureId = textureId;
}

@Override
public void draw(GL10 gl) {
    rotation += 1.0f;
    gl.glRotatef(rotation, 1f, 0f, 0f);
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    vertexBuffer.position(0);
    gl.glVertexPointer(3, GL10.GL_FLOAT, VERTEX_SIZE, vertexBuffer);
    vertexBuffer.position(3);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertexBuffer);
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 4 * 3);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

}

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    glView = new GLSurfaceView(this);
    glView.setRenderer(new MyOpenGLTextureRenderer());
    setContentView(glView);
}

private class MyOpenGLTextureRenderer implements Renderer {

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, .0f, -6.0f);
        cube.draw(gl);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        texture = BitmapFactory.decodeResource(getResources(), R.drawable.texture);
        int textureIds[] = new int[1];
        gl.glGenTextures(1, textureIds, 0);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIds[0]);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,
        GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D,
        GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
        texture.recycle();
        cube = new TexturedCube(textureIds[0]);
    }

}

如果有人可以通过Android的例子向我建议一个好的OGL教程,它将是完美的......

修改 这是解决方案

您必须在gl.glEnable(GL10.GL_DEPTH_TEST);

中的onSurfaceChanged()中添加Activity
public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        ....
}

0 个答案:

没有答案