@@ -75,45 +75,68 @@ public class Rasterizer3D extends Rasterizer {
75
75
}
76
76
}
77
77
78
- public static void drawScanLine (int [] dest , int destOffset , int loops , int startX , int endX ) {
78
+ public static void drawScanLine (int [] dest , int destOffset , int color , int startX , int endX ) {
79
+ // restrict_edges indicates if there's a need to restrict the drawing operation within certain boundaries (viewport)
79
80
if (restrict_edges ) {
81
+ // If the ending point is beyond the viewport's right boundary, clip it to the boundary
80
82
if (endX > viewportRx ) {
81
83
endX = viewportRx ;
82
84
}
85
+
86
+ // If the starting point is before the viewport's left boundary, clip it to the boundary
83
87
if (startX < 0 ) {
84
88
startX = 0 ;
85
89
}
86
90
}
91
+
92
+ // If the start point is after or at the end point, there's no need to draw, return
87
93
if (startX >= endX ) {
88
94
return ;
89
95
}
96
+
97
+ // Update the destination array offset with the start point
90
98
destOffset += startX ;
91
- int rgba = endX - startX >> 2 ;
99
+
100
+ // Calculate the number of times the loop will run for 4-pixel chunks
101
+ int loops = endX - startX >> 2 ;
102
+
103
+ // If the alpha is 0 (no transparency), the loop simply draws each pixel with the provided color.
92
104
if (alpha == 0 ) {
93
- while (--rgba >= 0 ) {
94
- dest [destOffset ++] = loops ;
95
- dest [destOffset ++] = loops ;
96
- dest [destOffset ++] = loops ;
97
- dest [destOffset ++] = loops ;
105
+ // Draw the color 4 times, for each pixel in the 4-pixel chunk
106
+ while (--loops >= 0 ) {
107
+ dest [destOffset ++] = color ;
108
+ dest [destOffset ++] = color ;
109
+ dest [destOffset ++] = color ;
110
+ dest [destOffset ++] = color ;
98
111
}
99
- rgba = endX - startX & 0x3 ;
100
- while (--rgba >= 0 ) {
101
- dest [destOffset ++] = loops ;
112
+
113
+ // For the remaining pixels, draw them one by one
114
+ loops = endX - startX & 0x3 ;
115
+ while (--loops >= 0 ) {
116
+ dest [destOffset ++] = color ;
102
117
}
103
118
return ;
104
119
}
120
+
121
+ // If alpha is not 0 (there is some transparency), the pixels need to be blended with the destination.
105
122
int destAlpha = alpha ;
106
123
int sourceAlpha = 256 - alpha ;
107
- loops = ((loops & 0xff00ff ) * sourceAlpha >> 8 & 0xff00ff ) + ((loops & 0xff00 ) * sourceAlpha >> 8 & 0xff00 );
108
- while (--rgba >= 0 ) {
109
- dest [destOffset ++] = loops + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
110
- dest [destOffset ++] = loops + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
111
- dest [destOffset ++] = loops + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
112
- dest [destOffset ++] = loops + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
124
+
125
+ // Pre-calculate the source color multiplied by its alpha
126
+ color = ((color & 0xff00ff ) * sourceAlpha >> 8 & 0xff00ff ) + ((color & 0xff00 ) * sourceAlpha >> 8 & 0xff00 );
127
+
128
+ // For each 4-pixel chunk, blend the source color with the destination color
129
+ while (--loops >= 0 ) {
130
+ dest [destOffset ++] = color + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
131
+ dest [destOffset ++] = color + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
132
+ dest [destOffset ++] = color + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
133
+ dest [destOffset ++] = color + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
113
134
}
114
- rgba = endX - startX & 0x3 ;
115
- while (--rgba >= 0 ) {
116
- dest [destOffset ++] = loops + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
135
+
136
+ // For the remaining pixels, blend them one by one
137
+ loops = endX - startX & 0x3 ;
138
+ while (--loops >= 0 ) {
139
+ dest [destOffset ++] = color + ((dest [destOffset ] & 0xff00ff ) * destAlpha >> 8 & 0xff00ff ) + ((dest [destOffset ] & 0xff00 ) * destAlpha >> 8 & 0xff00 );
117
140
}
118
141
}
119
142
0 commit comments