Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit e31a839

Browse files
committed
Fixed to use model to world matrices
1 parent a7eb0dc commit e31a839

File tree

9 files changed

+317
-276
lines changed

9 files changed

+317
-276
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Winforms Z Buffer Project

winforms-z-buffer/Display.cs

Lines changed: 132 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace winforms_z_buffer
99
public partial class Display : Form
1010
{
1111
List<Cube> cubes = new List<Cube>();
12-
Cube CSO;
12+
List<Cube> selectedCubes = new List<Cube>();
1313

1414
PictureBox pb;
1515

@@ -52,32 +52,27 @@ void initializePicturebox()
5252

5353
void initializeCamera()
5454
{
55-
new Camera(/*fov, near, far : set to default*/);
55+
new Camera(/* fov, near, far, aspect ratio */);
5656
}
5757

5858
void initializeCubes()
5959
{
6060
cubes.Clear();
6161

62-
// Coordinate System Origin
63-
//CSO = Cube.UnitCube(0);
64-
//CSO.Rescale(0.1, 0.1, 0.1);
65-
//cubes.Add(CSO);
66-
6762
//Cubes
68-
Cube c1 = Cube.UnitCube(24);
69-
c1.Rescale(2, 2, 2);
70-
c1.RotateAroundOrigin(0, -Math.PI / 3, -Math.PI / 9);
63+
Cube c1 = Cube.UnitCube(42);
64+
c1.Rescale(5, 2, 2);
65+
c1.RotateAroundLocalAxis(0, -Math.PI / 3, -Math.PI / 9);
7166
c1.Translate(new Vector3D(10, 10, -3));
7267
cubes.Add(c1);
7368

74-
Cube c2 = Cube.UnitCube(51);
69+
Cube c2 = Cube.UnitCube(2137);
7570
c2.Rescale(5, 5, 5);
7671
c2.RotateAroundLocalAxis(0, Math.PI / 4, Math.PI / 12);
7772
c2.Translate(new Vector3D(-10, 5, -2));
7873
cubes.Add(c2);
7974

80-
Cube c3 = Cube.UnitCube(69);
75+
Cube c3 = Cube.UnitCube(1337);
8176
c3.Rescale(2, 5, 15);
8277
c3.Translate(new Vector3D(0, -15, 0));
8378
c3.RotateAroundOrigin(-Math.PI / 3, Math.PI / 8, -Math.PI / 2);
@@ -88,6 +83,14 @@ void initializeCubes()
8883
c4.Translate(new Vector3D(-15, -15, -15));
8984
c4.RotateAroundOrigin(Math.PI / 7, 0, Math.PI / 64);
9085
cubes.Add(c4);
86+
87+
Cube c5 = Cube.UnitCube(0);
88+
c5.Rescale(0.5, 0.5, 20);
89+
c5.Translate(new Vector3D(0, 15, -15));
90+
c5.RotateAroundLocalAxis(Math.PI / 7, 0, Math.PI / 64);
91+
cubes.Add(c5);
92+
93+
selectedCubes.AddRange(cubes);
9194
}
9295

9396
protected override void OnPaint(PaintEventArgs args)
@@ -96,31 +99,46 @@ protected override void OnPaint(PaintEventArgs args)
9699
var gr = Graphics.FromImage(bmp);
97100
gr.Clear(Color.Black);
98101

99-
var drawing = new Drawing(bmp, Size, /*draw edges*/false, /*draw faces*/true);
102+
var drawing = new Drawing(bmp, Size, /*draw faces*/true);
100103

101104
foreach (var c in cubes)
102105
drawing.Draw(c);
103106

104107
pb.Image = drawing.GetResult();
105108
}
106109

110+
#region shapes control
111+
107112
void TranslateCubes(Vector3D displacement)
108113
{
109-
foreach (var c in cubes)
114+
foreach (var c in selectedCubes)
110115
c.Translate(displacement);
111116
}
112-
113117
void ScaleCubes(double factor)
114118
{
115-
foreach (var c in cubes)
119+
foreach (var c in selectedCubes)
116120
c.Rescale(factor, factor, factor);
117121
}
118-
void RotateCubes(double angleX, double angleY, double angleZ)
122+
void RotateCubesAroundOrigin(double angleX, double angleY, double angleZ)
119123
{
120-
foreach (var c in cubes)
124+
foreach (var c in selectedCubes)
121125
c.RotateAroundOrigin(angleX, angleY, angleZ);
122126
}
127+
void RotateCubesAroundLocal(double angleX, double angleY, double angleZ)
128+
{
129+
foreach (var c in selectedCubes)
130+
c.RotateAroundLocalAxis(angleX, angleY, angleZ);
131+
}
132+
133+
void ChangeSelectedCubes(int index)
134+
{
135+
selectedCubes.Clear();
123136

137+
if (index == -1)
138+
selectedCubes.AddRange(cubes);
139+
else if (index > -1 && index < cubes.Count)
140+
selectedCubes.Add(cubes[index]);
141+
}
124142
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
125143
{
126144
double standardSpeed = 2;
@@ -178,43 +196,82 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
178196
goto case Keys.R;
179197

180198
// Rotation
181-
case Keys.W:
182-
RotateCubes(0, standardSpeed * rotationStep, 0);
199+
case Keys.A:
200+
RotateCubesAroundOrigin(0, standardSpeed * rotationStep, 0);
183201
goto case Keys.R;
184-
case (Keys.W | Keys.Control):
185-
RotateCubes(0, fastSpeed * rotationStep, 0);
202+
case (Keys.A | Keys.Control):
203+
RotateCubesAroundOrigin(0, fastSpeed * rotationStep, 0);
186204
goto case Keys.R;
205+
case Keys.D:
206+
RotateCubesAroundOrigin(0, -standardSpeed * rotationStep, 0);
207+
goto case Keys.R;
208+
case (Keys.D | Keys.Control):
209+
RotateCubesAroundOrigin(0, -fastSpeed * rotationStep, 0);
210+
goto case Keys.R;
211+
212+
case Keys.Q:
213+
RotateCubesAroundOrigin(0, 0, standardSpeed * rotationStep);
214+
goto case Keys.R;
215+
case (Keys.Q | Keys.Control):
216+
RotateCubesAroundOrigin(0, 0, fastSpeed * rotationStep);
217+
goto case Keys.R;
218+
case Keys.E:
219+
RotateCubesAroundOrigin(0, 0, -standardSpeed * rotationStep);
220+
goto case Keys.R;
221+
case (Keys.E | Keys.Control):
222+
RotateCubesAroundOrigin(0, 0, -fastSpeed * rotationStep);
223+
goto case Keys.R;
224+
187225
case Keys.S:
188-
RotateCubes(0, -standardSpeed * rotationStep, 0);
226+
RotateCubesAroundOrigin(standardSpeed * rotationStep, 0, 0);
189227
goto case Keys.R;
190228
case (Keys.S | Keys.Control):
191-
RotateCubes(0, -fastSpeed * rotationStep, 0);
229+
RotateCubesAroundOrigin(fastSpeed * rotationStep, 0, 0);
230+
goto case Keys.R;
231+
case Keys.W:
232+
RotateCubesAroundOrigin(-standardSpeed * rotationStep, 0, 0);
233+
goto case Keys.R;
234+
case (Keys.W | Keys.Control):
235+
RotateCubesAroundOrigin(-fastSpeed * rotationStep, 0, 0);
192236
goto case Keys.R;
193237

194-
case Keys.A:
195-
RotateCubes(0, 0, standardSpeed * rotationStep);
238+
case (Keys.A | Keys.Shift):
239+
RotateCubesAroundLocal(0, standardSpeed * rotationStep, 0);
196240
goto case Keys.R;
197-
case (Keys.A | Keys.Control):
198-
RotateCubes(0, 0, fastSpeed * rotationStep);
241+
case (Keys.A | Keys.Control | Keys.Shift):
242+
RotateCubesAroundLocal(0, fastSpeed * rotationStep, 0);
199243
goto case Keys.R;
200-
case Keys.D:
201-
RotateCubes(0, 0, -standardSpeed * rotationStep);
244+
case (Keys.D | Keys.Shift):
245+
RotateCubesAroundLocal(0, -standardSpeed * rotationStep, 0);
202246
goto case Keys.R;
203-
case (Keys.D | Keys.Control):
204-
RotateCubes(0, 0, -fastSpeed * rotationStep);
247+
case (Keys.D | Keys.Control | Keys.Shift):
248+
RotateCubesAroundLocal(0, -fastSpeed * rotationStep, 0);
205249
goto case Keys.R;
206250

207-
case Keys.E:
208-
RotateCubes(standardSpeed * rotationStep, 0, 0);
251+
case (Keys.Q | Keys.Shift):
252+
RotateCubesAroundLocal(0, 0, standardSpeed * rotationStep);
209253
goto case Keys.R;
210-
case (Keys.E | Keys.Control):
211-
RotateCubes(fastSpeed * rotationStep, 0, 0);
254+
case (Keys.Q | Keys.Control | Keys.Shift):
255+
RotateCubesAroundLocal(0, 0, fastSpeed * rotationStep);
212256
goto case Keys.R;
213-
case Keys.Q:
214-
RotateCubes(-standardSpeed * rotationStep, 0, 0);
257+
case (Keys.E | Keys.Shift):
258+
RotateCubesAroundLocal(0, 0, -standardSpeed * rotationStep);
215259
goto case Keys.R;
216-
case (Keys.Q | Keys.Control):
217-
RotateCubes(-fastSpeed * rotationStep, 0, 0);
260+
case (Keys.E | Keys.Control | Keys.Shift):
261+
RotateCubesAroundLocal(0, 0, -fastSpeed * rotationStep);
262+
goto case Keys.R;
263+
264+
case (Keys.S | Keys.Shift):
265+
RotateCubesAroundLocal(standardSpeed * rotationStep, 0, 0);
266+
goto case Keys.R;
267+
case (Keys.S | Keys.Control | Keys.Shift):
268+
RotateCubesAroundLocal(fastSpeed * rotationStep, 0, 0);
269+
goto case Keys.R;
270+
case (Keys.W | Keys.Shift):
271+
RotateCubesAroundLocal(-standardSpeed * rotationStep, 0, 0);
272+
goto case Keys.R;
273+
case (Keys.W | Keys.Control | Keys.Shift):
274+
RotateCubesAroundLocal(-fastSpeed * rotationStep, 0, 0);
218275
goto case Keys.R;
219276

220277
// Scale
@@ -232,18 +289,49 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
232289
goto case Keys.R;
233290

234291
// Camera
235-
case Keys.D1:
292+
case Keys.O:
236293
Camera.Instance.ChangeFOV(Math.PI / 12);
237294
goto case Keys.R;
238-
case Keys.D2:
295+
case Keys.P:
239296
Camera.Instance.ChangeFOV(-Math.PI / 12);
240297
goto case Keys.R;
298+
299+
// Cube selection
300+
case Keys.D1:
301+
ChangeSelectedCubes(0);
302+
goto case Keys.R;
303+
case Keys.D2:
304+
ChangeSelectedCubes(1);
305+
goto case Keys.R;
306+
case Keys.D3:
307+
ChangeSelectedCubes(2);
308+
goto case Keys.R;
309+
case Keys.D4:
310+
ChangeSelectedCubes(3);
311+
goto case Keys.R;
312+
case Keys.D5:
313+
ChangeSelectedCubes(4);
314+
goto case Keys.R;
315+
case Keys.D6:
316+
ChangeSelectedCubes(5);
317+
goto case Keys.R;
318+
case Keys.D7:
319+
ChangeSelectedCubes(6);
320+
goto case Keys.R;
321+
case Keys.D8:
322+
ChangeSelectedCubes(7);
323+
goto case Keys.R;
324+
case Keys.D9:
325+
ChangeSelectedCubes(8);
326+
goto case Keys.R;
327+
case Keys.D0:
328+
ChangeSelectedCubes(-1);
329+
goto case Keys.R;
241330
}
242331

243332
return base.ProcessCmdKey(ref msg, keyData);
244333
}
245334

246-
247-
335+
#endregion
248336
}
249337
}

winforms-z-buffer/Drawing/Camera.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class Camera
1313

1414
double fov = Math.PI / 2;
1515
double near = 0.001;
16-
double far = 1000000;
16+
double far = 10000;
17+
double aspect = 1;
1718

1819
public Camera(double fov, double near, double far) : this()
1920
{
@@ -27,10 +28,10 @@ public Camera(double fov, double near, double far) : this()
2728
public Matrix3D GetProjectionMatrix()
2829
{
2930
double fn = far - near;
30-
double fovRad = 1 / Math.Tan(fov * 0.5);
31+
double fovRad = 1 / Math.Tan(fov / 2);
3132

3233
Matrix3D matrix = new Matrix3D(
33-
fovRad, 0, 0, 0,
34+
aspect * fovRad, 0, 0, 0,
3435
0, fovRad, 0, 0,
3536
0, 0, far / fn, -far * near / fn,
3637
0, 0, 1, 0

winforms-z-buffer/Drawing/Drawing.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,15 @@ namespace winforms_z_buffer
1313
public class Drawing
1414
{
1515
ZBuffer zBuffer;
16-
Graphics graphics;
1716
Bitmap bmp;
1817

1918
Size w;
20-
21-
bool drawBorders;
2219
bool drawFill;
2320

24-
public Drawing(Graphics g, Size windowSize, bool drawBorders, bool drawFill)
25-
{
26-
graphics = g;
27-
zBuffer = new ZBuffer(windowSize);
28-
this.drawBorders = drawBorders;
29-
this.drawFill = drawFill;
30-
w = windowSize;
31-
}
32-
public Drawing(Bitmap bmp, Size windowSize, bool drawBorders, bool drawFill)
21+
public Drawing(Bitmap bmp, Size windowSize, bool drawFill)
3322
{
3423
this.bmp = bmp;
3524
zBuffer = new ZBuffer(windowSize);
36-
this.drawBorders = drawBorders;
3725
this.drawFill = drawFill;
3826
w = windowSize;
3927
}
@@ -49,25 +37,12 @@ public void Draw(Cube c)
4937

5038
public void DrawTriangle(Triangle t)
5139
{
52-
if (!drawFill && !drawBorders)
40+
if (!drawFill)
5341
return;
5442

5543
var points = t.TriangleVertexScreenPointsAsPoint3D();
5644

57-
if (drawFill)
58-
drawTriangleFill(points, t.FaceColor);
59-
60-
if (drawBorders)
61-
drawTriangleEdges(points, t.EdgeColor);
62-
}
63-
64-
void drawTriangleEdges(Point3D[] vertices, Color color)
65-
{
66-
var points = new List<Point3D> { vertices[0], vertices[1], vertices[2] };
67-
68-
69-
foreach (var p in Line.OutlineTriangle(points))
70-
drawPoint(p, color);
45+
drawTriangleFill(points, t.FaceColor);
7146
}
7247

7348
void drawTriangleFill(Point3D[] vertices, Color color)

0 commit comments

Comments
 (0)