correct bilinear interpolation for ambient occlusion #29

Closed
rdh wants to merge 1 commits from rdh/celeritas:stonecutter into stonecutter
pull from: rdh/celeritas:stonecutter
8 changed files with 65 additions and 5 deletions

No files matched your search

@@ -32,6 +32,8 @@ public class ChunkMeshBufferBuilder {
} }
public void push(ChunkVertexEncoder.Vertex[] vertices, Material material) { public void push(ChunkVertexEncoder.Vertex[] vertices, Material material) {
prepareBilinearCorrection(vertices);
var vertexStart = this.count * this.stride; var vertexStart = this.count * this.stride;
var vertexSize = vertices.length * this.stride; var vertexSize = vertices.length * this.stride;
@@ -54,6 +56,34 @@ public class ChunkMeshBufferBuilder {
this.count += vertices.length; this.count += vertices.length;
} }
private static void prepareBilinearCorrection(ChunkVertexEncoder.Vertex[] vertices) {
if (vertices.length != 4) {
for (var vertex : vertices) {
vertex.rdhFactor = 0;
}
return;
}
int color0 = vertices[0].color;
int color1 = vertices[1].color;
int color2 = vertices[2].color;
int color3 = vertices[3].color;
int factor = encodeBilinearCorrection(color3, color1, color0, color2, 0)
| encodeBilinearCorrection(color3, color1, color0, color2, 8) << 8
| encodeBilinearCorrection(color3, color1, color0, color2, 16) << 16
| encodeBilinearCorrection(color3, color1, color0, color2, 24) << 24;
for (var vertex : vertices) {
vertex.rdhFactor = factor;
}
}
private static int encodeBilinearCorrection(int positive0, int positive1, int negative0, int negative1, int shift) {
int factor = ((positive0 >> shift) & 0xFF) + ((positive1 >> shift) & 0xFF)
- ((negative0 >> shift) & 0xFF) - ((negative1 >> shift) & 0xFF);
return Math.round(factor * (127.0F / 510.0F)) & 0xFF;
}
private void grow(int bytesNeeded) { private void grow(int bytesNeeded) {
// Grow by a factor of 2, or by however many bytes more we need, whichever is larger. // Grow by a factor of 2, or by however many bytes more we need, whichever is larger.
int newCapacity = Math.max(this.capacity * 2, this.capacity + bytesNeeded); int newCapacity = Math.max(this.capacity * 2, this.capacity + bytesNeeded);
@@ -10,6 +10,7 @@ public interface ChunkVertexEncoder {
public float y; public float y;
public float z; public float z;
public int color; public int color;
public int rdhFactor;
public float u; public float u;
public float v; public float v;
public int light; public int light;
@@ -10,14 +10,15 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class CompactChunkVertex implements ChunkVertexType { public class CompactChunkVertex implements ChunkVertexType {
public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(20) public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(24)
.addElement("a_PosId", 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 4, false, true) .addElement("a_PosId", 0, GlVertexAttributeFormat.UNSIGNED_SHORT, 4, false, true)
.addElement("a_Color", 8, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false) .addElement("a_Color", 8, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false)
.addElement("a_TexCoord", 12, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) .addElement("a_TexCoord", 12, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)
.addElement("a_LightCoord", 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, true) .addElement("a_LightCoord", 16, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, true)
.addElement("a_RdhFactor", 20, GlVertexAttributeFormat.BYTE, 4, true, false)
.build(); .build();
public static final int STRIDE = 20; public static final int STRIDE = 24;
private static final int POSITION_MAX_VALUE = 65536; private static final int POSITION_MAX_VALUE = 65536;
private static final int TEXTURE_MAX_VALUE = 32768; private static final int TEXTURE_MAX_VALUE = 32768;
@@ -65,6 +66,7 @@ public class CompactChunkVertex implements ChunkVertexType {
LWJGL.memPutShort(ptr + 14, encodeTexture(vertex.v)); LWJGL.memPutShort(ptr + 14, encodeTexture(vertex.v));
LWJGL.memPutInt(ptr + 16, vertex.light); LWJGL.memPutInt(ptr + 16, vertex.light);
LWJGL.memPutInt(ptr + 20, vertex.rdhFactor);
return ptr + STRIDE; return ptr + STRIDE;
}; };
@@ -12,13 +12,14 @@ import static org.taumc.celeritas.lwjgl.LWJGLServiceProvider.LWJGL;
* compatible with mods & resource packs that need high precision for models. * compatible with mods & resource packs that need high precision for models.
*/ */
public class VanillaLikeChunkVertex implements ChunkVertexType { public class VanillaLikeChunkVertex implements ChunkVertexType {
public static final int STRIDE = 28; public static final int STRIDE = 32;
public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE) public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE)
.addElement("a_PosId", 0, GlVertexAttributeFormat.FLOAT, 3, false, false) .addElement("a_PosId", 0, GlVertexAttributeFormat.FLOAT, 3, false, false)
.addElement("a_Color", 12, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false) .addElement("a_Color", 12, GlVertexAttributeFormat.UNSIGNED_BYTE, 4, true, false)
.addElement("a_TexCoord", 16, GlVertexAttributeFormat.FLOAT, 2, false, false) .addElement("a_TexCoord", 16, GlVertexAttributeFormat.FLOAT, 2, false, false)
.addElement("a_LightCoord", 24, GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true) .addElement("a_LightCoord", 24, GlVertexAttributeFormat.UNSIGNED_INT, 1, false, true)
.addElement("a_RdhFactor", 28, GlVertexAttributeFormat.BYTE, 4, true, false)
.build(); .build();
@Override @Override
@@ -51,6 +52,7 @@ public class VanillaLikeChunkVertex implements ChunkVertexType {
LWJGL.memPutFloat(ptr + 16, encodeTexture(vertex.u)); LWJGL.memPutFloat(ptr + 16, encodeTexture(vertex.u));
LWJGL.memPutFloat(ptr + 20, encodeTexture(vertex.v)); LWJGL.memPutFloat(ptr + 20, encodeTexture(vertex.v));
LWJGL.memPutInt(ptr + 24, (encodeDrawParameters(material, sectionIndex) << 0) | (encodeLight(vertex.light) << 16)); LWJGL.memPutInt(ptr + 24, (encodeDrawParameters(material, sectionIndex) << 0) | (encodeLight(vertex.light) << 16));
LWJGL.memPutInt(ptr + 28, vertex.rdhFactor);
return ptr + STRIDE; return ptr + STRIDE;
}; };
@@ -3,6 +3,9 @@
#import <sodium:include/fog.glsl> #import <sodium:include/fog.glsl>
in vec4 v_Color; in vec4 v_Color;
in vec4 v_RdhFactor;
in vec2 v_QuadCoord;
in vec3 v_LightColor;
in vec2 v_TexCoord; in vec2 v_TexCoord;
#if defined(USE_FOG) && defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0 #if defined(USE_FOG) && defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0
@@ -57,6 +60,11 @@ void main() {
#endif #endif
vec4 m_color = v_Color; vec4 m_color = v_Color;
float correctionWeight = min(v_QuadCoord.x, v_QuadCoord.y)
* (1.0 - max(v_QuadCoord.x, v_QuadCoord.y));
vec4 correction = v_RdhFactor * correctionWeight;
correction.rgb *= v_LightColor;
m_color += correction;
#ifdef USE_VANILLA_COLOR_FORMAT #ifdef USE_VANILLA_COLOR_FORMAT
// Apply per-vertex color. AO shade is applied ahead of time on the CPU. // Apply per-vertex color. AO shade is applied ahead of time on the CPU.
@@ -6,6 +6,9 @@
#import <sodium:include/chunk_material.glsl> #import <sodium:include/chunk_material.glsl>
out vec4 v_Color; out vec4 v_Color;
out vec4 v_RdhFactor;
out vec2 v_QuadCoord;
out vec3 v_LightColor;
out vec2 v_TexCoord; out vec2 v_TexCoord;
#if defined(USE_FOG) && defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0 #if defined(USE_FOG) && defined(CHUNK_FADE_IN_DURATION_MS) && CHUNK_FADE_IN_DURATION_MS > 0
@@ -60,9 +63,16 @@ void main() {
// Add the light color to the vertex color, and pass the texture coordinates to the fragment shader // Add the light color to the vertex color, and pass the texture coordinates to the fragment shader
#ifdef CELERITAS_NO_LIGHTMAP #ifdef CELERITAS_NO_LIGHTMAP
v_Color = _vert_color; v_Color = _vert_color;
v_LightColor = vec3(1.0);
#else #else
v_Color = _vert_color * _sample_lightmap(u_LightTex, _vert_tex_light_coord); vec4 lightColor = _sample_lightmap(u_LightTex, _vert_tex_light_coord);
v_Color = _vert_color * lightColor;
v_LightColor = lightColor.rgb;
#endif #endif
v_RdhFactor = _vert_rdh_factor * 2.0;
int corner = gl_VertexID & 3;
v_QuadCoord = vec2(corner >= 2 ? 1.0 : 0.0,
corner == 1 || corner == 2 ? 1.0 : 0.0);
v_TexCoord = _vert_tex_diffuse_coord; v_TexCoord = _vert_tex_diffuse_coord;
v_MaterialMipBias = _material_mip_bias(_material_params); v_MaterialMipBias = _material_mip_bias(_material_params);
@@ -10,6 +10,9 @@ ivec2 _vert_tex_light_coord;
// The color of the vertex // The color of the vertex
vec4 _vert_color; vec4 _vert_color;
// The "rdh factor" of the vertex, used when applying AO correction
vec4 _vert_rdh_factor;
// The index of the draw command which this vertex belongs to // The index of the draw command which this vertex belongs to
uint _draw_id; uint _draw_id;
@@ -32,6 +35,7 @@ in uvec4 a_PosId;
in vec4 a_Color; in vec4 a_Color;
in vec2 a_TexCoord; in vec2 a_TexCoord;
in ivec2 a_LightCoord; in ivec2 a_LightCoord;
in vec4 a_RdhFactor;
#if !defined(VERT_POS_SCALE) #if !defined(VERT_POS_SCALE)
#error "VERT_POS_SCALE not defined" #error "VERT_POS_SCALE not defined"
@@ -46,6 +50,7 @@ void _vert_init() {
_vert_tex_diffuse_coord = (a_TexCoord * VERT_TEX_SCALE); _vert_tex_diffuse_coord = (a_TexCoord * VERT_TEX_SCALE);
_vert_tex_light_coord = a_LightCoord; _vert_tex_light_coord = a_LightCoord;
_vert_color = a_Color; _vert_color = a_Color;
_vert_rdh_factor = a_RdhFactor;
_draw_id = (a_PosId.w >> 8u) & 0xFFu; _draw_id = (a_PosId.w >> 8u) & 0xFFu;
_material_params = (a_PosId.w >> 0u) & 0xFFu; _material_params = (a_PosId.w >> 0u) & 0xFFu;
@@ -57,11 +62,13 @@ in vec3 a_PosId;
in vec4 a_Color; in vec4 a_Color;
in vec2 a_TexCoord; in vec2 a_TexCoord;
in uint a_LightCoord; in uint a_LightCoord;
in vec4 a_RdhFactor;
void _vert_init() { void _vert_init() {
_vert_position = a_PosId; _vert_position = a_PosId;
_vert_tex_diffuse_coord = a_TexCoord; _vert_tex_diffuse_coord = a_TexCoord;
_vert_color = a_Color; _vert_color = a_Color;
_vert_rdh_factor = a_RdhFactor;
uint packed_draw_params = (a_LightCoord & 0xFFFFu); uint packed_draw_params = (a_LightCoord & 0xFFFFu);
// Vertex Material // Vertex Material
@@ -12,7 +12,7 @@ import org.embeddedt.embeddium.impl.render.chunk.vertex.format.ChunkVertexType;
public class XHFPModelVertexType implements ChunkVertexType { public class XHFPModelVertexType implements ChunkVertexType {
public static final ChunkVertexType BASE_VERTEX_TYPE = ChunkMeshFormats.VANILLA_LIKE; public static final ChunkVertexType BASE_VERTEX_TYPE = ChunkMeshFormats.VANILLA_LIKE;
public static final int STRIDE = 48; public static final int STRIDE = 52;
public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE) public static final GlVertexFormat VERTEX_FORMAT = GlVertexFormat.builder(STRIDE)
.addAllElements(BASE_VERTEX_TYPE.getVertexFormat()) .addAllElements(BASE_VERTEX_TYPE.getVertexFormat())
.addElement("mc_midTexCoord", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false) .addElement("mc_midTexCoord", GlVertexFormat.NEXT_ALIGNED_POINTER, GlVertexAttributeFormat.UNSIGNED_SHORT, 2, false, false)