Skip to content

Commit

Permalink
Fix adjoining transparent face rendering
Browse files Browse the repository at this point in the history
- Render both faces when two different transparent faces are next to
  each other.
  • Loading branch information
atxi committed Jan 18, 2023
1 parent 0ae548b commit e141c8d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
10 changes: 10 additions & 0 deletions polymer/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,16 @@ struct Vector4f {
}
};

struct BoundingBox {
Vector3f min;
Vector3f max;

inline bool Intersects(const BoundingBox& b) const {
return (min.x <= b.max.x && max.x >= b.min.x && min.y <= b.max.y && max.y >= b.min.y && min.z <= b.max.z &&
max.z >= b.min.z);
}
};

// Column major matrix4x4
struct mat4 {
float data[4][4];
Expand Down
16 changes: 6 additions & 10 deletions polymer/render/block_mesher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,8 @@ FaceQuad GetFaceQuad(BlockElement& element, BlockFace direction) {
inline bool IsOccluding(BlockModel* from, BlockModel* to, BlockFace face) {
BlockFace opposite_face = world::GetOppositeFace(face);

bool from_is_transparent = !HasOccludableFace(*from, face);
bool to_is_transparent = !HasOccludableFace(*to, opposite_face);

// TODO: Clean this up once rotation is settled.
if (to->element_count == 0) return false;
//if (from->has_variant_rotation || to->has_variant_rotation) return false;
if (to->has_leaves || !to->has_shaded) return false;

for (size_t i = 0; i < from->element_count; ++i) {
Expand All @@ -220,16 +216,16 @@ inline bool IsOccluding(BlockModel* from, BlockModel* to, BlockFace face) {
// Check if the element of the 'to' model fully occludes the 'from' face
if (to_start.x <= from_start.x && to_start.y <= from_start.y && to_start.z <= from_start.z &&
to_end.x >= from_end.x && to_end.y >= from_end.y && to_end.z >= from_end.z) {
if (to_is_transparent) {
if (from_is_transparent) {
return true;
if (to_face.transparency) {
if (from_face.transparency) {
return from_face.texture_id == to_face.texture_id;
}
return false;
}

if (from_is_transparent) {
if (to_is_transparent) {
return true;
if (from_face.transparency) {
if (to_face.transparency) {
return from_face.texture_id == to_face.texture_id;
}
return false;
}
Expand Down
25 changes: 25 additions & 0 deletions polymer/world/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ struct FaceQuad {
Vector2f br_uv;
Vector2f tl_uv;
Vector2f tr_uv;

inline BoundingBox GetBoundsAt(const Vector3f& v) {
BoundingBox result;

result.min.x = ChooseMin(bl_pos.x, br_pos.x, tl_pos.x, tr_pos.x) + v.x;
result.min.y = ChooseMin(bl_pos.y, br_pos.y, tl_pos.y, tr_pos.y) + v.y;

result.max.x = ChooseMax(bl_pos.x, br_pos.x, tl_pos.x, tr_pos.x) + v.x;
result.max.y = ChooseMax(bl_pos.y, br_pos.y, tl_pos.y, tr_pos.y) + v.y;

return result;
}

private:
inline static float ChooseMin(float x0, float x1, float x2, float x3) {
float m1 = x0 < x1 ? x0 : x1;
float m2 = x2 < x3 ? x2 : x3;
return m1 < m2 ? m1 : m2;
}

inline static float ChooseMax(float x0, float x1, float x2, float x3) {
float m1 = x0 > x1 ? x0 : x1;
float m2 = x2 > x3 ? x2 : x3;
return m1 > m2 ? m1 : m2;
}
};

struct RenderableFace {
Expand Down

0 comments on commit e141c8d

Please sign in to comment.