mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +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)
|
return i.framebuffer.DrawTexture(c, img.texture, quads, &options.GeoM, &options.ColorM)
|
||||||
}
|
}
|
||||||
|
|
||||||
func u(x float64, width int) float32 {
|
func u(x float32, width int) float32 {
|
||||||
return float32(x) / float32(internal.NextPowerOf2Int(width))
|
return x / float32(internal.NextPowerOf2Int(width))
|
||||||
}
|
}
|
||||||
|
|
||||||
func v(y float64, height int) float32 {
|
func v(y float32, height int) float32 {
|
||||||
return float32(y) / float32(internal.NextPowerOf2Int(height))
|
return y / float32(internal.NextPowerOf2Int(height))
|
||||||
}
|
}
|
||||||
|
|
||||||
type textureQuads struct {
|
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) {
|
func (t *textureQuads) Texture(i int) (u0, v0, u1, v1 float32) {
|
||||||
src := t.parts[i].Src
|
src := t.parts[i].Src
|
||||||
w, h := t.width, t.height
|
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.
|
// 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, "vertex", stride, uintptr(float32Size*0))
|
||||||
c.VertexAttribPointer(program, "tex_coord", stride, uintptr(float32Size*2))
|
c.VertexAttribPointer(program, "tex_coord", stride, uintptr(float32Size*2))
|
||||||
|
|
||||||
vertices := []float32{}
|
vertices := make([]float32, 0, quads.Len())
|
||||||
for i := 0; i < quads.Len(); i++ {
|
for i := 0; i < quads.Len(); i++ {
|
||||||
x0, y0, x1, y1 := quads.Vertex(i)
|
x0, y0, x1, y1 := quads.Vertex(i)
|
||||||
u0, v0, u1, v1 := quads.Texture(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.
|
// TODO: Check the performance.
|
||||||
program := programColorMatrix
|
program := programColorMatrix
|
||||||
|
|
||||||
c.Uniform(program, "projection_matrix", projectionMatrix)
|
c.UniformFloats(program, "projection_matrix", projectionMatrix)
|
||||||
|
|
||||||
ma := float32(geo.Element(0, 0))
|
ma := float32(geo.Element(0, 0))
|
||||||
mb := float32(geo.Element(0, 1))
|
mb := float32(geo.Element(0, 1))
|
||||||
@ -87,8 +87,8 @@ func useProgramColorMatrix(c *opengl.Context, projectionMatrix []float32, geo Ma
|
|||||||
0, 0, 1, 0,
|
0, 0, 1, 0,
|
||||||
tx, ty, 0, 1,
|
tx, ty, 0, 1,
|
||||||
}
|
}
|
||||||
c.Uniform(program, "modelview_matrix", glModelviewMatrix)
|
c.UniformFloats(program, "modelview_matrix", glModelviewMatrix)
|
||||||
c.Uniform(program, "texture", 0)
|
c.UniformInt(program, "texture", 0)
|
||||||
|
|
||||||
e := [4][5]float32{}
|
e := [4][5]float32{}
|
||||||
for i := 0; i < 4; i++ {
|
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][2], e[1][2], e[2][2], e[3][2],
|
||||||
e[0][3], e[1][3], e[2][3], e[3][3],
|
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{
|
glColorMatrixTranslation := []float32{
|
||||||
e[0][4], e[1][4], e[2][4], e[3][4],
|
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
|
return program
|
||||||
}
|
}
|
||||||
|
@ -174,22 +174,20 @@ func (c *Context) UseProgram(p Program) {
|
|||||||
gl.Program(p).Use()
|
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)
|
l := gl.Program(p).GetUniformLocation(location)
|
||||||
switch v := v.(type) {
|
l.Uniform1i(v)
|
||||||
case int:
|
}
|
||||||
l.Uniform1i(v)
|
|
||||||
case []float32:
|
func (c *Context) UniformFloats(p Program, location string, v []float32) {
|
||||||
switch len(v) {
|
l := gl.Program(p).GetUniformLocation(location)
|
||||||
case 4:
|
switch len(v) {
|
||||||
l.Uniform4fv(1, v)
|
case 4:
|
||||||
case 16:
|
l.Uniform4fv(1, v)
|
||||||
v2 := [16]float32{}
|
case 16:
|
||||||
copy(v2[:], v)
|
v2 := [16]float32{}
|
||||||
l.UniformMatrix4fv(false, v2)
|
copy(v2[:], v)
|
||||||
default:
|
l.UniformMatrix4fv(false, v2)
|
||||||
panic("not reach")
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
panic("not reach")
|
panic("not reach")
|
||||||
}
|
}
|
||||||
|
@ -196,7 +196,7 @@ func (c *Context) UseProgram(p Program) {
|
|||||||
gl.UseProgram(p)
|
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
|
gl := c.gl
|
||||||
key := locationCacheKey{p, location}
|
key := locationCacheKey{p, location}
|
||||||
l, ok := uniformLocationCache[key]
|
l, ok := uniformLocationCache[key]
|
||||||
@ -204,18 +204,22 @@ func (c *Context) Uniform(p Program, location string, v interface{}) {
|
|||||||
l = gl.GetUniformLocation(p, location)
|
l = gl.GetUniformLocation(p, location)
|
||||||
uniformLocationCache[key] = l
|
uniformLocationCache[key] = l
|
||||||
}
|
}
|
||||||
switch v := v.(type) {
|
gl.Uniform1i(l, v)
|
||||||
case int:
|
}
|
||||||
gl.Uniform1i(l, v)
|
|
||||||
case []float32:
|
func (c *Context) UniformFloats(p Program, location string, v []float32) {
|
||||||
switch len(v) {
|
gl := c.gl
|
||||||
case 4:
|
key := locationCacheKey{p, location}
|
||||||
gl.Call("uniform4fv", l, v)
|
l, ok := uniformLocationCache[key]
|
||||||
case 16:
|
if !ok {
|
||||||
gl.UniformMatrix4fv(l, false, v)
|
l = gl.GetUniformLocation(p, location)
|
||||||
default:
|
uniformLocationCache[key] = l
|
||||||
panic("not reach")
|
}
|
||||||
}
|
switch len(v) {
|
||||||
|
case 4:
|
||||||
|
gl.Call("uniform4fv", l, v)
|
||||||
|
case 16:
|
||||||
|
gl.UniformMatrix4fv(l, false, v)
|
||||||
default:
|
default:
|
||||||
panic("not reach")
|
panic("not reach")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user