-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRayTracer.java
309 lines (241 loc) · 11.1 KB
/
RayTracer.java
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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class RayTracer {
// Renders the scene using raycasting
double[] depth_buffer;
Level level;
public RayTracer(Level level, int width) {
this.level = level;
depth_buffer = new double[width];
}
public void RenderCol(FrameBuffer buffer, Vector2 fromPos, double fromRot, int col, int screenH, Sector s) {
RenderCol(buffer, fromPos, fromRot, col, screenH, 0.0, 0, s);
}
public void RenderCol(FrameBuffer buffer, Vector2 FromPos, double FromRot, int col, int screenH, double inWallDist, int rc, Sector s) {
if (s == null) {
s = level.sectors.get(0); // Fall back to sector 0 if out of bounds or something
}
double angle = Math.atan2(col - buffer.width / 2, buffer.width / 2) + FromRot;
ArrayList<Wall> walls = new ArrayList<Wall>();
for (Wall wall : s.walls) {
walls.add(wall);
}
Ray ray = new Ray(FromPos, angle);
Wall closestWall = null;
double closestDist = Double.MAX_VALUE;
for (Wall wall : walls) {
// Check if the ray intersects the wall
if (ray.Intersects(wall)) {
// Get the distance to the wall
double dist = Vector2.Distance(FromPos, ray.Intersection(wall));
// Check if the distance is less than the closest distance
if (dist < closestDist && dist > inWallDist) {
// Set the closest wall to the current wall
closestWall = wall;
// Set the closest distance to the current distance
closestDist = dist;
}
}
}
if (closestWall == null) {
return;
}
// Get the wall's texture
String wallTex = "texture/" + closestWall.texture + ".png";
// Load the texture using ImageIO
BufferedImage tex = null;
try {
tex = ImageIO.read(new File(wallTex));
} catch (IOException e) {
// Load texture/missing.png instead
try {
tex = ImageIO.read(new File("texture/missing.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Get the height of the texture
int texH = tex.getHeight();
// Get the width of the texture
int texW = tex.getWidth();
// Get the intersection point
Vector2 intersection = ray.Intersection(closestWall);
// Add this column to the depth buffer
depth_buffer[col] = closestDist;
// Get the length of the wall
double wallLength = Vector2.Distance(closestWall.vertA, closestWall.vertB);
// Get the local x position of the intersection point
double localX = Vector2.Distance(closestWall.vertA, intersection);
double texCol = localX / wallLength * texW;
texCol *= (wallLength / 2);
texCol %= texW;
// Get the distance from the ray origin to the intersection point
double distance = Vector2.Distance(FromPos, intersection) * Math.cos(angle - FromRot);
// Get the height of the wall on screen
double height = screenH / distance;
// Get the y position of the wall
double y = (screenH - height) / 2;
// Calcuate the shade of the wall based on the camera angle and wall normal
double shade = Math.abs(Math.cos((FromRot + (1.5 * Math.PI)) - closestWall.getAngle()));
// Light should be affected by distance but not by a factor of more than half
shade = shade * (1 - (distance / (buffer.width / 2)));
// Make sure the shade is between 40 and 255
shade = Math.max(0.4, Math.min(1, shade));
// Apply fake banding to the shade
shade = Math.floor(shade * 16) / 16;
// Calculate the color of the wall
int r = (int) (255 * shade);
int g = (int) (255 * shade);
int b = (int) (255 * shade);
Color color = new Color(r, g, b);
// Draw the wall using its texture and shade
for (int i = 0; i < height; i++) {
// Check if the Y pixel is on screen
if (y + i < 0 || y + i >= screenH) continue;
// Get the y position of the texture
int texY = (int) (i / height * texH);
// Make sure the texture Y position is between 0 and the texture height
texY = (int) Util.wrap(texY, 0, texH - 1);
texCol = (int)Util.wrap(texCol, 0, texW - 1);
// Get the color of the pixel in the texture
//System.out.println(texCol + " " + texY);
int texColor = tex.getRGB((int) texCol, texY);
// Get the color of the pixel in the texture
int texR = (texColor >> 16) & 0xFF;
int texG = (texColor >> 8) & 0xFF;
int texB = (texColor >> 0) & 0xFF;
// Calculate the color of the pixel on the wall
r = (int) (texR * shade);
g = (int) (texG * shade);
b = (int) (texB * shade);
color = new Color(r, g, b);
// Draw the pixel
buffer.setPixel(col, (int) y + i, color);
}
// Draw the floor
buffer.drawFastVLine(col, (int) (y + height), (int) (screenH - (y + height)), new Color(200, 200, 200));
buffer.drawFastVLine(col, 0, (int) y, new Color(180, 180, 180));
// Convert the depth to a grayscale color
int depthColor = (int) (255 * (closestDist / (buffer.width / 2)));
// Make sure the depth color is between 0 and 255
depthColor = (int) Util.wrap(depthColor, 0, 255);
// Set the top 20 pixels to the depth color
//buffer.drawFastVLine(col, 0, 20, new Color(depthColor, depthColor, depthColor));
}
public void RenderColPass2(FrameBuffer buffer, Vector2 FromPos, double FromRot, int col, int screenH) {
double angle = Math.atan2(col - buffer.width / 2, buffer.width / 2) + FromRot;
ArrayList<Wall> walls = new ArrayList<Wall>();
for (Entity entity : level.entities) {
walls.add(entity.makeWall());
}
Ray ray = new Ray(FromPos, angle);
Wall closestWall = null;
double closestDist = Double.MAX_VALUE;
for (Wall wall : walls) {
// Check if the ray intersects the wall
if (ray.Intersects(wall)) {
// Get the distance to the wall
double dist = Vector2.Distance(FromPos, ray.Intersection(wall));
if (dist < closestDist) {
// Set the closest wall to the current wall
closestWall = wall;
// Set the closest distance to the current distance
closestDist = dist;
}
}
}
if (closestWall == null) {
return;
}
// Check the depth buffer
if (depth_buffer[col] < closestDist) {
return;
}
// Get the wall's texture
String wallTex = "texture/" + closestWall.texture + ".png";
// Load the texture using ImageIO
BufferedImage tex = null;
try {
tex = ImageIO.read(new File(wallTex));
} catch (IOException e) {
// Load texture/missing.png instead
try {
tex = ImageIO.read(new File("texture/missing.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
}
// Get the height of the texture
int texH = tex.getHeight();
// Get the width of the texture
int texW = tex.getWidth();
// Get the intersection point
Vector2 intersection = ray.Intersection(closestWall);
// Get the length of the wall
double wallLength = Vector2.Distance(closestWall.vertA, closestWall.vertB);
// Get the local x position of the intersection point
double localX = Vector2.Distance(closestWall.vertA, intersection);
double texCol = localX / wallLength * texW;
texCol *= (wallLength / 2);
texCol %= texW;
// Get the distance from the ray origin to the intersection point
double distance = Vector2.Distance(FromPos, intersection) * Math.cos(angle - FromRot);
// Get the height of the wall on screen
double height = screenH / distance;
// Get the y position of the wall
double y = (screenH - height) / 2;
// Calcuate the shade of the wall based on the camera angle and wall normal
double shade = Math.abs(Math.cos((FromRot + (1.5 * Math.PI)) - closestWall.getAngle()));
// Light should be affected by distance but not by a factor of more than half
shade = shade * (1 - (distance / (buffer.width / 2)));
// Make sure the shade is between 40 and 255
shade = Math.max(0.4, Math.min(1, shade));
// Apply fake banding to the shade
shade = Math.floor(shade * 16) / 16;
// Calculate the color of the wall
int r = (int) (255 * shade);
int g = (int) (255 * shade);
int b = (int) (255 * shade);
Color color = new Color(r, g, b);
// Draw the wall using its texture and shade
for (int i = 0; i < height; i++) {
// Check if the Y pixel is on screen
if (y + i < 0 || y + i >= screenH) continue;
// Get the y position of the texture
int texY = (int) (i / height * texH);
// Make sure the texture Y position is between 0 and the texture height
texY = (int) Util.wrap(texY, 0, texH - 1);
texCol = (int)Util.wrap(texCol, 0, texW - 1);
// Get the color of the pixel in the texture
//System.out.println(texCol + " " + texY);
int texColor = tex.getRGB((int) texCol, texY);
// Get the color of the pixel in the texture
int texR = (texColor >> 16) & 0xFF;
int texG = (texColor >> 8) & 0xFF;
int texB = (texColor >> 0) & 0xFF;
int texA = (texColor >> 24) & 0xFF;
// Calculate the color of the pixel on the wall
r = (int) (texR * shade);
g = (int) (texG * shade);
b = (int) (texB * shade);
color = new Color(r, g, b);
if (texA == 0) {
continue;
}
// Draw the pixel
buffer.setPixel(col, (int) y + i, color);
}
// Draw the floor
buffer.drawFastVLine(col, (int) (y + height), (int) (screenH - (y + height)), new Color(200, 200, 200));
buffer.drawFastVLine(col, 0, (int) y, new Color(180, 180, 180));
// Convert the depth to a grayscale color
int depthColor = (int) (255 * (closestDist / (buffer.width / 2)));
// Make sure the depth color is between 0 and 255
depthColor = (int) Util.wrap(depthColor, 0, 255);
// Set the top 20 pixels to the depth color
//buffer.drawFastVLine(col, 0, 20, new Color(depthColor, depthColor, depthColor));
}
}