public static Mesh GeneratePyramid(float width, float depth, float height, Point3D loc, Material mat) { Mesh m; Point3D a = new Point3D(loc.X - width / 2, loc.Y - height / 2, loc.Z); Point3D b = new Point3D(loc.X + width / 2, loc.Y - height / 2, loc.Z); Point3D c = new Point3D(loc.X + width / 2, loc.Y + height / 2, loc.Z); Point3D d = new Point3D(loc.X - width / 2, loc.Y + height / 2, loc.Z); Point3D e = new Point3D(loc.X, loc.Y, loc.Z + height / 2); Triangle3D tri = new Triangle3D(d, a, e); Triangle3D tri2 = new Triangle3D(a, b, e); Triangle3D tri3 = new Triangle3D(b, c, e); Triangle3D tri4 = new Triangle3D(c, d, e); Triangle3D tri5 = new Triangle3D(c, a, d); Triangle3D tri6 = new Triangle3D(b, a, c); List <Point3D> points = new List <Point3D>(); List <Triangle3D> triangles = new List <Triangle3D>(); points.Add(a); points.Add(b); points.Add(c); points.Add(d); points.Add(e); triangles.Add(tri); triangles.Add(tri2); triangles.Add(tri3); triangles.Add(tri4); triangles.Add(tri5); triangles.Add(tri6); m = new Mesh(points, triangles, mat); m.shaded = false; return(m); }
public void Render(Tuple <int, ConsoleColor>[,] screen) { int screenWidth = screen.GetLength(0); int screenHeight = screen.Length / screen.GetLength(0); List <Tuple <Triangle3D, int, ConsoleColor> > orderedTriangles = getTriangles(); foreach (Tuple <Triangle3D, int, ConsoleColor> t in orderedTriangles) { Triangle3D tri = t.Item1; if (triangleInView(tri)) { tri.Translate(new Point3D(1, 1, 0)); tri.Scale(new Point3D(screenWidth / 2, screenHeight / 2, 1)); Triangle2D tri2D = new Triangle2D(new Point2D(tri.V1.X, tri.V1.Y), new Point2D(tri.V2.X, tri.V2.Y), new Point2D(tri.V3.X, tri.V3.Y)); // screen, lightvalue, color if (triangleInBounds(tri2D, screen) && tri2D.Area < 4000) { tri2D.drawTriangle(screen, t.Item2, t.Item3); } } } }
private bool triangleInView(Triangle3D tri) { return((tri.V1.Z <= 1 && tri.V1.Z >= -1) && (tri.V2.Z <= 1 && tri.V2.Z >= -1) && (tri.V3.Z <= 1 && tri.V3.Z >= -1) && ((tri.V1.X <= 1 && tri.V1.X >= -1) || (tri.V2.X <= 1 && tri.V2.X >= -1) || (tri.V3.X <= 1 && tri.V3.X >= -1)) && ((tri.V1.Y <= 1 && tri.V1.Y >= -1) || (tri.V2.Y <= 1 && tri.V2.Y >= -1) || (tri.V3.Y <= 1 && tri.V3.Y >= -1))); }
private List <Tuple <Triangle3D, int, ConsoleColor> > getTriangles() { List <Tuple <Triangle3D, int, ConsoleColor> > tri = new List <Tuple <Triangle3D, int, ConsoleColor> >(); foreach (Mesh m in meshes) { // get all the faces, in some random order List <Triangle3D> faces = m.Faces(); //List<Triangle3D> temp = new List<Triangle3D>(); foreach (Triangle3D t in faces) { //int lightVal = (int)t.getLightValue(lights); int lightVal = (int)(new Point3D(t.Center.X, t.Center.Y, t.Center.Z, t.Normal)).getLightValue(lights, camera.ViewVector); //convert to camera space //sort this triangle list Matrix conversion = camera.WorldtoCameraMatrix; Matrix viewConversion = camera.getCameraViewMatrix(); Point3D reference = t.V1; Matrix cameraVersion = reference * conversion; //Matrix cameraVersion = conversion * reference; Matrix v1boxCoords = cameraVersion * viewConversion; Point3D boxV1 = v1boxCoords.boxCoordsToPoint3D(); reference = t.V2; cameraVersion = reference * conversion; Matrix v2boxCoords = cameraVersion * viewConversion; Point3D boxV2 = v2boxCoords.boxCoordsToPoint3D(); reference = t.V3; cameraVersion = reference * conversion; Matrix v3boxCoords = cameraVersion * viewConversion; Point3D boxV3 = v3boxCoords.boxCoordsToPoint3D(); Triangle3D boxTriangle = new Triangle3D(boxV1, boxV2, boxV3); ConsoleColor col = ConsoleColor.White; if (m.Material != null) { col = m.Material.Colour; } //if (Point3D.Dot(camera.ViewVector, t.Normal) > -.5) Tuple <Triangle3D, int, ConsoleColor> tuple = new Tuple <Triangle3D, int, ConsoleColor>(boxTriangle, lightVal, col); tri.Add(tuple); } } List <Tuple <Triangle3D, int, ConsoleColor> > ordered = tri.OrderBy(o => o.Item1.Center.Z).ToList(); return(ordered); }
public void RenderShaded(Tuple <int, ConsoleColor>[,] screen) { int screenWidth = screen.GetLength(0); int screenHeight = screen.Length / screen.GetLength(0); List <Tuple <Triangle3D, int, int, int, ConsoleColor> > tri = new List <Tuple <Triangle3D, int, int, int, ConsoleColor> >(); foreach (Mesh m in meshes) { // get all the faces, in some random order List <Triangle3D> faces = m.Faces(); //List<Triangle3D> temp = new List<Triangle3D>(); foreach (Triangle3D t in faces) { int l1 = (int)t.V1.getLightValue(lights, camera.ViewVector); int l2 = (int)t.V2.getLightValue(lights, camera.ViewVector); int l3 = (int)t.V3.getLightValue(lights, camera.ViewVector); if (!m.Shaded) { l1 = (int)(new Point3D(t.Center.X, t.Center.Y, t.Center.Z, t.Normal)).getLightValue(lights, camera.ViewVector); l2 = l1; l3 = l2; } //int lightVal = (int)t.getLightValue(lights); //convert to camera space //sort this triangle list Matrix conversion = camera.WorldtoCameraMatrix; Matrix viewConversion = camera.getCameraViewMatrix(); Point3D reference = t.V1; Matrix cameraVersion = reference * conversion; //Matrix cameraVersion = conversion * reference; Matrix v1boxCoords = cameraVersion * viewConversion; Point3D boxV1 = v1boxCoords.boxCoordsToPoint3D(); reference = t.V2; cameraVersion = reference * conversion; Matrix v2boxCoords = cameraVersion * viewConversion; Point3D boxV2 = v2boxCoords.boxCoordsToPoint3D(); reference = t.V3; cameraVersion = reference * conversion; Matrix v3boxCoords = cameraVersion * viewConversion; Point3D boxV3 = v3boxCoords.boxCoordsToPoint3D(); Triangle3D boxTriangle = new Triangle3D(boxV1, boxV2, boxV3); ConsoleColor col = ConsoleColor.White; if (m.Material != null) { col = m.Material.Colour; } //if (Point3D.Dot(camera.ViewVector, t.Normal) > -.5) Tuple <Triangle3D, int, int, int, ConsoleColor> tuple = new Tuple <Triangle3D, int, int, int, ConsoleColor>(boxTriangle, l1, l2, l3, col); tri.Add(tuple); } } List <Tuple <Triangle3D, int, int, int, ConsoleColor> > ordered = tri.OrderBy(o => o.Item1.Center.Z).ToList(); foreach (Tuple <Triangle3D, int, int, int, ConsoleColor> t in ordered) { Triangle3D triTemp = t.Item1; if (triangleInView(triTemp)) { triTemp.Translate(new Point3D(1, 1, 0)); triTemp.Scale(new Point3D(screenWidth / 2, screenHeight / 2, 1)); Triangle2D tri2D = new Triangle2D(new Point2D(triTemp.V1.X, triTemp.V1.Y), new Point2D(triTemp.V2.X, triTemp.V2.Y), new Point2D(triTemp.V3.X, triTemp.V3.Y)); // screen, lightvalue, color if (triangleInBounds(tri2D, screen) && tri2D.Area < 4000) { tri2D.drawTriangleShaded(screen, t.Item2, t.Item3, t.Item4, t.Item5); } } } }