Skip to content

Instantly share code, notes, and snippets.

@elementbound
Created June 18, 2017 17:51
Show Gist options
  • Save elementbound/f39b054926787e54214c22e599cdecdd to your computer and use it in GitHub Desktop.
Save elementbound/f39b054926787e54214c22e599cdecdd to your computer and use it in GitHub Desktop.
///matrix_ortho(x,y, width,height, znear,zfar)
// Thanks, mesa!
// Source from src/mesa/math/m_matrix.c:1019
// Modified according to the formula at the bottom of
// https://msdn.microsoft.com/en-us/library/windows/desktop/bb205347(v=vs.85).aspx
var _x,_y,w,h;
_x = argument0;
_y = argument1;
w = argument2;
h = argument3;
var m, left, right, bottom, top, nearval, farval;
left = _x-w/2;
right = _x+w/2;
bottom = _y+h/2;
top = _y-h/2;
nearval = argument4;
farval = argument5;
// #define M(row,col) m[col*4+row]
m[0 + 4*0] = 2.0 / (right-left);
m[0 + 4*1] = 0.0;
m[0 + 4*2] = 0.0;
m[0 + 4*3] = (left+right) / (left-right);
m[1 + 4*0] = 0.0;
m[1 + 4*1] = 2.0 / (top-bottom);
m[1 + 4*2] = 0.0;
m[1 + 4*3] = (top+bottom) / (bottom-top);
m[2 + 4*0] = 0.0;
m[2 + 4*1] = 0.0;
m[2 + 4*2] = 1 / (farval-nearval);
m[2 + 4*3] = nearval / (nearval-farval);
m[3 + 4*0] = 0.0;
m[3 + 4*1] = 0.0;
m[3 + 4*2] = 0.0;
m[3 + 4*3] = 1.0;
return m;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment