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.
The convention in OpenGL is to work with a coordinate system in which the positive z-direction points toward the viewer and the negative z-direction points away from the viewer. The transformation into default coordinates reverses the direction of the z-axis.
OpenGL expects all the vertices, that we want to become visible, to be in normalized device coordinates after each vertex shader run. That is, the x , y and z coordinates of each vertex should be between -1.0 and 1.0 ; coordinates outside this range will not be visible.
The default coordinate system in OpenGL(TM) is right-handed: the positive x and y axes point right and up, and the negative z axis points forward.
Setting up the perspective projection matrix in OpenGL was done through a call to glFrustum. The function took six arguments: glFrustum(float left, float right, float bottom, float top, float near, float far); The implementation of this function can be found in the code below (line 20).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With