-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferGenerator.cs
503 lines (462 loc) · 22.5 KB
/
BufferGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;
using SharpDX.Toolkit;
using SharpDX.Toolkit.Graphics;
using SharpDX.Toolkit.Input;
using System.Diagnostics;
namespace Project1
{
class BufferGenerator
{
private float pix;
private float baseline;
private float landscapeWidth;
private float landscapeHeight;
private float lowest = 100;
private float highest = -100;
private float average;
private int iteration;
private VertexPositionColor[] colorBuffer;
private Vector3[] buffer;
private ColorSetting colorSetting;
private int level;
private double distance;
private Vector3 dpos1;
private VertexPositionColor cannonPosColor_5; // this is the centre postion of the cannon
private VertexPositionColor targetPosColor_5; // this is the centre postion of the target
public void setLevel(int level, float landscapeWidth, float landscapeHeight, Vector3 dpos1)
{
this.level = level;
this.landscapeHeight = landscapeHeight;
this.landscapeWidth = landscapeWidth;
this.distance = this.landscapeHeight /pix * 0.9;
this.dpos1 = dpos1;
}
public BufferGenerator()
{
this.colorBuffer = new VertexPositionColor[] { };
this.buffer = new Vector3[]{};
this.colorSetting = new ColorSetting();
}
//return the position of cannon
public VertexPositionColor getCannonPos()
{
return cannonPosColor_5;
}
public VertexPositionColor getTargetPos()
{
return targetPosColor_5;
}
//set the position of cannon and target on landscape
public VertexPositionColor[] setCannonPos()
{
cannonPosColor_5 = colorBuffer[0];
VertexPositionColor cannonPosColor_4 = colorBuffer[0];
VertexPositionColor cannonPosColor_3 = colorBuffer[0];
VertexPositionColor cannonPosColor_2 = colorBuffer[0];
VertexPositionColor cannonPosColor_1 = colorBuffer[0];
targetPosColor_5 = colorBuffer[0];
VertexPositionColor targetPosColor_4 = colorBuffer[0];
VertexPositionColor targetPosColor_3 = colorBuffer[0];
VertexPositionColor targetPosColor_2 = colorBuffer[0];
VertexPositionColor targetPosColor_1 = colorBuffer[0];
Random rnd = new Random();
//TODO change 2 and 12 according to the landwidth later
Debug.WriteLine("pix = " + pix);
Debug.WriteLine("landwidth = " + landscapeWidth);
float pixNumber = landscapeWidth / pix - 8;
Debug.WriteLine(pixNumber);
int opt = rnd.Next(10, (int)pixNumber);
double targetX = dpos1.X + opt * pix;
double targetZ = Math.Sqrt((distance * distance - opt * opt));
targetZ = Math.Round(targetZ);
targetZ = dpos1.Z - targetZ * pix;
for (int i = 3; i <= colorBuffer.Length - 1 -3 ; i++)
{
VertexPositionColor posColor5 = colorBuffer[i-1];
VertexPositionColor posColor4 = colorBuffer[i+3];
VertexPositionColor posColor3 = colorBuffer[i-3];
VertexPositionColor posColor2 = colorBuffer[i-2];
VertexPositionColor posColor1 = colorBuffer[i];
// for target position
if (posColor1.Position.X == targetX && posColor1.Position.Z == targetZ
&& posColor2.Position.X == targetX + pix && posColor2.Position.Z == targetZ
&& posColor3.Position.X == targetX + pix && posColor3.Position.Z == targetZ - pix
&& posColor4.Position.X == targetX && posColor4.Position.Z == targetZ - pix)
{
float averageHight = (posColor1.Position.Y + posColor2.Position.Y + posColor3.Position.Y + posColor4.Position.Y + posColor5.Position.Y) / 5;
posColor1.Position.Y = averageHight;
posColor2.Position.Y = averageHight;
posColor3.Position.Y = averageHight;
posColor4.Position.Y = averageHight;
posColor5.Position.Y = averageHight;
posColor1.Color = new Color(1, 0, 0);
posColor2.Color = new Color(1, 0, 0);
posColor3.Color = new Color(1, 0, 0);
posColor4.Color = new Color(1, 0, 0);
posColor5.Color = new Color(1, 0, 0);
targetPosColor_5 = posColor5;
targetPosColor_4 = posColor4;
targetPosColor_3 = posColor3;
targetPosColor_2 = posColor2;
targetPosColor_1 = posColor1;
}
// for cannon position
if (posColor1.Position.X == dpos1.X + pix * 7f && posColor1.Position.Z == dpos1.Z - pix * 7f
&& posColor2.Position.X == dpos1.X + pix * 8f && posColor2.Position.Z == dpos1.Z - pix * 7f
&& posColor3.Position.X == dpos1.X + pix * 8f && posColor3.Position.Z == dpos1.Z - pix * 8f
&& posColor4.Position.X == dpos1.X + pix * 7f && posColor4.Position.Z == dpos1.Z - pix * 8f)
{
Debug.WriteLine("posColor1 = "+posColor1);
Debug.WriteLine("posColor2 = " + posColor2);
Debug.WriteLine("posColor3 = " + posColor3);
Debug.WriteLine("posColor4 = " + posColor4);
float averageHight = (posColor1.Position.Y + posColor2.Position.Y + posColor3.Position.Y + posColor4.Position.Y + posColor5.Position.Y)/5;
posColor1.Position.Y = averageHight;
posColor2.Position.Y = averageHight;
posColor3.Position.Y = averageHight;
posColor4.Position.Y = averageHight;
posColor5.Position.Y = averageHight;
posColor1.Color = new Color(1, 0, 0);
posColor2.Color = new Color(1, 0, 0);
posColor3.Color = new Color(1, 0, 0);
posColor4.Color = new Color(1, 0, 0);
posColor5.Color = new Color(1, 0, 0);
cannonPosColor_5 = posColor5;
cannonPosColor_4 = posColor4;
cannonPosColor_3 = posColor3;
cannonPosColor_2 = posColor2;
cannonPosColor_1 = posColor1;;
}
}
for (int i = 3; i <= colorBuffer.Length - 1; i++)
{
VertexPositionColor posColor = colorBuffer[i];
if (posColor.Position.X == cannonPosColor_1.Position.X &&
posColor.Position.Z == cannonPosColor_1.Position.Z)
{
posColor = cannonPosColor_1;
}
if (posColor.Position.X == cannonPosColor_2.Position.X &&
posColor.Position.Z == cannonPosColor_2.Position.Z)
{
posColor = cannonPosColor_2;
}
if (posColor.Position.X == cannonPosColor_3.Position.X &&
posColor.Position.Z == cannonPosColor_3.Position.Z)
{
posColor = cannonPosColor_3;
}
if (posColor.Position.X == cannonPosColor_4.Position.X &&
posColor.Position.Z == cannonPosColor_4.Position.Z)
{
posColor = cannonPosColor_4;
}
if (posColor.Position.X == cannonPosColor_5.Position.X &&
posColor.Position.Z == cannonPosColor_5.Position.Z)
{
posColor = cannonPosColor_5;
}
if (posColor.Position.X == targetPosColor_1.Position.X &&
posColor.Position.Z == targetPosColor_1.Position.Z)
{
posColor = targetPosColor_1;
}
if (posColor.Position.X == targetPosColor_2.Position.X &&
posColor.Position.Z == targetPosColor_2.Position.Z)
{
posColor = targetPosColor_2;
}
if (posColor.Position.X == targetPosColor_3.Position.X &&
posColor.Position.Z == targetPosColor_3.Position.Z)
{
posColor = targetPosColor_3;
}
if (posColor.Position.X == targetPosColor_4.Position.X &&
posColor.Position.Z == targetPosColor_4.Position.Z)
{
posColor = targetPosColor_4;
}
if (posColor.Position.X == targetPosColor_5.Position.X &&
posColor.Position.Z == targetPosColor_5.Position.Z)
{
posColor = targetPosColor_5;
}
colorBuffer[i] = posColor;
}
return colorBuffer;
}
public void setColorRanges()
{
this.colorSetting.setAverage(this.average/iteration);
this.colorSetting.setHighest(this.highest);
this.colorSetting.setLowest(this.lowest);
this.colorSetting.setRoughness(this.level);
}
public void SetLandscapeWidth(float landscapeWidth)
{
this.landscapeWidth = landscapeWidth;
}
public void SetLandscapeHeight(float landscapeHeight)
{
this.landscapeHeight = landscapeHeight;
}
public void SetBaseLine(float baseline)
{
this.baseline = baseline;
colorSetting.SetBaseLine(baseline);
}
public void SetPix(float pix)
{
this.pix = pix;
}
//This is the recursive function that implements the random midpoint
//displacement algorithm. It will call itself until the grid pieces
//become smaller than one pixel.
public Vector3[] DivideGrid(float x, float z, float width, float height, float c1, float c2, float c3, float c4)
{
Random rd = new Random();
float wr = (float)rd.NextDouble(0, 1.0);
float hr = (float)rd.NextDouble(0, 1.0);
float Edge1, Edge2, Edge3, Edge4, Middle;
float newWidth = width / 2;
float newHeight = height / 2;
if (width > pix)
{
Middle = (c1 + c2 + c3 + c4) / 4 + Displace(newWidth + newHeight); //Randomly displace the midpoint
Edge1 = (c1 + c2) / 2; //Calculate the edges by averaging the two corners of each edge.
Edge2 = (c2 + c3) / 2;
Edge3 = (c3 + c4) / 2;
Edge4 = (c4 + c1) / 2;
//Make sure that the midpoint doesn't accidentally "randomly displaced" past the boundaries!
if (Middle < 0)
{
Middle = 0;
}
else if (Middle > 1.0f)
{
Middle = 1.0f;
}
DivideGrid(x, z, newWidth, newHeight, c1, Edge1, Middle, Edge4);
DivideGrid(x + newWidth, z, newWidth, newHeight, Edge1, c2, Edge2, Middle);
DivideGrid(x + newWidth, z - newHeight, newWidth, newHeight, Middle, Edge2, c3, Edge3);
DivideGrid(x, z - newHeight, newWidth, newHeight, Edge4, Middle, Edge3, c4);
}
else
{
float c = (c1 + c2 + c3 + c4) / 4;
Vector3 pos1 = new Vector3(x, c1, z);//look from the front, front left up conor
Vector3 pos2 = new Vector3(x + width, c2, z);//look from the front, front right up conor
Vector3 pos3 = new Vector3(x + width, c3, z - height);//look from the front, front right down conor
Vector3 pos4 = new Vector3(x, c4, z - height);//look from the front, front left down conor
Vector3 posMid = new Vector3(x + width / 2, c, z - height / 2);//look from the front, front left down conor
//below is for the vextors without color settings
List<Vector3> bufferList = this.buffer.ToList();
bufferList.Add(pos4);
bufferList.Add(posMid);
bufferList.Add(pos3);
bufferList.Add(pos2);
bufferList.Add(posMid);
bufferList.Add(pos1);
bufferList.Add(pos1);
bufferList.Add(posMid);
bufferList.Add(pos4);
bufferList.Add(pos3);
bufferList.Add(posMid);
bufferList.Add(pos2);
this.buffer = bufferList.ToArray();
//to records the tallest height, the lowest height and average height
check(pos1, pos2, pos3, pos4, posMid);
};
return this.buffer;
}
public VertexPositionColor[] setColor()
{
List<VertexPositionColor> colorBufferList = this.colorBuffer.ToList();
for (int i = 0; i <= buffer.Length - 1; i++)
{
Vector3 pos = buffer[i];
colorBufferList.Add(new VertexPositionColor(pos, colorSetting.GetColor(pos)));
}
this.colorBuffer = colorBufferList.ToArray();
return colorBuffer;
}
//add the base of lanscape
public VertexPositionColor[] AddBase(Vector3 dpos1, Vector3 dpos2, Vector3 dpos3, Vector3 dpos4)
{
List<VertexPositionColor> colorBufferList = this.colorBuffer.ToList();
Vector3 top1 = dpos1;
Vector3 top2 = dpos2;
Vector3 top3 = dpos3;
Vector3 top4 = dpos4;
top1.Y = top2.Y = top3.Y = top4.Y = 1.5f;
Vector3 down1 = dpos1;
Vector3 down2 = dpos2;
Vector3 down3 = dpos3;
Vector3 down4 = dpos4;
down1.Y = down2.Y = down3.Y = down4.Y = 0f;
// for the front side
colorBufferList.Add(new VertexPositionColor(dpos4, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos4, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos3, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(0, 100, 0)));
// for the back side
colorBufferList.Add(new VertexPositionColor(dpos2, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos2, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(0, 100, 0)));
// for the left side
colorBufferList.Add(new VertexPositionColor(dpos1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos4, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(0, 100, 0)));
// for the right side
colorBufferList.Add(new VertexPositionColor(dpos3, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos3, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(dpos2, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(0, 100, 0)));
// for the down side
colorBufferList.Add(new VertexPositionColor(down1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(0, 100, 0)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(0, 100, 0)));
this.colorBuffer = colorBufferList.ToArray();
return colorBuffer;
}
//add the box which contain the lanscape
public VertexPositionColor[] AddBox(Vector3 dpos1, Vector3 dpos2, Vector3 dpos3, Vector3 dpos4)
{
List<VertexPositionColor> colorBufferList = this.colorBuffer.ToList();
Vector3 top1 = dpos1;
Vector3 top2 = dpos2;
Vector3 top3 = dpos3;
Vector3 top4 = dpos4;
top1.Y = top2.Y = top3.Y = top4.Y = 1.5f;
Vector3 down1 = dpos1;
Vector3 down2 = dpos2;
Vector3 down3 = dpos3;
Vector3 down4 = dpos4;
down1.Y = down2.Y = down3.Y = down4.Y = 0f;
//top side
//VertexPositionTexture(top1, new Vector2(1.0f, 1.0f))
// for the top side
colorBufferList.Add(new VertexPositionColor(top1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top4, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top3, new Color(1, 2, 3)));
// for the down side
colorBufferList.Add(new VertexPositionColor(down1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(1, 2, 3)));
// for the front side
colorBufferList.Add(new VertexPositionColor(top4, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top4, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(1, 2, 3)));
// for the front side
colorBufferList.Add(new VertexPositionColor(top1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(1, 2, 3)));
// for the left side
colorBufferList.Add(new VertexPositionColor(top4, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top4, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down1, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down4, new Color(1, 2, 3)));
// for the front side
colorBufferList.Add(new VertexPositionColor(top3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top2, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(top3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down3, new Color(1, 2, 3)));
colorBufferList.Add(new VertexPositionColor(down2, new Color(1, 2, 3)));
this.colorBuffer = colorBufferList.ToArray();
return colorBuffer;
}
//Randomly displaces color value for midpoint depending on size
//of grid piece.
private float Displace(float num)
{
Random rd = new Random();
float max = num / (float)(landscapeWidth + landscapeHeight) * 2.0f *level;
float h = ((float)rd.NextDouble(0, 1) - 0.5f) * max;
if (num == landscapeWidth)
{
baseline += h;
colorSetting.SetBaseLine(baseline);
}
return h;//
}
private void check(Vector3 pos1, Vector3 pos2, Vector3 pos3, Vector3 pos4, Vector3 posMid)
{
//for heighest
if (pos1.Y > highest)
{
highest = pos1.Y;
}
if (pos2.Y > highest)
{
highest = pos2.Y;
}
if (pos3.Y > highest)
{
highest = pos3.Y;
}
if (pos4.Y > highest)
{
highest = pos4.Y;
}
if (posMid.Y > highest)
{
highest = posMid.Y;
}
//for lowest
if (pos1.Y < lowest)
{
lowest = pos1.Y;
}
if (pos2.Y < lowest)
{
lowest = pos2.Y;
}
if (pos3.Y < lowest)
{
lowest = pos3.Y;
}
if (pos4.Y < lowest)
{
lowest = pos4.Y;
}
if (posMid.Y < lowest)
{
lowest = posMid.Y;
}
iteration++;
average += (pos1.Y + pos2.Y + pos3.Y + pos4.Y + posMid.Y) /5;
}
}
}