Is there anyway I can make OpenGL ES 2.0 to accept coordinates (for Vextex
for example) in int
meaning the pixels instead of the default float
system? Also how can I set the point (0,0)
to be at the top left corner of screen instead of the default one at the exact middle of the screen (to make using the pixel coordinates system easier)?
3 Answers
If you are making a 2D style game, you can use, for example a 320x480 screen coordinates in your code, and then, in your vertex shader, you need to transform them into OpenGL ES coordinates.
I recommend you to use a MVP matrix, for all your vertex operations, but for a naive implementation of an orthographic 2D projection, you can do it manually as follows:
// Minimalist vertex shader
#version 100
attribute vec3 position;
void main(void)
{
gl_Position = vec4(position.x * 2.0 / 320.0 - 1.0,
position.y * -2.0 / 480.0 + 1.0,
position.z,
1.0);
}
This vertex shader puts the screen coordinates origin at the top-left corner. And transform your coordinates from 320 x 480. From here, replace the 320 and 480 values as you need.
The orthographic projection matrix is shown here: https://gamedev.stackexchange.com/a/4312/3020
I don't think you can use int's when you're sending your data through the shaders. And if so, you shouldn't. The GPU is designed to fast process floats in parallel so try to stick with that if possible.
For the second question you should read up a little on how the projection of a 3d scene works when it gets projected onto the "view" or screen as mentioned here: https://stackoverflow.com/a/6150298/341358
I'm assuming you are trying to draw in a 2D system. Your best bet is to use an orthographic projection by calling
glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal)
or the equivalent function for the matrix creation functions/helpers you are using. This way, you can specify your coordinates relative to the screen, and if your screen resolution is 800x600 you can use
glOrtho(0, 800, 600, 0, 0, 1)
to get a transformation matrix that will let you specify your coordinates in pixel values and with the origin in the top left corner.
-
1\$\begingroup\$ Sadly
glOrtho
is not available in OpenGL ES 2.0 \$\endgroup\$ Commented Aug 6, 2012 at 0:21 -
\$\begingroup\$ What platform are you writing your code for? iOS? Android? WebGL? \$\endgroup\$– r2d2rigoCommented Aug 6, 2012 at 0:48
-
\$\begingroup\$ BlackBerry, but I'll port the game to iOS and Android after it's finished. \$\endgroup\$ Commented Aug 6, 2012 at 9:15