-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.glsl
162 lines (140 loc) · 4.89 KB
/
wall.glsl
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
#version 430
#define PI 3.14159265358979323846
layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
struct Column {
int padding;
int textureId;
float brightness;
float lineHeight;
float lineOffset;
float textureColumnOffset;
};
struct Tile {
int ceiling;
int wall;
int floor;
int padding;
};
layout (std430, binding = 1) writeonly restrict buffer Columns {
Column outputData[];
};
layout (std430, binding = 2) readonly restrict buffer Constants {
int depthOfField;
int viewportWidth;
int viewportHeight;
float viewportHalfHeight;
float columnAngleStart;
float columnAngleStep;
ivec2 tileSize;
ivec2 tileMapSize;
};
layout (std430, binding = 3) readonly restrict buffer MapData {
int mapWidth;
int mapHeight;
Tile mapData[];
};
layout (std430, binding = 4) readonly restrict buffer FrameData {
vec2 playerPosition;
ivec2 playerMapCoords;
vec2 playerTileCoords;
vec2 playerDirection;
vec2 cameraPlane;
};
void main() {
uint n = gl_GlobalInvocationID.x;
if (n >= viewportWidth) {
return;
}
// cameraX = coordinate (between -1 and 1) of the ray on the x-axis
// of the camera plane
float cameraX = (2.0f * (float(n) / viewportWidth)) - 1.0f;
// Compute the angle of the ray
float angle = columnAngleStart + n * columnAngleStep;
// Clamp the angle between 0 and 2*PI
while (angle > 2 * PI) {
angle -= 2 * PI;
}
while (angle < 0.0) {
angle += 2 * PI;
}
// Compute the ray direction
vec2 rayDirection = playerDirection + cameraPlane * cameraX;
// Compute the distance along the ray direction to the next intersection
// with the y-axis
float yDeltaDistance = (rayDirection.x == 0) ? 1e30 : abs(1.0 / rayDirection.x);
// Compute the distance along the ray direction to the next intersection
// with the x-axis
float xDeltaDistance = (rayDirection.y == 0) ? 1e30 : abs(1.0 / rayDirection.y);
// Compute the distance in the horizontal direction of the ray to
// the border of the cell
float xDistance = (rayDirection.x > 0.0) ? (1.0 - playerTileCoords.x) : playerTileCoords.x;
// Compute the distance in the vertical direction of the ray to
// the border of the cell
float yDistance = (rayDirection.y > 0.0) ? (1.0 - playerTileCoords.y) : playerTileCoords.y;
// Compute the distance along the ray direction to the first intersection
// with the y-axis
float yIntersectionDistance = yDeltaDistance * xDistance;
// Compute the distance along the ray direction to the first intersection
// with the x-axis
float xIntersectionDistance = xDeltaDistance * yDistance;
// Find the direction we are moving in the map
ivec2 step = ivec2(sign(rayDirection));
// Ray's map coordinates
ivec2 mapCoords = playerMapCoords;
int cellId = 0;
bool vertical = false;
// Step the rays until one hits
int i;
for (i = 0; i < depthOfField; i++) {
int mapOffset = mapCoords.y * mapWidth + mapCoords.x;
if (mapOffset >= 0 && mapOffset < (mapWidth * mapHeight) && (cellId = mapData[mapOffset].wall) != 0) {
break;
}
if (yIntersectionDistance < xIntersectionDistance) {
yIntersectionDistance += yDeltaDistance;
mapCoords.x += step.x;
vertical = true;
} else {
xIntersectionDistance += xDeltaDistance;
mapCoords.y += step.y;
vertical = false;
}
}
// Check if the ray hit an empty cell
if (cellId == 0) {
if (i == depthOfField) {
outputData[n].lineHeight = 0.1;
}
return;
}
// If we didn't, store the hit information for the first
// ray to hit a wall
float rayDistance, brightness;
if (vertical) {
rayDistance = yIntersectionDistance - yDeltaDistance;
brightness = 1.0;
} else {
rayDistance = xIntersectionDistance - xDeltaDistance;
brightness = 0.85;
}
vec2 coordinates = playerPosition + rayDirection * rayDistance;
float textureColumnOffset;
if (vertical) {
textureColumnOffset = coordinates.y - mapCoords.y;
textureColumnOffset = (step.x > 0) ? textureColumnOffset : (1.0 - textureColumnOffset);
} else {
textureColumnOffset = coordinates.x - mapCoords.x;
textureColumnOffset = (step.y < 0) ? textureColumnOffset : (1.0 - textureColumnOffset);
}
float lineHeight = viewportHeight / rayDistance;
float lineOffset = 0.0;
if (lineHeight > viewportHeight) {
lineOffset = lineHeight - viewportHeight;
lineHeight = viewportHeight;
}
outputData[n].textureId = cellId - 1;
outputData[n].brightness = brightness;
outputData[n].lineHeight = lineHeight;
outputData[n].lineOffset = lineOffset;
outputData[n].textureColumnOffset = textureColumnOffset;
}