2017-02-10 19:44:29 +01:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-10-28 15:03:06 +01:00
|
|
|
package graphics
|
2017-02-10 19:44:29 +01:00
|
|
|
|
2019-01-18 18:06:48 +01:00
|
|
|
import (
|
2019-02-07 09:19:24 +01:00
|
|
|
"fmt"
|
2019-01-18 18:06:48 +01:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2017-05-27 14:59:25 +02:00
|
|
|
var (
|
|
|
|
theVerticesBackend = &verticesBackend{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type verticesBackend struct {
|
|
|
|
backend []float32
|
|
|
|
head int
|
2019-01-18 18:06:48 +01:00
|
|
|
m sync.Mutex
|
2017-05-27 14:59:25 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:45 +01:00
|
|
|
const (
|
|
|
|
IndicesNum = (1 << 16) / 3 * 3 // Adjust num for triangles.
|
2018-12-22 17:40:43 +01:00
|
|
|
VertexFloatNum = 12
|
2018-11-06 17:49:45 +01:00
|
|
|
)
|
2018-11-05 18:34:52 +01:00
|
|
|
|
2018-10-28 14:54:36 +01:00
|
|
|
func (v *verticesBackend) slice(n int) []float32 {
|
2018-11-05 18:34:52 +01:00
|
|
|
const num = 1024
|
2018-10-28 14:54:36 +01:00
|
|
|
if n > num {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("graphics: n must be <= num but not: n: %d, num: %d", n, num))
|
2017-05-27 14:59:25 +02:00
|
|
|
}
|
2018-10-28 14:54:36 +01:00
|
|
|
|
2019-01-18 18:06:48 +01:00
|
|
|
v.m.Lock()
|
|
|
|
|
2018-11-05 18:34:52 +01:00
|
|
|
need := n * VertexFloatNum
|
2018-10-28 14:54:36 +01:00
|
|
|
if v.head+need > len(v.backend) {
|
2017-05-27 14:59:25 +02:00
|
|
|
v.backend = nil
|
|
|
|
v.head = 0
|
|
|
|
}
|
2018-10-28 14:54:36 +01:00
|
|
|
|
|
|
|
if v.backend == nil {
|
2018-11-05 18:34:52 +01:00
|
|
|
v.backend = make([]float32, VertexFloatNum*num)
|
2018-10-28 14:54:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s := v.backend[v.head : v.head+need]
|
|
|
|
v.head += need
|
2019-01-18 18:06:48 +01:00
|
|
|
|
|
|
|
v.m.Unlock()
|
2017-05-27 14:59:25 +02:00
|
|
|
return s
|
|
|
|
}
|
2017-05-21 16:49:56 +02:00
|
|
|
|
2018-08-09 20:33:28 +02:00
|
|
|
func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, cr, cg, cb, ca float32) []float32 {
|
2019-02-14 15:44:02 +01:00
|
|
|
// width and height are the source image's size.
|
|
|
|
|
2019-02-14 13:04:34 +01:00
|
|
|
// For performance reason, graphics.InternalImageSize is not applied to width/height here.
|
|
|
|
|
|
|
|
if !isInternalImageSize(width) {
|
|
|
|
panic(fmt.Sprintf("graphics: width must be an internal image size at QuadVertices: %d", width))
|
2018-06-17 16:05:10 +02:00
|
|
|
}
|
2019-02-14 13:04:34 +01:00
|
|
|
if !isInternalImageSize(height) {
|
|
|
|
panic(fmt.Sprintf("graphics: height must be an internal image size at QuadVertices: %d", height))
|
2018-06-17 16:05:10 +02:00
|
|
|
}
|
|
|
|
|
2017-12-14 03:52:11 +01:00
|
|
|
if sx0 >= sx1 || sy0 >= sy1 {
|
2017-01-17 17:35:21 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-15 17:27:30 +01:00
|
|
|
if sx1 <= 0 || sy1 <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-17 16:05:10 +02:00
|
|
|
wf := float32(width)
|
|
|
|
hf := float32(height)
|
2017-01-17 17:35:21 +01:00
|
|
|
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
|
2019-02-16 05:38:23 +01:00
|
|
|
return quadVerticesImpl(wf, hf, float32(sx1-sx0), float32(sy1-sy0), u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca)
|
2018-06-16 19:15:40 +02:00
|
|
|
}
|
2017-12-02 12:51:18 +01:00
|
|
|
|
2019-02-16 05:38:23 +01:00
|
|
|
const TexelAdjustmentFactor = 512.0
|
|
|
|
|
|
|
|
func quadVerticesImpl(sw, sh, x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca float32) []float32 {
|
2018-06-22 20:18:54 +02:00
|
|
|
// Specifying a range explicitly here is redundant but this helps optimization
|
2018-12-03 18:23:25 +01:00
|
|
|
// to eliminate boundary checks.
|
2018-11-05 18:34:52 +01:00
|
|
|
//
|
2018-12-22 17:40:43 +01:00
|
|
|
// 4*VertexFloatNum is better than 48, but in GopherJS, optimization might not work.
|
|
|
|
vs := theVerticesBackend.slice(4)[0:48]
|
2018-06-17 10:29:22 +02:00
|
|
|
|
2018-06-17 13:11:31 +02:00
|
|
|
ax, by, cx, dy := a*x, b*y, c*x, d*y
|
|
|
|
|
2019-02-16 05:38:23 +01:00
|
|
|
du := 1.0 / sw / TexelAdjustmentFactor
|
|
|
|
dv := 1.0 / sh / TexelAdjustmentFactor
|
|
|
|
|
2017-12-02 12:51:18 +01:00
|
|
|
// Vertex coordinates
|
2018-06-17 12:45:09 +02:00
|
|
|
vs[0] = tx
|
|
|
|
vs[1] = ty
|
2017-12-02 12:51:18 +01:00
|
|
|
|
|
|
|
// Texture coordinates: first 2 values indicates the actual coodinate, and
|
|
|
|
// the second indicates diagonally opposite coodinates.
|
|
|
|
// The second is needed to calculate source rectangle size in shader programs.
|
2018-11-17 15:23:47 +01:00
|
|
|
vs[2] = u0
|
|
|
|
vs[3] = v0
|
2018-12-22 17:40:43 +01:00
|
|
|
vs[4] = u0
|
|
|
|
vs[5] = v0
|
2019-02-16 05:38:23 +01:00
|
|
|
vs[6] = u1 - du
|
|
|
|
vs[7] = v1 - dv
|
2018-12-22 17:40:43 +01:00
|
|
|
vs[8] = cr
|
|
|
|
vs[9] = cg
|
|
|
|
vs[10] = cb
|
|
|
|
vs[11] = ca
|
2017-12-02 12:51:18 +01:00
|
|
|
|
2018-01-14 08:01:55 +01:00
|
|
|
// and the same for the other three coordinates
|
2018-12-22 17:40:43 +01:00
|
|
|
vs[12] = ax + tx
|
|
|
|
vs[13] = cx + ty
|
|
|
|
vs[14] = u1
|
|
|
|
vs[15] = v0
|
|
|
|
vs[16] = u0
|
|
|
|
vs[17] = v0
|
2019-02-16 05:38:23 +01:00
|
|
|
vs[18] = u1 - du
|
|
|
|
vs[19] = v1 - dv
|
2018-12-22 17:40:43 +01:00
|
|
|
vs[20] = cr
|
|
|
|
vs[21] = cg
|
|
|
|
vs[22] = cb
|
|
|
|
vs[23] = ca
|
|
|
|
|
|
|
|
vs[24] = by + tx
|
|
|
|
vs[25] = dy + ty
|
|
|
|
vs[26] = u0
|
|
|
|
vs[27] = v1
|
|
|
|
vs[28] = u0
|
|
|
|
vs[29] = v0
|
2019-02-16 05:38:23 +01:00
|
|
|
vs[30] = u1 - du
|
|
|
|
vs[31] = v1 - dv
|
2018-12-22 17:40:43 +01:00
|
|
|
vs[32] = cr
|
|
|
|
vs[33] = cg
|
|
|
|
vs[34] = cb
|
|
|
|
vs[35] = ca
|
|
|
|
|
|
|
|
vs[36] = ax + by + tx
|
|
|
|
vs[37] = cx + dy + ty
|
|
|
|
vs[38] = u1
|
|
|
|
vs[39] = v1
|
|
|
|
vs[40] = u0
|
|
|
|
vs[41] = v0
|
2019-02-16 05:38:23 +01:00
|
|
|
vs[42] = u1 - du
|
|
|
|
vs[43] = v1 - dv
|
2018-12-22 17:40:43 +01:00
|
|
|
vs[44] = cr
|
|
|
|
vs[45] = cg
|
|
|
|
vs[46] = cb
|
|
|
|
vs[47] = ca
|
2018-06-17 10:29:22 +02:00
|
|
|
|
|
|
|
return vs
|
2017-05-21 16:49:56 +02:00
|
|
|
}
|
2018-07-20 20:40:37 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
|
|
|
|
)
|
|
|
|
|
|
|
|
func QuadIndices() []uint16 {
|
|
|
|
return quadIndices
|
|
|
|
}
|