public Mat4f(Vec4f row1, Vec4f row2, Vec4f row3, Vec4f row4) { data = new Vec4f[4]; data[0] = row1; data[1] = row2; data[2] = row3; data[3] = row4; }
public Mat4f() { data = new Vec4f[4]; data[0] = new Vec4f(1.0f, 0.0f, 0.0f, 0.0f); data[1] = new Vec4f(0.0f, 1.0f, 0.0f, 0.0f); data[2] = new Vec4f(0.0f, 0.0f, 1.0f, 0.0f); data[3] = new Vec4f(0.0f, 0.0f, 0.0f, 1.0f); }
public Mat4f(float s) { data = new Vec4f[4]; data[0] = new Vec4f(s); data[1] = new Vec4f(s); data[2] = new Vec4f(s); data[3] = new Vec4f(s); }
public Mat4f inverse() { float Coef00 = data[2][2] * data[3][3] - data[3][2] * data[2][3]; float Coef02 = data[1][2] * data[3][3] - data[3][2] * data[1][3]; float Coef03 = data[1][2] * data[2][3] - data[2][2] * data[1][3]; float Coef04 = data[2][1] * data[3][3] - data[3][1] * data[2][3]; float Coef06 = data[1][1] * data[3][3] - data[3][1] * data[1][3]; float Coef07 = data[1][1] * data[2][3] - data[2][1] * data[1][3]; float Coef08 = data[2][1] * data[3][2] - data[3][1] * data[2][2]; float Coef10 = data[1][1] * data[3][2] - data[3][1] * data[1][2]; float Coef11 = data[1][1] * data[2][2] - data[2][1] * data[1][2]; float Coef12 = data[2][0] * data[3][3] - data[3][0] * data[2][3]; float Coef14 = data[1][0] * data[3][3] - data[3][0] * data[1][3]; float Coef15 = data[1][0] * data[2][3] - data[2][0] * data[1][3]; float Coef16 = data[2][0] * data[3][2] - data[3][0] * data[2][2]; float Coef18 = data[1][0] * data[3][2] - data[3][0] * data[1][2]; float Coef19 = data[1][0] * data[2][2] - data[2][0] * data[1][2]; float Coef20 = data[2][0] * data[3][1] - data[3][0] * data[2][1]; float Coef22 = data[1][0] * data[3][1] - data[3][0] * data[1][1]; float Coef23 = data[1][0] * data[2][1] - data[2][0] * data[1][1]; Vec4f Fac0 = new Vec4f(Coef00, Coef00, Coef02, Coef03); Vec4f Fac1 = new Vec4f(Coef04, Coef04, Coef06, Coef07); Vec4f Fac2 = new Vec4f(Coef08, Coef08, Coef10, Coef11); Vec4f Fac3 = new Vec4f(Coef12, Coef12, Coef14, Coef15); Vec4f Fac4 = new Vec4f(Coef16, Coef16, Coef18, Coef19); Vec4f Fac5 = new Vec4f(Coef20, Coef20, Coef22, Coef23); Vec4f Vec0 = new Vec4f(data[1][0], data[0][0], data[0][0], data[0][0]); Vec4f Vec1 = new Vec4f(data[1][1], data[0][1], data[0][1], data[0][1]); Vec4f Vec2 = new Vec4f(data[1][2], data[0][2], data[0][2], data[0][2]); Vec4f Vec3 = new Vec4f(data[1][3], data[0][3], data[0][3], data[0][3]); Vec4f Inv0 = new Vec4f(Vec1 * Fac0 - Vec2 * Fac1 + Vec3 * Fac2); Vec4f Inv1 = new Vec4f(Vec0 * Fac0 - Vec2 * Fac3 + Vec3 * Fac4); Vec4f Inv2 = new Vec4f(Vec0 * Fac1 - Vec1 * Fac3 + Vec3 * Fac5); Vec4f Inv3 = new Vec4f(Vec0 * Fac2 - Vec1 * Fac4 + Vec2 * Fac5); Vec4f SignA = new Vec4f(+1, -1, +1, -1); Vec4f SignB = new Vec4f(-1, +1, -1, +1); Mat4f Inverse = new Mat4f(Inv0 * SignA, Inv1 * SignB, Inv2 * SignA, Inv3 * SignB); Vec4f Row0 = new Vec4f(Inverse.data[0][0], Inverse.data[1][0], Inverse.data[2][0], Inverse.data[3][0]); Vec4f Dot0 = new Vec4f(data[0] * Row0); float Dot1 = (Dot0.x + Dot0.y) + (Dot0.z + Dot0.w); float OneOverDeterminant = 1.0f / Dot1; return(Inverse * OneOverDeterminant); }
public static Mat4f ProjectionMatrix4(float angleOfView, float near, float far) { float scale = 1.0f / (float)(Math.Tan(angleOfView * 0.5f * (float)(Math.PI) / 180.0f)); Vec4f row1 = new Vec4f(scale, 0.0f, 0.0f, 0.0f); Vec4f row2 = new Vec4f(0.0f, scale, 0.0f, 0.0f); Vec4f row3 = new Vec4f(0.0f, 0.0f, -far / (far - near), -1.0f); Vec4f row4 = new Vec4f(0.0f, 0.0f, -far * near / (far - near), 0.0f); return(new Mat4f(row1, row2, row3, row4)); }
public static Mat4f PerspectiveZ(float fovy, float aspect, float zNear, float zFar) { float tanHalfFovy = (float)Math.Tan(fovy / 2.0f); Vec4f row1 = new Vec4f(1.0f / (aspect * tanHalfFovy), 0.0f, 0.0f, 0.0f); Vec4f row2 = new Vec4f(0.0f, 1.0f / (tanHalfFovy), 0.0f, 0.0f); Vec4f row3 = new Vec4f(0.0f, 0.0f, 0.0f, -1.0f); Vec4f row4 = new Vec4f(0.0f, 0.0f, 1.0f, 0.0f); return(new Mat4f(row1, row2, row3, row4)); }
public static Mat4f RotationXMatrix(float angle) { float cosTheta = (float)Math.Cos(angle); float sinTheta = (float)Math.Sin(angle); Vec4f row1 = new Vec4f(1.0f, 0.0f, 0.0f, 0.0f); Vec4f row2 = new Vec4f(0.0f, cosTheta, -sinTheta, 0.0f); Vec4f row3 = new Vec4f(0.0f, sinTheta, cosTheta, 0.0f); Vec4f row4 = new Vec4f(0.0f, 0.0f, 0.0f, 1.0f); return(new Mat4f(row1, row2, row3, row4)); }
public static Mat4f PerspectiveLH(float fovy, float aspect, float zNear, float zFar) { float tanHalfFovy = (float)Math.Tan(fovy / 2.0f); Vec4f row1 = new Vec4f(1.0f / (aspect * tanHalfFovy), 0.0f, 0.0f, 0.0f); Vec4f row2 = new Vec4f(0.0f, 1.0f / (tanHalfFovy), 0.0f, 0.0f); Vec4f row3 = new Vec4f(0.0f, 0.0f, (zFar + zNear) / (zFar - zNear), 1.0f); Vec4f row4 = new Vec4f(0.0f, 0.0f, -(2.0f * zFar * zNear) / (zFar - zNear), 0.0f); //Vec4f row1 = new Vec4f(1.0f / (aspect * tanHalfFovy), 0.0f, 0.0f, 0.0f); //Vec4f row2 = new Vec4f(0.0f, 1.0f / (tanHalfFovy), 0.0f, 0.0f); //Vec4f row3 = new Vec4f(0.0f, 0.0f, zFar / (zFar - zNear), 1.0f); //Vec4f row4 = new Vec4f(0.0f, 0.0f, -(zFar * zNear) / (zFar - zNear), 0.0f); return(new Mat4f(row1, row2, row3, row4)); }
public static Vec4f operator *(Mat4f m1, Vec4f v1) { Vec4f outRes = new Vec4f(); outRes.x = v1.x * m1.data[0].x + v1.y * m1.data[1].x + v1.z * m1.data[2].x + m1.data[3].x; outRes.y = v1.x * m1.data[0].y + v1.y * m1.data[1].y + v1.z * m1.data[2].y + m1.data[3].y; outRes.z = v1.x * m1.data[0].z + v1.y * m1.data[1].z + v1.z * m1.data[2].z + m1.data[3].z; float w = v1.x * m1.data[0].w + v1.y * m1.data[1].w + v1.z * m1.data[2].w + m1.data[3].w; if (w != 1.0f && w != 0.0f) { outRes.x /= w; outRes.y /= w; outRes.z /= w; outRes.w /= w; } return(outRes); }
public float determinant() { float SubFactor00 = data[2][2] * data[3][3] - data[3][2] * data[2][3]; float SubFactor01 = data[2][1] * data[3][3] - data[3][1] * data[2][3]; float SubFactor02 = data[2][1] * data[3][2] - data[3][1] * data[2][2]; float SubFactor03 = data[2][0] * data[3][3] - data[3][0] * data[2][3]; float SubFactor04 = data[2][0] * data[3][2] - data[3][0] * data[2][2]; float SubFactor05 = data[2][0] * data[3][1] - data[3][0] * data[2][1]; Vec4f DetCof = new Vec4f( +(data[1][1] * SubFactor00 - data[1][2] * SubFactor01 + data[1][3] * SubFactor02), -(data[1][0] * SubFactor00 - data[1][2] * SubFactor03 + data[1][3] * SubFactor04), +(data[1][0] * SubFactor01 - data[1][1] * SubFactor03 + data[1][3] * SubFactor05), -(data[1][0] * SubFactor02 - data[1][1] * SubFactor04 + data[1][2] * SubFactor05) ); return (data[0][0] * DetCof[0] + data[0][1] * DetCof[1] + data[0][2] * DetCof[2] + data[0][3] * DetCof[3]); }
public static Mat4f operator *(Mat4f m1, Mat4f m2) { Vec4f ap1 = m1.data[0]; Vec4f ap2 = m1.data[1]; Vec4f ap3 = m1.data[2]; Vec4f ap4 = m1.data[3]; Vec4f bp1 = m2.data[0]; Vec4f bp2 = m2.data[1]; Vec4f bp3 = m2.data[2]; Vec4f bp4 = m2.data[3]; Vec4f cp1 = new Vec4f(); Vec4f cp2 = new Vec4f(); Vec4f cp3 = new Vec4f(); Vec4f cp4 = new Vec4f(); float a0, a1, a2, a3; a0 = ap1.x; a1 = ap1.y; a2 = ap1.z; a3 = ap1.w; cp1.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x; cp1.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y; cp1.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z; cp1.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w; a0 = ap2.x; a1 = ap2.y; a2 = ap2.z; a3 = ap2.w; cp2.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x; cp2.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y; cp2.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z; cp2.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w; a0 = ap3.x; a1 = ap3.y; a2 = ap3.z; a3 = ap3.w; cp3.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x; cp3.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y; cp3.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z; cp3.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w; a0 = ap4.x; a1 = ap4.y; a2 = ap4.z; a3 = ap4.w; cp4.x = a0 * bp1.x + a1 * bp2.x + a2 * bp3.x + a3 * bp4.x; cp4.y = a0 * bp1.y + a1 * bp2.y + a2 * bp3.y + a3 * bp4.y; cp4.z = a0 * bp1.z + a1 * bp2.z + a2 * bp3.z + a3 * bp4.z; cp4.w = a0 * bp1.w + a1 * bp2.w + a2 * bp3.w + a3 * bp4.w; return(new Mat4f(cp1, cp2, cp3, cp4)); }
public Triangle2f(Vec4f v0, Vec4f v1, Vec4f v2) { this.v0 = new Vec2f(v0); this.v1 = new Vec2f(v1); this.v2 = new Vec2f(v2); }
public static Vec2i fromCut(Vec4f other) { return(new Vec2i((int)other.x, (int)other.y)); }
public static Vec2i fromRound(Vec4f other) { return(new Vec2i((int)Math.Round(other.x), (int)Math.Round(other.y))); }
public static Vec2i fromCeil(Vec4f other) { return(new Vec2i((int)Math.Ceiling(other.x), (int)Math.Ceiling(other.y))); }
public Vec3f(Vec4f other) { this.x = other.x; this.y = other.y; this.z = other.z; }