-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegHandler.cs
More file actions
532 lines (460 loc) · 17.4 KB
/
SegHandler.cs
File metadata and controls
532 lines (460 loc) · 17.4 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
using System;
using System.Linq;
using Microsoft.Xna.Framework;
namespace Doom;
public class SegHandler
{
private readonly DoomEngine _engine;
private readonly Player _player;
private readonly ViewRenderer _renderer;
private const float MaxScale = 64.0f;
private const float MinScale = 0.00390625f;
private readonly float[] XToAngle;
public SegHandler(DoomEngine engine)
{
_engine = engine;
_player = _engine.Player;
_renderer = _engine.ViewRenderer;
XToAngle = CreateXToAngleLookup();
}
public Seg CurrentSeg { get; private set; }
public float CurrentRwAngle1 { get; private set; }
public int[] ScreenRange { get; private set; }
public int[] UpperClip { get; private set; }
public int[] LowerClip { get; private set; }
private float[] CreateXToAngleLookup()
{
var table = new float[Settings.Width + 1];
for (var i = 0; i < table.Length; i++)
{
var angle = MathHelper.ToDegrees(MathF.Atan((Settings.HalfWidth - i) / Settings.ScreenDistance));
table[i] = angle;
}
return table;
}
public void Update(GameTime gameTime)
{
InitializeFloorAndCeilingClipHeights();
InitializeScreenRange();
}
public void HandleSeg(Seg seg, int x1, int x2, float rwAngle1, int colorSeed)
{
CurrentSeg = seg;
CurrentRwAngle1 = rwAngle1;
// If the x-coordinates are the same, then the seg has
// 0 horizontal width and should not be drawn.
if (x1 == x2)
{
return;
}
var backSector = seg.BackSector;
var frontSector = seg.FrontSector;
// If the seg is one-sided (front) then we know that
// it is a solid wall.
if (backSector == null)
{
ClipSolidWalls(x1, x2, colorSeed);
return;
}
// Logic indicates a window
if (frontSector.CeilingHeight != backSector.CeilingHeight ||
frontSector.FloorHeight != backSector.FloorHeight)
{
ClipPortalWalls(x1, x2, colorSeed);
return;
}
// Reject empty lines used for triggers and
// other special effects. Identical floor
// and ceiling textures, light levels, and
// no middle texture.
if (backSector.CeilingTexture == frontSector.CeilingTexture
&& backSector.FloorTexture == frontSector.FloorTexture
&& backSector.LightLevel == frontSector.LightLevel
&& CurrentSeg.Linedef.FrontSidedef.MiddleTexture == null)
{
return;
}
// Different light levels and textures
ClipPortalWalls(x1, x2, colorSeed);
}
private void ClipSolidWalls(int xStart, int xEnd, int colorSeed = 0)
{
// If the horizontal range of the screen has
// been exhausted, then we can return. In fact,
// we should ALSO tell the BSP to stop traversing.
if (ScreenRange.Length == 0)
{
_engine.BSP.CanTraverse = false;
return;
}
// Something will have gone wrong if we have
// this condition as true.
if (xStart >= xEnd)
{
return;
}
var wall = new int[xEnd - xStart];
for (var i = 0; i < wall.Length; i++)
{
wall[i] = xStart + i;
}
var intersection = ScreenRange.Intersect(wall).ToArray();
// If the intersection is empty, then we can just ignore.
if (intersection.Length == 0)
{
return;
}
// If the intersection is the same length as the wall, then
// we can just draw the entire wall.
if (intersection.Length == wall.Length)
{
DrawSolidWallRange(xStart, xEnd -1, colorSeed);
}
else
{
var sortedIntersection = intersection.OrderBy(i => i).ToArray();
var x = sortedIntersection.First();
var x2 = sortedIntersection.Last();
for (var i = 0; i < sortedIntersection.Length - 1; i++)
{
var x1 = sortedIntersection[i];
x2 = sortedIntersection[i + 1];
if (x2 - x1 > 1)
{
DrawSolidWallRange(x, x1, colorSeed);
x = x2;;
}
}
DrawSolidWallRange(x, x2, colorSeed);
}
// Remove the intersection from the available screen range.
ScreenRange = ScreenRange.Except(intersection).ToArray();
}
private void ClipPortalWalls(int xStart, int xEnd, int colorSeed = 0)
{
// Something will have gone wrong if we have
// this condition as true.
if (xStart >= xEnd)
{
return;
}
var wall = new int[xEnd - xStart];
for (var i = 0; i < wall.Length; i++)
{
wall[i] = xStart + i;
}
var intersection = ScreenRange.Intersect(wall).ToArray();
// If the intersection is empty, then we can just ignore.
if (intersection.Length == 0)
{
return;
}
// If the intersection is the same length as the wall, then
// we can just draw the entire wall.
if (intersection.Length == wall.Length)
{
DrawPortalWallRange(xStart, xEnd - 1, colorSeed);
}
else
{
var sortedIntersection = intersection.OrderBy(i => i).ToArray();
var x = sortedIntersection.First();
for (var i = 0; i < sortedIntersection.Length - 1; i++)
{
var x1 = sortedIntersection[i];
var x2 = sortedIntersection[i + 1];
if (x2 - x1 > 1)
{
DrawPortalWallRange(x, x1, colorSeed);
x = x2;;
}
}
DrawPortalWallRange(x, sortedIntersection.Last(), colorSeed);
}
}
private void DrawSolidWallRange(int x1, int x2, int colorSeed = 0)
{
var wallTexture = CurrentSeg.Linedef.FrontSidedef.MiddleTexture;
var ceilingTexture = CurrentSeg.FrontSector.CeilingTexture;
var floorTexture = CurrentSeg.FrontSector.FloorTexture;
var lightLevel = CurrentSeg.FrontSector.LightLevel;
var worldFrontZ1 = CurrentSeg.FrontSector.CeilingHeight - _player.Height;
var worldFrontZ2 = CurrentSeg.FrontSector.FloorHeight - _player.Height;
var drawWall = wallTexture != "-";
var drawCeiling = worldFrontZ1 > 0;
var drawFloor = worldFrontZ2 < 0;
var rwNormalAngle = CurrentSeg.Angle + 90;
var offsetAngle = rwNormalAngle - CurrentRwAngle1;
var hypotenuse = Vector2.Distance(_player.Position, CurrentSeg.StartVertex);
var rwDistance = hypotenuse * (float)MathF.Cos(MathHelper.ToRadians(offsetAngle));
var rwScale1 = ScaleFromGlobalAngle(x1, rwNormalAngle, rwDistance);
var scale2 = ScaleFromGlobalAngle(x2, rwNormalAngle, rwDistance);
var rwScaleStep = (scale2 - rwScale1) / (x2 - x1);
var wallY1 = Settings.HalfHeight - worldFrontZ1 * rwScale1;
var wallY1Step = -rwScaleStep * worldFrontZ1;
var wallY2 = Settings.HalfHeight - worldFrontZ2 * rwScale1;
var wallY2Step = -rwScaleStep * worldFrontZ2;
for (var i = x1; i < x2 + 1; i++)
{
var drawWallY1 = wallY1 - 1;
var drawWallY2 = wallY2;
if (drawCeiling)
{
var cy1 = UpperClip[i] + 1;
var cy2 = Math.Min(drawWallY1 - 1, LowerClip[i] - 1);
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = cy1,
YBottom = (int)cy2,
Texture = ceilingTexture,
LightLevel = lightLevel
});
}
if (drawWall)
{
var wy1 = Math.Max(drawWallY1, UpperClip[i] + 1);
var wy2 = Math.Min(drawWallY2, LowerClip[i] - 1);
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = (int)wy1,
YBottom = (int)wy2,
Texture = wallTexture,
LightLevel = lightLevel
});
}
if (drawFloor)
{
var fy1 = Math.Max(drawWallY2 + 1, UpperClip[i] + 1);
var fy2 = LowerClip[i] - 1;
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = (int)fy1,
YBottom = fy2,
Texture = floorTexture,
LightLevel = lightLevel
});
}
wallY1 += wallY1Step;
wallY2 += wallY2Step;
}
}
private void DrawPortalWallRange(int x1, int x2, int colorSeed = 0)
{
var frontSector = CurrentSeg.FrontSector;
var backSector = CurrentSeg.BackSector;
var side = CurrentSeg.Linedef.FrontSidedef;
var upperWallTexture = CurrentSeg.Linedef.FrontSidedef.UpperTexture;
var lowerWallTexture = CurrentSeg.Linedef.FrontSidedef.LowerTexture;
var ceilingTexture = CurrentSeg.FrontSector.CeilingTexture;
var floorTexture = CurrentSeg.FrontSector.FloorTexture;
var lightLevel = CurrentSeg.FrontSector.LightLevel;
var worldFrontZ1 = CurrentSeg.FrontSector.CeilingHeight - _player.Height;
var worldFrontZ2 = CurrentSeg.FrontSector.FloorHeight - _player.Height;
var worldBackZ1 = CurrentSeg.BackSector.CeilingHeight - _player.Height;
var worldBackZ2 = CurrentSeg.BackSector.FloorHeight - _player.Height;
var drawCeiling = false;
var drawFloor = false;
var drawUpperWall = false;
var drawLowerWall = false;
if (worldFrontZ1 != worldBackZ1
|| frontSector.LightLevel != backSector.LightLevel
|| frontSector.CeilingTexture != backSector.CeilingTexture)
{
drawUpperWall = side.UpperTexture != "-" && worldBackZ1 < worldFrontZ1;
drawCeiling = worldFrontZ1 >= 0;
}
if (worldFrontZ2 != worldBackZ2
|| frontSector.LightLevel != backSector.LightLevel
|| frontSector.FloorTexture != backSector.FloorTexture)
{
drawLowerWall = side.LowerTexture != "-" && worldBackZ2 > worldFrontZ2;
drawFloor = worldFrontZ2 <= 0;
}
// Nothing to draw here
if (!drawCeiling && !drawFloor && !drawUpperWall && !drawLowerWall)
{
return;
}
var rwNormalAngle = CurrentSeg.Angle + 90;
var offsetAngle = rwNormalAngle - CurrentRwAngle1;
var hypotenuse = Vector2.Distance(_player.Position, CurrentSeg.StartVertex);
var rwDistance = hypotenuse * (float)MathF.Cos(MathHelper.ToRadians(offsetAngle));
var rwScale1 = ScaleFromGlobalAngle(x1, rwNormalAngle, rwDistance);
var scale2 = ScaleFromGlobalAngle(x2, rwNormalAngle, rwDistance);
var rwScaleStep = (scale2 - rwScale1) / (x2 - x1);
var wallY1 = Settings.HalfHeight - worldFrontZ1 * rwScale1;
var wallY1Step = -rwScaleStep * worldFrontZ1;
var wallY2 = Settings.HalfHeight - worldFrontZ2 * rwScale1;
var wallY2Step = -rwScaleStep * worldFrontZ2;
var portalY1 = 0f;
var portalY1Step = 0f;
if (worldBackZ1 > worldFrontZ2)
{
portalY1 = Settings.HalfHeight - worldBackZ1 * rwScale1;
portalY1Step = -rwScaleStep * worldBackZ1;
}
else
{
portalY1 = wallY2;
portalY1Step = wallY2Step;
}
var portalY2 = 0f;
var portalY2Step = 0f;
if (worldBackZ2 < worldFrontZ1)
{
portalY2 = Settings.HalfHeight - worldBackZ2 * rwScale1;
portalY2Step = -rwScaleStep * worldBackZ2;
}
else
{
portalY2 = wallY1;
portalY2Step = wallY1Step;
}
for (var i = x1; i < x2 + 1; i++)
{
var drawWallY1 = wallY1 - 1;
var drawWallY2 = wallY2;
if (drawUpperWall)
{
var drawUpperWallY1 = wallY1 - 1;
var drawUpperWallY2 = portalY1;
if (drawCeiling)
{
var cy1 = UpperClip[i] + 1;
var cy2 = (int)Math.Min(drawWallY1 - 1, LowerClip[i] - 1);
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = cy1,
YBottom = cy2,
Texture = ceilingTexture,
LightLevel = lightLevel
});
}
var wy1 = (int)Math.Max(drawUpperWallY1, UpperClip[i] + 1);
var wy2 = (int)Math.Min(drawUpperWallY2, LowerClip[i] - 1);
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = wy1,
YBottom = wy2,
Texture = upperWallTexture,
LightLevel = lightLevel
});
if (UpperClip[i] < wy2)
{
UpperClip[i] = wy2;
}
portalY1 += portalY1Step;
}
if (drawCeiling)
{
var cy1 = UpperClip[i] + 1;
var cy2 = (int)Math.Min(drawWallY1 - 1, LowerClip[i] - 1);
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = cy1,
YBottom = cy2,
Texture = ceilingTexture,
LightLevel = lightLevel
});
if (UpperClip[i] < cy2)
{
UpperClip[i] = cy2;
}
}
if (drawLowerWall)
{
if (drawFloor)
{
var fy1 = (int)Math.Max(drawWallY2 + 1, UpperClip[i] + 1);
var fy2 = LowerClip[i] - 1;
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = fy1,
YBottom = fy2,
Texture = floorTexture,
LightLevel = lightLevel
});
}
var drawLowerWallY1 = portalY2 - 1;
var drawLowerWallY2 = wallY2;
var wy1 = (int)Math.Max(drawLowerWallY1, UpperClip[i] + 1);
var wy2 = (int)Math.Min(drawLowerWallY2, LowerClip[i] - 1);
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = wy1,
YBottom = wy2,
Texture = lowerWallTexture,
LightLevel = lightLevel
});
if (LowerClip[i] > wy1)
{
LowerClip[i] = wy1;
}
portalY2 += portalY2Step;
}
if (drawFloor)
{
var fy1 = (int)Math.Max(drawWallY2 + 1, UpperClip[i] + 1);
var fy2 = LowerClip[i] - 1;
_renderer.VertsToDraw.AddLast(
new Vert
{
X = i,
YTop = fy1,
YBottom = fy2,
Texture = floorTexture,
LightLevel = lightLevel
});
if (LowerClip[i] > drawWallY2 + 1)
{
LowerClip[i] = fy1;
}
}
wallY1 += wallY1Step;
wallY2 += wallY2Step;
}
}
private float ScaleFromGlobalAngle(int x, float rwNormalAngle, float rwDistance)
{
var xAngle = XToAngle[x];
var numerator = Settings.ScreenDistance * MathF.Cos(MathHelper.ToRadians(rwNormalAngle - xAngle - _player.Angle));
var denominator = rwDistance * MathF.Cos(MathHelper.ToRadians(xAngle));
var scale = numerator / denominator;
scale = Math.Min(MaxScale, Math.Max(MinScale, scale));
return scale;
}
private void InitializeScreenRange()
{
ScreenRange = new int[Settings.Width];
for (var i = 0; i < ScreenRange.Length; i++)
{
ScreenRange[i] = i;
}
}
private void InitializeFloorAndCeilingClipHeights()
{
UpperClip = new int[Settings.Width];
LowerClip = new int[Settings.Width];
for (var i = 0; i < Settings.Width; i++)
{
UpperClip[i] = -1;
LowerClip[i] = Settings.Height;
}
}
}