2010-07-16 97 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); 

这解决了这个问题对我来说,应该是正交投影矩阵