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
|
2023-05-30 13:24:14 +03:00
|
|
|
Transform
|
2024-01-08 10:22:09 +03:00
|
|
|
buffered bool
|
|
|
|
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.
|
2023-12-20 22:39:33 +03:00
|
|
|
func (c *Camera)RealMatrix() Matrix {
|
2023-12-24 15:05:34 +03:00
|
|
|
// Bufferization
|
2024-01-08 10:22:09 +03:00
|
|
|
if c.buffered {
|
|
|
|
return c.buf
|
2023-12-24 15:05:34 +03:00
|
|
|
}
|
2024-01-08 10:22:09 +03:00
|
|
|
g := Matrix{}
|
2024-01-18 06:06:27 +03:00
|
|
|
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)
|
2023-12-23 00:09:07 +03:00
|
|
|
|
2023-12-20 22:39:33 +03:00
|
|
|
c.buf = g
|
2024-01-08 10:22:09 +03:00
|
|
|
c.buffered = true
|
2023-12-20 22:39:33 +03:00
|
|
|
|
2024-01-08 10:22:09 +03:00
|
|
|
return g
|
2023-02-18 03:51:43 +03:00
|
|
|
}
|
|
|
|
|
2023-12-20 22:39:33 +03:00
|
|
|
// 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()
|
2023-11-23 23:12:41 +03:00
|
|
|
m.Invert()
|
|
|
|
return m
|
|
|
|
}
|