Android glOrthof显示3D立方体

时间:2016-03-21 18:39:08

标签: android 3d perspective orthographic

我尝试在正交视图中在Android中显示3D立方体。

我按照this教程获取了一个立方体,现在我想在正交视图中显示它。但无论我如何选择glorthof参数,多维数据集都不会从"缩放"出点。

我错过了什么点?

这是我的代码:

public class OpenGLRenderer implements GLSurfaceView.Renderer {

    private Cube _cube = new Cube();
    private float _rotation;
    float _width;
    float _height;
    float _halfwidth;
    float _halfheight;
    float _zoom = 2.0f;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor((195.0f / 255.0f), (190.0f / 255.0f), (196.0f / 255.0f), 0.5f);

        gl.glClearDepthf(1.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        // Set the projection
        _width = width;
        _height = height;
        _halfwidth = (float)width/2;
        _halfheight = (float)height/2;

        SetOrthograficView(gl);
    }

    private void SetOrthograficView(GL10 gl) {
        // glOrthof(float left, float right, float bottom, float top, float zNear, float zFar)
        gl.glOrthof( -_halfwidth * _zoom, _halfwidth * _zoom, -_halfheight * _zoom, _halfheight * _zoom, -10.0f, 10.0f);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glRotatef(_rotation, 1.0f, 1.0f, 1.0f);

        _cube.draw(gl);
        _rotation -= 0.15f;
    }
} 

这是Cube类(与教程完全相同):

public class Cube {
    private FloatBuffer mVertexBuffer;
    private FloatBuffer mColorBuffer;
    private ByteBuffer  mIndexBuffer;

    private float vertices[] = {
            -1.0f, -1.0f, -1.0f,
            1.0f, -1.0f, -1.0f,
            1.0f,  1.0f, -1.0f,
            -1.0f, 1.0f, -1.0f,
            -1.0f, -1.0f,  1.0f,
            1.0f, -1.0f,  1.0f,
            1.0f,  1.0f,  1.0f,
            -1.0f,  1.0f,  1.0f
    };
    private float colors[] = {
            0.0f,  1.0f,  0.0f,  1.0f,
            0.0f,  1.0f,  0.0f,  1.0f,
            1.0f,  0.5f,  0.0f,  1.0f,
            1.0f,  0.5f,  0.0f,  1.0f,
            1.0f,  0.0f,  0.0f,  1.0f,
            1.0f,  0.0f,  0.0f,  1.0f,
            0.0f,  0.0f,  1.0f,  1.0f,
            1.0f,  0.0f,  1.0f,  1.0f
    };

    private byte indices[] = {
            0, 4, 5, 0, 5, 1,
            1, 5, 6, 1, 6, 2,
            2, 6, 7, 2, 7, 3,
            3, 7, 4, 3, 4, 0,
            4, 7, 6, 4, 6, 5,
            3, 0, 1, 3, 1, 2
    };

    public Cube() {
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        mVertexBuffer = byteBuf.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        byteBuf = ByteBuffer.allocateDirect(colors.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        mColorBuffer = byteBuf.asFloatBuffer();
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
    }

    public void draw(GL10 gl) {
        gl.glFrontFace(GL10.GL_CW);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        gl.glDrawElements(GL10.GL_TRIANGLES, 36, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    }
}

1 个答案:

答案 0 :(得分:0)

我可以借助这三个答案达到我想要的目标:

true isometric projection with opengl

How to render with isometric perspective?

OpenGL stretched shapes - aspect ratio

现在我的代码看起来像这样:

float _zoom = 5.0f;

public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    // Set the projection
    _aspectRatio = (float) width / (float) height;
    gl.glOrthof(-_zoom*_aspectRatio, _zoom*_aspectRatio, -_zoom, _zoom, -10.0f, 10.0f);

    /* use this length so that camera is 1 unit away from origin */
    float dist = (float)Math.sqrt(1 / 3.0);

    GLU.gluLookAt(gl, dist, dist, dist,  /* position of camera */
                      0.0f,  0.0f,  0.0f,   /* where camera is pointing at */
                      0.0f,  1.0f,  0.0f);  /* which direction is up */

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glRotatef(35.264f, 1.0f, 0.0f, 0.0f);
    gl.glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
}

public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

    _cube.draw(gl);
}

Android Orthografic 3D Cube