|
1 | 1 | package me.cortex.voxy.client.core.model.bakery; |
2 | 2 |
|
3 | 3 | 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; |
4 | 7 | import org.joml.Matrix4f; |
5 | 8 | import org.joml.Vector3f; |
6 | 9 | import org.joml.Vector4f; |
@@ -92,18 +95,17 @@ private void rasterQuad(Matrix4f transform, long addr) { |
92 | 95 | this.a1.set(this.qmuv1); |
93 | 96 | this.a2.set(this.qmuv2); |
94 | 97 | this.a3.set(this.qmuv3); |
95 | | - this.rasterTriangle(); |
| 98 | + this.rasterTriangle(false); |
96 | 99 | this.scratchR1.set(this.scratch3); |
97 | 100 | this.scratchR2.set(this.scratch4); |
98 | 101 | this.scratchR3.set(this.scratch1); |
99 | 102 | this.a1.set(this.qmuv3); |
100 | 103 | this.a2.set(this.qmuv4); |
101 | 104 | this.a3.set(this.qmuv1); |
102 | | - this.rasterTriangle(); |
103 | | - |
| 105 | + this.rasterTriangle(true); |
104 | 106 | } |
105 | 107 |
|
106 | | - private void rasterTriangle() { |
| 108 | + private void rasterTriangle(boolean orZero) { |
107 | 109 | Vector3f v1 = this.scratchR1; |
108 | 110 | Vector3f v2 = this.scratchR2; |
109 | 111 | Vector3f v3 = this.scratchR3; |
@@ -142,7 +144,7 @@ private void rasterTriangle() { |
142 | 144 | float w1 = edge(v2, v3, cx, cy)*invArea; |
143 | 145 | float w2 = edge(v3, v1, cx, cy)*invArea; |
144 | 146 | 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)) { |
146 | 148 | //Dont need to worry about perspective correction afak as it should already be all correct |
147 | 149 |
|
148 | 150 | //pixel is inside the triangle |
@@ -193,17 +195,39 @@ private void rasterPixel(int index, float b1, float b2, float b3) {//Barry coord |
193 | 195 | int srcColour = (int) this.framebuffer[index]; |
194 | 196 | this.framebuffer[index] &= ~Integer.toUnsignedLong(-1); |
195 | 197 |
|
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); |
198 | 198 | if (this.doTheBlending) {//Blending |
199 | 199 | //mutate colour var |
| 200 | + colour = doBlending(srcColour, colour); |
200 | 201 | } |
201 | 202 |
|
202 | 203 |
|
203 | 204 | //Remember ABGR FORMAT |
204 | 205 | this.framebuffer[index] |= Integer.toUnsignedLong(colour); |
205 | 206 | } |
206 | 207 |
|
| 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 | + |
207 | 231 | private static float edge(Vector3f a, Vector3f b, Vector3f c) { |
208 | 232 | return (c.x-a.x)*(b.y-a.y) - (c.y-a.y) * (b.x-a.x); |
209 | 233 | } |
|
0 commit comments