2013-03-17 4 views
6

So habe ich eine Kugel in OpenGL ES (speziell OpenGL ES 2.0, in Java, für Android) generiert. Wenn diese Kugel an der gleichen Stelle wie das Zentrum für meine Ansichtsmatrix platziert ist, ist es in Ordnung, aber wenn sie nicht zentriert ist, ist die Kugel ziemlich stark verzogen (siehe unten).(OpenGL ES) Objekte vom Blick Zentrum sind gestreckt

Warum passiert das und wie kann ich damit aufhören?

enter image description here

, dass die gleiche Sphäre ist. Der obere rechts wird nur in x und y (nicht z) übersetzt.

Einige Schnipsel des Codes aus meiner Implementierung von GLSurfaceView.renderer,

public void onSurfaceCreated(GL10 unused, EGLConfig config) { 
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    GLES20.glEnable(GLES20.GL_CULL_FACE); 
    GLES20.glEnable(GLES20.GL_DEPTH_TEST); 

    // Both centred on (0,0,0), of radius 1.0. 
    outerSphere = new Sphere(); 
    centreSphere = new Sphere(); 
} 

public void onSurfaceChanged(GL10 unused, int width, int height) { 
    GLES20.glViewport(0, 0, width, height); 

    ratio = (float) width/height; 

    final float left = -ratio; 
    final float right = ratio; 
    final float bottom = -1.0f; 
    final float top = 1.0f; 
    final float near = 1.0f; 
    final float far = 100.0f; 

    Matrix.frustumM(projMatrix, 0, left, right, bottom, top, near, far); 
} 

public void onDrawFrame(GL10 unused) { 
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); 

    float eyeX = 0.0f; 
    float eyeY = 0.0f; 
    float eyeZ = 10.0f; 

    final float lookX = 0.0f; 
    final float lookY = 0.0f; 
    final float lookZ = 0.0f; 

    final float upX = 0.0f; 
    final float upY = 1.0f; 
    final float upZ = 0.0f; 

    Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, 
         upX, upY, upZ); 

    // Set identity matrix as input for translations. 
    Matrix.setIdentityM(outerModelMatrix, 0); 

    // Translate outer sphere by 5 in x and y. 
    Matrix.translateM(outerModelMatrix, 0, 5.0f, 5.0f, 0.0f); 

    // MVP matrix = Projection * View * Model. 
    Matrix.multiplyMM(centreMVPMatrix, 0, viewMatrix, 0, centreModelMatrix, 0); 
    Matrix.multiplyMM(centreMVPMatrix, 0, projectionMatrix, 0, centreMVPMatrix, 0); 
    Matrix.multiplyMM(outerMVPMatrix, 0, viewMatrix, 0, outerModelMatrix, 0); 
    Matrix.multiplyMM(outerMVPMatrix, 0, projectionMatrix, 0, outerMVPMatrix, 0); 

    outerSphere.draw(outerMVPMatrix); 
    centreSphere.draw(outerMVPMatrix); 

} 

Und meine Shadern sind einfach,

private final static String vertexShaderCode = 
    "uniform mat4 u_MVPMatrix;" + 
    "attribute vec4 a_Position;" + 
    "uniform vec4 u_Color;" + 
    "void main() {" + 
    " gl_Position = u_MVPMatrix * a_Position;" + 
    "}"; 
private final static String fragmentShaderCode = 
    "precision mediump float;" + 
    "uniform vec4 u_Color;" + 
    "void main() {" + 
    " gl_FragColor = u_Color;" + 
    "}"; 

Ich habe fast den gesamten Code aus der Sphere-Klasse ausgelassen, und andere Dinge, die ich nicht für notwendig halte (?), aber wenn sie gebraucht werden, werde ich sie aufstellen.

Antwort

5

Willkommen in der Welt von perspective distortion!

Ein wenig mehr Detail: Ihr Field-Of-View ist zu schmal, Sie müssen den Frustum so gestalten, dass er ein bisschen größer ist.

+3

Aha! Und jetzt weiß ich, was ich suche, natürlich sind die Informationen leicht verfügbar (inkl. Ähnliche Fragen zum Stack - Überlauf. Für jeden, der darauf stößt, siehe zB http://StackOverflow.com/a/8891510/927046 und http://Stackoverflow.com/a/13270443/927046.) Danke. – Sam

+1

Zu wissen, was Google ist oft der schwierigste Teil :) Ich bin froh, ich könnte Sie in die richtige Richtung zeigen. –

+0

Ich sollte auch hinzufügen, dass ich denke, das Problem war ein zu * breites * Sichtfeld. Es musste enger sein (mit der Kamera platziert Vater weg zu kompensieren). – Sam