From 9418d4c5773ec3ba691c19daa6f8658103a6a380 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 12 Jan 2015 21:04:52 +0900 Subject: [PATCH] Change useProgramTexture not to return program --- .../graphics/internal/shader/drawtexture.go | 24 ++++------------- internal/graphics/internal/shader/program.go | 27 +++++++++++++++++-- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/internal/graphics/internal/shader/drawtexture.go b/internal/graphics/internal/shader/drawtexture.go index acfb2ecc9..fc6968d60 100644 --- a/internal/graphics/internal/shader/drawtexture.go +++ b/internal/graphics/internal/shader/drawtexture.go @@ -40,12 +40,11 @@ type TextureQuads interface { var initialized = false func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4][4]float64, quads TextureQuads, geo Matrix, color Matrix) error { - // unsafe.SizeOf can't be used because unsafe doesn't work with GopherJS. - const float32Size = 4 - // TODO: WebGL doesn't seem to have Check gl.MAX_ELEMENTS_VERTICES or gl.MAX_ELEMENTS_INDICES so far. - // Let's use them to compare to len(quads). + // Let's use them to compare to len(quads) in the future. + const stride = 4 * 4 + if !initialized { if err := initialize(c); err != nil { return err @@ -57,21 +56,8 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4 return nil } - program := useProgramTexture(c, glMatrix(projectionMatrix), geo, color) - - // We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture - // See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml - c.BindTexture(texture) - - c.EnableVertexAttribArray(program, "vertex") - c.EnableVertexAttribArray(program, "tex_coord") - defer func() { - c.DisableVertexAttribArray(program, "tex_coord") - c.DisableVertexAttribArray(program, "vertex") - }() - - c.VertexAttribPointer(program, "vertex", stride, uintptr(float32Size*0)) - c.VertexAttribPointer(program, "tex_coord", stride, uintptr(float32Size*2)) + f := useProgramTexture(c, glMatrix(projectionMatrix), texture, geo, color) + defer f.FinishProgram() vertices := make([]float32, 0, stride*quads.Len()) for i := 0; i < quads.Len(); i++ { diff --git a/internal/graphics/internal/shader/program.go b/internal/graphics/internal/shader/program.go index b0d25937c..7dc6be8e0 100644 --- a/internal/graphics/internal/shader/program.go +++ b/internal/graphics/internal/shader/program.go @@ -64,7 +64,17 @@ func initialize(c *opengl.Context) error { var lastProgram opengl.Program -func useProgramTexture(c *opengl.Context, projectionMatrix []float32, geo Matrix, color Matrix) opengl.Program { +type programFinisher func() + +func (p programFinisher) FinishProgram() { + p() +} + +func useProgramTexture(c *opengl.Context, projectionMatrix []float32, texture opengl.Texture, geo Matrix, color Matrix) programFinisher { + // unsafe.SizeOf can't be used because unsafe doesn't work with GopherJS. + const float32Size = 4 + const stride = 4 * 4 + if lastProgram != programTexture { c.UseProgram(programTexture) lastProgram = programTexture @@ -107,5 +117,18 @@ func useProgramTexture(c *opengl.Context, projectionMatrix []float32, geo Matrix } c.UniformFloats(program, "color_matrix_translation", glColorMatrixTranslation) - return program + // We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture + // See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml + c.BindTexture(texture) + + c.EnableVertexAttribArray(program, "vertex") + c.EnableVertexAttribArray(program, "tex_coord") + + c.VertexAttribPointer(program, "vertex", stride, uintptr(float32Size*0)) + c.VertexAttribPointer(program, "tex_coord", stride, uintptr(float32Size*2)) + + return func() { + c.DisableVertexAttribArray(program, "tex_coord") + c.DisableVertexAttribArray(program, "vertex") + } }