Avoid type switch

This commit is contained in:
Hajime Hoshi 2015-01-03 15:52:02 +09:00
parent 71d463c102
commit 13a94d3446
5 changed files with 41 additions and 39 deletions

View File

@ -68,12 +68,12 @@ func (i *innerImage) drawImage(c *opengl.Context, img *innerImage, options *Draw
return i.framebuffer.DrawTexture(c, img.texture, quads, &options.GeoM, &options.ColorM)
}
func u(x float64, width int) float32 {
return float32(x) / float32(internal.NextPowerOf2Int(width))
func u(x float32, width int) float32 {
return x / float32(internal.NextPowerOf2Int(width))
}
func v(y float64, height int) float32 {
return float32(y) / float32(internal.NextPowerOf2Int(height))
func v(y float32, height int) float32 {
return y / float32(internal.NextPowerOf2Int(height))
}
type textureQuads struct {
@ -94,7 +94,7 @@ func (t *textureQuads) Vertex(i int) (x0, y0, x1, y1 float32) {
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) {
src := t.parts[i].Src
w, h := t.width, t.height
return u(float64(src.Min.X), w), v(float64(src.Min.Y), h), u(float64(src.Max.X), w), v(float64(src.Max.Y), h)
return u(float32(src.Min.X), w), v(float32(src.Min.Y), h), u(float32(src.Max.X), w), v(float32(src.Max.Y), h)
}
// Image represents an image.

View File

@ -73,7 +73,7 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4
c.VertexAttribPointer(program, "vertex", stride, uintptr(float32Size*0))
c.VertexAttribPointer(program, "tex_coord", stride, uintptr(float32Size*2))
vertices := []float32{}
vertices := make([]float32, 0, quads.Len())
for i := 0; i < quads.Len(); i++ {
x0, y0, x1, y1 := quads.Vertex(i)
u0, v0, u1, v1 := quads.Texture(i)

View File

@ -73,7 +73,7 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma
// TODO: Check the performance.
program := programColorMatrix
c.Uniform(program, "projection_matrix", projectionMatrix)
c.UniformFloats(program, "projection_matrix", projectionMatrix)
ma := float32(geo.Element(0, 0))
mb := float32(geo.Element(0, 1))
@ -87,8 +87,8 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma
0, 0, 1, 0,
tx, ty, 0, 1,
}
c.Uniform(program, "modelview_matrix", glModelviewMatrix)
c.Uniform(program, "texture", 0)
c.UniformFloats(program, "modelview_matrix", glModelviewMatrix)
c.UniformInt(program, "texture", 0)
e := [4][5]float32{}
for i := 0; i < 4; i++ {
@ -103,11 +103,11 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma
e[0][2], e[1][2], e[2][2], e[3][2],
e[0][3], e[1][3], e[2][3], e[3][3],
}
c.Uniform(program, "color_matrix", glColorMatrix)
c.UniformFloats(program, "color_matrix", glColorMatrix)
glColorMatrixTranslation := []float32{
e[0][4], e[1][4], e[2][4], e[3][4],
}
c.Uniform(program, "color_matrix_translation", glColorMatrixTranslation)
c.UniformFloats(program, "color_matrix_translation", glColorMatrixTranslation)
return program
}

View File

@ -174,12 +174,13 @@ func (c *Context) UseProgram(p Program) {
gl.Program(p).Use()
}
func (c *Context) Uniform(p Program, location string, v interface{}) {
func (c *Context) UniformInt(p Program, location string, v int) {
l := gl.Program(p).GetUniformLocation(location)
switch v := v.(type) {
case int:
l.Uniform1i(v)
case []float32:
}
func (c *Context) UniformFloats(p Program, location string, v []float32) {
l := gl.Program(p).GetUniformLocation(location)
switch len(v) {
case 4:
l.Uniform4fv(1, v)
@ -190,9 +191,6 @@ func (c *Context) Uniform(p Program, location string, v interface{}) {
default:
panic("not reach")
}
default:
panic("not reach")
}
}
func (c *Context) VertexAttribPointer(p Program, location string, stride int, v uintptr) {

View File

@ -196,7 +196,7 @@ func (c *Context) UseProgram(p Program) {
gl.UseProgram(p)
}
func (c *Context) Uniform(p Program, location string, v interface{}) {
func (c *Context) UniformInt(p Program, location string, v int) {
gl := c.gl
key := locationCacheKey{p, location}
l, ok := uniformLocationCache[key]
@ -204,10 +204,17 @@ func (c *Context) Uniform(p Program, location string, v interface{}) {
l = gl.GetUniformLocation(p, location)
uniformLocationCache[key] = l
}
switch v := v.(type) {
case int:
gl.Uniform1i(l, v)
case []float32:
}
func (c *Context) UniformFloats(p Program, location string, v []float32) {
gl := c.gl
key := locationCacheKey{p, location}
l, ok := uniformLocationCache[key]
if !ok {
l = gl.GetUniformLocation(p, location)
uniformLocationCache[key] = l
}
switch len(v) {
case 4:
gl.Call("uniform4fv", l, v)
@ -216,9 +223,6 @@ func (c *Context) Uniform(p Program, location string, v interface{}) {
default:
panic("not reach")
}
default:
panic("not reach")
}
}
func (c *Context) VertexAttribPointer(p Program, location string, stride int, v uintptr) {