Skip to content

Commit dcd0289

Browse files
authored
Merge pull request #14 from Patch2PDF/rotation-fix
added missing rotation of normals
2 parents 8a717cd + ebefce2 commit dcd0289

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

pkg/MeshTypes/matrix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ func (a Matrix) MulPosition(b Vector) Vector {
4343
z := a.X20*b.X + a.X21*b.Y + a.X22*b.Z + a.X23
4444
return Vector{x, y, z}
4545
}
46+
47+
func (a Matrix) MulDirection(b Vector) Vector {
48+
x := a.X00*b.X + a.X01*b.Y + a.X02*b.Z
49+
y := a.X10*b.X + a.X11*b.Y + a.X12*b.Z
50+
z := a.X20*b.X + a.X21*b.Y + a.X22*b.Z
51+
return Vector{x, y, z}.Normalize()
52+
}

pkg/MeshTypes/mesh.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ func (obj *Mesh) RotateAndTranslate(translationMatrix Matrix) {
3131
triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position)
3232
triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position)
3333
triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position)
34+
if triangle.V0.Normal != nil {
35+
n0 := translationMatrix.MulDirection(*triangle.V0.Normal)
36+
triangle.V0.Normal = &n0
37+
}
38+
if triangle.V1.Normal != nil {
39+
n1 := translationMatrix.MulDirection(*triangle.V1.Normal)
40+
triangle.V1.Normal = &n1
41+
}
42+
if triangle.V2.Normal != nil {
43+
n2 := translationMatrix.MulDirection(*triangle.V2.Normal)
44+
triangle.V2.Normal = &n2
45+
}
3446
}
3547
}
3648

tests/MeshTypes/mesh_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ func TestRotateAndTranslate(t *testing.T) {
8585
triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position) // safe to use as func is tested in another place
8686
triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position)
8787
triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position)
88+
if triangle.V0.Normal != nil {
89+
n0 := translationMatrix.MulDirection(*triangle.V0.Normal)
90+
triangle.V0.Normal = &n0
91+
}
92+
if triangle.V1.Normal != nil {
93+
n1 := translationMatrix.MulDirection(*triangle.V1.Normal)
94+
triangle.V1.Normal = &n1
95+
}
96+
if triangle.V2.Normal != nil {
97+
n2 := translationMatrix.MulDirection(*triangle.V2.Normal)
98+
triangle.V2.Normal = &n2
99+
}
88100
}
89101
if !reflect.DeepEqual(result, want) {
90102
t.Errorf("Mesh RotateAndTranslate() Output does not match expected")

0 commit comments

Comments
 (0)