Skip to content

Commit

Permalink
Add Metal compatible projections
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Mar 8, 2024
1 parent 3e46a1a commit 6ee33a0
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions inmath/linalg.d
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,12 @@ struct Matrix(type, int rows_, int cols_) if((rows_ > 0) && (cols_ > 0)) {
return perspective(cdata[0], cdata[1], cdata[2], cdata[3], cdata[4], cdata[5]);
}

/// Returns a perspective matrix (4x4 and floating-point matrices only).
static Matrix perspective01(mt width, mt height, mt fov, mt near, mt far) {
mt[6] cdata = cperspective(-width, -height, -fov, near, far);
return perspective01(cdata[0], cdata[1], cdata[2], cdata[3], cdata[4], cdata[5]);
}

/// ditto
static Matrix perspective(mt left, mt right, mt bottom, mt top, mt near, mt far)
in {
Expand All @@ -1330,6 +1336,31 @@ struct Matrix(type, int rows_, int cols_) if((rows_ > 0) && (cols_ > 0)) {
return ret;
}



/// Returns an perspective matrix (4x4 and floating-point matrices only).
/// This matrix is made for Metal's NDC.
static Matrix perspective01(mt left, mt right, mt bottom, mt top, mt near, mt far)
in {
assert(right-left != 0);
assert(top-bottom != 0);
assert(far-near != 0);
}
do {
Matrix ret;
ret.clear(0);

ret.matrix[0][0] = (2*near)/(right-left);
ret.matrix[0][2] = (right+left)/(right-left);
ret.matrix[1][1] = (2*near)/(top-bottom);
ret.matrix[1][2] = (top+bottom)/(top-bottom);
ret.matrix[2][2] = -((far)/(far-near));
ret.matrix[2][3] = -((far*near)/(far-near));
ret.matrix[3][2] = -1;

return ret;
}

/// Returns an inverse perspective matrix (4x4 and floating-point matrices only).
static Matrix persperctiveInverse(mt width, mt height, mt fov, mt near, mt far) {
mt[6] cdata = cperspective(width, height, fov, near, far);
Expand Down Expand Up @@ -1380,6 +1411,30 @@ struct Matrix(type, int rows_, int cols_) if((rows_ > 0) && (cols_ > 0)) {
return ret;
}


/// Returns an orthographic matrix (4x4 and floating-point matrices only).
/// This matrix is made for Metal's NDC.
static Matrix orthographic01(mt left, mt right, mt bottom, mt top, mt near, mt far)
in {
assert(right-left != 0);
assert(top-bottom != 0);
assert(far-near != 0);
}
do {
Matrix ret;
ret.clear(0);

ret.matrix[0][0] = 2/(right-left);
ret.matrix[0][3] = -(right+left)/(right-left);
ret.matrix[1][1] = 2/(top-bottom);
ret.matrix[1][3] = -(top+bottom)/(top-bottom);
ret.matrix[2][2] = 1/(far-near);
ret.matrix[2][3] = -1/(far-near);
ret.matrix[3][3] = 1;

return ret;
}

// (1) and (2) say this one is correct
/// Returns an inverse ortographic matrix (4x4 and floating-point matrices only).
static Matrix orthographicInverse(mt left, mt right, mt bottom, mt top, mt near, mt far) {
Expand Down

0 comments on commit 6ee33a0

Please sign in to comment.