Skip to content

Commit 52bd5f4

Browse files
committed
attempted to add blending and fix overlapping triangle seam
1 parent f729e51 commit 52bd5f4

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

src/main/java/me/cortex/voxy/client/core/model/bakery/SoftwareRasterizer.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package me.cortex.voxy.client.core.model.bakery;
22

33
import me.cortex.voxy.client.core.model.ModelFactory;
4+
import net.caffeinemc.mods.sodium.api.util.ColorABGR;
5+
import net.caffeinemc.mods.sodium.api.util.ColorARGB;
6+
import net.caffeinemc.mods.sodium.api.util.ColorMixer;
47
import org.joml.Matrix4f;
58
import org.joml.Vector3f;
69
import org.joml.Vector4f;
@@ -92,18 +95,17 @@ private void rasterQuad(Matrix4f transform, long addr) {
9295
this.a1.set(this.qmuv1);
9396
this.a2.set(this.qmuv2);
9497
this.a3.set(this.qmuv3);
95-
this.rasterTriangle();
98+
this.rasterTriangle(false);
9699
this.scratchR1.set(this.scratch3);
97100
this.scratchR2.set(this.scratch4);
98101
this.scratchR3.set(this.scratch1);
99102
this.a1.set(this.qmuv3);
100103
this.a2.set(this.qmuv4);
101104
this.a3.set(this.qmuv1);
102-
this.rasterTriangle();
103-
105+
this.rasterTriangle(true);
104106
}
105107

106-
private void rasterTriangle() {
108+
private void rasterTriangle(boolean orZero) {
107109
Vector3f v1 = this.scratchR1;
108110
Vector3f v2 = this.scratchR2;
109111
Vector3f v3 = this.scratchR3;
@@ -142,7 +144,7 @@ private void rasterTriangle() {
142144
float w1 = edge(v2, v3, cx, cy)*invArea;
143145
float w2 = edge(v3, v1, cx, cy)*invArea;
144146
float w3 = 1.0f-w1-w2;
145-
if (w1>=0.0f&&w2>=0.0f&&w3>=0.0f) {
147+
if ((w1>0.0f&&w2>0.0f&&w3>0.0f)||(orZero&&w1>=0.0f&&w2>=0.0f&&w3>=0.0f)) {
146148
//Dont need to worry about perspective correction afak as it should already be all correct
147149

148150
//pixel is inside the triangle
@@ -193,17 +195,39 @@ private void rasterPixel(int index, float b1, float b2, float b3) {//Barry coord
193195
int srcColour = (int) this.framebuffer[index];
194196
this.framebuffer[index] &= ~Integer.toUnsignedLong(-1);
195197

196-
//When blending is enabled do this
197-
// ARBDrawBuffersBlend.glBlendFuncSeparateiARB(0, GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
198198
if (this.doTheBlending) {//Blending
199199
//mutate colour var
200+
colour = doBlending(srcColour, colour);
200201
}
201202

202203

203204
//Remember ABGR FORMAT
204205
this.framebuffer[index] |= Integer.toUnsignedLong(colour);
205206
}
206207

208+
209+
// ARBDrawBuffersBlend.glBlendFuncSeparateiARB(0, GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
210+
private static int doBlending(int scr, int dst) {
211+
int srcAlpha = (scr>>>24)&0xFF;
212+
if (srcAlpha == 0) {
213+
return dst;
214+
}
215+
int dstAlpha = (dst>>>24)&0xFF;
216+
scr &= ~(0xFF<<24);
217+
dst &= ~(0xFF<<24);
218+
int blendAlpha = Math.min(0xFF,srcAlpha+((dstAlpha*(255-srcAlpha))>>8));
219+
//how much did we actually get
220+
221+
int blend = ColorMixer.mix(dst, scr, dstAlpha);//addRGB(ColorABGR.mulRGB(scr, 255-dstAlpha),ColorABGR.mulRGB(dst, dstAlpha));
222+
return blend|(blendAlpha<<24);
223+
}
224+
225+
private static int addRGB(int a, int b) {
226+
return Math.min(0xFF,(a&0xFF)+(b&0xFF))|
227+
Math.min((0xFF<<8),(a&(0xFF<<8))+(b&(0xFF<<8)))|
228+
Math.min((0xFF<<16),(a&(0xFF<<16))+(b&(0xFF<<16)));
229+
}
230+
207231
private static float edge(Vector3f a, Vector3f b, Vector3f c) {
208232
return (c.x-a.x)*(b.y-a.y) - (c.y-a.y) * (b.x-a.x);
209233
}

0 commit comments

Comments
 (0)