private Color Count(int x, int y, double ks, double kd, int m) { Color mainColor = fill_color; if (loadTexture.Checked && texture_loaded) { mainColor = textureBmp.GetPixel(x % textureBmp.Width, y % textureBmp.Height); } Vector3 N = new Vector3(0, 0, 1); if (!staticVector.Checked && normal_map_loaded) { var color = new ColorVector(normalMap.GetPixel(x % normalMap.Width, y % normalMap.Height)); N = new Vector3(color.R, color.G, color.B); } Vector3 L = new Vector3(0, 0, 1); if (!staticL.Checked) { L = new Vector3(light_source.X - x, y - light_source.Y, light_source.Z); } Vector3 R = new Vector3(2 * DotProd(L, N) * N.x - L.x, 2 * DotProd(L, N) * N.y - L.y, 2 * DotProd(L, N) * N.z - L.z); R.Wersolize(); int r = (int)Math.Round(kd * light_color.R * mainColor.R * cosin(N, L) + ks * light_color.R * mainColor.R * Math.Pow(cosin(V, R), m)); int g = (int)Math.Round(kd * light_color.G * mainColor.G * cosin(N, L) + ks * light_color.G * mainColor.G * Math.Pow(cosin(V, R), m)); int b = (int)Math.Round(kd * light_color.B * mainColor.B * cosin(N, L) + ks * light_color.B * mainColor.B * Math.Pow(cosin(V, R), m)); r = r < 0 ? 0 : r; g = g < 0 ? 0 : g; b = b < 0 ? 0 : b; r = r > 255 ? 255 : r; g = g > 255 ? 255 : g; b = b > 255 ? 255 : b; return(Color.FromArgb(r, g, b)); }
private void FillFigureHybrid(Polygon polygon, double ks, double kd, int m) { if (randomParameters.Checked) { ks = rand.NextDouble(); kd = rand.NextDouble(); m = (int)(rand.NextDouble() * 100); } List <Edge>[] edges_table = new List <Edge> [drawing_panel.Height]; for (int i = 0; i < polygon.edges.Count; i++) { Edge curr = polygon.edges[i]; if (edges_table[curr.a.Get().Y] is null) { edges_table[curr.a.Get().Y] = new List <Edge>(); } edges_table[curr.a.Get().Y].Add(curr); } List <Edge> active_edges_table = new List <Edge>(); int current = 0; for (int i = 0; i < drawing_panel.Height; i++) { if (!(edges_table[i] is null)) { current = i; break; } } Point pointa = new Point(polygon.vertices[0].Get().X, polygon.vertices[0].Get().Y); Point pointb = new Point(polygon.vertices[1].Get().X, polygon.vertices[1].Get().Y); Point pointc = new Point(polygon.vertices[2].Get().X, polygon.vertices[2].Get().Y); Color preva = fill_color; Color prevb = fill_color; Color prevc = fill_color; if (!selectObjectColor.Checked && texture_loaded) { preva = textureBmp.GetPixel(pointa.X % textureBmp.Width, pointa.Y % textureBmp.Height); prevb = textureBmp.GetPixel(pointb.X % textureBmp.Width, pointb.Y % textureBmp.Height); prevc = textureBmp.GetPixel(pointc.X % textureBmp.Width, pointc.Y % textureBmp.Height); } var Na = new Vector3(0, 0, 1); var Nb = new Vector3(0, 0, 1); var Nc = new Vector3(0, 0, 1); if (!staticVector.Checked && normal_map_loaded) { var color = new ColorVector(normalMap.GetPixel(pointa.X % normalMap.Width, pointa.Y % normalMap.Height)); Na = new Vector3(color.R, color.G, color.B); color = new ColorVector(normalMap.GetPixel(pointb.X % normalMap.Width, pointb.Y % normalMap.Height)); Nb = new Vector3(color.R, color.G, color.B); color = new ColorVector(normalMap.GetPixel(pointc.X % normalMap.Width, pointc.Y % normalMap.Height)); Nc = new Vector3(color.R, color.G, color.B); } active_edges_table.AddRange(edges_table[current]); while (active_edges_table.Count > 0 && current < drawing_panel.Height - 1) { active_edges_table.RemoveAll((Edge ed) => { return(ed.b.Get().Y <= current); }); active_edges_table.Sort((Edge e1, Edge e2) => { return(e1.x_min.CompareTo(e2.x_min)); }); for (int i = 0; i < active_edges_table.Count - 1; i += 2) { FillLineHybrid((int)Math.Round(active_edges_table[i].x_min), (int)Math.Round(active_edges_table[i + 1].x_min), current, pointa, pointb, pointc, preva, prevb, prevc, Na, Nb, Nc, ks, kd, m); active_edges_table[i].x_min += active_edges_table[i].one_div_m; active_edges_table[i + 1].x_min += active_edges_table[i + 1].one_div_m; } current++; if (!(edges_table[current] is null)) { active_edges_table.AddRange(edges_table[current]); } } for (int i = 0; i < polygon.edges.Count; i++) { polygon.edges[i].SetXmin(); } }