rework Transform API

This commit is contained in:
faiface 2017-01-25 18:06:52 +01:00
parent cb699e7057
commit 5ebbb3fa31
1 changed files with 90 additions and 46 deletions

View File

@ -9,97 +9,141 @@ import "github.com/go-gl/mathgl/mgl32"
// the scale factor is 2, the object becomes 2x bigger. Finally, all points are moved, so that // the scale factor is 2, the object becomes 2x bigger. Finally, all points are moved, so that
// the original anchor is located precisely at the position. // the original anchor is located precisely at the position.
// //
// Create a Transform object with the Position function. This sets the position variable, // Create a Transform object with Position/Anchor/Rotation/... function. This sets the position
// which is the most important. Then use methods, like Scale and Rotate to change scale, // one of it's properties. Then use methods, like Scale and Rotate to change scale, rotation and
// rotation and achor. The order in which you apply these methods is irrelevant. // achor. The order in which you apply these methods is irrelevant.
// //
// pixel.Position(pixel.V(100, 100)).Rotate(math.Pi / 3).Scale(1.5) // pixel.Position(pixel.V(100, 100)).Rotate(math.Pi / 3).Scale(1.5)
//
// Also note, that no method changes the Transform. All simply return a new, changed Transform.
type Transform struct { type Transform struct {
pos, anc, sca Vec pos, anc, sca Vec
rot float64 rot float64
} }
// Position creates a Transformation object with specified position. Anchor is (0, 0), rotation // ZT stands for Zero-Transform. This Transform is a neutral Transform, does not change anything.
// is 0 and scale is 1. var ZT = Transform{sca: V(1, 1)}
func Position(position Vec) Transform {
return Transform{ // Position returns a Zero-Transform with Position set to pos.
pos: position, func Position(pos Vec) Transform {
sca: V(1, 1), return ZT.Position(pos)
}
} }
// GetPosition returns the position of a transform. // Anchor returns a Zero-Transform with Anchor set to anchor.
func (t Transform) GetPosition() Vec { func Anchor(anchor Vec) Transform {
return t.pos return ZT.Anchor(anchor)
} }
// GetAnchor returns the anchor of a transform. // Scale returns a Zero-Transform with Scale set to scale.
func (t Transform) GetAnchor() Vec { func Scale(scale float64) Transform {
return t.anc return ZT.Scale(scale)
} }
// GetScale returns the scale (2 dimensional) of transform. // ScaleXY returns a Zero-Transform with ScaleXY set to scale.
func (t Transform) GetScale() Vec { func ScaleXY(scale Vec) Transform {
return t.sca return ZT.ScaleXY(scale)
} }
// GetRotation returns the rotation of a transform (in radians). // Rotation returns a Zero-Transform with Rotation set to angle (in radians).
func (t Transform) GetRotation() float64 { func Rotation(angle float64) Transform {
return t.rot return ZT.Rotation(angle)
} }
// Position sets position. // Position moves an object by the specified vector. A zero vector will end up precisely at pos.
func (t Transform) Position(position Vec) Transform { func (t Transform) Position(pos Vec) Transform {
t.pos = position t.pos = pos
return t return t
} }
// Move adds delta to position. // AddPosition adds delta to the existing Position of this Transform.
func (t Transform) Move(delta Vec) Transform { func (t Transform) AddPosition(delta Vec) Transform {
t.pos += delta t.pos += delta
return t return t
} }
// Anchor sets anchor. Anchor is the rotation center and will be moved to the position. // Anchor specifies the zero vector, point originally located at anchor will be treated as zero.
// This affects Rotation and Position.
func (t Transform) Anchor(anchor Vec) Transform { func (t Transform) Anchor(anchor Vec) Transform {
t.anc = anchor t.anc = anchor
return t return t
} }
// MoveAnchor adds delta to anchor. // AddAnchor adds delta to the existing Anchor of this Transform.
func (t Transform) MoveAnchor(delta Vec) Transform { func (t Transform) AddAnchor(delta Vec) Transform {
t.anc += delta t.anc += delta
return t return t
} }
// Scale scales the transform by the supplied factor. // Scale specifies a factor by which an object will be scaled around it's Anchor.
// //
// Note, that subsequent calls to this method accumulate the final scale factor. Scaling two // Same as:
// times by 2 is equivalent to scaling once by 4. // t.ScaleXY(pixel.V(scale, scale)).
func (t Transform) Scale(scale float64) Transform { func (t Transform) Scale(scale float64) Transform {
t.sca = t.sca.Scaled(scale) t.sca = V(scale, scale)
return t return t
} }
// ScaleXY scales the transform by the supplied X and Y factor. Note, that scale is applied // MulScale multiplies the existing Scale of this Transform by factor.
// before rotation.
// //
// Note, that subsequent calls to this method accumulate the final scale factor. Scaling two // Same as:
// times by 2 is equivalent to scaling once by 4. // t.MulScaleXY(pixel.V(factor, factor)).
func (t Transform) MulScale(factor float64) Transform {
t.sca = t.sca.Scaled(factor)
return t
}
// ScaleXY specifies a factor in each dimension, by which an object will be scaled around it's
// Anchor.
func (t Transform) ScaleXY(scale Vec) Transform { func (t Transform) ScaleXY(scale Vec) Transform {
t.sca = V(t.sca.X()*scale.X(), t.sca.Y()*scale.Y()) t.sca = scale
return t return t
} }
// Rotate rotates the transform by the supplied angle in radians. // MulScaleXY multiplies the existing ScaleXY of this Transform by factor, component-wise.
// func (t Transform) MulScaleXY(factor Vec) Transform {
// Note, that subsequent calls to this method accumulate the final rotation. Rotating two times t.sca = V(
// by Pi/2 is equivalent to rotating once by Pi. t.sca.X()*factor.X(),
func (t Transform) Rotate(angle float64) Transform { t.sca.Y()*factor.Y(),
t.rot += angle )
return t return t
} }
// Rotation specifies an angle by which an object will be rotated around it's Anchor.
//
// The angle is in radians.
func (t Transform) Rotation(angle float64) Transform {
t.rot = angle
return t
}
// AddRotation adds delta to the existing Angle of this Transform.
//
// The delta is in radians.
func (t Transform) AddRotation(delta float64) Transform {
t.rot += delta
return t
}
// GetPosition returns the Position of the Transform.
func (t Transform) GetPosition() Vec {
return t.pos
}
// GetAnchor returns the Anchor of the Transform.
func (t Transform) GetAnchor() Vec {
return t.anc
}
// GetScaleXY returns the ScaleXY of the Transform.
func (t Transform) GetScaleXY() Vec {
return t.sca
}
// GetRotation returns the Rotation of the Transform.
func (t Transform) GetRotation() float64 {
return t.rot
}
// Project transforms a vector by a transform. // Project transforms a vector by a transform.
func (t Transform) Project(v Vec) Vec { func (t Transform) Project(v Vec) Vec {
mat := t.Mat() mat := t.Mat()
@ -142,5 +186,5 @@ func (t Transform) InvMat() mgl32.Mat3 {
// //
// It is possible to apply additional rotations, scales and moves to the returned transform. // It is possible to apply additional rotations, scales and moves to the returned transform.
func Camera(center, zoom, screenSize Vec) Transform { func Camera(center, zoom, screenSize Vec) Transform {
return Position(0).Anchor(center).ScaleXY(2 * zoom).ScaleXY(V(1/screenSize.X(), 1/screenSize.Y())) return Anchor(center).ScaleXY(2 * zoom).MulScaleXY(V(1/screenSize.X(), 1/screenSize.Y()))
} }