graphics: Refactoring: Introduce arrayBufferLayoutPart

This commit is contained in:
Hajime Hoshi 2016-10-17 10:36:33 +09:00
parent 8ee859df31
commit cbcbdb0b97

View File

@ -20,41 +20,72 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/graphics/opengl"
) )
type arrayBufferLayoutPart struct {
name string
unit int // e.g. int16 is 2 [bytes]
num int
normalize bool
}
type arrayBufferLayout struct { type arrayBufferLayout struct {
parts []arrayBufferLayoutPart
}
func (a *arrayBufferLayout) newArrayBuffer(c *opengl.Context) opengl.Buffer {
total := 0
for _, p := range a.parts {
total += p.unit * p.num
}
return c.NewBuffer(opengl.ArrayBuffer, total*4*maxQuads, opengl.DynamicDraw)
} }
func (a *arrayBufferLayout) enable(c *opengl.Context, program opengl.Program) { func (a *arrayBufferLayout) enable(c *opengl.Context, program opengl.Program) {
c.EnableVertexAttribArray(program, "vertex") for _, p := range a.parts {
c.EnableVertexAttribArray(program, "tex_coord") c.EnableVertexAttribArray(program, p.name)
}
total := 0
for _, p := range a.parts {
total += p.unit * p.num
}
offset := 0
for _, p := range a.parts {
b := p.unit * p.num
// TODO: Argument order doesn't match with glVertexAttribPointer: Fix them. // TODO: Argument order doesn't match with glVertexAttribPointer: Fix them.
c.VertexAttribPointer(program, "vertex", false, a.totalBytes(), a.vertexNum(), 0) c.VertexAttribPointer(program, p.name, p.normalize, total, b, offset)
c.VertexAttribPointer(program, "tex_coord", true, a.totalBytes(), a.texCoordNum(), a.vertexBytes()) offset += b
}
} }
func (a *arrayBufferLayout) disable(c *opengl.Context, program opengl.Program) { func (a *arrayBufferLayout) disable(c *opengl.Context, program opengl.Program) {
c.DisableVertexAttribArray(program, "tex_coord") // TODO: Disabling should be done in reversed order?
c.DisableVertexAttribArray(program, "vertex") for _, p := range a.parts {
c.DisableVertexAttribArray(program, p.name)
}
} }
func (a *arrayBufferLayout) vertexNum() int { // unsafe.SizeOf can't be used because unsafe doesn't work with GopherJS.
return 2 const (
} int16Size = 2
)
func (a *arrayBufferLayout) vertexBytes() int { var (
return int16Size * a.vertexNum() theArrayBufferLayout = arrayBufferLayout{
} parts: []arrayBufferLayoutPart{
{
func (a *arrayBufferLayout) texCoordNum() int { name: "vertex",
return 2 unit: int16Size,
} num: 2,
normalize: false,
func (a *arrayBufferLayout) texCoordBytes() int { },
return int16Size * a.texCoordNum() {
} name: "tex_coord",
unit: int16Size,
func (a *arrayBufferLayout) totalBytes() int { num: 2,
return a.vertexBytes() + a.texCoordBytes() normalize: true,
},
},
} }
)
type openGLState struct { type openGLState struct {
arrayBuffer opengl.Buffer arrayBuffer opengl.Buffer
@ -70,7 +101,6 @@ type openGLState struct {
var ( var (
theOpenGLState openGLState theOpenGLState openGLState
theArrayBufferLayout = arrayBufferLayout{}
zeroBuffer opengl.Buffer zeroBuffer opengl.Buffer
zeroProgram opengl.Program zeroProgram opengl.Program
@ -82,11 +112,6 @@ const (
maxQuads = indicesNum / 6 maxQuads = indicesNum / 6
) )
// unsafe.SizeOf can't be used because unsafe doesn't work with GopherJS.
const (
int16Size = 2
)
func Reset(context *opengl.Context) error { func Reset(context *opengl.Context) error {
return theOpenGLState.reset(context) return theOpenGLState.reset(context)
} }
@ -131,8 +156,7 @@ func (s *openGLState) reset(context *opengl.Context) error {
return err return err
} }
stride := theArrayBufferLayout.totalBytes() s.arrayBuffer = theArrayBufferLayout.newArrayBuffer(context)
s.arrayBuffer = context.NewBuffer(opengl.ArrayBuffer, stride*4*maxQuads, opengl.DynamicDraw)
indices := make([]uint16, 6*maxQuads) indices := make([]uint16, 6*maxQuads)
for i := uint16(0); i < maxQuads; i++ { for i := uint16(0); i < maxQuads; i++ {