@@ -41,6 +41,9 @@ public class Rasterizer3D extends Rasterizer {
41
41
*/
42
42
public static int anInt2942 ;
43
43
public static int [] sinetable = new int [2048 ];
44
+ /**
45
+ * TODO (jkm) investigate and rename, don't think this is accurate
46
+ */
44
47
public static boolean notTextured = true ;
45
48
public static int viewportRx ;
46
49
public static int [] cosinetable = new int [2048 ];
@@ -1152,113 +1155,198 @@ public static void drawShadedLine562(int dest[], int dest_off, int startX, int e
1152
1155
}
1153
1156
1154
1157
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
1159
1159
int color ;
1160
+
1161
+ // the number of iterations needed to draw
1160
1162
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
1161
1166
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.
1163
1171
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.
1164
1175
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 );
1166
1177
} else {
1167
- off = 0 ;
1178
+ color_slope = 0 ;
1168
1179
}
1180
+
1181
+ // If the end point of the line exceeds the viewport, we clip it to the boundary edge of the viewport.
1169
1182
if (end_x > viewportRx ) {
1170
1183
end_x = viewportRx ;
1171
1184
}
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.
1172
1189
if (start_x < 0 ) {
1173
- color_index -= start_x * off ;
1190
+ color_index -= start_x * color_slope ;
1174
1191
start_x = 0 ;
1175
1192
}
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.
1176
1196
if (start_x >= end_x ) {
1177
1197
return ;
1178
1198
}
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.
1179
1202
dest_off += start_x ;
1180
1203
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 ;
1182
1207
} 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.
1183
1211
if (start_x >= end_x ) {
1184
1212
return ;
1185
1213
}
1214
+
1215
+ // Calculate the destination offset and number of 4-pixel chunks.
1186
1216
dest_off += start_x ;
1187
1217
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.
1188
1221
if (loops > 0 ) {
1189
- off = (grad - color_index ) * shadowDecay [loops ] >> 15 ;
1222
+ color_slope = (grad - color_index ) * shadowDecay [loops ] >> 15 ;
1190
1223
} else {
1191
- off = 0 ;
1224
+ color_slope = 0 ;
1192
1225
}
1193
1226
}
1227
+
1228
+ // If the alpha value is 0, it means the line has no transparency.
1194
1229
if (alpha == 0 ) {
1230
+ // Repeat for each 4-pixel chunk in the line.
1195
1231
while (--loops >= 0 ) {
1232
+ // Convert the color index to an RGB color.
1196
1233
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.
1198
1239
dest [dest_off ++] = color ;
1199
1240
dest [dest_off ++] = color ;
1200
1241
dest [dest_off ++] = color ;
1201
1242
dest [dest_off ++] = color ;
1202
1243
}
1244
+
1245
+ // Calculate the remainder of pixels (less than 4).
1203
1246
loops = end_x - start_x & 0x3 ;
1247
+
1248
+ // If there are remaining pixels, paint them as well.
1204
1249
if (loops > 0 ) {
1205
1250
color = hsl2rgb [color_index >> 8 ];
1206
1251
do {
1207
1252
dest [dest_off ++] = color ;
1208
1253
} while (--loops > 0 );
1209
1254
}
1210
1255
} else {
1256
+ // When the alpha value is not 0, the line has some level of transparency.
1257
+
1211
1258
int src_alpha = alpha ;
1212
1259
int dest_alpha = 256 - alpha ;
1260
+
1261
+ // Repeat for the number of 4-pixel chunks in the line.
1213
1262
while (--loops >= 0 ) {
1263
+ // Convert the color index to an RGB color.
1214
1264
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.
1216
1271
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.
1217
1275
dest [dest_off ++] = color + ((dest [dest_off ] & 0xff00ff ) * src_alpha >> 8 & 0xff00ff ) + ((dest [dest_off ] & 0xff00 ) * src_alpha >> 8 & 0xff00 );
1218
1276
dest [dest_off ++] = color + ((dest [dest_off ] & 0xff00ff ) * src_alpha >> 8 & 0xff00ff ) + ((dest [dest_off ] & 0xff00 ) * src_alpha >> 8 & 0xff00 );
1219
1277
dest [dest_off ++] = color + ((dest [dest_off ] & 0xff00ff ) * src_alpha >> 8 & 0xff00ff ) + ((dest [dest_off ] & 0xff00 ) * src_alpha >> 8 & 0xff00 );
1220
1278
dest [dest_off ++] = color + ((dest [dest_off ] & 0xff00ff ) * src_alpha >> 8 & 0xff00ff ) + ((dest [dest_off ] & 0xff00 ) * src_alpha >> 8 & 0xff00 );
1221
1279
}
1280
+ // Calculate the remainder of pixels (less than 4).
1222
1281
loops = end_x - start_x & 0x3 ;
1282
+
1283
+
1284
+ // If there are remaining pixels, paint them as well.
1223
1285
if (loops > 0 ) {
1224
1286
color = hsl2rgb [color_index >> 8 ];
1287
+
1288
+ // Apply alpha transparency again as we just re-retrieved the color
1225
1289
color = ((color & 0xff00ff ) * dest_alpha >> 8 & 0xff00ff ) + ((color & 0xff00 ) * dest_alpha >> 8 & 0xff00 );
1226
1290
do {
1227
1291
dest [dest_off ++] = color + ((dest [dest_off ] & 0xff00ff ) * src_alpha >> 8 & 0xff00ff ) + ((dest [dest_off ] & 0xff00 ) * src_alpha >> 8 & 0xff00 );
1228
1292
} while (--loops > 0 );
1229
1293
}
1230
1294
}
1231
1295
} else {
1296
+ // If the start of the line is before the end.
1232
1297
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.
1234
1302
if (restrict_edges ) {
1303
+ // Clip the line to the right edge of the viewport.
1235
1304
if (end_x > viewportRx ) {
1236
1305
end_x = viewportRx ;
1237
1306
}
1307
+
1308
+ // If the line starts before the left edge of the viewport, clip it and adjust the starting color.
1238
1309
if (start_x < 0 ) {
1239
- color_index -= start_x * i ;
1310
+ color_index -= start_x * color_slope ;
1240
1311
start_x = 0 ;
1241
1312
}
1313
+
1314
+ // If the start of the line is now after the end, there is nothing to draw.
1242
1315
if (start_x >= end_x ) {
1243
1316
return ;
1244
1317
}
1245
1318
}
1319
+
1320
+ // Adjust the offset into the destination array for the new starting point.
1246
1321
dest_off += start_x ;
1322
+ // Calculate the number of pixels to draw.
1247
1323
loops = end_x - start_x ;
1324
+
1325
+ // If the alpha is 0 (no transparency), the loop simply paints each pixel with the color.
1248
1326
if (alpha == 0 ) {
1249
1327
do {
1328
+ // Paint the pixel with the color corresponding to the current color index.
1250
1329
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)
1253
1333
} else {
1334
+ // If alpha is not 0 (there is some transparency), the pixels need to be blended with the destination.
1254
1335
int i_43_ = alpha ;
1255
1336
int i_44_ = 256 - alpha ;
1256
1337
do {
1338
+ // Calculate the color for the current pixel.
1257
1339
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.
1259
1345
color = ((color & 0xff00ff ) * i_44_ >> 8 & 0xff00ff ) + ((color & 0xff00 ) * i_44_ >> 8 & 0xff00 );
1346
+
1347
+ // Blend the color with the destination pixel.
1260
1348
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)
1262
1350
}
1263
1351
}
1264
1352
}
0 commit comments