From 8e7e960b56c1c2bdeb3d5fbb7f8d6bf2a304e802 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 2 Dec 2017 20:51:18 +0900 Subject: [PATCH] graphics: Limit source-rectangle range Remove texture adjustment introduced due to #317 --- image_test.go | 6 +- internal/graphics/program.go | 2 +- internal/graphics/shader.go | 20 +++++- internal/restorable/images_test.go | 10 +-- vertices.go | 99 ++++++++++++++++-------------- vertices_ios.go | 22 ------- vertices_notios.go | 28 --------- 7 files changed, 78 insertions(+), 109 deletions(-) delete mode 100644 vertices_ios.go delete mode 100644 vertices_notios.go diff --git a/image_test.go b/image_test.go index a3051f36c..171d136ee 100644 --- a/image_test.go +++ b/image_test.go @@ -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) } diff --git a/internal/graphics/program.go b/internal/graphics/program.go index 5718ca41e..9a063703d 100644 --- a/internal/graphics/program.go +++ b/internal/graphics/program.go @@ -92,7 +92,7 @@ var ( { name: "tex_coord", dataType: opengl.Float, - num: 2, + num: 4, normalize: false, }, { diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index 740e482c8..df9daf898 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -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) { diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 66258b411..5bfee4510 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -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, } } diff --git a/vertices.go b/vertices.go index 9239e5dc1..c89d1e99d 100644 --- a/vertices.go +++ b/vertices.go @@ -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 } diff --git a/vertices_ios.go b/vertices_ios.go deleted file mode 100644 index 34e1979c5..000000000 --- a/vertices_ios.go +++ /dev/null @@ -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 -} diff --git a/vertices_notios.go b/vertices_notios.go deleted file mode 100644 index bd8d401f2..000000000 --- a/vertices_notios.go +++ /dev/null @@ -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 - } -}