Skip to content

Commit 8b77471

Browse files
committed
refactor: correct drawShadedLine variables and add comments
1 parent c7d9d68 commit 8b77471

File tree

1 file changed

+107
-19
lines changed

1 file changed

+107
-19
lines changed

src/main/java/org/runejs/client/media/Rasterizer3D.java

Lines changed: 107 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class Rasterizer3D extends Rasterizer {
4141
*/
4242
public static int anInt2942;
4343
public static int[] sinetable = new int[2048];
44+
/**
45+
* TODO (jkm) investigate and rename, don't think this is accurate
46+
*/
4447
public static boolean notTextured = true;
4548
public static int viewportRx;
4649
public static int[] cosinetable = new int[2048];
@@ -1152,113 +1155,198 @@ public static void drawShadedLine562(int dest[], int dest_off, int startX, int e
11521155
}
11531156

11541157
public static void drawShadedLine(int[] dest, int dest_off, int start_x, int end_x, int color_index, int grad) {
1155-
// if(useLatestShadeLine) {//divert all calls to the new method as its better
1156-
// drawShadedLine562(dest, dest_off, start_x, end_x, color_index, grad);
1157-
// return;
1158-
// }
1158+
// the color currently being drawn
11591159
int color;
1160+
1161+
// the number of iterations needed to draw
11601162
int loops;
1163+
1164+
// this variable is called "nonTextured" but it actually controls whether the line is broken down
1165+
// into chunks of 4 pixels or not
11611166
if(notTextured) {
1162-
int off;
1167+
// how much to increase the color by each pixel
1168+
int color_slope;
1169+
1170+
// When edge restrictions are applied, the line should not be drawn beyond the predefined viewport.
11631171
if(restrict_edges) {
1172+
// Calculate the slope of the color gradient. If the line is longer than 3 pixels, the slope is
1173+
// calculated as the ratio of color difference to line length. For lines that are 3 pixels or shorter,
1174+
// the color slope is set to zero as there wouldn't be a noticeable gradient.
11641175
if(end_x - start_x > 3) {
1165-
off = (grad - color_index) / (end_x - start_x);
1176+
color_slope = (grad - color_index) / (end_x - start_x);
11661177
} else {
1167-
off = 0;
1178+
color_slope = 0;
11681179
}
1180+
1181+
// If the end point of the line exceeds the viewport, we clip it to the boundary edge of the viewport.
11691182
if(end_x > viewportRx) {
11701183
end_x = viewportRx;
11711184
}
1185+
1186+
// If the start point of the line is negative, we adjust the initial color index to compensate
1187+
// and set the start point to zero. This ensures that the color gradient starts correctly even
1188+
// if the line starts outside the viewport.
11721189
if(start_x < 0) {
1173-
color_index -= start_x * off;
1190+
color_index -= start_x * color_slope;
11741191
start_x = 0;
11751192
}
1193+
1194+
// If the start point is now greater than or equal to the end point, this means the line is
1195+
// completely outside the viewport and we return without drawing anything.
11761196
if(start_x >= end_x) {
11771197
return;
11781198
}
1199+
1200+
// Update the destination offset to account for the corrected start point.
1201+
// Calculate the number of iterations required for 4-pixel chunks in the line.
11791202
dest_off += start_x;
11801203
loops = end_x - start_x >> 2;
1181-
off <<= 2;
1204+
1205+
// Since we're dealing with 4-pixel chunks, we multiply the color gradient by 4.
1206+
color_slope <<= 2;
11821207
} else {
1208+
// When there are no edge restrictions, we just proceed to draw the line if it's within the viewport.
1209+
1210+
// If start_x is greater than or equal to end_x, the line is outside of the viewport and we return.
11831211
if(start_x >= end_x) {
11841212
return;
11851213
}
1214+
1215+
// Calculate the destination offset and number of 4-pixel chunks.
11861216
dest_off += start_x;
11871217
loops = end_x - start_x >> 2;
1218+
1219+
// Calculate the slope of the color gradient using the shadowDecay array if the line is long enough.
1220+
// For very short lines (length less than or equal to 0), there is no gradient and the slope is zero.
11881221
if(loops > 0) {
1189-
off = (grad - color_index) * shadowDecay[loops] >> 15;
1222+
color_slope = (grad - color_index) * shadowDecay[loops] >> 15;
11901223
} else {
1191-
off = 0;
1224+
color_slope = 0;
11921225
}
11931226
}
1227+
1228+
// If the alpha value is 0, it means the line has no transparency.
11941229
if(alpha == 0) {
1230+
// Repeat for each 4-pixel chunk in the line.
11951231
while(--loops >= 0) {
1232+
// Convert the color index to an RGB color.
11961233
color = hsl2rgb[color_index >> 8];
1197-
color_index += off;
1234+
1235+
// Adjust the color index for the next iteration using the color slope.
1236+
color_index += color_slope;
1237+
1238+
// Paint 4 pixels at a time with the calculated color.
11981239
dest[dest_off++] = color;
11991240
dest[dest_off++] = color;
12001241
dest[dest_off++] = color;
12011242
dest[dest_off++] = color;
12021243
}
1244+
1245+
// Calculate the remainder of pixels (less than 4).
12031246
loops = end_x - start_x & 0x3;
1247+
1248+
// If there are remaining pixels, paint them as well.
12041249
if(loops > 0) {
12051250
color = hsl2rgb[color_index >> 8];
12061251
do {
12071252
dest[dest_off++] = color;
12081253
} while(--loops > 0);
12091254
}
12101255
} else {
1256+
// When the alpha value is not 0, the line has some level of transparency.
1257+
12111258
int src_alpha = alpha;
12121259
int dest_alpha = 256 - alpha;
1260+
1261+
// Repeat for the number of 4-pixel chunks in the line.
12131262
while(--loops >= 0) {
1263+
// Convert the color index to an RGB color.
12141264
color = hsl2rgb[color_index >> 8];
1215-
color_index += off;
1265+
1266+
// Adjust the color index for the next iteration using the color slope.
1267+
color_index += color_slope;
1268+
1269+
// Apply the alpha transparency to the color. This is done separately for the red/blue and green
1270+
// components of the color, as indicated by the bitwise AND operations with 0xff00ff and 0xff00.
12161271
color = ((color & 0xff00ff) * dest_alpha >> 8 & 0xff00ff) + ((color & 0xff00) * dest_alpha >> 8 & 0xff00);
1272+
1273+
// Blend the color of each pixel with the destination pixel, considering the source alpha.
1274+
// This is done for each of the 4 pixels in the chunk.
12171275
dest[dest_off++] = color + ((dest[dest_off] & 0xff00ff) * src_alpha >> 8 & 0xff00ff) + ((dest[dest_off] & 0xff00) * src_alpha >> 8 & 0xff00);
12181276
dest[dest_off++] = color + ((dest[dest_off] & 0xff00ff) * src_alpha >> 8 & 0xff00ff) + ((dest[dest_off] & 0xff00) * src_alpha >> 8 & 0xff00);
12191277
dest[dest_off++] = color + ((dest[dest_off] & 0xff00ff) * src_alpha >> 8 & 0xff00ff) + ((dest[dest_off] & 0xff00) * src_alpha >> 8 & 0xff00);
12201278
dest[dest_off++] = color + ((dest[dest_off] & 0xff00ff) * src_alpha >> 8 & 0xff00ff) + ((dest[dest_off] & 0xff00) * src_alpha >> 8 & 0xff00);
12211279
}
1280+
// Calculate the remainder of pixels (less than 4).
12221281
loops = end_x - start_x & 0x3;
1282+
1283+
1284+
// If there are remaining pixels, paint them as well.
12231285
if(loops > 0) {
12241286
color = hsl2rgb[color_index >> 8];
1287+
1288+
// Apply alpha transparency again as we just re-retrieved the color
12251289
color = ((color & 0xff00ff) * dest_alpha >> 8 & 0xff00ff) + ((color & 0xff00) * dest_alpha >> 8 & 0xff00);
12261290
do {
12271291
dest[dest_off++] = color + ((dest[dest_off] & 0xff00ff) * src_alpha >> 8 & 0xff00ff) + ((dest[dest_off] & 0xff00) * src_alpha >> 8 & 0xff00);
12281292
} while(--loops > 0);
12291293
}
12301294
}
12311295
} else {
1296+
// If the start of the line is before the end.
12321297
if(start_x < end_x) {
1233-
int i = (grad - color_index) / (end_x - start_x);
1298+
// The color slope determines the rate of change of the color across the line.
1299+
int color_slope = (grad - color_index) / (end_x - start_x);
1300+
1301+
// If restrict_edges is true, it means we have to clip the line to the viewport.
12341302
if(restrict_edges) {
1303+
// Clip the line to the right edge of the viewport.
12351304
if(end_x > viewportRx) {
12361305
end_x = viewportRx;
12371306
}
1307+
1308+
// If the line starts before the left edge of the viewport, clip it and adjust the starting color.
12381309
if(start_x < 0) {
1239-
color_index -= start_x * i;
1310+
color_index -= start_x * color_slope;
12401311
start_x = 0;
12411312
}
1313+
1314+
// If the start of the line is now after the end, there is nothing to draw.
12421315
if(start_x >= end_x) {
12431316
return;
12441317
}
12451318
}
1319+
1320+
// Adjust the offset into the destination array for the new starting point.
12461321
dest_off += start_x;
1322+
// Calculate the number of pixels to draw.
12471323
loops = end_x - start_x;
1324+
1325+
// If the alpha is 0 (no transparency), the loop simply paints each pixel with the color.
12481326
if(alpha == 0) {
12491327
do {
1328+
// Paint the pixel with the color corresponding to the current color index.
12501329
dest[dest_off++] = hsl2rgb[color_index >> 8];
1251-
color_index += i;
1252-
} while(--loops > 0);
1330+
// Adjust the color index for the next pixel.
1331+
color_index += color_slope;
1332+
} while(--loops > 0); // Repeat for the number of pixels in the line (no chunking)
12531333
} else {
1334+
// If alpha is not 0 (there is some transparency), the pixels need to be blended with the destination.
12541335
int i_43_ = alpha;
12551336
int i_44_ = 256 - alpha;
12561337
do {
1338+
// Calculate the color for the current pixel.
12571339
color = hsl2rgb[color_index >> 8];
1258-
color_index += i;
1340+
1341+
// Adjust the color index for the next pixel.
1342+
color_index += color_slope;
1343+
1344+
// Apply the alpha transparency to the color.
12591345
color = ((color & 0xff00ff) * i_44_ >> 8 & 0xff00ff) + ((color & 0xff00) * i_44_ >> 8 & 0xff00);
1346+
1347+
// Blend the color with the destination pixel.
12601348
dest[dest_off++] = color + ((dest[dest_off] & 0xff00ff) * i_43_ >> 8 & 0xff00ff) + ((dest[dest_off] & 0xff00) * i_43_ >> 8 & 0xff00);
1261-
} while(--loops > 0);
1349+
} while(--loops > 0); // Repeat for the number of pixels in the line (no chunking)
12621350
}
12631351
}
12641352
}

0 commit comments

Comments
 (0)