簡體   English   中英

Android glOrthof顯示3D立方體

[英]Android glOrthof display 3D cube

我嘗試以正交視圖在Android中顯示3D立方體。

我按照教程獲得了一個立方體,現在我想在正交視圖中顯示它。 但是,無論我如何選擇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);
    }
}

在以下三個答案的幫助下,我可以達到想要的目標:

OpenGL的真實等距投影

如何用等軸測透視圖渲染?

OpenGL拉伸形狀-長寬比

現在我的代碼如下:

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正交3D立方體

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM