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-06-10 16:08:22 +02:00
|
|
|
package graphicsutil
|
2017-02-10 19:44:29 +01:00
|
|
|
|
2017-05-01 16:23:58 +02:00
|
|
|
import (
|
2018-10-28 12:10:05 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
|
2017-05-01 16:23:58 +02:00
|
|
|
)
|
|
|
|
|
2017-05-27 14:59:25 +02:00
|
|
|
var (
|
|
|
|
theVerticesBackend = &verticesBackend{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type verticesBackend struct {
|
|
|
|
backend []float32
|
|
|
|
head int
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:54:36 +01:00
|
|
|
func (v *verticesBackend) slice(n int) []float32 {
|
2017-05-27 14:59:25 +02:00
|
|
|
const num = 256
|
2018-10-28 14:54:36 +01:00
|
|
|
if n > num {
|
|
|
|
panic("not reached")
|
2017-05-27 14:59:25 +02:00
|
|
|
}
|
2018-10-28 14:54:36 +01:00
|
|
|
|
|
|
|
need := n * graphicscommand.VertexFloatNum()
|
|
|
|
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 {
|
|
|
|
size := 4 * graphicscommand.VertexFloatNum()
|
|
|
|
v.backend = make([]float32, size*num)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := v.backend[v.head : v.head+need]
|
|
|
|
v.head += need
|
2017-05-27 14:59:25 +02:00
|
|
|
return s
|
|
|
|
}
|
2017-05-21 16:49:56 +02:00
|
|
|
|
2018-06-17 16:05:10 +02:00
|
|
|
func isPowerOf2(x int) bool {
|
|
|
|
return (x & (x - 1)) == 0
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-17 16:05:10 +02:00
|
|
|
if !isPowerOf2(width) {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
if !isPowerOf2(height) {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
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
|
2018-08-09 20:33:28 +02:00
|
|
|
return quadVerticesImpl(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
|
|
|
|
2018-08-09 20:33:28 +02:00
|
|
|
func quadVerticesImpl(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
|
|
|
|
// to eliminate boundry checks.
|
2018-10-28 14:54:36 +01:00
|
|
|
vs := theVerticesBackend.slice(4)[0:40]
|
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
|
|
|
|
|
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.
|
2017-01-17 17:35:21 +01:00
|
|
|
vs[2] = u0
|
|
|
|
vs[3] = v0
|
2017-12-02 12:51:18 +01:00
|
|
|
vs[4] = u1
|
|
|
|
vs[5] = v1
|
2018-08-09 20:33:28 +02:00
|
|
|
vs[6] = cr
|
|
|
|
vs[7] = cg
|
|
|
|
vs[8] = cb
|
|
|
|
vs[9] = 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-08-09 20:33:28 +02:00
|
|
|
vs[10] = ax + tx
|
|
|
|
vs[11] = cx + ty
|
|
|
|
vs[12] = u1
|
|
|
|
vs[13] = v0
|
2018-08-05 14:30:06 +02:00
|
|
|
vs[14] = u0
|
|
|
|
vs[15] = v1
|
2018-08-09 20:33:28 +02:00
|
|
|
vs[16] = cr
|
|
|
|
vs[17] = cg
|
|
|
|
vs[18] = cb
|
|
|
|
vs[19] = ca
|
2018-08-05 14:30:06 +02:00
|
|
|
|
2018-08-09 20:33:28 +02:00
|
|
|
vs[20] = by + tx
|
|
|
|
vs[21] = dy + ty
|
2018-08-05 14:30:06 +02:00
|
|
|
vs[22] = u0
|
2018-08-09 20:33:28 +02:00
|
|
|
vs[23] = v1
|
|
|
|
vs[24] = u1
|
|
|
|
vs[25] = v0
|
|
|
|
vs[26] = cr
|
|
|
|
vs[27] = cg
|
|
|
|
vs[28] = cb
|
|
|
|
vs[29] = ca
|
|
|
|
|
|
|
|
vs[30] = ax + by + tx
|
|
|
|
vs[31] = cx + dy + ty
|
|
|
|
vs[32] = u1
|
|
|
|
vs[33] = v1
|
|
|
|
vs[34] = u0
|
|
|
|
vs[35] = v0
|
|
|
|
vs[36] = cr
|
|
|
|
vs[37] = cg
|
|
|
|
vs[38] = cb
|
|
|
|
vs[39] = 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
|
|
|
|
}
|
2018-06-12 03:33:09 +02:00
|
|
|
|
|
|
|
func Vertex(width, height int, dx, dy, sx, sy float32, cr, cg, cb, ca float32) []float32 {
|
|
|
|
if !isPowerOf2(width) {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
if !isPowerOf2(height) {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
wf := float32(width)
|
|
|
|
hf := float32(height)
|
|
|
|
|
2018-10-06 08:01:12 +02:00
|
|
|
// Specify -1 for the source region, which means the source region is ignored.
|
|
|
|
//
|
|
|
|
// NaN would make more sense to represent an invalid state, but vertices including NaN values doesn't work on
|
|
|
|
// some machines (#696). Let's use negative numbers to represent such state.
|
2018-10-28 14:54:36 +01:00
|
|
|
vs := theVerticesBackend.slice(1)[0:10]
|
2018-06-12 03:33:09 +02:00
|
|
|
vs[0] = dx
|
|
|
|
vs[1] = dy
|
|
|
|
vs[2] = sx / wf
|
|
|
|
vs[3] = sy / hf
|
2018-10-03 19:02:19 +02:00
|
|
|
vs[4] = -1
|
|
|
|
vs[5] = -1
|
2018-06-12 03:33:09 +02:00
|
|
|
vs[6] = cr
|
|
|
|
vs[7] = cg
|
|
|
|
vs[8] = cb
|
|
|
|
vs[9] = ca
|
|
|
|
|
|
|
|
return vs
|
|
|
|
}
|