public Scene(List <Mesh> meshList, List <Light> lights, Rasterization r, VertexProcessor v) { foreach (Mesh m in meshList) { m.MakeNormals(); foreach (Light l in lights) { m.Light(l, v); m.DrawMesh(r, v, l); } } }
public void Triangle(Vector3 pos1, Vector3 pos2, Vector3 pos3, Vertex v1, Vertex v2, Vertex v3, Light l, VertexProcessor vert) { float p1x = (pos1.X + 1) * buff.colorBuffer.Width * 0.5f; float p1y = (pos1.Y + 1) * buff.colorBuffer.Height * 0.5f; float p2x = (pos2.X + 1) * buff.colorBuffer.Width * 0.5f; float p2y = (pos2.Y + 1) * buff.colorBuffer.Height * 0.5f; float p3x = (pos3.X + 1) * buff.colorBuffer.Width * 0.5f; float p3y = (pos3.Y + 1) * buff.colorBuffer.Height * 0.5f; int minx = (int)Math.Min(p1x, Math.Min(p2x, p3x)); int miny = (int)Math.Min(p1y, Math.Min(p2y, p3y)); int maxx = (int)Math.Max(p1x, Math.Max(p2x, p3x)); int maxy = (int)Math.Max(p1y, Math.Max(p2y, p3y)); minx = Math.Max(minx, 0); maxx = Math.Min(maxx, buff.colorBuffer.Width - 1); miny = Math.Max(miny, 0); maxy = Math.Min(maxy, buff.colorBuffer.Height - 1); bool tl1, tl2, tl3; tl1 = tl2 = tl3 = false; float dy12 = p1y - p2y; float dy23 = p2y - p3y; float dy31 = p3y - p1y; float dx12 = p1x - p2x; float dx23 = p2x - p3x; float dx31 = p3x - p1x; if (dy12 < 0 || (dy12 == 0 && dx12 > 0)) { tl1 = true; } if (dy23 < 0 || (dy23 == 0 && dx23 > 0)) { tl2 = true; } if (dy31 < 0 || (dy31 == 0 && dx31 > 0)) { tl3 = true; } for (int x = minx; x <= maxx; x++) { for (int y = miny; y <= maxy; y++) { if ((((p1x - p2x) * (y - p1y) - (p1y - p2y) * (x - p1x) > 0 && !tl1) || ((p1x - p2x) * (y - p1y) - (p1y - p2y) * (x - p1x) >= 0 && tl1)) && (((p2x - p3x) * (y - p2y) - (p2y - p3y) * (x - p2x) > 0 && !tl2) || ((p2x - p3x) * (y - p2y) - (p2y - p3y) * (x - p2x) >= 0 && tl2)) && (((p3x - p1x) * (y - p3y) - (p3y - p1y) * (x - p3x) > 0 && !tl3) || ((p3x - p1x) * (y - p3y) - (p3y - p1y) * (x - p3x) >= 0 && tl3))) { //WSPOLRZEDNE BARYCENTRYCZNE float lambda1 = (((p2y - p3y) * (x - p3x)) + ((p3x - p2x) * (y - p3y))) / (((p2y - p3y) * (p1x - p3x)) + ((p3x - p2x) * (p1y - p3y))); float lambda2 = (((p3y - p1y) * (x - p3x)) + ((p1x - p3x) * (y - p3y))) / (((p3y - p1y) * (p2x - p3x)) + ((p1x - p3x) * (p2y - p3y))); float lambda3 = 1 - lambda1 - lambda2; //bufor głębokości float depth = lambda1 * pos1.Z + lambda2 * pos2.Z + lambda3 * pos3.Z; if (depth < buff.depthBuffer[x, y]) { //OSWIETLENIE PER VERTEX Vector3 ambient = new Vector3(50, 0, 0); Vector3 color = (v1.light * lambda1) + (v2.light * lambda2) + (v3.light * lambda3); //Vector3 light = l.GoraudShading(vert, v1, v2, v3); Color col = Color.FromArgb(maxCol((int)(color.X + ambient.X)), maxCol((int)(color.Y + ambient.Y)), maxCol((int)(color.Z + ambient.Z))); buff.depthBuffer[x, y] = depth; buff.colorBuffer.SetPixel(x, y, col); } } } } }
static void Main(string[] args) { #region Wstępne ładowanie Console.WriteLine("Rasteryzer - Jędrzej Kołecki - 228012\nStart...\n"); Buffer buff = new Buffer(1500, 1500, Color.BlanchedAlmond); List <Mesh> meshList = new List <Mesh>(); List <Light> lightList = new List <Light>(); Rasterization render = new Rasterization(buff); VertexProcessor vertex = new VertexProcessor(); var objLoaderFactory = new ObjLoaderFactory(); var objLoader = objLoaderFactory.Create(); Vector3 p0 = new Vector3(2f, 5f, 5f); DirectionalLight light = new DirectionalLight(p0); #endregion FileStream stream = new FileStream("Faces.txt", FileMode.OpenOrCreate); StreamWriter writer = new StreamWriter(stream); #region Dodanie obiektów do sceny ObjMesh obj = new ObjMesh(); Console.WriteLine("Podaj nazwe pliku OBJ: "); string nameObj = Console.ReadLine(); var fileStream = new FileStream(nameObj, FileMode.Open); var result = objLoader.Load(fileStream); //ladowanie pozycji wierzcholkow foreach (ObjLoader.Loader.Data.VertexData.Vertex l in result.Vertices) { Vector3 pos = new Vector3(l.X, l.Y, l.Z); Vertex newVert = new Vertex(pos); obj.vertexes.Add(newVert); } Console.WriteLine("Ilosc wierzcholkow w pliku .obj: " + obj.vertexes.Count() + "\n"); List <int> indexes = new List <int>(); foreach (ObjLoader.Loader.Data.Elements.Group n in result.Groups) { int orderNumber = 0; foreach (ObjLoader.Loader.Data.Elements.Face f in n.Faces) { writer.WriteLine("Face: " + orderNumber); for (int i = 0; i < f._vertices.Count; i++) { indexes.Add(f._vertices[i].VertexIndex - 1); writer.Write((f._vertices[i].VertexIndex - 1) + " "); } orderNumber++; writer.WriteLine("\n"); } } writer.Close(); lightList.Add(light); obj.indexes = indexes; meshList.Add(obj); Scene scene = new Scene(meshList, lightList, render, vertex); #endregion buff.SaveImage(); Console.WriteLine("\nRasteryzacja zakonczona sukcesem."); Console.ReadKey(); }