59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package gg
|
|
|
|
// Implements the camera component
|
|
// for the main window.
|
|
type Camera struct {
|
|
// The shaders that will be applied to everything
|
|
// that the camera shows.
|
|
ShaderOptions
|
|
Transform
|
|
buffered bool
|
|
buf Matrix
|
|
engine *Engine
|
|
}
|
|
|
|
func (e *Engine) NewCamera() *Camera {
|
|
ret := &Camera{}
|
|
ret.Transform = T()
|
|
ret.engine = e
|
|
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)RealMatrix() Matrix {
|
|
// Bufferization
|
|
if c.buffered {
|
|
return c.buf
|
|
}
|
|
g := Matrix{}
|
|
position := c.Position().Neg()
|
|
around := c.Around()
|
|
scale := c.Scale()
|
|
rotation := c.Rotation()
|
|
|
|
g.Translate(-position.X, -position.Y)
|
|
g.Rotate(rotation)
|
|
|
|
size := c.engine.AbsWinSize()
|
|
g.Translate(around.X * size.X, around.Y * size.Y)
|
|
|
|
g.Scale(scale.X, scale.Y)
|
|
|
|
c.buf = g
|
|
c.buffered = true
|
|
|
|
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) AbsMatrix() Matrix {
|
|
m := c.RealMatrix()
|
|
m.Invert()
|
|
return m
|
|
}
|