2010-07-16 95 views
3

我試圖用OpenGL ES繪製正交模式,點(0,0)位於屏幕的左下角。但是,我想讓它位於左上角。在OpenGL ES中翻轉Y軸?

這裏就是我在我的Android應用進行設置了:

public void onSurfaceChanged(final GL10 gl, final int width, final int height) { 
    assert gl != null; 

    // use orthographic projection (no depth perception) 
    GLU.gluOrtho2D(gl, 0, width, 0, height); 
} 

我試圖改變在許多方面上面的調用,包括:

GLU.gluOrtho2D(gl, 0, width, 0, height); 
    GLU.gluOrtho2D(gl, 0, width, 0, -height); 
    GLU.gluOrtho2D(gl, 0, width, height, 0); 
    GLU.gluOrtho2D(gl, 0, width, -height, 0); 

我也試圖與視口打無效:

public void onSurfaceChanged(final GL10 gl, final int width, final int height) { 
    assert gl != null; 

    // define the viewport 
    gl.glViewport(0, 0, width, height); 
    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glLoadIdentity(); 

    // use orthographic projection (no depth perception) 
    GLU.gluOrtho2D(gl, 0, width, 0, height); 
} 

而且,我試着玩視口設置無濟於事:

gl.glViewport(0, 0, width, height); 
    gl.glViewport(0, 0, width, -height); 
    gl.glViewport(0, height, width, 0); 
    gl.glViewport(0, -height, width, 0); 

任何關於如何讓點(0,0)到屏幕左上角的線索?謝謝!

回答

4

glScalef(1f, -1f, 1f);怎麼樣?

+0

glScalef應該用於縮放類似,不固定座標投影。 – rioki 2010-07-16 12:58:41

+2

您認爲'gluOrtho2D'如何工作?它當然會調整投影矩陣,「glScale」也會改變。 – 2010-07-16 14:25:20

+0

GL ES不提供矩陣運算符 – 2013-12-10 01:13:54

1
double fov = 60.f * 3.1415f/180.f; 
float aspect = (float) width/(float) height; 
float zNear = 0.01f; 
float zFar = 3000.0f; 
// cotangent 
float f = (float) (Math.cos(fov*0.5f)/Math.sin(fov*0.5f)); 
float perspMtx[] = new float[16]; 

// columns first 

for(int i=0; i<16; ++i) 
perspMtx[i] = 0.0f; 

perspMtx[0] = f/aspect; 
perspMtx[5] = -f; // flip Y? <- THIS IS YOUR ANSWER 

perspMtx[10] = (zFar+zNear)/(zNear - zFar); 
perspMtx[11] = -1.0f; 
perspMtx[14] = 2.0f*(zFar*zNear)/(zNear - zFar); 

gl.glMultMatrixf(perspMtx, 0); 

這解決了這個問題對我來說,應該是正交投影矩陣