public Mat4(Vec4 row1, Vec4 row2, Vec4 row3, Vec4 row4) { data = new Vec4[4]; data[0] = row1; data[1] = row2; data[2] = row3; data[3] = row4; }
public Mat4() { data = new Vec4[4]; data[0] = new Vec4(1.0f, 0.0f, 0.0f, 0.0f); data[1] = new Vec4(0.0f, 1.0f, 0.0f, 0.0f); data[2] = new Vec4(0.0f, 0.0f, 1.0f, 0.0f); data[3] = new Vec4(0.0f, 0.0f, 0.0f, 1.0f); }
public static Mat4 ProjectionMatrix4(float angleOfView, float near, float far) { float scale = 1.0f / (float)(Math.Tan(angleOfView * 0.5f * (float)(Math.PI) / 180.0f)); Vec4 row1 = new Vec4(scale, 0.0f, 0.0f, 0.0f); Vec4 row2 = new Vec4(0.0f, scale, 0.0f, 0.0f); Vec4 row3 = new Vec4(0.0f, 0.0f, -far / (far - near), -1.0f); Vec4 row4 = new Vec4(0.0f, 0.0f, -far * near / (far - near), 0.0f); return(new Mat4(row1, row2, row3, row4)); }
public static Mat4 RotationXMatrix(float angle) { float cosTheta = (float)Math.Cos(angle); float sinTheta = (float)Math.Sin(angle); Vec4 row1 = new Vec4(1.0f, 0.0f, 0.0f, 0.0f); Vec4 row2 = new Vec4(0.0f, cosTheta, -sinTheta, 0.0f); Vec4 row3 = new Vec4(0.0f, sinTheta, cosTheta, 0.0f); Vec4 row4 = new Vec4(0.0f, 0.0f, 0.0f, 1.0f); return(new Mat4(row1, row2, row3, row4)); }
public static Vec4 operator *(Mat4 m1, Vec4 v1) { Vec4 outRes = new Vec4(); 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) { outRes.x /= w; outRes.y /= w; outRes.z /= w; outRes.w /= w; } return(outRes); }
static void Main(string[] args) { MilkShapeObject obj = new MilkShapeObject(); //obj.LoadFromFile("triangle.ms3d"); //obj.LoadFromFile("cube.ms3d"); obj.LoadFromFile("tree.ms3d"); //obj.LoadFromFile("doom.ms3d"); Color8[,] screen = new Color8[80, 25]; float[,] zbuffer = new float[80, 25]; float angle = 0.0f; while (true) { clearScreen(screen, zbuffer); Mat4 model = new Mat4( new Vec4(1.0f, 0.0f, 0.0f, 0.0f), new Vec4(0.0f, 1.0f, 0.0f, 0.0f), new Vec4(0.0f, 0.0f, 1.0f, 0.0f), new Vec4(0.0f, 0.0f, 0.0f, 1.0f) ); model = model * Mat4.RotationYMatrix(angle); Mat4 view = new Mat4( new Vec4(1.0f, 0.0f, 0.0f, 0.0f), new Vec4(0.0f, 1.0f, 0.0f, 0.0f), new Vec4(0.0f, 0.0f, 1.0f, 0.0f), new Vec4(0.0f, -30.0f, 70.0f, 1.0f) ); Mat4 modelView = model * view; Mat4 proj = Mat4.ProjectionMatrix4(60.0f, 0.1f, 1000.0f); int numTriangles = obj.arrTriangles.Count; for (int i = 0; i < numTriangles; ++i) { float x1 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[0]].vertex[0]; float y1 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[0]].vertex[1]; float z1 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[0]].vertex[2]; Vec4 point1 = modelView * new Vec4(x1, y1, z1, 0.0f); point1 = proj * point1; point1.x = (point1.x + 1.0f) / 2.0f * 80.0f; point1.y = (point1.y + 1.0f) / 2.0f * 25.0f; float x2 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[1]].vertex[0]; float y2 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[1]].vertex[1]; float z2 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[1]].vertex[2]; Vec4 point2 = modelView * new Vec4(x2, y2, z2, 0.0f); point2 = proj * point2; point2.x = (point2.x + 1.0f) / 2.0f * 80.0f; point2.y = (point2.y + 1.0f) / 2.0f * 25.0f; float x3 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[2]].vertex[0]; float y3 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[2]].vertex[1]; float z3 = obj.arrVertices[obj.arrTriangles[i].vertexIndices[2]].vertex[2]; Vec4 point3 = modelView * new Vec4(x3, y3, z3, 0.0f); point3 = proj * point3; point3.x = (point3.x + 1.0f) / 2.0f * 80.0f; point3.y = (point3.y + 1.0f) / 2.0f * 25.0f; printLine(screen, zbuffer, (int)point1.x, (int)point1.y, (int)point2.x, (int)point2.y, new Color8(255, 255, 255) ); printLine(screen, zbuffer, (int)point2.x, (int)point2.y, (int)point3.x, (int)point3.y, new Color8(255, 255, 255) ); printLine(screen, zbuffer, (int)point3.x, (int)point3.y, (int)point1.x, (int)point1.y, new Color8(255, 255, 255) ); } render(screen, zbuffer); angle += (float)(Math.PI / 32.0f); } Console.ReadKey(); }
public static Mat4 operator *(Mat4 m1, Mat4 m2) { Vec4 ap1 = m1.data[0]; Vec4 ap2 = m1.data[1]; Vec4 ap3 = m1.data[2]; Vec4 ap4 = m1.data[3]; Vec4 bp1 = m2.data[0]; Vec4 bp2 = m2.data[1]; Vec4 bp3 = m2.data[2]; Vec4 bp4 = m2.data[3]; Vec4 cp1 = new Vec4(); Vec4 cp2 = new Vec4(); Vec4 cp3 = new Vec4(); Vec4 cp4 = new Vec4(); 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 Mat4(cp1, cp2, cp3, cp4)); }