From 7bf79ff628dc3c46445a3a5eaf70ac0aa96c927a Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Wed, 13 Jun 2012 18:15:58 +0100 Subject: [PATCH] c3algebra: Added ortho matrices Including screen oriented ones Signed-off-by: Michel Pollet --- examples/shared/libc3/src/c3algebra.c | 46 +++++++++++++++++++++++++++ examples/shared/libc3/src/c3algebra.h | 6 ++++ 2 files changed, 52 insertions(+) diff --git a/examples/shared/libc3/src/c3algebra.c b/examples/shared/libc3/src/c3algebra.c index 7512346..c78b1fb 100644 --- a/examples/shared/libc3/src/c3algebra.c +++ b/examples/shared/libc3/src/c3algebra.c @@ -1033,3 +1033,49 @@ c3mat4 perspective3D(c3f fov, c3f aspect, c3f zNear, c3f zFar) return frustum3D(xmin, xmax, ymin, ymax, zNear, zFar); } + +c3mat4 ortho3D( + c3f left, c3f right, c3f bottom, c3f top, + c3f near, c3f far) +{ + c3f a = 2.0f / (right - left); + c3f b = 2.0f / (top - bottom); + c3f c = -2.0f / (far - near); + + c3f tx = - (right + left)/(right - left); + c3f ty = - (top + bottom)/(top - bottom); + c3f tz = - (far + near)/(far - near); + + float ortho[16] = { + a, 0, 0, 0, + 0, b, 0, 0, + 0, 0, c, 0, + tx, ty, tz, 1 + }; + return *((c3mat4p)ortho); +} + +/* + * Same as previous, but this one is screen aligned, with 0,0 being top,left + * instead of bottom,left + */ +c3mat4 screen_ortho3D( + c3f left, c3f right, c3f bottom, c3f top, + c3f near, c3f far) +{ + c3f a = 2.0f / (right - left); + c3f b = 2.0f / (top - bottom); + c3f c = -2.0f / (far - near); + + c3f tx = - (right + left)/(right - left); + c3f ty = - (top + bottom)/(top - bottom); + c3f tz = - (far + near)/(far - near); + + float ortho[16] = { + a, 0, 0, 0, + 0, -b, 0, 0, + 0, 0, c, 0, + tx, -ty, tz, 1 + }; + return *((c3mat4p)ortho); +} diff --git a/examples/shared/libc3/src/c3algebra.h b/examples/shared/libc3/src/c3algebra.h index 98f93bd..9118031 100644 --- a/examples/shared/libc3/src/c3algebra.h +++ b/examples/shared/libc3/src/c3algebra.h @@ -216,5 +216,11 @@ c3mat4 frustum3D( c3f left, c3f right, c3f bottom, c3f top, c3f znear, c3f zfar); c3mat4 perspective3D(c3f fov, c3f aspect, c3f znear, c3f zfar); +c3mat4 ortho3D( + c3f left, c3f right, c3f bottom, c3f top, + c3f near, c3f far); +c3mat4 screen_ortho3D( + c3f left, c3f right, c3f bottom, c3f top, + c3f near, c3f far); #endif /* __C3ALGEBRA_H___ */ -- 2.39.5