gg/ox/camera.go

84 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-05-28 11:24:12 +03:00
package ox
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/mx"
// Default camera implementation.
2024-06-01 16:07:28 +03:00
var _ = gg.Camera(&Camera{})
2024-05-28 11:24:12 +03:00
type Camera struct {
2024-06-01 22:01:17 +03:00
ObjectImpl
2024-05-28 11:24:12 +03:00
// 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.
2024-06-01 16:07:28 +03:00
absMatrice, realMatrice mx.Matrice
2024-05-28 11:24:12 +03:00
}
// Returns the new camera
// with default settings.
2024-06-01 16:07:28 +03:00
func NewCamera() Camera {
ret := Camera{}
2024-06-02 22:27:11 +03:00
ret.Transform = *T()
2024-05-28 11:24:12 +03:00
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.
2024-06-01 16:07:28 +03:00
if !c.Transform.IsDirty() {
2024-05-28 11:24:12 +03:00
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)
2024-06-01 16:07:28 +03:00
size := ctx.Engine().GetAbsWinSize()
2024-05-28 11:24:12 +03:00
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.
2024-06-01 16:07:28 +03:00
func (camera *Camera) GetAbsMatrice(ctx gg.Context) mx.Matrice {
if !camera.Transform.IsDirty() {
return camera.absMatrice
2024-05-28 11:24:12 +03:00
}
2024-06-01 16:07:28 +03:00
m := camera.GetRealMatrice(ctx)
2024-05-28 11:24:12 +03:00
m.Invert()
2024-06-01 16:07:28 +03:00
camera.absMatrice = m
2024-05-28 11:24:12 +03:00
return m
}
2024-06-01 16:07:28 +03:00
func (camera *Camera) GetAbsWinSize(
ctx gg.Context,
) mx.Vector {
return ctx.Engine().GetRealWinSize().
Div(camera.GetScale())
}