2013-11-26 17:46:07 +01:00
|
|
|
package offscreen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Offscreen struct {
|
|
|
|
screenHeight int
|
|
|
|
screenScale int
|
2014-01-08 08:38:03 +01:00
|
|
|
currentRenderTarget *rendertarget.RenderTarget
|
2014-01-08 06:37:07 +01:00
|
|
|
mainFramebufferTexture *rendertarget.RenderTarget
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(screenWidth, screenHeight, screenScale int) *Offscreen {
|
|
|
|
offscreen := &Offscreen{
|
|
|
|
screenHeight: screenHeight,
|
|
|
|
screenScale: screenScale,
|
|
|
|
}
|
|
|
|
|
2014-01-08 10:47:38 +01:00
|
|
|
offscreen.mainFramebufferTexture = rendertarget.NewWithCurrentFramebuffer(
|
2013-11-26 17:46:07 +01:00
|
|
|
screenWidth*screenScale,
|
2014-01-08 10:47:38 +01:00
|
|
|
screenHeight*screenScale)
|
2013-11-26 17:46:07 +01:00
|
|
|
|
|
|
|
return offscreen
|
|
|
|
}
|
|
|
|
|
2014-01-08 06:37:07 +01:00
|
|
|
func (o *Offscreen) Set(rt *rendertarget.RenderTarget) {
|
2014-01-08 08:38:03 +01:00
|
|
|
o.currentRenderTarget = rt
|
|
|
|
rt.SetAsViewport()
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Offscreen) SetMainFramebuffer() {
|
|
|
|
o.Set(o.mainFramebufferTexture)
|
|
|
|
}
|
|
|
|
|
2014-01-08 06:37:07 +01:00
|
|
|
func (o *Offscreen) DrawTexture(texture *texture.Texture,
|
2013-11-26 17:46:07 +01:00
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2014-01-08 08:38:03 +01:00
|
|
|
projectionMatrix := o.projectionMatrix()
|
2014-01-08 10:58:15 +01:00
|
|
|
texture.Draw(projectionMatrix, geometryMatrix, colorMatrix)
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 06:37:07 +01:00
|
|
|
func (o *Offscreen) DrawTextureParts(texture *texture.Texture,
|
2013-11-26 17:46:07 +01:00
|
|
|
parts []graphics.TexturePart,
|
|
|
|
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
|
2014-01-08 08:38:03 +01:00
|
|
|
projectionMatrix := o.projectionMatrix()
|
2014-01-08 10:03:21 +01:00
|
|
|
texture.DrawParts(parts, projectionMatrix, geometryMatrix, colorMatrix)
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 08:38:03 +01:00
|
|
|
func (o *Offscreen) projectionMatrix() [16]float32 {
|
|
|
|
matrix := o.currentRenderTarget.ProjectionMatrix()
|
|
|
|
if o.currentRenderTarget == o.mainFramebufferTexture {
|
2014-01-08 06:37:07 +01:00
|
|
|
actualScreenHeight := o.screenHeight * o.screenScale
|
2013-11-26 17:46:07 +01:00
|
|
|
// Flip Y and move to fit with the top of the window.
|
|
|
|
matrix[1][1] *= -1
|
2014-01-08 08:38:03 +01:00
|
|
|
matrix[1][3] += float64(actualScreenHeight) /
|
|
|
|
float64(graphics.AdjustSizeForTexture(actualScreenHeight)) * 2
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|
|
|
|
|
2014-01-08 08:38:03 +01:00
|
|
|
projectionMatrix := [16]float32{}
|
2013-11-26 17:46:07 +01:00
|
|
|
for j := 0; j < 4; j++ {
|
|
|
|
for i := 0; i < 4; i++ {
|
2014-01-08 08:38:03 +01:00
|
|
|
projectionMatrix[i+j*4] = float32(matrix[i][j])
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-08 08:38:03 +01:00
|
|
|
return projectionMatrix
|
2013-11-26 17:46:07 +01:00
|
|
|
}
|