private void Start_Click(object sender, RoutedEventArgs e) { try { SetControlsEnabled(false); _zoom = 5; _screenWidth = Image.Width; _screenHeight = Image.Height; var dpiScale = VisualTreeHelper.GetDpi(this); _pixelsPerInchX = dpiScale.PixelsPerInchX; _pixelsPerInchY = dpiScale.PixelsPerInchY; var textureIndex = int.Parse(TextureIndex.Text); var options = new TriangleOptions() { SingleColor = bool.Parse(SingleColor.Text), PerformBackfaceCulling = bool.Parse(PerformBackfaceCulling.Text), DiffuseLambert = bool.Parse(DiffuseLambert.Text), SpecularPhong = bool.Parse(SpecularPhong.Text), SpecularPhongFactor = float.Parse(SpecularPhongFactor.Text), Texture = textureIndex >= 0 ? _textures[textureIndex] : null, BilinearFilter = bool.Parse(BilinearFilter.Text), GammaCorrect = bool.Parse(GammaCorrect.Text) }; var triangles = GetTriangles((int)_screenWidth, (int)_screenHeight, options); var lightSources = new LightSource[] { new LightSource("w", new Vector3(0.5f, 0.5f, -5), Colors.White) }; _scene = new SceneB((int)_screenWidth, (int)_screenHeight, _pixelsPerInchX, _pixelsPerInchY, triangles, lightSources, bool.Parse(GammaCorrect.Text), bool.Parse(ZBuffer.Text), bool.Parse(DeferredRender.Text)); _isRunning = true; CompositionTarget.Rendering += CompositionTarget_Rendering; } catch (Exception ex) { MessageBox.Show(ex.ToString()); SetControlsEnabled(true); } }
private Triangle[] GetTriangles(int screenWidth, int screenHeight, TriangleOptions options) { var triangles = new List <Triangle>(); var points = new Vector3[] { new Vector3(-1, -1, -1), new Vector3(+1, -1, -1), new Vector3(+1, +1, -1), new Vector3(-1, +1, -1), new Vector3(-1, -1, +1), new Vector3(+1, -1, +1), new Vector3(+1, +1, +1), new Vector3(-1, +1, +1) }; var triangleIdx = new List <(int, int, int, int, int, int, int)> { (0, 1, 2, 2, 3, 1, 0), // top (0, 2, 3, 2, 3, 0, 2), (7, 6, 5, 3, 3, 1, 0), // bottom (7, 5, 4, 3, 3, 0, 2), (0, 3, 7, 0, 3, 1, 0), // left (0, 7, 4, 0, 3, 0, 2), (2, 1, 5, 1, 3, 1, 0), // right (2, 5, 6, 1, 3, 0, 2), (3, 2, 6, 4, 3, 1, 0), // front (3, 6, 7, 4, 3, 0, 2), (1, 0, 4, 5, 3, 1, 0), // back (1, 4, 5, 5, 3, 0, 2) }; var colors = new Vector3[] { new Vector3(1, 0, 0), // red new Vector3(0, 1, 0), // green new Vector3(0, 0, 1) // blue }; var normals = new Vector3[] { -Vector3.UnitX, Vector3.UnitX, -Vector3.UnitY, Vector3.UnitY, -Vector3.UnitZ, Vector3.UnitZ }; var textureCoordinates = new Vector2[] { new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 0), new Vector2(0, 1) }; var random = new Random(); foreach (var t in triangleIdx) { var n = normals[t.Item4]; bool hasTexture = options.Texture != null; var t1 = Vector2.Zero; if (hasTexture) { t1 = textureCoordinates[t.Item5]; t1 = new Vector2(t1.X * options.Texture.Width, t1.Y * options.Texture.Height); } var v1 = new Vertex(points[t.Item1], colors[random.Next(0, colors.Length)], screenWidth, screenHeight, t1); var t2 = Vector2.Zero; if (hasTexture) { t2 = textureCoordinates[t.Item6]; t2 = new Vector2(t2.X * options.Texture.Width, t2.Y * options.Texture.Height); } var v2 = new Vertex(points[t.Item2], colors[random.Next(0, colors.Length)], screenWidth, screenHeight, t2); var t3 = Vector2.Zero; if (hasTexture) { t3 = textureCoordinates[t.Item7]; t3 = new Vector2(t3.X * options.Texture.Width, t3.Y * options.Texture.Height); } var v3 = new Vertex(points[t.Item3], colors[random.Next(0, colors.Length)], screenWidth, screenHeight, t3); triangles.Add(new Triangle(v1, v2, v3, options)); } return(triangles.ToArray()); }