gg/ox/camera.go
2024-05-28 13:24:12 +05:00

76 lines
1.5 KiB
Go

package ox
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/mx"
// Default camera implementation.
type Camera struct {
ObjectImpl
// The Transform to interact with
// to change camera position, rotation etc.
Transform
// The shaders that will be applied to everything
// that the camera shows.
Shaderity
// The bufferization implementation
// to keep everything fast.
absMatrice realMatrice
}
// Returns the new camera
// with default settings.
func NewCamera() *Camera {
ret := &Camera{}
ret.Transform = T()
return ret
}
// Returns the matrix satysfying camera
// position, scale and rotation to apply
// it to the objects to get the real
// transform to display on the screen.
func (c *Camera) GetRealMatrice(ctx gg.Context) mx.Matrice {
// Bufferization
// so we do not need to recalculate
// Transform again.
if !c.Transform.Dirty() {
return c.realMatrice
}
var g mx.Matrice
position := c.GetPosition().Neg()
around := c.GetAround()
scale := c.GetScale()
rotation := c.GetRotation()
g.Translate(-position.X, -position.Y)
g.Rotate(rotation)
size := ctx.Engine.AbsWinSize()
g.Translate(around.X * size.X, around.Y * size.Y)
g.Scale(scale.X, scale.Y)
c.realMatrice = g
return g
}
// The matrix to convert things into the
// inside engine representation,
// get the position of cursor inside the world
// basing on its window position.
func (c *Camera) GetAbsMatrice(ctx gg.Context) mx.Matrice {
if !c.Transform.Dirty() {
return c.absMatrice
}
m := c.GetRealMatrice(ctx)
m.Invert()
c.absMatrice = m
return m
}