【问题标题】:Android glOrthof display 3D cubeAndroid glOrthof 显示3D立方体
【发布时间】:2016-07-08 10:28:31
【问题描述】:

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

我按照this 教程获得了一个立方体,现在我想在正交视图中显示它。但无论我如何选择 glothof 参数,立方体都不会从“缩小”的点显示。

我错过了什么?

这是我的代码:

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);
    }
}

【问题讨论】:

    标签: android 3d perspective orthographic


    【解决方案1】:

    借助这三个答案,我可以达到我想要的效果:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多