mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
affine: Refactoring: Unify two element slices into one
This commit is contained in:
parent
2b4785f863
commit
da662b384c
17
colorm.go
17
colorm.go
@ -39,12 +39,12 @@ type ColorM struct {
|
|||||||
|
|
||||||
// String returns a string representation of ColorM.
|
// String returns a string representation of ColorM.
|
||||||
func (c *ColorM) String() string {
|
func (c *ColorM) String() string {
|
||||||
b, t := c.impl.UnsafeElements()
|
es := c.impl.UnsafeElements()
|
||||||
return fmt.Sprintf("[[%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f]]",
|
return fmt.Sprintf("[[%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f]]",
|
||||||
b[0], b[4], b[8], b[12], t[0],
|
es[0], es[4], es[8], es[12], es[16],
|
||||||
b[1], b[5], b[9], b[13], t[1],
|
es[1], es[5], es[9], es[13], es[17],
|
||||||
b[2], b[6], b[10], b[14], t[2],
|
es[2], es[6], es[10], es[14], es[18],
|
||||||
b[3], b[7], b[11], b[15], t[3])
|
es[3], es[7], es[11], es[15], es[19])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset resets the ColorM as identity.
|
// Reset resets the ColorM as identity.
|
||||||
@ -99,11 +99,8 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale
|
|||||||
|
|
||||||
// Element returns a value of a matrix at (i, j).
|
// Element returns a value of a matrix at (i, j).
|
||||||
func (c *ColorM) Element(i, j int) float64 {
|
func (c *ColorM) Element(i, j int) float64 {
|
||||||
b, t := c.impl.UnsafeElements()
|
es := c.impl.UnsafeElements()
|
||||||
if j < ColorMDim-1 {
|
return float64(es[i+j*(ColorMDim-1)])
|
||||||
return float64(b[i+j*(ColorMDim-1)])
|
|
||||||
}
|
|
||||||
return float64(t[i])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetElement sets an element at (i, j).
|
// SetElement sets an element at (i, j).
|
||||||
|
@ -14,15 +14,25 @@
|
|||||||
|
|
||||||
package affine
|
package affine
|
||||||
|
|
||||||
func mulSquare(lhs, rhs []float32, dim int) []float32 {
|
func affineAt(es []float32, i, j int, dim int) float32 {
|
||||||
|
if i == dim-1 {
|
||||||
|
if j == dim-1 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return es[i+j*(dim-1)]
|
||||||
|
}
|
||||||
|
|
||||||
|
func mulAffine(lhs, rhs []float32, dim int) []float32 {
|
||||||
result := make([]float32, len(lhs))
|
result := make([]float32, len(lhs))
|
||||||
for i := 0; i < dim; i++ {
|
for i := 0; i < dim-1; i++ {
|
||||||
for j := 0; j < dim; j++ {
|
for j := 0; j < dim; j++ {
|
||||||
e := float32(0.0)
|
e := float32(0.0)
|
||||||
for k := 0; k < dim; k++ {
|
for k := 0; k < dim; k++ {
|
||||||
e += lhs[i*dim+k] * rhs[k*dim+j]
|
e += affineAt(lhs, i, k, dim) * affineAt(rhs, k, j, dim)
|
||||||
}
|
}
|
||||||
result[i*dim+j] = e
|
result[i+j*(dim-1)] = e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
@ -23,13 +23,11 @@ import (
|
|||||||
const ColorMDim = 5
|
const ColorMDim = 5
|
||||||
|
|
||||||
var (
|
var (
|
||||||
colorMIdentityBody = []float32{
|
colorMIdentity = []float32{
|
||||||
1, 0, 0, 0,
|
1, 0, 0, 0,
|
||||||
0, 1, 0, 0,
|
0, 1, 0, 0,
|
||||||
0, 0, 1, 0,
|
0, 0, 1, 0,
|
||||||
0, 0, 0, 1,
|
0, 0, 0, 1,
|
||||||
}
|
|
||||||
colorMIdentityTranslate = []float32{
|
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -45,8 +43,8 @@ var (
|
|||||||
type ColorM struct {
|
type ColorM struct {
|
||||||
// When elements is nil, this matrix is identity.
|
// When elements is nil, this matrix is identity.
|
||||||
// elements are immutable and a new array must be created when updating.
|
// elements are immutable and a new array must be created when updating.
|
||||||
body []float32
|
// Note that elements are transposed for OpenGL.
|
||||||
translate []float32
|
elements []float32
|
||||||
}
|
}
|
||||||
|
|
||||||
func clamp(x float32) float32 {
|
func clamp(x float32) float32 {
|
||||||
@ -60,7 +58,7 @@ func clamp(x float32) float32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) isInited() bool {
|
func (c *ColorM) isInited() bool {
|
||||||
return c != nil && (c.body != nil || c.translate != nil)
|
return c != nil && c.elements != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) Apply(clr color.Color) color.Color {
|
func (c *ColorM) Apply(clr color.Color) color.Color {
|
||||||
@ -76,18 +74,14 @@ func (c *ColorM) Apply(clr color.Color) color.Color {
|
|||||||
bf = float32(b) / float32(a)
|
bf = float32(b) / float32(a)
|
||||||
af = float32(a) / 0xffff
|
af = float32(a) / 0xffff
|
||||||
}
|
}
|
||||||
eb := c.body
|
es := c.elements
|
||||||
if eb == nil {
|
if es == nil {
|
||||||
eb = colorMIdentityBody
|
es = colorMIdentity
|
||||||
}
|
}
|
||||||
et := c.translate
|
rf2 := es[0]*rf + es[4]*gf + es[8]*bf + es[12]*af + es[16]
|
||||||
if et == nil {
|
gf2 := es[1]*rf + es[5]*gf + es[9]*bf + es[13]*af + es[17]
|
||||||
et = colorMIdentityTranslate
|
bf2 := es[2]*rf + es[6]*gf + es[10]*bf + es[14]*af + es[18]
|
||||||
}
|
af2 := es[3]*rf + es[7]*gf + es[11]*bf + es[15]*af + es[19]
|
||||||
rf2 := eb[0]*rf + eb[4]*gf + eb[8]*bf + eb[12]*af + et[0]
|
|
||||||
gf2 := eb[1]*rf + eb[5]*gf + eb[9]*bf + eb[13]*af + et[1]
|
|
||||||
bf2 := eb[2]*rf + eb[6]*gf + eb[10]*bf + eb[14]*af + et[2]
|
|
||||||
af2 := eb[3]*rf + eb[7]*gf + eb[11]*bf + eb[15]*af + et[3]
|
|
||||||
rf2 = clamp(rf2)
|
rf2 = clamp(rf2)
|
||||||
gf2 = clamp(gf2)
|
gf2 = clamp(gf2)
|
||||||
bf2 = clamp(bf2)
|
bf2 = clamp(bf2)
|
||||||
@ -100,42 +94,23 @@ func (c *ColorM) Apply(clr color.Color) color.Color {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) UnsafeElements() ([]float32, []float32) {
|
func (c *ColorM) UnsafeElements() []float32 {
|
||||||
if !c.isInited() {
|
if !c.isInited() {
|
||||||
return colorMIdentityBody, colorMIdentityTranslate
|
return colorMIdentity
|
||||||
}
|
}
|
||||||
eb := c.body
|
return c.elements
|
||||||
if eb == nil {
|
|
||||||
eb = colorMIdentityBody
|
|
||||||
}
|
|
||||||
et := c.translate
|
|
||||||
if et == nil {
|
|
||||||
et = colorMIdentityTranslate
|
|
||||||
}
|
|
||||||
return eb, et
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetElement sets an element at (i, j).
|
// SetElement sets an element at (i, j).
|
||||||
func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
|
func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
|
||||||
newC := &ColorM{
|
newC := &ColorM{
|
||||||
body: make([]float32, 16),
|
elements: make([]float32, 20),
|
||||||
translate: make([]float32, 4),
|
|
||||||
}
|
}
|
||||||
copy(newC.body, colorMIdentityBody)
|
copy(newC.elements, colorMIdentity)
|
||||||
copy(newC.translate, colorMIdentityTranslate)
|
|
||||||
if c.isInited() {
|
if c.isInited() {
|
||||||
if c.body != nil {
|
copy(newC.elements, c.elements)
|
||||||
copy(newC.body, c.body)
|
|
||||||
}
|
|
||||||
if c.translate != nil {
|
|
||||||
copy(newC.translate, c.translate)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if j < (ColorMDim - 1) {
|
|
||||||
newC.body[i+j*(ColorMDim-1)] = element
|
|
||||||
} else {
|
|
||||||
newC.translate[i] = element
|
|
||||||
}
|
}
|
||||||
|
newC.elements[i+j*(ColorMDim-1)] = element
|
||||||
return newC
|
return newC
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,72 +124,36 @@ func (c *ColorM) Concat(other *ColorM) *ColorM {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
lhsb := colorMIdentityBody
|
lhs := colorMIdentity
|
||||||
lhst := colorMIdentityTranslate
|
rhs := colorMIdentity
|
||||||
rhsb := colorMIdentityBody
|
|
||||||
rhst := colorMIdentityTranslate
|
|
||||||
if other.isInited() {
|
if other.isInited() {
|
||||||
if other.body != nil {
|
lhs = other.elements
|
||||||
lhsb = other.body
|
|
||||||
}
|
|
||||||
if other.translate != nil {
|
|
||||||
lhst = other.translate
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if c.isInited() {
|
if c.isInited() {
|
||||||
if c.body != nil {
|
rhs = c.elements
|
||||||
rhsb = c.body
|
|
||||||
}
|
|
||||||
if c.translate != nil {
|
|
||||||
rhst = c.translate
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ColorM{
|
return &ColorM{
|
||||||
// TODO: This is a temporary hack to calculate multiply of transposed matrices.
|
elements: mulAffine(lhs, rhs, ColorMDim),
|
||||||
// Fix mulSquare implmentation and swap the arguments.
|
|
||||||
body: mulSquare(rhsb, lhsb, ColorMDim-1),
|
|
||||||
translate: []float32{
|
|
||||||
lhsb[0]*rhst[0] + lhsb[4]*rhst[1] + lhsb[8]*rhst[2] + lhsb[12]*rhst[3] + lhst[0],
|
|
||||||
lhsb[1]*rhst[0] + lhsb[5]*rhst[1] + lhsb[9]*rhst[2] + lhsb[13]*rhst[3] + lhst[1],
|
|
||||||
lhsb[2]*rhst[0] + lhsb[6]*rhst[1] + lhsb[10]*rhst[2] + lhsb[14]*rhst[3] + lhst[2],
|
|
||||||
lhsb[3]*rhst[0] + lhsb[7]*rhst[1] + lhsb[11]*rhst[2] + lhsb[15]*rhst[3] + lhst[3],
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add is deprecated.
|
// Add is deprecated.
|
||||||
func (c *ColorM) Add(other *ColorM) *ColorM {
|
func (c *ColorM) Add(other *ColorM) *ColorM {
|
||||||
lhsb := colorMIdentityBody
|
lhs := colorMIdentity
|
||||||
lhst := colorMIdentityTranslate
|
rhs := colorMIdentity
|
||||||
rhsb := colorMIdentityBody
|
|
||||||
rhst := colorMIdentityTranslate
|
|
||||||
if other.isInited() {
|
if other.isInited() {
|
||||||
if other.body != nil {
|
lhs = other.elements
|
||||||
lhsb = other.body
|
|
||||||
}
|
|
||||||
if other.translate != nil {
|
|
||||||
lhst = other.translate
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if c.isInited() {
|
if c.isInited() {
|
||||||
if c.body != nil {
|
rhs = c.elements
|
||||||
rhsb = c.body
|
|
||||||
}
|
|
||||||
if c.translate != nil {
|
|
||||||
rhst = c.translate
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newC := &ColorM{
|
newC := &ColorM{
|
||||||
body: make([]float32, 16),
|
elements: make([]float32, 20),
|
||||||
translate: make([]float32, 4),
|
|
||||||
}
|
}
|
||||||
for i := range lhsb {
|
for i := range lhs {
|
||||||
newC.body[i] = lhsb[i] + rhsb[i]
|
newC.elements[i] = lhs[i] + rhs[i]
|
||||||
}
|
|
||||||
for i := range lhst {
|
|
||||||
newC.translate[i] = lhst[i] + rhst[i]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return newC
|
return newC
|
||||||
@ -224,41 +163,26 @@ func (c *ColorM) Add(other *ColorM) *ColorM {
|
|||||||
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
||||||
if !c.isInited() {
|
if !c.isInited() {
|
||||||
return &ColorM{
|
return &ColorM{
|
||||||
body: []float32{
|
elements: []float32{
|
||||||
r, 0, 0, 0,
|
r, 0, 0, 0,
|
||||||
0, g, 0, 0,
|
0, g, 0, 0,
|
||||||
0, 0, b, 0,
|
0, 0, b, 0,
|
||||||
0, 0, 0, a,
|
0, 0, 0, a,
|
||||||
|
0, 0, 0, 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eb := make([]float32, len(colorMIdentityBody))
|
es := make([]float32, len(colorMIdentity))
|
||||||
if c.body != nil {
|
copy(es, c.elements)
|
||||||
copy(eb, c.body)
|
for i := 0; i < ColorMDim; i++ {
|
||||||
for i := 0; i < ColorMDim-1; i++ {
|
es[i*(ColorMDim-1)] *= r
|
||||||
eb[i*(ColorMDim-1)] *= r
|
es[i*(ColorMDim-1)+1] *= g
|
||||||
eb[i*(ColorMDim-1)+1] *= g
|
es[i*(ColorMDim-1)+2] *= b
|
||||||
eb[i*(ColorMDim-1)+2] *= b
|
es[i*(ColorMDim-1)+3] *= a
|
||||||
eb[i*(ColorMDim-1)+3] *= a
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
eb[0] = r
|
|
||||||
eb[5] = g
|
|
||||||
eb[10] = b
|
|
||||||
eb[15] = a
|
|
||||||
}
|
|
||||||
|
|
||||||
et := make([]float32, len(colorMIdentityTranslate))
|
|
||||||
if c.translate != nil {
|
|
||||||
et[0] = c.translate[0] * r
|
|
||||||
et[1] = c.translate[1] * g
|
|
||||||
et[2] = c.translate[2] * b
|
|
||||||
et[3] = c.translate[3] * a
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ColorM{
|
return &ColorM{
|
||||||
body: eb,
|
elements: es,
|
||||||
translate: et,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,20 +190,23 @@ func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
|||||||
func (c *ColorM) Translate(r, g, b, a float32) *ColorM {
|
func (c *ColorM) Translate(r, g, b, a float32) *ColorM {
|
||||||
if !c.isInited() {
|
if !c.isInited() {
|
||||||
return &ColorM{
|
return &ColorM{
|
||||||
translate: []float32{r, g, b, a},
|
elements: []float32{
|
||||||
|
1, 0, 0, 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1,
|
||||||
|
r, g, b, a,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
es := make([]float32, len(colorMIdentityTranslate))
|
es := make([]float32, len(colorMIdentity))
|
||||||
if c.translate != nil {
|
copy(es, c.elements)
|
||||||
copy(es, c.translate)
|
es[16] += r
|
||||||
}
|
es[17] += g
|
||||||
es[0] += r
|
es[18] += b
|
||||||
es[1] += g
|
es[19] += a
|
||||||
es[2] += b
|
|
||||||
es[3] += a
|
|
||||||
return &ColorM{
|
return &ColorM{
|
||||||
body: c.body,
|
elements: es,
|
||||||
translate: es,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -290,19 +217,21 @@ var (
|
|||||||
// Cr: [-0.5 - 0.5]
|
// Cr: [-0.5 - 0.5]
|
||||||
|
|
||||||
rgbToYCbCr = &ColorM{
|
rgbToYCbCr = &ColorM{
|
||||||
body: []float32{
|
elements: []float32{
|
||||||
0.2990, -0.1687, 0.5000, 0,
|
0.2990, -0.1687, 0.5000, 0,
|
||||||
0.5870, -0.3313, -0.4187, 0,
|
0.5870, -0.3313, -0.4187, 0,
|
||||||
0.1140, 0.5000, -0.0813, 0,
|
0.1140, 0.5000, -0.0813, 0,
|
||||||
0, 0, 0, 1,
|
0, 0, 0, 1,
|
||||||
|
0, 0, 0, 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
yCbCrToRgb = &ColorM{
|
yCbCrToRgb = &ColorM{
|
||||||
body: []float32{
|
elements: []float32{
|
||||||
1, 1, 1, 0,
|
1, 1, 1, 0,
|
||||||
0, -0.34414, 1.77200, 0,
|
0, -0.34414, 1.77200, 0,
|
||||||
1.40200, -0.71414, 0, 0,
|
1.40200, -0.71414, 0, 0,
|
||||||
0, 0, 0, 1,
|
0, 0, 0, 1,
|
||||||
|
0, 0, 0, 0,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -318,11 +247,12 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float32, valueScale
|
|||||||
s32, c32 := float32(sin), float32(cos)
|
s32, c32 := float32(sin), float32(cos)
|
||||||
c = c.Concat(rgbToYCbCr)
|
c = c.Concat(rgbToYCbCr)
|
||||||
c = c.Concat(&ColorM{
|
c = c.Concat(&ColorM{
|
||||||
body: []float32{
|
elements: []float32{
|
||||||
1, 0, 0, 0,
|
1, 0, 0, 0,
|
||||||
0, c32, s32, 0,
|
0, c32, s32, 0,
|
||||||
0, -s32, c32, 0,
|
0, -s32, c32, 0,
|
||||||
0, 0, 0, 1,
|
0, 0, 0, 1,
|
||||||
|
0, 0, 0, 0,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
s := saturationScale
|
s := saturationScale
|
||||||
|
@ -75,7 +75,7 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty float32, colorm *
|
|||||||
vs := theVerticesBackend.sliceForOneQuad()[0:104]
|
vs := theVerticesBackend.sliceForOneQuad()[0:104]
|
||||||
|
|
||||||
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
||||||
cbody, ctranslate := colorm.UnsafeElements()
|
ce := colorm.UnsafeElements()
|
||||||
|
|
||||||
// Vertex coordinates
|
// Vertex coordinates
|
||||||
vs[0] = tx
|
vs[0] = tx
|
||||||
@ -112,17 +112,11 @@ func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty float32, colorm *
|
|||||||
vs[83] = v0
|
vs[83] = v0
|
||||||
|
|
||||||
// Use for loop since subslicing is heavy on GopherJS.
|
// Use for loop since subslicing is heavy on GopherJS.
|
||||||
for i := 0; i < 16; i++ {
|
for i, e := range ce {
|
||||||
vs[6+i] = cbody[i]
|
vs[6+i] = e
|
||||||
vs[32+i] = cbody[i]
|
vs[32+i] = e
|
||||||
vs[58+i] = cbody[i]
|
vs[58+i] = e
|
||||||
vs[84+i] = cbody[i]
|
vs[84+i] = e
|
||||||
}
|
|
||||||
for i := 0; i < 4; i++ {
|
|
||||||
vs[22+i] = ctranslate[i]
|
|
||||||
vs[48+i] = ctranslate[i]
|
|
||||||
vs[74+i] = ctranslate[i]
|
|
||||||
vs[100+i] = ctranslate[i]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return vs
|
return vs
|
||||||
|
Loading…
Reference in New Issue
Block a user