mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
graphics: CompositionMode -> CompositeMode (#170)
This commit is contained in:
parent
1eb623cf16
commit
c6fc5ab865
@ -43,7 +43,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
screen.DrawImage(ebitenImage, op)
|
screen.DrawImage(ebitenImage, op)
|
||||||
op = &ebiten.DrawImageOptions{}
|
op = &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Translate(ox+float64(w), oy)
|
op.GeoM.Translate(ox+float64(w), oy)
|
||||||
op.CompositionMode = ebiten.CompositionModeLighter
|
op.CompositeMode = ebiten.CompositeModeLighter
|
||||||
screen.DrawImage(ebitenImage, op)
|
screen.DrawImage(ebitenImage, op)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Composition Mode (Ebiten Demo)"); err != nil {
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Composite Mode (Ebiten Demo)"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ func update(screen *ebiten.Image) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
op = &ebiten.DrawImageOptions{}
|
op = &ebiten.DrawImageOptions{}
|
||||||
op.CompositionMode = ebiten.CompositionModeSourceOut
|
op.CompositeMode = ebiten.CompositeModeSourceOut
|
||||||
if err := maskImage.DrawImage(fiveyearsImage, op); err != nil {
|
if err := maskImage.DrawImage(fiveyearsImage, op); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
28
graphics.go
28
graphics.go
@ -36,22 +36,22 @@ func glFilter(c *opengl.Context, filter Filter) opengl.Filter {
|
|||||||
panic("not reach")
|
panic("not reach")
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompositionMode represents Porter-Duff composition mode.
|
// CompositeMode represents Porter-Duff composition mode.
|
||||||
type CompositionMode int
|
type CompositeMode int
|
||||||
|
|
||||||
// Note: This name convention follow CSS compositing: https://drafts.fxtf.org/compositing-2/
|
// Note: This name convention follow CSS compositing: https://drafts.fxtf.org/compositing-2/
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CompositionModeSourceOver CompositionMode = CompositionMode(opengl.CompositionModeSourceOver) // regular alpha blending
|
CompositeModeSourceOver CompositeMode = CompositeMode(opengl.CompositeModeSourceOver) // regular alpha blending
|
||||||
CompositionModeClear = CompositionMode(opengl.CompositionModeClear)
|
CompositeModeClear = CompositeMode(opengl.CompositeModeClear)
|
||||||
CompositionModeCopy = CompositionMode(opengl.CompositionModeCopy)
|
CompositeModeCopy = CompositeMode(opengl.CompositeModeCopy)
|
||||||
CompositionModeDestination = CompositionMode(opengl.CompositionModeDestination)
|
CompositeModeDestination = CompositeMode(opengl.CompositeModeDestination)
|
||||||
CompositionModeSourceIn = CompositionMode(opengl.CompositionModeSourceIn)
|
CompositeModeSourceIn = CompositeMode(opengl.CompositeModeSourceIn)
|
||||||
CompositionModeDestinationIn = CompositionMode(opengl.CompositionModeDestinationIn)
|
CompositeModeDestinationIn = CompositeMode(opengl.CompositeModeDestinationIn)
|
||||||
CompositionModeSourceOut = CompositionMode(opengl.CompositionModeSourceOut)
|
CompositeModeSourceOut = CompositeMode(opengl.CompositeModeSourceOut)
|
||||||
CompositionModeDestinationOut = CompositionMode(opengl.CompositionModeDestinationOut)
|
CompositeModeDestinationOut = CompositeMode(opengl.CompositeModeDestinationOut)
|
||||||
CompositionModeSourceAtop = CompositionMode(opengl.CompositionModeSourceAtop)
|
CompositeModeSourceAtop = CompositeMode(opengl.CompositeModeSourceAtop)
|
||||||
CompositionModeDestinationAtop = CompositionMode(opengl.CompositionModeDestinationAtop)
|
CompositeModeDestinationAtop = CompositeMode(opengl.CompositeModeDestinationAtop)
|
||||||
CompositionModeXor = CompositionMode(opengl.CompositionModeXor)
|
CompositeModeXor = CompositeMode(opengl.CompositeModeXor)
|
||||||
CompositionModeLighter = CompositionMode(opengl.CompositionModeLighter) // sum of source and destination (a.k.a. 'plus' or 'additive')
|
CompositeModeLighter = CompositeMode(opengl.CompositeModeLighter) // sum of source and destination (a.k.a. 'plus' or 'additive')
|
||||||
)
|
)
|
||||||
|
4
image.go
4
image.go
@ -99,7 +99,7 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
|
|||||||
}
|
}
|
||||||
w, h := image.Size()
|
w, h := image.Size()
|
||||||
quads := &textureQuads{parts: parts, width: w, height: h}
|
quads := &textureQuads{parts: parts, width: w, height: h}
|
||||||
m := opengl.CompositionMode(options.CompositionMode)
|
m := opengl.CompositeMode(options.CompositeMode)
|
||||||
return i.framebuffer.DrawTexture(glContext, image.texture, quads, &options.GeoM, &options.ColorM, m)
|
return i.framebuffer.DrawTexture(glContext, image.texture, quads, &options.GeoM, &options.ColorM, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ type DrawImageOptions struct {
|
|||||||
ImageParts ImageParts
|
ImageParts ImageParts
|
||||||
GeoM GeoM
|
GeoM GeoM
|
||||||
ColorM ColorM
|
ColorM ColorM
|
||||||
CompositionMode CompositionMode
|
CompositeMode CompositeMode
|
||||||
|
|
||||||
// Deprecated (as of 1.1.0-alpha): Use ImageParts instead.
|
// Deprecated (as of 1.1.0-alpha): Use ImageParts instead.
|
||||||
Parts []ImagePart
|
Parts []ImagePart
|
||||||
|
@ -246,7 +246,7 @@ func min(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImageCompositionModeLighter(t *testing.T) {
|
func TestImageCompositeModeLighter(t *testing.T) {
|
||||||
img0, _, err := openEbitenImage("testdata/ebiten.png")
|
img0, _, err := openEbitenImage("testdata/ebiten.png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -261,7 +261,7 @@ func TestImageCompositionModeLighter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
img1.Fill(color.RGBA{0x01, 0x02, 0x03, 0x04})
|
img1.Fill(color.RGBA{0x01, 0x02, 0x03, 0x04})
|
||||||
op := &DrawImageOptions{}
|
op := &DrawImageOptions{}
|
||||||
op.CompositionMode = CompositionModeLighter
|
op.CompositeMode = CompositeModeLighter
|
||||||
img1.DrawImage(img0, op)
|
img1.DrawImage(img0, op)
|
||||||
for j := 0; j < img1.Bounds().Size().Y; j++ {
|
for j := 0; j < img1.Bounds().Size().Y; j++ {
|
||||||
for i := 0; i < img1.Bounds().Size().X; i++ {
|
for i := 0; i < img1.Bounds().Size().X; i++ {
|
||||||
|
@ -37,7 +37,7 @@ var vertices = make([]int16, 16*quadsMaxNum)
|
|||||||
|
|
||||||
var shadersInitialized = false
|
var shadersInitialized = false
|
||||||
|
|
||||||
func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix, mode opengl.CompositionMode) error {
|
func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix, mode opengl.CompositeMode) error {
|
||||||
c.BlendFunc(mode)
|
c.BlendFunc(mode)
|
||||||
|
|
||||||
// NOTE: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
|
// NOTE: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far.
|
||||||
|
@ -116,7 +116,7 @@ func (f *Framebuffer) Fill(c *opengl.Context, clr color.Color) error {
|
|||||||
return c.FillFramebuffer(r, g, b, a)
|
return c.FillFramebuffer(r, g, b, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQuads, geo, clr Matrix, mode opengl.CompositionMode) error {
|
func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQuads, geo, clr Matrix, mode opengl.CompositeMode) error {
|
||||||
if err := f.setAsViewport(c); err != nil {
|
if err := f.setAsViewport(c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ func (p Program) id() programID {
|
|||||||
type context struct {
|
type context struct {
|
||||||
locationCache *locationCache
|
locationCache *locationCache
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
lastCompositionMode CompositionMode
|
lastCompositeMode CompositeMode
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext() *Context {
|
func NewContext() *Context {
|
||||||
@ -73,7 +73,7 @@ func NewContext() *Context {
|
|||||||
}
|
}
|
||||||
c.locationCache = newLocationCache()
|
c.locationCache = newLocationCache()
|
||||||
c.funcs = make(chan func())
|
c.funcs = make(chan func())
|
||||||
c.lastCompositionMode = CompositionModeUnknown
|
c.lastCompositeMode = CompositeModeUnknown
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,15 +107,15 @@ func (c *Context) Init() {
|
|||||||
// Textures' pixel formats are alpha premultiplied.
|
// Textures' pixel formats are alpha premultiplied.
|
||||||
gl.Enable(gl.BLEND)
|
gl.Enable(gl.BLEND)
|
||||||
})
|
})
|
||||||
c.BlendFunc(CompositionModeSourceOver)
|
c.BlendFunc(CompositeModeSourceOver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) BlendFunc(mode CompositionMode) {
|
func (c *Context) BlendFunc(mode CompositeMode) {
|
||||||
c.RunOnContextThread(func() {
|
c.RunOnContextThread(func() {
|
||||||
if c.lastCompositionMode == mode {
|
if c.lastCompositeMode == mode {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.lastCompositionMode = mode
|
c.lastCompositeMode = mode
|
||||||
s, d := c.operations(mode)
|
s, d := c.operations(mode)
|
||||||
gl.BlendFunc(uint32(s), uint32(d))
|
gl.BlendFunc(uint32(s), uint32(d))
|
||||||
})
|
})
|
||||||
|
@ -67,7 +67,7 @@ type context struct {
|
|||||||
lastFramebuffer Framebuffer
|
lastFramebuffer Framebuffer
|
||||||
locationCache *locationCache
|
locationCache *locationCache
|
||||||
lastProgramID programID
|
lastProgramID programID
|
||||||
lastCompositionMode CompositionMode
|
lastCompositeMode CompositeMode
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext() *Context {
|
func NewContext() *Context {
|
||||||
@ -115,7 +115,7 @@ func NewContext() *Context {
|
|||||||
}
|
}
|
||||||
c.gl = gl
|
c.gl = gl
|
||||||
c.locationCache = newLocationCache()
|
c.locationCache = newLocationCache()
|
||||||
c.lastCompositionMode = CompositionModeUnknown
|
c.lastCompositeMode = CompositeModeUnknown
|
||||||
c.init()
|
c.init()
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@ -125,14 +125,14 @@ func (c *Context) init() {
|
|||||||
// Textures' pixel formats are alpha premultiplied.
|
// Textures' pixel formats are alpha premultiplied.
|
||||||
gl.Enable(gl.BLEND)
|
gl.Enable(gl.BLEND)
|
||||||
//gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
//gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
||||||
c.BlendFunc(CompositionModeSourceOver)
|
c.BlendFunc(CompositeModeSourceOver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) BlendFunc(mode CompositionMode) {
|
func (c *Context) BlendFunc(mode CompositeMode) {
|
||||||
if c.lastCompositionMode == mode {
|
if c.lastCompositeMode == mode {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.lastCompositionMode = mode
|
c.lastCompositeMode = mode
|
||||||
s, d := c.operations(mode)
|
s, d := c.operations(mode)
|
||||||
gl := c.gl
|
gl := c.gl
|
||||||
gl.BlendFunc(int(s), int(d))
|
gl.BlendFunc(int(s), int(d))
|
||||||
|
@ -41,52 +41,52 @@ type Context struct {
|
|||||||
context
|
context
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompositionMode int
|
type CompositeMode int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CompositionModeSourceOver CompositionMode = iota // This value must be 0 (= initial value)
|
CompositeModeSourceOver CompositeMode = iota // This value must be 0 (= initial value)
|
||||||
CompositionModeClear
|
CompositeModeClear
|
||||||
CompositionModeCopy
|
CompositeModeCopy
|
||||||
CompositionModeDestination
|
CompositeModeDestination
|
||||||
CompositionModeDestinationOver
|
CompositeModeDestinationOver
|
||||||
CompositionModeSourceIn
|
CompositeModeSourceIn
|
||||||
CompositionModeDestinationIn
|
CompositeModeDestinationIn
|
||||||
CompositionModeSourceOut
|
CompositeModeSourceOut
|
||||||
CompositionModeDestinationOut
|
CompositeModeDestinationOut
|
||||||
CompositionModeSourceAtop
|
CompositeModeSourceAtop
|
||||||
CompositionModeDestinationAtop
|
CompositeModeDestinationAtop
|
||||||
CompositionModeXor
|
CompositeModeXor
|
||||||
CompositionModeLighter
|
CompositeModeLighter
|
||||||
CompositionModeUnknown
|
CompositeModeUnknown
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Context) operations(mode CompositionMode) (src operation, dst operation) {
|
func (c *Context) operations(mode CompositeMode) (src operation, dst operation) {
|
||||||
switch mode {
|
switch mode {
|
||||||
case CompositionModeSourceOver:
|
case CompositeModeSourceOver:
|
||||||
return c.one, c.oneMinusSrcAlpha
|
return c.one, c.oneMinusSrcAlpha
|
||||||
case CompositionModeClear:
|
case CompositeModeClear:
|
||||||
return c.zero, c.zero
|
return c.zero, c.zero
|
||||||
case CompositionModeCopy:
|
case CompositeModeCopy:
|
||||||
return c.one, c.zero
|
return c.one, c.zero
|
||||||
case CompositionModeDestination:
|
case CompositeModeDestination:
|
||||||
return c.zero, c.one
|
return c.zero, c.one
|
||||||
case CompositionModeDestinationOver:
|
case CompositeModeDestinationOver:
|
||||||
return c.oneMinusDstAlpha, c.one
|
return c.oneMinusDstAlpha, c.one
|
||||||
case CompositionModeSourceIn:
|
case CompositeModeSourceIn:
|
||||||
return c.dstAlpha, c.zero
|
return c.dstAlpha, c.zero
|
||||||
case CompositionModeDestinationIn:
|
case CompositeModeDestinationIn:
|
||||||
return c.zero, c.srcAlpha
|
return c.zero, c.srcAlpha
|
||||||
case CompositionModeSourceOut:
|
case CompositeModeSourceOut:
|
||||||
return c.oneMinusDstAlpha, c.zero
|
return c.oneMinusDstAlpha, c.zero
|
||||||
case CompositionModeDestinationOut:
|
case CompositeModeDestinationOut:
|
||||||
return c.zero, c.oneMinusSrcAlpha
|
return c.zero, c.oneMinusSrcAlpha
|
||||||
case CompositionModeSourceAtop:
|
case CompositeModeSourceAtop:
|
||||||
return c.dstAlpha, c.oneMinusSrcAlpha
|
return c.dstAlpha, c.oneMinusSrcAlpha
|
||||||
case CompositionModeDestinationAtop:
|
case CompositeModeDestinationAtop:
|
||||||
return c.oneMinusDstAlpha, c.srcAlpha
|
return c.oneMinusDstAlpha, c.srcAlpha
|
||||||
case CompositionModeXor:
|
case CompositeModeXor:
|
||||||
return c.oneMinusDstAlpha, c.oneMinusSrcAlpha
|
return c.oneMinusDstAlpha, c.oneMinusSrcAlpha
|
||||||
case CompositionModeLighter:
|
case CompositeModeLighter:
|
||||||
return c.one, c.one
|
return c.one, c.one
|
||||||
default:
|
default:
|
||||||
panic("not reach")
|
panic("not reach")
|
||||||
|
Loading…
Reference in New Issue
Block a user