问题
Android opengl-es view question. So in openGL, the default position of the camera and view is at 0,0. How do you set the view and camera so that it acts basically the same as computer screen coordinates with 0,0 at the top. I've called gl.glOrthof(-screenWidth/2, ScreenWidth/2, -ScreenHeight/2, ScreenHeight/2). But I think this is wrong. I also need to set the camera to view the entire field. I'm not sure how to use gl.glFrustumf to accomplish this task.
回答1:
To use your vertex coordinates as screen coordinates, just use glOrtho(0, width, height, 0, -1, 1)
on the projection matrix and keep your modelview matrix identity (which is the default). Note that I flipped bottom and top, as in GL (0,0) is at the lower left and you want it at the top (but keep in mind that this also flips every object and therefore the triangle ordering). You also forgot to set the near and far planes (everything with a z out of this interval won't get displayed). But when you now draw all your objects with z=0 (which is the default, when drawing only 2d vertices), all should be fine.
glFrustum
is just an alternative to glOrtho
. Where glOrtho
constrcuts an orthographic (parallel) view, glFrustum
constructs a perspective view. So you don't need glFrustum
.
来源:https://stackoverflow.com/questions/6091747/opengl-coordinates-to-match-screen-coordinates