2

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

在此处输入图像描述

这是代码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]);
    }

}

如果有人可以建议我一个很好的 OGL 教程和 android 的例子,那将是完美的......

编辑: 这是解决方案

你必须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);
        ....
}
4

0 回答 0