Skip to content

Commit 795e31c

Browse files
authored
feat(rasterization): adjusted bounding box + backface culling
Triangle rasterization adjustments + dependency bump
2 parents 11eebc2 + 2b209e1 commit 795e31c

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ module github.com/Patch2PDF/Stagemodel-Rasterizer
33
go 1.25.6
44

55
require (
6-
github.com/Patch2PDF/GDTF-Mesh-Reader/v2 v2.1.0
7-
github.com/Patch2PDF/GDTF-Parser v0.4.0
8-
github.com/Patch2PDF/MVR-Parser v0.3.0
6+
github.com/Patch2PDF/GDTF-Mesh-Reader/v2 v2.2.0
7+
github.com/Patch2PDF/GDTF-Parser v0.4.1
8+
github.com/Patch2PDF/MVR-Parser v0.3.1
99
)
1010

1111
require golang.org/x/text v0.34.0 // indirect
1212

1313
require (
1414
github.com/qmuntal/gltf v0.28.0 // indirect
1515
golang.org/x/image v0.36.0
16-
golang.org/x/sync v0.19.0 // indirect
16+
golang.org/x/sync v0.20.0 // indirect
1717
)
1818

1919
// replace github.com/Patch2PDF/GDTF-Mesh-Reader/v2 => ../GDTF-Mesh-Reader

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
github.com/Patch2PDF/GDTF-Mesh-Reader/v2 v2.1.0 h1:Do1+QFw7sNf6B1lE8T9rokVGQQ3I1EhYqK7OPL4ep1Q=
2-
github.com/Patch2PDF/GDTF-Mesh-Reader/v2 v2.1.0/go.mod h1:zAcGHlYdE75hdFo624nQfQHTzw9+NfPJT8Eo2mB1lI8=
3-
github.com/Patch2PDF/GDTF-Parser v0.4.0 h1:ZdZDxIjm4K63tTVtf48K0sYJCyppuID1m4P2b4QfvM8=
4-
github.com/Patch2PDF/GDTF-Parser v0.4.0/go.mod h1:i3hZmZ2wIJ294MllgqQ+wQn4RtuYIWL1KLSn9Gseqdw=
5-
github.com/Patch2PDF/MVR-Parser v0.3.0 h1:bdvRFxkTc0oK2uxo7o0SK0JMtcdJu+/e2nsKNmODE80=
6-
github.com/Patch2PDF/MVR-Parser v0.3.0/go.mod h1:DwJgsZcqTyn+eD3KG6OVUwHvpK56HjXFqGdI/UY4z+E=
1+
github.com/Patch2PDF/GDTF-Mesh-Reader/v2 v2.2.0 h1:ykkdyTIl++IaxBbgo2WFF3/k4jy7IwcQ5FsXWBG4Zhg=
2+
github.com/Patch2PDF/GDTF-Mesh-Reader/v2 v2.2.0/go.mod h1:zAcGHlYdE75hdFo624nQfQHTzw9+NfPJT8Eo2mB1lI8=
3+
github.com/Patch2PDF/GDTF-Parser v0.4.1 h1:RLmzQwIHGuEYc6C6hNtWA3HOpjhUizhkg36u0Px1a7E=
4+
github.com/Patch2PDF/GDTF-Parser v0.4.1/go.mod h1:2l8WWbsYr4D0Uo069HMh9tImKtaUzy2+OeUpl7IN+S4=
5+
github.com/Patch2PDF/MVR-Parser v0.3.1 h1:lg9MieoOL93Bm+huKz1yMlJWuFLzHyJ+afZj1S8UKbs=
6+
github.com/Patch2PDF/MVR-Parser v0.3.1/go.mod h1:2ekLlUhkPCkDli56afCQT/9EGdE/llx3NCF5E1+YiXc=
77
github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg=
88
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
99
github.com/qmuntal/gltf v0.28.0 h1:C4A1temWMPtcI2+qNfpfRq8FEJxoBGUN3ZZM8BCc+xU=
1010
github.com/qmuntal/gltf v0.28.0/go.mod h1:YoXZOt0Nc0kIfSKOLZIRoV4FycdC+GzE+3JgiAGYoMs=
1111
golang.org/x/image v0.36.0 h1:Iknbfm1afbgtwPTmHnS2gTM/6PPZfH+z2EFuOkSbqwc=
1212
golang.org/x/image v0.36.0/go.mod h1:YsWD2TyyGKiIX1kZlu9QfKIsQ4nAAK9bdgdrIsE7xy4=
13-
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
14-
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
13+
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
14+
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
1515
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
1616
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=

triangle.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,26 @@ func (triangle Triangle) boundingTriangle(canvas *Canvas, color color.NRGBA, per
5757
return 0, 0, 0, 0, fmt.Errorf("Triangle has 0 height")
5858
}
5959

60-
total_area := triangle.signed_triangle_area()
60+
total_area := math.Abs(triangle.signed_triangle_area()) // absolute to disable backface culling
6161
if total_area < 1 {
62-
return 0, 0, 0, 0, fmt.Errorf("Triangle area is less than 1 pixel") // backface culling + discarding triangles that cover less than a pixel // TODO: test / improve
62+
return 0, 0, 0, 0, fmt.Errorf("Triangle area is less than 1 pixel") // discarding triangles that cover less than a pixel
6363
}
6464

65-
// TODO: add canvas bounds check (via min/max with canvas bounds?)
66-
bbminx = int(math.Min(math.Min(triangle.a.x, triangle.b.x), triangle.c.x))
67-
bbminy = int(math.Min(math.Min(triangle.a.y, triangle.b.y), triangle.c.y))
68-
bbmaxx = int(math.Max(math.Max(triangle.a.x, triangle.b.x), triangle.c.x))
69-
bbmaxy = int(math.Max(math.Max(triangle.a.y, triangle.b.y), triangle.c.y))
65+
// outer min / max resembles a canvas bounds check (0 to width/height)
66+
borders := canvas.canvas.Rect
67+
minx := math.Min(math.Min(triangle.a.x, triangle.b.x), triangle.c.x)
68+
miny := math.Min(math.Min(triangle.a.y, triangle.b.y), triangle.c.y)
69+
maxx := math.Max(math.Max(triangle.a.x, triangle.b.x), triangle.c.x)
70+
maxy := math.Max(math.Max(triangle.a.y, triangle.b.y), triangle.c.y)
71+
72+
bbminx = max(int(math.Floor(minx)), borders.Min.X)
73+
bbminy = max(int(math.Floor(miny)), borders.Min.Y)
74+
bbmaxx = min(int(math.Ceil(maxx)), borders.Max.X-1)
75+
bbmaxy = min(int(math.Ceil(maxy)), borders.Max.Y-1)
76+
77+
if bbminx > bbmaxx || bbminy > bbmaxy {
78+
return
79+
}
7080

7181
inv_total_area := 1 / total_area
7282

0 commit comments

Comments
 (0)