graphics: Limit source-rectangle range

Remove texture adjustment introduced due to #317
This commit is contained in:
Hajime Hoshi 2017-12-02 20:51:18 +09:00
parent c2fab6cca3
commit 8e7e960b56
7 changed files with 78 additions and 109 deletions

View File

@ -529,10 +529,8 @@ func TestImageEdge(t *testing.T) {
}
red := color.RGBA{0xff, 0, 0, 0xff}
transparent := color.RGBA{0, 0, 0, 0}
// Unfortunately, TravisCI couldn't pass this test for some angles.
// https://travis-ci.org/hajimehoshi/ebiten/builds/200454658
// Let's use 'decent' angles here.
for _, a := range []int{0, 45, 90, 135, 180, 225, 270, 315, 360} {
for a := 0; a < 360; a += 5 {
if err := img1.Clear(); err != nil {
t.Fatal(err)
}

View File

@ -92,7 +92,7 @@ var (
{
name: "tex_coord",
dataType: opengl.Float,
num: 2,
num: 4,
normalize: false,
},
{

View File

@ -29,13 +29,19 @@ var shaders = map[shaderId]string{
shaderVertexModelview: `
uniform mat4 projection_matrix;
attribute vec2 vertex;
attribute vec2 tex_coord;
attribute vec4 tex_coord;
attribute vec4 geo_matrix_body;
attribute vec2 geo_matrix_translation;
varying vec2 vertex_out_tex_coord;
varying vec2 vertex_out_tex_coord_min;
varying vec2 vertex_out_tex_coord_max;
void main(void) {
vertex_out_tex_coord = tex_coord;
vertex_out_tex_coord = vec2(tex_coord[0], tex_coord[1]);
vertex_out_tex_coord_min =
vec2(min(tex_coord[0], tex_coord[2]), min(tex_coord[1], tex_coord[3]));
vertex_out_tex_coord_max =
vec2(max(tex_coord[0], tex_coord[2]), max(tex_coord[1], tex_coord[3]));
mat4 geo_matrix = mat4(
vec4(geo_matrix_body[0], geo_matrix_body[2], 0, 0),
vec4(geo_matrix_body[1], geo_matrix_body[3], 0, 0),
@ -58,9 +64,17 @@ uniform sampler2D texture;
uniform mat4 color_matrix;
uniform vec4 color_matrix_translation;
varying vec2 vertex_out_tex_coord;
varying vec2 vertex_out_tex_coord_min;
varying vec2 vertex_out_tex_coord_max;
void main(void) {
vec4 color = texture2D(texture, vertex_out_tex_coord);
vec4 color = vec4(0, 0, 0, 0);
if (vertex_out_tex_coord_min.x <= vertex_out_tex_coord.x &&
vertex_out_tex_coord_min.y <= vertex_out_tex_coord.y &&
vertex_out_tex_coord.x < vertex_out_tex_coord_max.x &&
vertex_out_tex_coord.y < vertex_out_tex_coord_max.y) {
color = texture2D(texture, vertex_out_tex_coord);
}
// Un-premultiply alpha
if (0.0 < color.a) {

View File

@ -75,11 +75,13 @@ func vertices(sw, sh int, x, y int) []float32 {
shf := float32(sh)
tx := float32(x)
ty := float32(y)
// For the rule of values, see vertices.go.
return []float32{
0, 0, 0, 0, a, b, c, d, tx, ty,
0, shf, 0, 1, a, b, c, d, tx, ty,
swf, 0, 1, 0, a, b, c, d, tx, ty,
swf, shf, 1, 1, a, b, c, d, tx, ty,
0, 0, 0, 0, 1, 1, a, b, c, d, tx, ty,
0, shf, 0, 1, 1, 0, a, b, c, d, tx, ty,
swf, 0, 1, 0, 0, 1, a, b, c, d, tx, ty,
swf, shf, 1, 1, 0, 0, a, b, c, d, tx, ty,
}
}

View File

@ -19,12 +19,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/restorable"
)
// texelAdjustment represents a number to be used to adjust texel.
// Texels are adjusted by amount propotional to inverse of texelAdjustment.
// This is necessary not to use unexpected pixels outside of texels.
// See #317.
var texelAdjustment float32 = 256
var (
quadFloat32Num = restorable.QuadVertexSizeInBytes() / 4
theVerticesBackend = &verticesBackend{}
@ -74,54 +68,65 @@ func vertices(sx0, sy0, sx1, sy1 int, width, height int, geo *affine.GeoM) []flo
hf := float32(h)
x0, y0, x1, y1 := float32(0), float32(0), float32(sx1-sx0), float32(sy1-sy0)
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
// Adjust texels to fix a problem that outside texels are used (#317).
if texelAdjustment > 0 {
u1 -= 1.0 / wf / texelAdjustment
v1 -= 1.0 / hf / texelAdjustment
}
// Vertex coordinates
vs[0] = x0
vs[1] = y0
// 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.
vs[2] = u0
vs[3] = v0
vs[4] = g0
vs[5] = g1
vs[6] = g2
vs[7] = g3
vs[8] = g4
vs[9] = g5
vs[4] = u1
vs[5] = v1
vs[10] = x1
vs[11] = y0
vs[12] = u1
vs[13] = v0
vs[14] = g0
vs[15] = g1
vs[16] = g2
vs[17] = g3
vs[18] = g4
vs[19] = g5
// Geometry matrix
vs[6] = g0
vs[7] = g1
vs[8] = g2
vs[9] = g3
vs[10] = g4
vs[11] = g5
vs[20] = x0
vs[21] = y1
vs[22] = u0
vs[23] = v1
vs[24] = g0
vs[25] = g1
vs[26] = g2
vs[27] = g3
vs[28] = g4
vs[29] = g5
vs[12] = x1
vs[13] = y0
vs[14] = u1
vs[15] = v0
vs[16] = u0
vs[17] = v1
vs[18] = g0
vs[19] = g1
vs[20] = g2
vs[21] = g3
vs[22] = g4
vs[23] = g5
vs[30] = x1
vs[31] = y1
vs[32] = u1
vs[33] = v1
vs[34] = g0
vs[35] = g1
vs[36] = g2
vs[37] = g3
vs[38] = g4
vs[39] = g5
vs[24] = x0
vs[25] = y1
vs[26] = u0
vs[27] = v1
vs[28] = u1
vs[29] = v0
vs[30] = g0
vs[31] = g1
vs[32] = g2
vs[33] = g3
vs[34] = g4
vs[35] = g5
vs[36] = x1
vs[37] = y1
vs[38] = u1
vs[39] = v1
vs[40] = u0
vs[41] = v0
vs[42] = g0
vs[43] = g1
vs[44] = g2
vs[45] = g3
vs[46] = g4
vs[47] = g5
return vs
}

View File

@ -1,22 +0,0 @@
// 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.
// +build ios
package ebiten
func init() {
// Texel adjustment causes glitches on iOS.
texelAdjustment = 0
}

View File

@ -1,28 +0,0 @@
// 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.
// +build !ios
package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/web"
)
func init() {
if web.IsIOSSafari() {
// Texel adjustment causes glitches on iOS.
texelAdjustment = 0
}
}