graphics: Bug fix: wrong texels were used (#546)

This commit is contained in:
Hajime Hoshi 2018-03-11 03:02:03 +09:00
parent b474946619
commit efea65ee58
2 changed files with 34 additions and 23 deletions

View File

@ -545,10 +545,11 @@ func TestImageEdge(t *testing.T) {
} }
} }
img0.ReplacePixels(pixels) img0.ReplacePixels(pixels)
img1, _ := NewImage(img1Width, img1Height, FilterNearest) img1, _ := NewImage(img1Width, img1Height, FilterDefault)
red := color.RGBA{0xff, 0, 0, 0xff} red := color.RGBA{0xff, 0, 0, 0xff}
transparent := color.RGBA{0, 0, 0, 0} transparent := color.RGBA{0, 0, 0, 0}
for _, f := range []Filter{FilterNearest, FilterLinear} {
for a := 0; a < 360; a += 5 { for a := 0; a < 360; a += 5 {
img1.Clear() img1.Clear()
op := &DrawImageOptions{} op := &DrawImageOptions{}
@ -558,17 +559,27 @@ func TestImageEdge(t *testing.T) {
op.GeoM.Translate(-float64(img0Width)/2, -float64(img0Height)/2) op.GeoM.Translate(-float64(img0Width)/2, -float64(img0Height)/2)
op.GeoM.Rotate(float64(a) * math.Pi / 180) op.GeoM.Rotate(float64(a) * math.Pi / 180)
op.GeoM.Translate(img1Width/2, img1Height/2) op.GeoM.Translate(img1Width/2, img1Height/2)
op.Filter = f
img1.DrawImage(img0, op) img1.DrawImage(img0, op)
for j := 0; j < img1Height; j++ { for j := 0; j < img1Height; j++ {
for i := 0; i < img1Width; i++ { for i := 0; i < img1Width; i++ {
c := img1.At(i, j) c := img1.At(i, j)
if c == red {
continue
}
if c == transparent { if c == transparent {
continue continue
} }
t.Errorf("img1.At(%d, %d) (angle: %d) want: red or transparent, got: %v", i, j, a, c) switch f {
case FilterNearest:
if c == red {
continue
}
case FilterLinear:
_, g, b, _ := c.RGBA()
if g == 0 && b == 0 {
continue
}
}
t.Errorf("img1.At(%d, %d) (filter: %d, angle: %d) want: red or transparent, got: %v", i, j, f, a, c)
}
} }
} }
} }

View File

@ -114,8 +114,8 @@ void main(void) {
pos = roundTexel(pos); pos = roundTexel(pos);
highp vec2 texel_size = 1.0 / source_size; highp vec2 texel_size = 1.0 / source_size;
highp vec2 p0 = pos; highp vec2 p0 = pos - texel_size / 2.0;
highp vec2 p1 = pos + texel_size; highp vec2 p1 = pos + texel_size / 2.0;
vec4 c0 = texture2D(texture, p0); vec4 c0 = texture2D(texture, p0);
vec4 c1 = texture2D(texture, vec2(p1.x, p0.y)); vec4 c1 = texture2D(texture, vec2(p1.x, p0.y));
vec4 c2 = texture2D(texture, vec2(p0.x, p1.y)); vec4 c2 = texture2D(texture, vec2(p0.x, p1.y));
@ -137,7 +137,7 @@ void main(void) {
c3 = vec4(0, 0, 0, 0); c3 = vec4(0, 0, 0, 0);
} }
vec2 rate = fract(pos * source_size); vec2 rate = fract(p0 * source_size);
vec4 color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y); vec4 color = mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y);
#endif #endif