2016-03-21 7 views
0

Ich versuche einen 3D Würfel in Android in orthographischer Ansicht anzuzeigen.Android GlOrthof Display 3D Würfel

Ich folgte this Tutorial, um einen Würfel zu bekommen und jetzt möchte ich es in der orthographischen Ansicht anzeigen. Aber egal, wie ich die glortof Parameter auswähle, der Würfel wird nicht von einem "gezoomten" Punkt angezeigt.

Welchen Punkt vermisse ich?

Hier mein Code:

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

Und hier ist die Cube-Klasse (die genau wie im Tutorial):

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

Antwort

0

konnte ich erreichen, was ich mit Hilfe dieser drei wollte Antwort:

true isometric projection with opengl

How to render with isometric perspective?

OpenGL stretched shapes - aspect ratio

Jetzt sieht mein Code wie folgt aus:

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