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.
|
||||
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]]",
|
||||
b[0], b[4], b[8], b[12], t[0],
|
||||
b[1], b[5], b[9], b[13], t[1],
|
||||
b[2], b[6], b[10], b[14], t[2],
|
||||
b[3], b[7], b[11], b[15], t[3])
|
||||
es[0], es[4], es[8], es[12], es[16],
|
||||
es[1], es[5], es[9], es[13], es[17],
|
||||
es[2], es[6], es[10], es[14], es[18],
|
||||
es[3], es[7], es[11], es[15], es[19])
|
||||
}
|
||||
|
||||
// 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).
|
||||
func (c *ColorM) Element(i, j int) float64 {
|
||||
b, t := c.impl.UnsafeElements()
|
||||
if j < ColorMDim-1 {
|
||||
return float64(b[i+j*(ColorMDim-1)])
|
||||
}
|
||||
return float64(t[i])
|
||||
es := c.impl.UnsafeElements()
|
||||
return float64(es[i+j*(ColorMDim-1)])
|
||||
}
|
||||
|
||||
// SetElement sets an element at (i, j).
|
||||
|
@ -14,15 +14,25 @@
|
||||
|
||||
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))
|
||||
for i := 0; i < dim; i++ {
|
||||
for i := 0; i < dim-1; i++ {
|
||||
for j := 0; j < dim; j++ {
|
||||
e := float32(0.0)
|
||||
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
|
||||
|
@ -23,13 +23,11 @@ import (
|
||||
const ColorMDim = 5
|
||||
|
||||
var (
|
||||
colorMIdentityBody = []float32{
|
||||
colorMIdentity = []float32{
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1,
|
||||
}
|
||||
colorMIdentityTranslate = []float32{
|
||||
0, 0, 0, 0,
|
||||
}
|
||||
)
|
||||
@ -45,8 +43,8 @@ var (
|
||||
type ColorM struct {
|
||||
// When elements is nil, this matrix is identity.
|
||||
// elements are immutable and a new array must be created when updating.
|
||||
body []float32
|
||||
translate []float32
|
||||
// Note that elements are transposed for OpenGL.
|
||||
elements []float32
|
||||
}
|
||||
|
||||
func clamp(x float32) float32 {
|
||||
@ -60,7 +58,7 @@ func clamp(x float32) float32 {
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -76,18 +74,14 @@ func (c *ColorM) Apply(clr color.Color) color.Color {
|
||||
bf = float32(b) / float32(a)
|
||||
af = float32(a) / 0xffff
|
||||
}
|
||||
eb := c.body
|
||||
if eb == nil {
|
||||
eb = colorMIdentityBody
|
||||
es := c.elements
|
||||
if es == nil {
|
||||
es = colorMIdentity
|
||||
}
|
||||
et := c.translate
|
||||
if et == nil {
|
||||
et = colorMIdentityTranslate
|
||||
}
|
||||
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 := es[0]*rf + es[4]*gf + es[8]*bf + es[12]*af + es[16]
|
||||
gf2 := es[1]*rf + es[5]*gf + es[9]*bf + es[13]*af + es[17]
|
||||
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 = clamp(rf2)
|
||||
gf2 = clamp(gf2)
|
||||
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() {
|
||||
return colorMIdentityBody, colorMIdentityTranslate
|
||||
return colorMIdentity
|
||||
}
|
||||
eb := c.body
|
||||
if eb == nil {
|
||||
eb = colorMIdentityBody
|
||||
}
|
||||
et := c.translate
|
||||
if et == nil {
|
||||
et = colorMIdentityTranslate
|
||||
}
|
||||
return eb, et
|
||||
return c.elements
|
||||
}
|
||||
|
||||
// SetElement sets an element at (i, j).
|
||||
func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
|
||||
newC := &ColorM{
|
||||
body: make([]float32, 16),
|
||||
translate: make([]float32, 4),
|
||||
elements: make([]float32, 20),
|
||||
}
|
||||
copy(newC.body, colorMIdentityBody)
|
||||
copy(newC.translate, colorMIdentityTranslate)
|
||||
copy(newC.elements, colorMIdentity)
|
||||
if c.isInited() {
|
||||
if c.body != nil {
|
||||
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
|
||||
copy(newC.elements, c.elements)
|
||||
}
|
||||
newC.elements[i+j*(ColorMDim-1)] = element
|
||||
return newC
|
||||
}
|
||||
|
||||
@ -149,72 +124,36 @@ func (c *ColorM) Concat(other *ColorM) *ColorM {
|
||||
return c
|
||||
}
|
||||
|
||||
lhsb := colorMIdentityBody
|
||||
lhst := colorMIdentityTranslate
|
||||
rhsb := colorMIdentityBody
|
||||
rhst := colorMIdentityTranslate
|
||||
lhs := colorMIdentity
|
||||
rhs := colorMIdentity
|
||||
if other.isInited() {
|
||||
if other.body != nil {
|
||||
lhsb = other.body
|
||||
}
|
||||
if other.translate != nil {
|
||||
lhst = other.translate
|
||||
}
|
||||
lhs = other.elements
|
||||
}
|
||||
if c.isInited() {
|
||||
if c.body != nil {
|
||||
rhsb = c.body
|
||||
}
|
||||
if c.translate != nil {
|
||||
rhst = c.translate
|
||||
}
|
||||
rhs = c.elements
|
||||
}
|
||||
|
||||
return &ColorM{
|
||||
// TODO: This is a temporary hack to calculate multiply of transposed matrices.
|
||||
// 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],
|
||||
},
|
||||
elements: mulAffine(lhs, rhs, ColorMDim),
|
||||
}
|
||||
}
|
||||
|
||||
// Add is deprecated.
|
||||
func (c *ColorM) Add(other *ColorM) *ColorM {
|
||||
lhsb := colorMIdentityBody
|
||||
lhst := colorMIdentityTranslate
|
||||
rhsb := colorMIdentityBody
|
||||
rhst := colorMIdentityTranslate
|
||||
lhs := colorMIdentity
|
||||
rhs := colorMIdentity
|
||||
if other.isInited() {
|
||||
if other.body != nil {
|
||||
lhsb = other.body
|
||||
}
|
||||
if other.translate != nil {
|
||||
lhst = other.translate
|
||||
}
|
||||
lhs = other.elements
|
||||
}
|
||||
if c.isInited() {
|
||||
if c.body != nil {
|
||||
rhsb = c.body
|
||||
}
|
||||
if c.translate != nil {
|
||||
rhst = c.translate
|
||||
}
|
||||
rhs = c.elements
|
||||
}
|
||||
|
||||
newC := &ColorM{
|
||||
body: make([]float32, 16),
|
||||
translate: make([]float32, 4),
|
||||
elements: make([]float32, 20),
|
||||
}
|
||||
for i := range lhsb {
|
||||
newC.body[i] = lhsb[i] + rhsb[i]
|
||||
}
|
||||
for i := range lhst {
|
||||
newC.translate[i] = lhst[i] + rhst[i]
|
||||
for i := range lhs {
|
||||
newC.elements[i] = lhs[i] + rhs[i]
|
||||
}
|
||||
|
||||
return newC
|
||||
@ -224,41 +163,26 @@ func (c *ColorM) Add(other *ColorM) *ColorM {
|
||||
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
||||
if !c.isInited() {
|
||||
return &ColorM{
|
||||
body: []float32{
|
||||
elements: []float32{
|
||||
r, 0, 0, 0,
|
||||
0, g, 0, 0,
|
||||
0, 0, b, 0,
|
||||
0, 0, 0, a,
|
||||
0, 0, 0, 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
eb := make([]float32, len(colorMIdentityBody))
|
||||
if c.body != nil {
|
||||
copy(eb, c.body)
|
||||
for i := 0; i < ColorMDim-1; i++ {
|
||||
eb[i*(ColorMDim-1)] *= r
|
||||
eb[i*(ColorMDim-1)+1] *= g
|
||||
eb[i*(ColorMDim-1)+2] *= b
|
||||
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
|
||||
es := make([]float32, len(colorMIdentity))
|
||||
copy(es, c.elements)
|
||||
for i := 0; i < ColorMDim; i++ {
|
||||
es[i*(ColorMDim-1)] *= r
|
||||
es[i*(ColorMDim-1)+1] *= g
|
||||
es[i*(ColorMDim-1)+2] *= b
|
||||
es[i*(ColorMDim-1)+3] *= a
|
||||
}
|
||||
|
||||
return &ColorM{
|
||||
body: eb,
|
||||
translate: et,
|
||||
elements: es,
|
||||
}
|
||||
}
|
||||
|
||||
@ -266,20 +190,23 @@ func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
||||
func (c *ColorM) Translate(r, g, b, a float32) *ColorM {
|
||||
if !c.isInited() {
|
||||
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))
|
||||
if c.translate != nil {
|
||||
copy(es, c.translate)
|
||||
}
|
||||
es[0] += r
|
||||
es[1] += g
|
||||
es[2] += b
|
||||
es[3] += a
|
||||
es := make([]float32, len(colorMIdentity))
|
||||
copy(es, c.elements)
|
||||
es[16] += r
|
||||
es[17] += g
|
||||
es[18] += b
|
||||
es[19] += a
|
||||
return &ColorM{
|
||||
body: c.body,
|
||||
translate: es,
|
||||
elements: es,
|
||||
}
|
||||
}
|
||||
|
||||
@ -290,19 +217,21 @@ var (
|
||||
// Cr: [-0.5 - 0.5]
|
||||
|
||||
rgbToYCbCr = &ColorM{
|
||||
body: []float32{
|
||||
elements: []float32{
|
||||
0.2990, -0.1687, 0.5000, 0,
|
||||
0.5870, -0.3313, -0.4187, 0,
|
||||
0.1140, 0.5000, -0.0813, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 0, 0,
|
||||
},
|
||||
}
|
||||
yCbCrToRgb = &ColorM{
|
||||
body: []float32{
|
||||
elements: []float32{
|
||||
1, 1, 1, 0,
|
||||
0, -0.34414, 1.77200, 0,
|
||||
1.40200, -0.71414, 0, 0,
|
||||
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)
|
||||
c = c.Concat(rgbToYCbCr)
|
||||
c = c.Concat(&ColorM{
|
||||
body: []float32{
|
||||
elements: []float32{
|
||||
1, 0, 0, 0,
|
||||
0, c32, s32, 0,
|
||||
0, -s32, c32, 0,
|
||||
0, 0, 0, 1,
|
||||
0, 0, 0, 0,
|
||||
},
|
||||
})
|
||||
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]
|
||||
|
||||
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
||||
cbody, ctranslate := colorm.UnsafeElements()
|
||||
ce := colorm.UnsafeElements()
|
||||
|
||||
// Vertex coordinates
|
||||
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
|
||||
|
||||
// Use for loop since subslicing is heavy on GopherJS.
|
||||
for i := 0; i < 16; i++ {
|
||||
vs[6+i] = cbody[i]
|
||||
vs[32+i] = cbody[i]
|
||||
vs[58+i] = cbody[i]
|
||||
vs[84+i] = cbody[i]
|
||||
}
|
||||
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]
|
||||
for i, e := range ce {
|
||||
vs[6+i] = e
|
||||
vs[32+i] = e
|
||||
vs[58+i] = e
|
||||
vs[84+i] = e
|
||||
}
|
||||
|
||||
return vs
|
||||
|
Loading…
Reference in New Issue
Block a user