Skip to content

Commit

Permalink
Filter degenerate quads, reducing total quad count in the base game f…
Browse files Browse the repository at this point in the history
…rom 266 to 250.

Degenerate quads were mainly created from the fence models.

Also fixed a bug with connections between fences.

fixes PixelGuys#280
  • Loading branch information
IntegratedQuantum committed Jul 4, 2024
1 parent be0406d commit 212b2f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/models.zig
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,19 @@ pub const Model = struct {
for(&quad.corners) |*corner| {
corner.* -= quad.normal;
}
const quadIndex = addQuad(quad);
const quadIndex = addQuad(quad) catch continue;
self.neighborFacingQuads[neighbor][indices[neighbor]] = quadIndex;
indices[neighbor] += 1;
} else {
const quadIndex = addQuad(quad);
const quadIndex = addQuad(quad) catch continue;
self.internalQuads[internalIndex] = quadIndex;
internalIndex += 1;
}
}
for(0..6) |i| {
self.neighborFacingQuads[i] = main.globalAllocator.realloc(self.neighborFacingQuads[i], indices[i]);
}
self.internalQuads = main.globalAllocator.realloc(self.internalQuads, internalIndex);
self.hasNeighborFacingQuads = false;
self.allNeighborsOccluded = true;
self.noNeighborsOccluded = true;
Expand Down Expand Up @@ -222,10 +226,18 @@ pub var fullCube: u16 = undefined;

var quadDeduplication: std.AutoHashMap([@sizeOf(QuadInfo)]u8, u16) = undefined;

fn addQuad(info: QuadInfo) u16 {
fn addQuad(info: QuadInfo) error{Degenerate}!u16 {
if(quadDeduplication.get(std.mem.toBytes(info))) |id| {
return id;
}
// Check if it's degenerate:
var cornerEqualities: u32 = 0;
for(0..4) |i| {
for(i+1..4) |j| {
if(@reduce(.And, info.corners[i] == info.corners[j])) cornerEqualities += 1;
}
}
if(cornerEqualities >= 2) return error.Degenerate; // One corner equality is fine, since then the quad degenerates to a triangle, which has a non-zero area.
const index: u16 = @intCast(quads.items.len);
quads.append(info);
quadDeduplication.put(std.mem.toBytes(info), index) catch unreachable;
Expand Down
5 changes: 3 additions & 2 deletions src/rotation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,10 @@ pub const RotationModes = struct {
}

pub fn updateData(block: *Block, neighborIndex: u3, neighbor: Block) bool {
const blockModel = blocks.meshes.modelIndexStart(block.*);
const blockBaseModel = blocks.meshes.modelIndexStart(block.*);
const neighborBaseModel = blocks.meshes.modelIndexStart(neighbor);
const neighborModel = blocks.meshes.model(neighbor);
const targetVal = neighbor.solid() and (blockModel == neighborModel or main.models.models.items[neighborModel].neighborFacingQuads[neighborIndex ^ 1].len != 0);
const targetVal = neighbor.solid() and (blockBaseModel == neighborBaseModel or main.models.models.items[neighborModel].neighborFacingQuads[neighborIndex ^ 1].len != 0);
var currentData: FenceData = @bitCast(@as(u4, @truncate(block.data)));
switch(neighborIndex) {
Neighbors.dirNegX => {
Expand Down

0 comments on commit 212b2f5

Please sign in to comment.