graphics: Refactoring

This commit is contained in:
Hajime Hoshi 2016-04-08 04:39:13 +09:00
parent e9ffe13739
commit 07da7a2348
2 changed files with 8 additions and 15 deletions

View File

@ -69,17 +69,12 @@ func (w *wholeImage) Src(i int) (x0, y0, x1, y1 int) {
return 0, 0, w.width, w.height return 0, 0, w.width, w.height
} }
type uv struct { func u(x, width2p int) int16 {
width2p int return int16(math.MaxInt16 * x / width2p)
height2p int
} }
func (c *uv) u(x int) int16 { func v(y, height2p int) int16 {
return int16(math.MaxInt16 * x / c.width2p) return int16(math.MaxInt16 * y / height2p)
}
func (c *uv) v(y int) int16 {
return int16(math.MaxInt16 * y / c.height2p)
} }
type textureQuads struct { type textureQuads struct {
@ -96,10 +91,8 @@ func (t *textureQuads) vertices(vertices []int16) int {
p := t.parts p := t.parts
w, h := t.width, t.height w, h := t.width, t.height
n := 0 n := 0
c := uv{ width2p := int(graphics.NextPowerOf2Int32(int32(w)))
width2p: int(graphics.NextPowerOf2Int32(int32(w))), height2p := int(graphics.NextPowerOf2Int32(int32(h)))
height2p: int(graphics.NextPowerOf2Int32(int32(h))),
}
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
dx0, dy0, dx1, dy1 := p.Dst(i) dx0, dy0, dx1, dy1 := p.Dst(i)
if dx0 == dx1 || dy0 == dy1 { if dx0 == dx1 || dy0 == dy1 {
@ -110,7 +103,7 @@ func (t *textureQuads) vertices(vertices []int16) int {
if sx0 == sx1 || sy0 == sy1 { if sx0 == sx1 || sy0 == sy1 {
continue continue
} }
u0, v0, u1, v1 := c.u(sx0), c.v(sy0), c.u(sx1), c.v(sy1) u0, v0, u1, v1 := u(sx0, width2p), v(sy0, height2p), u(sx1, width2p), v(sy1, height2p)
vertices[16*n] = x0 vertices[16*n] = x0
vertices[16*n+1] = y0 vertices[16*n+1] = y0
vertices[16*n+2] = u0 vertices[16*n+2] = u0

View File

@ -66,7 +66,7 @@ func drawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
} }
p.begin() p.begin()
defer p.end() defer p.end()
c.BufferSubData(c.ArrayBuffer, vertices[:16*n]) c.BufferSubData(c.ArrayBuffer, vertices)
c.DrawElements(c.Triangles, 6*n) c.DrawElements(c.Triangles, 6*n)
return nil return nil
} }