diff options
| author | Andre Weissflog <floooh@gmail.com> | 2019-03-24 20:27:57 +0100 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2019-03-24 20:27:57 +0100 |
| commit | c8486f6b8c013d36207df18de4203ebf38b7ca23 (patch) | |
| tree | 61df45f629d37864dd061af41342b8755d73e278 | |
| parent | b85a83bfc013050272e034cc52622fd6e24b32fb (diff) | |
sokol_gl.h: sgl_frustum and sgl_ortho
| -rw-r--r-- | util/sokol_gl.h | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/util/sokol_gl.h b/util/sokol_gl.h index 503a1ee6..bebdeca6 100644 --- a/util/sokol_gl.h +++ b/util/sokol_gl.h @@ -679,6 +679,43 @@ static void _sgl_translate(_sgl_matrix_t* dst, float x, float y, float z) { } } +static void _sgl_frustum(_sgl_matrix_t* dst, float left, float right, float bottom, float top, float near, float far) { + float x = (2.0f * near) / (right - left); + float y = (2.0f * near) / (top - bottom); + float a = (right + left) / (right - left); + float b = (top + bottom) / (top - bottom); + float c = -(far + near) / (far - near); + float d = -(2.0f * far * near) / (far - near); + _sgl_matrix_t m; + m.v[0][0] = x; m.v[0][1] = 0.0f; m.v[0][2] = 0.0f; m.v[0][3] = 0.0f; + m.v[1][0] = 0.0f; m.v[1][1] = y; m.v[1][2] = 0.0f; m.v[1][3] = 0.0f; + m.v[2][0] = a; m.v[2][1] = b; m.v[2][2] = c; m.v[2][3] = -1.0f; + m.v[3][0] = 0.0f; m.v[3][1] = 0.0f; m.v[3][2] = d; m.v[3][3] = 0.0f; + _sgl_matmul4(dst, dst, &m); +} + +static void _sgl_ortho(_sgl_matrix_t* dst, float left, float right, float bottom, float top, float near, float far) { + _sgl_matrix_t m; + m.v[0][0] = 2.0f / (right - left); + m.v[1][0] = 0.0f; + m.v[2][0] = 0.0f; + m.v[3][0] = -(right + left) / (right - left); + m.v[0][1] = 0.0f; + m.v[1][1] = 2.0f / (top - bottom); + m.v[2][1] = 0.0f; + m.v[3][1] = -(top + bottom) / (top - bottom); + m.v[0][2] = 0.0f; + m.v[1][2] = 0.0f; + m.v[2][2] = -2.0f / (far - near); + m.v[3][2] = -(far + near) / (far - near); + m.v[0][3] = 0.0f; + m.v[1][3] = 0.0f; + m.v[2][3] = 0.0f; + m.v[3][3] = 1.0f; + + _sgl_matmul4(dst, dst, &m); +} + /* current top-of-stack projection matrix */ static inline _sgl_matrix_t* _sgl_matrix_projection(void) { return &_sgl.matrix_stack[SGL_MATRIXMODE_PROJECTION][_sgl.top_of_stack[SGL_MATRIXMODE_PROJECTION]]; @@ -1156,6 +1193,16 @@ SOKOL_API_IMPL void sgl_translate(float x, float y, float z) { _sgl_translate(_sgl_matrix(), x, y, z); } +SOKOL_API_IMPL void sgl_frustum(float l, float r, float b, float t, float n, float f) { + SOKOL_ASSERT(_SGL_INIT_COOKIE == _sgl.init_cookie); + _sgl_frustum(_sgl_matrix(), l, r, b, t, n, f); +} + +SOKOL_API_IMPL void sgl_ortho(float l, float r, float b, float t, float n, float f) { + SOKOL_ASSERT(_SGL_INIT_COOKIE == _sgl.init_cookie); + _sgl_ortho(_sgl_matrix(), l, r, b, t, n, f); +} + /* this draw the accumulated draw commands via sokol-gfx */ SOKOL_API_IMPL void sgl_draw(void) { SOKOL_ASSERT(_SGL_INIT_COOKIE == _sgl.init_cookie); |