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 Answers
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
.

- 45,360
- 10
- 108
- 185
-
shouldn't it be glOrtho(0, width, 0, height, -1, 1)? – darethas Nov 21 '13 at 04:44
-
@treehau5 Not if you read the whole answer, especially the sentence right after the `glOrtho` call: *"Note that I flipped bottom and top, as in GL (0,0) is at the lower left and you want it at the top"*. – Christian Rau Nov 21 '13 at 09:30
-
Right. The main concern is now, why would you do that? – darethas Nov 22 '13 at 02:46
-
1@treehau5 Well, what do I know why the OP needs the origin at the top left. But there are use cases for that, given that many 2D drawing and windowing systems work this way. – Christian Rau Nov 22 '13 at 11:59