gg/camera.go

53 lines
1.1 KiB
Go
Raw Normal View History

2023-10-23 15:45:18 +03:00
package gg
2023-02-18 03:51:43 +03:00
// Implements the camera component
// for the main window.
type Camera struct {
2023-12-23 00:09:07 +03:00
// The shaders that will be applied to everything
// that the camera shows.
ShaderOptions
Transform
buf *Matrix
2023-12-23 00:09:07 +03:00
engine *Engine
}
func (e *Engine) NewCamera() *Camera {
ret := &Camera{}
ret.Transform = T()
ret.engine = e
return ret
2023-02-18 03:51:43 +03:00
}
// 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.
// (Should implement buffering it so we do not
// need to calculate it each time for each object. )
func (c *Camera)RealMatrix() Matrix {
2023-12-23 00:09:07 +03:00
/*if c.buf != nil {
return *(c.buf)
}*/
2023-02-18 03:51:43 +03:00
g := &Matrix{}
2023-12-23 00:09:07 +03:00
g.Translate(-c.Position.X, -c.Position.Y)
g.Rotate(c.Rotation)
siz := c.engine.AbsWinSize()
g.Translate(c.Around.X * siz.X, c.Around.Y * siz.Y)
g.Scale(c.Scale.X, c.Scale.Y)
2023-02-18 03:51:43 +03:00
c.buf = g
2023-02-18 03:51:43 +03:00
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
}