-
Notifications
You must be signed in to change notification settings - Fork 19
/
Voxel Renderer.ahk
executable file
·89 lines (79 loc) · 2.27 KB
/
Voxel Renderer.ahk
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
/*
Octree
------
struct node {
boolean leaf whether the node is a leaf or not
char full bit mask of full subnodes
char empty bit mask of empty subnodes
}
*/
Resolution := 1
VarSetCapacity(Octree,8 ** Resolution,0)
RayTraceGrid(X,Y,Angle,Callback)
{
static Radians := 3.141592653589793 / 180
;limit the angle to between 0 and limit 360
Angle := Mod(Angle,360)
If Angle < 0
Angle += 360
BaseX := Floor(X), BaseY := Floor(Y)
If (Angle = 0 || Angle = 180) ;vertical ray
{
StepX := 0
ComponentX := 0
IntersectX := ~0 ;wip: represents infinity
}
Else If Angle < 180 ;line slopes rightward
{
StepX := 1
ComponentX := 1 / Sin(Angle * Radians)
IntersectX := ((BaseX - X) + 1) * ComponentX
}
Else ;line slopes leftward
{
StepX := -1
ComponentX := 1 / -Sin(Angle * Radians)
IntersectX := (X - BaseX) * ComponentX
}
If (Angle = 90 || Angle = 270) ;horizontal ray
{
StepY := 0
ComponentY := 0
IntersectY := ~0 ;wip: represents infinity
}
Else If (Angle < 90 || Angle > 270) ;line slopes upward
{
StepY := 1
ComponentY := 1 / Cos(Angle * Radians)
IntersectY := ((BaseY - Y) + 1) * ComponentY
}
Else ;line slopes downward
{
StepY := -1
ComponentY := 1 / -Cos(Angle * Radians)
IntersectY := (Y - BaseY) * ComponentY
}
Distance := 0
Loop
{
Callback.(BaseX,BaseY,Distance)
If (IntersectX < IntersectY) ;intersection with left or right
{
Distance := IntersectX
BaseX += StepX
IntersectX += ComponentX
}
Else If (IntersectX > IntersectY) ;intersection with top or bottom
{
Distance := IntersectY
BaseY += StepY
IntersectY += ComponentY
}
Else ;passing through the grid corner exactly
{
Distance := IntersectX
BaseX += StepX, BaseY += StepY
IntersectX += ComponentX, IntersectY += ComponentY
}
}
}