|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.Drawing.Drawing2D; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Windows.Forms; |
| 12 | +using System.Windows.Media.Media3D; |
| 13 | + |
| 14 | +namespace winforms_z_buffer |
| 15 | +{ |
| 16 | + public partial class Display : Form |
| 17 | + { |
| 18 | + Cube[] cubes = null; |
| 19 | + Cube CSO; |
| 20 | + |
| 21 | + public Display() |
| 22 | + { |
| 23 | + // Setup Winform |
| 24 | + Width = Height = 800; |
| 25 | + StartPosition = FormStartPosition.CenterScreen; |
| 26 | + SetStyle( |
| 27 | + ControlStyles.AllPaintingInWmPaint | |
| 28 | + ControlStyles.UserPaint | |
| 29 | + ControlStyles.DoubleBuffer, |
| 30 | + true); |
| 31 | + |
| 32 | + CSO = Cube.UnitCube(Color.PaleGoldenrod); |
| 33 | + CSO.Rescale(0.1, 0.1, 0.1); |
| 34 | + |
| 35 | + // Create Cubes |
| 36 | + Cube c1 = Cube.UnitCube(Color.Red); |
| 37 | + c1.Rescale(10, 5, 2); |
| 38 | + // c1.Translate(new Vector3D(10, 10, -3)); |
| 39 | + |
| 40 | + //Cube c2 = Cube.UnitCube(Color.Blue); |
| 41 | + //c2.Rescale(5, 5, 5); |
| 42 | + //c2.Translate(new Vector3D(-10, 5, -2)); |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + cubes = new Cube[] { CSO, c1, }; |
| 47 | + } |
| 48 | + |
| 49 | + protected override void OnPaint(PaintEventArgs args) |
| 50 | + { |
| 51 | + var g = args.Graphics; |
| 52 | + g.SmoothingMode = SmoothingMode.HighQuality; |
| 53 | + g.Clear(Color.Black); |
| 54 | + |
| 55 | + // Move coordinate origin to center |
| 56 | + g.TranslateTransform(Width / 2, Height / 2); |
| 57 | + |
| 58 | + // Mark coordinate origin |
| 59 | + g.FillEllipse(Brushes.Purple, -4, -4, 8, 8); |
| 60 | + |
| 61 | + var drawing = new Drawing(g, true, true); |
| 62 | + |
| 63 | + if (cubes != null) |
| 64 | + foreach (var c in cubes) |
| 65 | + drawing.Draw(c); |
| 66 | + } |
| 67 | + |
| 68 | + void TranslateCubes(Vector3D displacement) |
| 69 | + { |
| 70 | + foreach (var c in cubes) |
| 71 | + c.Translate(displacement); |
| 72 | + } |
| 73 | + |
| 74 | + void ScaleCubes(double factor) |
| 75 | + { |
| 76 | + foreach (var c in cubes) |
| 77 | + c.Rescale(factor, factor, factor); |
| 78 | + } |
| 79 | + void RotateCubes(double angleX, double angleY, double angleZ) |
| 80 | + { |
| 81 | + foreach (var c in cubes) |
| 82 | + c.RotateAroundOrigin(angleX, angleY, angleZ); |
| 83 | + } |
| 84 | + |
| 85 | + protected override bool ProcessCmdKey(ref Message msg, Keys keyData) |
| 86 | + { |
| 87 | + double standardSpeed = 1; |
| 88 | + double fastSpeed = 10; |
| 89 | + double rotationStep = Math.PI / 64; |
| 90 | + |
| 91 | + switch (keyData) |
| 92 | + { |
| 93 | + case Keys.R: |
| 94 | + Refresh(); |
| 95 | + break; |
| 96 | + |
| 97 | + // Translation |
| 98 | + |
| 99 | + case Keys.Right: |
| 100 | + TranslateCubes(new Vector3D(-standardSpeed, 0, 0)); |
| 101 | + goto case Keys.R; |
| 102 | + case (Keys.Right | Keys.Control): |
| 103 | + TranslateCubes(new Vector3D(-fastSpeed, 0, 0)); |
| 104 | + goto case Keys.R; |
| 105 | + case Keys.Left: |
| 106 | + TranslateCubes(new Vector3D(standardSpeed, 0, 0)); |
| 107 | + goto case Keys.R; |
| 108 | + case (Keys.Left | Keys.Control): |
| 109 | + TranslateCubes(new Vector3D(fastSpeed, 0, 0)); |
| 110 | + goto case Keys.R; |
| 111 | + |
| 112 | + case Keys.Up: |
| 113 | + TranslateCubes(new Vector3D(0, standardSpeed, 0)); |
| 114 | + goto case Keys.R; |
| 115 | + case (Keys.Up | Keys.Control): |
| 116 | + TranslateCubes(new Vector3D(0, fastSpeed, 0)); |
| 117 | + goto case Keys.R; |
| 118 | + case Keys.Down: |
| 119 | + TranslateCubes(new Vector3D(0, -standardSpeed, 0)); |
| 120 | + goto case Keys.R; |
| 121 | + case (Keys.Down | Keys.Control): |
| 122 | + TranslateCubes(new Vector3D(0, -fastSpeed, 0)); |
| 123 | + goto case Keys.R; |
| 124 | + |
| 125 | + case Keys.Z: |
| 126 | + TranslateCubes(new Vector3D(0, 0, standardSpeed)); |
| 127 | + goto case Keys.R; |
| 128 | + case (Keys.Z | Keys.Control): |
| 129 | + TranslateCubes(new Vector3D(0, 0, fastSpeed)); |
| 130 | + goto case Keys.R; |
| 131 | + case Keys.X: |
| 132 | + TranslateCubes(new Vector3D(0, 0, -standardSpeed)); |
| 133 | + goto case Keys.R; |
| 134 | + case (Keys.X | Keys.Control): |
| 135 | + TranslateCubes(new Vector3D(0, 0, -fastSpeed)); |
| 136 | + goto case Keys.R; |
| 137 | + |
| 138 | + // Rotation |
| 139 | + |
| 140 | + case Keys.W: |
| 141 | + RotateCubes(0, standardSpeed * rotationStep, 0); |
| 142 | + goto case Keys.R; |
| 143 | + case (Keys.W | Keys.Control): |
| 144 | + RotateCubes(0, fastSpeed * rotationStep, 0); |
| 145 | + goto case Keys.R; |
| 146 | + case Keys.S: |
| 147 | + RotateCubes(0, -standardSpeed * rotationStep, 0); |
| 148 | + goto case Keys.R; |
| 149 | + case (Keys.S | Keys.Control): |
| 150 | + RotateCubes(0, -fastSpeed * rotationStep, 0); |
| 151 | + goto case Keys.R; |
| 152 | + |
| 153 | + case Keys.A: |
| 154 | + RotateCubes(0, 0, standardSpeed * rotationStep); |
| 155 | + goto case Keys.R; |
| 156 | + case (Keys.A | Keys.Control): |
| 157 | + RotateCubes(0, 0, fastSpeed * rotationStep); |
| 158 | + goto case Keys.R; |
| 159 | + case Keys.D: |
| 160 | + RotateCubes(0, 0, -standardSpeed * rotationStep); |
| 161 | + goto case Keys.R; |
| 162 | + case (Keys.D | Keys.Control): |
| 163 | + RotateCubes(0, 0, -fastSpeed * rotationStep); |
| 164 | + goto case Keys.R; |
| 165 | + |
| 166 | + case Keys.E: |
| 167 | + RotateCubes(standardSpeed * rotationStep, 0, 0); |
| 168 | + goto case Keys.R; |
| 169 | + case (Keys.E | Keys.Control): |
| 170 | + RotateCubes(fastSpeed * rotationStep, 0, 0); |
| 171 | + goto case Keys.R; |
| 172 | + case Keys.Q: |
| 173 | + RotateCubes(-standardSpeed * rotationStep, 0, 0); |
| 174 | + goto case Keys.R; |
| 175 | + case (Keys.Q | Keys.Control): |
| 176 | + RotateCubes(-fastSpeed * rotationStep, 0, 0); |
| 177 | + goto case Keys.R; |
| 178 | + |
| 179 | + // Scale |
| 180 | + case Keys.OemPeriod: |
| 181 | + ScaleCubes(1.5); |
| 182 | + goto case Keys.R; |
| 183 | + case (Keys.OemPeriod | Keys.Control): |
| 184 | + ScaleCubes(2); |
| 185 | + goto case Keys.R; |
| 186 | + case Keys.Oemcomma: |
| 187 | + ScaleCubes(0.75); |
| 188 | + goto case Keys.R; |
| 189 | + case (Keys.Oemcomma | Keys.Control): |
| 190 | + ScaleCubes(0.5); |
| 191 | + goto case Keys.R; |
| 192 | + } |
| 193 | + |
| 194 | + // Refresh(); |
| 195 | + |
| 196 | + return base.ProcessCmdKey(ref msg, keyData); |
| 197 | + } |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + } |
| 202 | +} |
0 commit comments