我正在尝试使用OpenGL ES绘制正交模式,并且点(0,0)位于屏幕的左下角。但是,我想把它放在左上角。
这是我在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)放到屏幕左上角的任何线索?谢谢!
答案 0 :(得分:8)
怎么样:
glViewport(0, 0, width, height);
gluOrtho2D(0, width, height, 0);
glViewport调用仅在设备坐标中设置视口。这是你的窗口系统。 glOrtho(gluOrtho2D)调用设置从世界坐标到设备坐标的坐标映射。
见:
答案 1 :(得分:4)
glScalef(1f, -1f, 1f);
怎么样?
答案 2 :(得分: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);
这解决了我的问题,对于正交投影矩阵应该是类似的