83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package ox
|
|
|
|
import "surdeus.su/core/gg"
|
|
import "surdeus.su/core/gg/mx"
|
|
|
|
|
|
// Default camera implementation.
|
|
var _ = gg.Camera(&Camera{})
|
|
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 mx.Matrice
|
|
}
|
|
|
|
// 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.IsDirty() {
|
|
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().GetAbsWinSize()
|
|
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 (camera *Camera) GetAbsMatrice(ctx gg.Context) mx.Matrice {
|
|
if !camera.Transform.IsDirty() {
|
|
return camera.absMatrice
|
|
}
|
|
m := camera.GetRealMatrice(ctx)
|
|
m.Invert()
|
|
|
|
camera.absMatrice = m
|
|
return m
|
|
}
|
|
|
|
func (camera *Camera) GetAbsWinSize(
|
|
ctx gg.Context,
|
|
) mx.Vector {
|
|
return ctx.Engine().GetRealWinSize().
|
|
Div(camera.GetScale())
|
|
}
|
|
|