Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/MeshTypes/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ func (a Matrix) MulPosition(b Vector) Vector {
z := a.X20*b.X + a.X21*b.Y + a.X22*b.Z + a.X23
return Vector{x, y, z}
}

func (a Matrix) MulDirection(b Vector) Vector {
x := a.X00*b.X + a.X01*b.Y + a.X02*b.Z
y := a.X10*b.X + a.X11*b.Y + a.X12*b.Z
z := a.X20*b.X + a.X21*b.Y + a.X22*b.Z
return Vector{x, y, z}.Normalize()
}
12 changes: 12 additions & 0 deletions pkg/MeshTypes/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func (obj *Mesh) RotateAndTranslate(translationMatrix Matrix) {
triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position)
triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position)
triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position)
if triangle.V0.Normal != nil {
n0 := translationMatrix.MulDirection(*triangle.V0.Normal)
triangle.V0.Normal = &n0
}
if triangle.V1.Normal != nil {
n1 := translationMatrix.MulDirection(*triangle.V1.Normal)
triangle.V1.Normal = &n1
}
if triangle.V2.Normal != nil {
n2 := translationMatrix.MulDirection(*triangle.V2.Normal)
triangle.V2.Normal = &n2
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/MeshTypes/mesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func TestRotateAndTranslate(t *testing.T) {
triangle.V0.Position = translationMatrix.MulPosition(triangle.V0.Position) // safe to use as func is tested in another place
triangle.V1.Position = translationMatrix.MulPosition(triangle.V1.Position)
triangle.V2.Position = translationMatrix.MulPosition(triangle.V2.Position)
if triangle.V0.Normal != nil {
n0 := translationMatrix.MulDirection(*triangle.V0.Normal)
triangle.V0.Normal = &n0
}
if triangle.V1.Normal != nil {
n1 := translationMatrix.MulDirection(*triangle.V1.Normal)
triangle.V1.Normal = &n1
}
if triangle.V2.Normal != nil {
n2 := translationMatrix.MulDirection(*triangle.V2.Normal)
triangle.V2.Normal = &n2
}
}
if !reflect.DeepEqual(result, want) {
t.Errorf("Mesh RotateAndTranslate() Output does not match expected")
Expand Down