@@ -9,7 +9,7 @@ namespace winforms_z_buffer
9
9
public partial class Display : Form
10
10
{
11
11
List < Cube > cubes = new List < Cube > ( ) ;
12
- Cube CSO ;
12
+ List < Cube > selectedCubes = new List < Cube > ( ) ;
13
13
14
14
PictureBox pb ;
15
15
@@ -52,32 +52,27 @@ void initializePicturebox()
52
52
53
53
void initializeCamera ( )
54
54
{
55
- new Camera ( /*fov, near, far : set to default */ ) ;
55
+ new Camera ( /* fov, near, far, aspect ratio */ ) ;
56
56
}
57
57
58
58
void initializeCubes ( )
59
59
{
60
60
cubes . Clear ( ) ;
61
61
62
- // Coordinate System Origin
63
- //CSO = Cube.UnitCube(0);
64
- //CSO.Rescale(0.1, 0.1, 0.1);
65
- //cubes.Add(CSO);
66
-
67
62
//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 ) ;
71
66
c1 . Translate ( new Vector3D ( 10 , 10 , - 3 ) ) ;
72
67
cubes . Add ( c1 ) ;
73
68
74
- Cube c2 = Cube . UnitCube ( 51 ) ;
69
+ Cube c2 = Cube . UnitCube ( 2137 ) ;
75
70
c2 . Rescale ( 5 , 5 , 5 ) ;
76
71
c2 . RotateAroundLocalAxis ( 0 , Math . PI / 4 , Math . PI / 12 ) ;
77
72
c2 . Translate ( new Vector3D ( - 10 , 5 , - 2 ) ) ;
78
73
cubes . Add ( c2 ) ;
79
74
80
- Cube c3 = Cube . UnitCube ( 69 ) ;
75
+ Cube c3 = Cube . UnitCube ( 1337 ) ;
81
76
c3 . Rescale ( 2 , 5 , 15 ) ;
82
77
c3 . Translate ( new Vector3D ( 0 , - 15 , 0 ) ) ;
83
78
c3 . RotateAroundOrigin ( - Math . PI / 3 , Math . PI / 8 , - Math . PI / 2 ) ;
@@ -88,6 +83,14 @@ void initializeCubes()
88
83
c4 . Translate ( new Vector3D ( - 15 , - 15 , - 15 ) ) ;
89
84
c4 . RotateAroundOrigin ( Math . PI / 7 , 0 , Math . PI / 64 ) ;
90
85
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 ) ;
91
94
}
92
95
93
96
protected override void OnPaint ( PaintEventArgs args )
@@ -96,31 +99,46 @@ protected override void OnPaint(PaintEventArgs args)
96
99
var gr = Graphics . FromImage ( bmp ) ;
97
100
gr . Clear ( Color . Black ) ;
98
101
99
- var drawing = new Drawing ( bmp , Size , /*draw edges*/ false , /*draw faces*/true ) ;
102
+ var drawing = new Drawing ( bmp , Size , /*draw faces*/ true ) ;
100
103
101
104
foreach ( var c in cubes )
102
105
drawing . Draw ( c ) ;
103
106
104
107
pb . Image = drawing . GetResult ( ) ;
105
108
}
106
109
110
+ #region shapes control
111
+
107
112
void TranslateCubes ( Vector3D displacement )
108
113
{
109
- foreach ( var c in cubes )
114
+ foreach ( var c in selectedCubes )
110
115
c . Translate ( displacement ) ;
111
116
}
112
-
113
117
void ScaleCubes ( double factor )
114
118
{
115
- foreach ( var c in cubes )
119
+ foreach ( var c in selectedCubes )
116
120
c . Rescale ( factor , factor , factor ) ;
117
121
}
118
- void RotateCubes ( double angleX , double angleY , double angleZ )
122
+ void RotateCubesAroundOrigin ( double angleX , double angleY , double angleZ )
119
123
{
120
- foreach ( var c in cubes )
124
+ foreach ( var c in selectedCubes )
121
125
c . RotateAroundOrigin ( angleX , angleY , angleZ ) ;
122
126
}
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 ( ) ;
123
136
137
+ if ( index == - 1 )
138
+ selectedCubes . AddRange ( cubes ) ;
139
+ else if ( index > - 1 && index < cubes . Count )
140
+ selectedCubes . Add ( cubes [ index ] ) ;
141
+ }
124
142
protected override bool ProcessCmdKey ( ref Message msg , Keys keyData )
125
143
{
126
144
double standardSpeed = 2 ;
@@ -178,43 +196,82 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
178
196
goto case Keys . R ;
179
197
180
198
// Rotation
181
- case Keys . W :
182
- RotateCubes ( 0 , standardSpeed * rotationStep , 0 ) ;
199
+ case Keys . A :
200
+ RotateCubesAroundOrigin ( 0 , standardSpeed * rotationStep , 0 ) ;
183
201
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 ) ;
186
204
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
+
187
225
case Keys . S :
188
- RotateCubes ( 0 , - standardSpeed * rotationStep , 0 ) ;
226
+ RotateCubesAroundOrigin ( standardSpeed * rotationStep , 0 , 0 ) ;
189
227
goto case Keys . R ;
190
228
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 ) ;
192
236
goto case Keys . R ;
193
237
194
- case Keys . A :
195
- RotateCubes ( 0 , 0 , standardSpeed * rotationStep ) ;
238
+ case ( Keys . A | Keys . Shift ) :
239
+ RotateCubesAroundLocal ( 0 , standardSpeed * rotationStep , 0 ) ;
196
240
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 ) ;
199
243
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 ) ;
202
246
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 ) ;
205
249
goto case Keys . R ;
206
250
207
- case Keys . E :
208
- RotateCubes ( standardSpeed * rotationStep , 0 , 0 ) ;
251
+ case ( Keys . Q | Keys . Shift ) :
252
+ RotateCubesAroundLocal ( 0 , 0 , standardSpeed * rotationStep ) ;
209
253
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 ) ;
212
256
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 ) ;
215
259
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 ) ;
218
275
goto case Keys . R ;
219
276
220
277
// Scale
@@ -232,18 +289,49 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
232
289
goto case Keys . R ;
233
290
234
291
// Camera
235
- case Keys . D1 :
292
+ case Keys . O :
236
293
Camera . Instance . ChangeFOV ( Math . PI / 12 ) ;
237
294
goto case Keys . R ;
238
- case Keys . D2 :
295
+ case Keys . P :
239
296
Camera . Instance . ChangeFOV ( - Math . PI / 12 ) ;
240
297
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 ;
241
330
}
242
331
243
332
return base . ProcessCmdKey ( ref msg , keyData ) ;
244
333
}
245
334
246
-
247
-
335
+ #endregion
248
336
}
249
337
}
0 commit comments