Example of OpenGL game coordinates system - done right? -
well not surprise default opengl screen coords scheme quite hard operate x-axis: -1.0 1.0, y-axis: -1.0 1.0, , (0.0,0.0) in center of screen.
so decided write wrapper local game coords next main ideas:
screen coords 0..100.0 (x-axis), 0..100.0 (y-axis) (0.0,0.0) in bottom left corner of screen. there different screens, different aspects. if draw quad, must remain quad, not squashed rectangle.by quad mean
quad_vert[0].x = -0.5f; quad_vert[0].y = -0.5f; quad_vert[0].z = 0.0f; quad_vert[1].x = 0.5f; quad_vert[1].y = -0.5f; quad_vert[1].z = 0.0f; quad_vert[2].x = -0.5f; quad_vert[2].y = 0.5f; quad_vert[2].z = 0.0f; quad_vert[3].x = 0.5f; quad_vert[3].y = 0.5f; quad_vert[3].z = 0.0f;
i utilize glm::ortho , glm::mat4 accomplish this:
#define loc_scr_size 100.0f typedef struct coords_manager { float screen_aspect; mat4 ortho_matrix;//glm 4*4 matrix }coords_manager; glviewport(0, 0, screen_width, screen_height); coords_manager cm; cm.screen_aspect = (float) screen_width / screen_height;
for illustration our aspect 1.7
cm.ortho_matrix = ortho(0.0f, loc_scr_size, 0.0f, loc_scr_size);
now bottom left (0,0) , top right (100.0, 100.0)
and works, mostly, can translate our quad (25.0, 25.0), scale (50.0, 50.0) , sit down @ bottom-left corner size of 50% percent of screen. problem not quad anymore looks rectangle, because our screen width not equal height.
so utilize our screen aspect:
cm.ortho_matrix = ortho(0.0f, loc_scr_size * cm.screen_aspect, 0.0f, loc_scr_size);
yeah right form problem - if position @ (50,25) kinda left center of screen, because our local scheme not 0..100 x-axis anymore, it's 0..170 (because multiply our aspect of 1.7), utilize next function before setting our quad translation
void loc_pos_to_gl_pos(vec2* pos) { pos->x = pos->x * cm.screen_aspect; }
and viola, right form squad @ right place.
but question - doing right?
opengl screen coords scheme quite hard operate x-axis: -1.0 1.0, y-axis: -1.0 1.0, , (0.0,0.0) in center of screen.
yes, never utilize them directly. there's projection matrix, transforms coordinates right space.
we kinda left center of screen, because our local scheme not 0..100 x-axis anymore,
that's why opengl maps ndc space (0,0,0) screen center. if draw quad coordinates symmetrically around origin remain in center.
but question - doing right?
depends on want achieve.
opengl-es opengl-es-2.0
No comments:
Post a Comment