mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
Avoid type switch
This commit is contained in:
parent
71d463c102
commit
13a94d3446
10
image.go
10
image.go
@ -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.
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -174,22 +174,20 @@ 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:
|
||||
switch len(v) {
|
||||
case 4:
|
||||
l.Uniform4fv(1, v)
|
||||
case 16:
|
||||
v2 := [16]float32{}
|
||||
copy(v2[:], v)
|
||||
l.UniformMatrix4fv(false, v2)
|
||||
default:
|
||||
panic("not reach")
|
||||
}
|
||||
l.Uniform1i(v)
|
||||
}
|
||||
|
||||
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)
|
||||
case 16:
|
||||
v2 := [16]float32{}
|
||||
copy(v2[:], v)
|
||||
l.UniformMatrix4fv(false, v2)
|
||||
default:
|
||||
panic("not reach")
|
||||
}
|
||||
|
@ -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,18 +204,22 @@ 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:
|
||||
switch len(v) {
|
||||
case 4:
|
||||
gl.Call("uniform4fv", l, v)
|
||||
case 16:
|
||||
gl.UniformMatrix4fv(l, false, v)
|
||||
default:
|
||||
panic("not reach")
|
||||
}
|
||||
gl.Uniform1i(l, v)
|
||||
}
|
||||
|
||||
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)
|
||||
case 16:
|
||||
gl.UniformMatrix4fv(l, false, v)
|
||||
default:
|
||||
panic("not reach")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user