1313import java .util .Random ;
1414
1515public class SoftwareRasterizer {
16- public static final int TARGET_SIZE = ModelFactory .MODEL_TEXTURE_SIZE ;
17-
1816 private final Vector4f scratch = new Vector4f ();
1917
2018 private final Vector3f scratch1 = new Vector3f ();
@@ -38,7 +36,9 @@ public class SoftwareRasterizer {
3836
3937 private static final long DEPTH_MASK = ((1L <<24 )-1 )<<(64 -24 );
4038 private static final long CLEAR_VALUE = DEPTH_MASK ;//set the depth to max value and rest of bits to 0
41- private final long [] framebuffer = new long [TARGET_SIZE *TARGET_SIZE ];
39+
40+ private final int targetSize ;
41+ private final long [] framebuffer ;
4242
4343 private boolean cullBackFace ;
4444 private boolean doTheBlending ;
@@ -47,7 +47,9 @@ public class SoftwareRasterizer {
4747 private int samplerHeight ;
4848 private int [] samplerTexture ;
4949
50- public SoftwareRasterizer () {
50+ public SoftwareRasterizer (int targetSize ) {
51+ this .targetSize = targetSize ;
52+ this .framebuffer = new long [targetSize *targetSize ];
5153 }
5254
5355 public void setFaceCull (boolean isBackFaceCulling ) {
@@ -71,12 +73,17 @@ private int sampleTexture(float u, float v) {
7173 return this .samplerTexture [this .samplerWidth *pv +pu ];
7274 }
7375
74- public void raster ( Matrix4f mvp , ReuseVertexConsumer vertices ) {
76+ public void clear ( ) {
7577 Arrays .fill (this .framebuffer , CLEAR_VALUE );
78+ }
7679
77- int qc = vertices .quadCount ();
78- for (int i = 0 ; i < qc ; i ++) {
79- this .rasterQuad (mvp , vertices .getAddress ()+ReuseVertexConsumer .VERTEX_FORMAT_SIZE *4 *i );
80+ public void raster (Matrix4f mvp , ReuseVertexConsumer vertices ) {
81+ this .raster (mvp , vertices .getAddress (), vertices .quadCount ());
82+ }
83+ public void raster (Matrix4f mvp , long verticesAddr , int quadCount ) {
84+ if (quadCount == 0 ) return ;
85+ for (int i = 0 ; i < quadCount ; i ++) {
86+ this .rasterQuad (mvp , verticesAddr +ReuseVertexConsumer .VERTEX_FORMAT_SIZE *4L *i );
8087 }
8188 //Arrays.fill(this.framebuffer, -1);
8289 }
@@ -132,9 +139,9 @@ private void rasterTriangle(boolean orZero) {
132139 }*/
133140
134141 int minX = Math .max ((int ) Math .floor (Math .min (Math .min (v1 .x , v2 .x ), v3 .x )), 0 );
135- int maxX = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .x , v2 .x ), v3 .x )), TARGET_SIZE -1 );
142+ int maxX = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .x , v2 .x ), v3 .x )), this . targetSize -1 );
136143 int minY = Math .max ((int ) Math .floor (Math .min (Math .min (v1 .y , v2 .y ), v3 .y )), 0 );
137- int maxY = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .y , v2 .y ), v3 .y )), TARGET_SIZE -1 );
144+ int maxY = Math .min ((int ) Math .ceil (Math .max (Math .max (v1 .y , v2 .y ), v3 .y )), this . targetSize -1 );
138145
139146 float invArea = 1.0f /area ;
140147 for (int py = minY ; py <=maxY ; py ++) {
@@ -148,7 +155,7 @@ private void rasterTriangle(boolean orZero) {
148155 //Dont need to worry about perspective correction afak as it should already be all correct
149156
150157 //pixel is inside the triangle
151- this .rasterPixel (px +py *TARGET_SIZE , w1 , w2 , w3 );
158+ this .rasterPixel (px +py *this . targetSize , w1 , w2 , w3 );
152159 }
153160 }
154161 }
@@ -171,6 +178,7 @@ private void rasterPixel(int index, float b1, float b2, float b3) {//Barry coord
171178
172179
173180 final int ALPHA_CUTOFF_THRESHOLD = 0 ;
181+ //TODO: meta&1 OR if we are blending
174182 if ((meta &1 )!=0 && (colour >>>24 )<=ALPHA_CUTOFF_THRESHOLD ) {//Discard on small alpha
175183 return ;
176184 }
@@ -244,7 +252,7 @@ private void loadTransformPos(Matrix4f transform, long addr, int vert, Vector3f
244252 var vec = transform .transformProject (this .scratch );
245253 if (Math .abs (this .scratch .w -1.0f )>0.000001f )
246254 throw new IllegalStateException ();
247- out .set (maintainPrecision (Math .fma (vec .x , 0.5f , 0.5f )*TARGET_SIZE ), maintainPrecision (Math .fma (vec .y , 0.5f , 0.5f )*TARGET_SIZE ), vec .z );//TODO: dont know if z transform is correct
255+ out .set (maintainPrecision (Math .fma (vec .x , 0.5f , 0.5f )*this . targetSize ), maintainPrecision (Math .fma (vec .y , 0.5f , 0.5f )*this . targetSize ), vec .z );//TODO: dont know if z transform is correct
248256 }
249257
250258
0 commit comments