fix face orientation

This commit is contained in:
catAngent 2024-09-20 10:03:22 +01:00
parent e560c0de6b
commit 5a76cdcd13
6 changed files with 71 additions and 39 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

Binary file not shown.

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Before After
Before After

View file

@ -74,7 +74,7 @@ pub fn main() !void {
const xt: i32 = @as(i32, @intCast(x)) - 16;
const yt: i32 = @as(i32, @intCast(y)) - 16;
const zt: i32 = @as(i32, @intCast(z)) - 16;
if (xt * xt + yt * yt + zt * zt < 15 * 15) chunk.setTile(x, y, z, 1);
if (xt * xt + yt * yt + zt * zt < 15 * 15) chunk.setTile(x, y, z, if (2 * xt > yt - 30) 1 else 2);
};
if (benchmark_chunk_meshing) {

View file

@ -95,16 +95,48 @@ pub const Chunk = struct {
const zmax: f32 = @as(f32, @floatFromInt(z2)) + 0.5;
const yleft: f32 = if (sign == 1) ymin else ymax;
const yright: f32 = if (sign == 1) ymax else ymin;
const raw_quad: RawQuad = .{
.tile = surface,
.top_left = v3.newShifted(xf + 0.5 * sign, yleft, zmin, d),
.top_right = v3.newShifted(xf + 0.5 * sign, yright, zmin, d),
.bottom_left = v3.newShifted(xf + 0.5 * sign, yleft, zmax, d),
.bottom_right = v3.newShifted(xf + 0.5 * sign, yright, zmax, d),
.normal = v3.newShifted(sign, 0, 0, d),
.width = ymax - ymin,
.height = zmax - zmin,
};
const zleft: f32 = if (sign == 1) zmin else zmax;
const zright: f32 = if (sign == 1) zmax else zmin;
var raw_quad: RawQuad = undefined;
switch (d) {
0 => {
raw_quad = .{
.tile = surface,
.top_left = v3.new(xf + 0.5 * sign, ymax, zright),
.top_right = v3.new(xf + 0.5 * sign, ymax, zleft),
.bottom_left = v3.new(xf + 0.5 * sign, ymin, zright),
.bottom_right = v3.new(xf + 0.5 * sign, ymin, zleft),
.normal = v3.new(sign, 0, 0),
.width = zmax - zmin,
.height = ymax - ymin,
};
},
1 => {
raw_quad = .{
.tile = surface,
.bottom_left = v3.new(yleft, zmin, xf + 0.5 * sign),
.top_left = v3.new(yleft, zmax, xf + 0.5 * sign),
.bottom_right = v3.new(yright, zmin, xf + 0.5 * sign),
.top_right = v3.new(yright, zmax, xf + 0.5 * sign),
.normal = v3.new(0, 0, sign),
.height = zmax - zmin,
.width = ymax - ymin,
};
},
2 => {
raw_quad = .{
.tile = surface,
.top_left = v3.new(zleft, xf + 0.5 * sign, ymin),
.top_right = v3.new(zright, xf + 0.5 * sign, ymin),
.bottom_left = v3.new(zleft, xf + 0.5 * sign, ymax),
.bottom_right = v3.new(zright, xf + 0.5 * sign, ymax),
.normal = v3.new(0, sign, 0),
.width = zmax - zmin,
.height = ymax - ymin,
};
},
else => unreachable,
}
try raw_quads.append(raw_quad);
};
}
@ -143,21 +175,21 @@ pub const Chunk = struct {
texcoords2[12 * i + 0] = 0.0;
texcoords2[12 * i + 1] = 0.0;
vertices[18 * i + 3] = raw_quad.top_right.x;
vertices[18 * i + 4] = raw_quad.top_right.y;
vertices[18 * i + 5] = raw_quad.top_right.z;
texcoords[12 * i + 2] = right_uv;
texcoords[12 * i + 3] = top_uv;
texcoords2[12 * i + 2] = raw_quad.width;
texcoords2[12 * i + 3] = 0.0;
vertices[18 * i + 3] = raw_quad.bottom_left.x;
vertices[18 * i + 4] = raw_quad.bottom_left.y;
vertices[18 * i + 5] = raw_quad.bottom_left.z;
texcoords[12 * i + 2] = left_uv;
texcoords[12 * i + 3] = bottom_uv;
texcoords2[12 * i + 2] = 0.0;
texcoords2[12 * i + 3] = raw_quad.height;
vertices[18 * i + 6] = raw_quad.bottom_left.x;
vertices[18 * i + 7] = raw_quad.bottom_left.y;
vertices[18 * i + 8] = raw_quad.bottom_left.z;
texcoords[12 * i + 4] = left_uv;
texcoords[12 * i + 5] = bottom_uv;
texcoords2[12 * i + 4] = 0.0;
texcoords2[12 * i + 5] = raw_quad.height;
vertices[18 * i + 6] = raw_quad.top_right.x;
vertices[18 * i + 7] = raw_quad.top_right.y;
vertices[18 * i + 8] = raw_quad.top_right.z;
texcoords[12 * i + 4] = right_uv;
texcoords[12 * i + 5] = top_uv;
texcoords2[12 * i + 4] = raw_quad.width;
texcoords2[12 * i + 5] = 0.0;
vertices[18 * i + 9] = raw_quad.bottom_right.x;
vertices[18 * i + 10] = raw_quad.bottom_right.y;
@ -167,21 +199,21 @@ pub const Chunk = struct {
texcoords2[12 * i + 6] = raw_quad.width;
texcoords2[12 * i + 7] = raw_quad.height;
vertices[18 * i + 12] = raw_quad.bottom_left.x;
vertices[18 * i + 13] = raw_quad.bottom_left.y;
vertices[18 * i + 14] = raw_quad.bottom_left.z;
texcoords[12 * i + 8] = left_uv;
texcoords[12 * i + 9] = bottom_uv;
texcoords2[12 * i + 8] = 0.0;
texcoords2[12 * i + 9] = raw_quad.height;
vertices[18 * i + 12] = raw_quad.top_right.x;
vertices[18 * i + 13] = raw_quad.top_right.y;
vertices[18 * i + 14] = raw_quad.top_right.z;
texcoords[12 * i + 8] = right_uv;
texcoords[12 * i + 9] = top_uv;
texcoords2[12 * i + 8] = raw_quad.width;
texcoords2[12 * i + 9] = 0.0;
vertices[18 * i + 15] = raw_quad.top_right.x;
vertices[18 * i + 16] = raw_quad.top_right.y;
vertices[18 * i + 17] = raw_quad.top_right.z;
texcoords[12 * i + 10] = right_uv;
texcoords[12 * i + 11] = top_uv;
texcoords2[12 * i + 10] = raw_quad.width;
texcoords2[12 * i + 11] = 0.0;
vertices[18 * i + 15] = raw_quad.bottom_left.x;
vertices[18 * i + 16] = raw_quad.bottom_left.y;
vertices[18 * i + 17] = raw_quad.bottom_left.z;
texcoords[12 * i + 10] = left_uv;
texcoords[12 * i + 11] = bottom_uv;
texcoords2[12 * i + 10] = 0.0;
texcoords2[12 * i + 11] = raw_quad.height;
}
var mesh = raylib.Mesh{