This commit is contained in:
Brandon 2018-10-06 10:01:05 -06:00
parent a25a444cbf
commit fa10844351
2 changed files with 15 additions and 39 deletions

View File

@ -17,7 +17,7 @@ import (
// It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor. // It supports TrianglesPosition, TrianglesColor, TrianglesPicture and PictureColor.
type Canvas struct { type Canvas struct {
gf *GLFrame gf *GLFrame
shader *GLShader shader *glShader
cmp pixel.ComposeMethod cmp pixel.ComposeMethod
mat mgl32.Mat3 mat mgl32.Mat3
@ -47,19 +47,14 @@ func NewCanvas(bounds pixel.Rect) *Canvas {
// attribute variable. If the uniform already exists, including defaults, they will be reassigned // attribute variable. If the uniform already exists, including defaults, they will be reassigned
// to the new value. The value can be a pointer. // to the new value. The value can be a pointer.
func (c *Canvas) SetUniform(Name string, Value interface{}) { func (c *Canvas) SetUniform(Name string, Value interface{}) {
c.shader.AddUniform(Name, Value) c.shader.SetUniform(Name, Value)
}
// UpdateShader needs to be called after any changes to the underlying GLShader
// are made, such as, SetUniform, SetFragmentShader...
func (c *Canvas) UpdateShader() {
c.shader.update()
} }
// SetFragmentShader allows you to set a new fragment shader on the underlying // SetFragmentShader allows you to set a new fragment shader on the underlying
// framebuffer. Argument "fs" is the GLSL source, not a filename. // framebuffer. Argument "fs" is the GLSL source, not a filename.
func (c *Canvas) SetFragmentShader(fs string) { func (c *Canvas) SetFragmentShader(fs string) {
c.shader.fs = fs c.shader.fs = fs
c.shader.update()
} }
// MakeTriangles creates a specialized copy of the supplied Triangles that draws onto this Canvas. // MakeTriangles creates a specialized copy of the supplied Triangles that draws onto this Canvas.
@ -189,25 +184,6 @@ func setBlendFunc(cmp pixel.ComposeMethod) {
} }
} }
// updates all uniform values for gl to consume
func (c *Canvas) setUniforms(texbounds pixel.Rect) {
mat := c.mat
col := c.col
c.shader.uniformDefaults.transform = mat
c.shader.uniformDefaults.colormask = col
dstBounds := c.Bounds()
c.shader.uniformDefaults.bounds = mgl32.Vec4{
float32(dstBounds.Min.X),
float32(dstBounds.Min.Y),
float32(dstBounds.W()),
float32(dstBounds.H()),
}
for loc, u := range c.shader.uniforms {
c.shader.s.SetUniformAttr(loc, u.Value())
}
}
// Clear fills the whole Canvas with a single color. // Clear fills the whole Canvas with a single color.
func (c *Canvas) Clear(color color.Color) { func (c *Canvas) Clear(color color.Color) {
c.gf.Dirty() c.gf.Dirty()

View File

@ -7,10 +7,10 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// GLShader is a type to assist with managing a canvas's underlying // glShader is a type to assist with managing a canvas's underlying
// shader configuration. This allows for customization of shaders on // shader configuration. This allows for customization of shaders on
// a per canvas basis. // a per canvas basis.
type GLShader struct { type glShader struct {
s *glhf.Shader s *glhf.Shader
vf, uf glhf.AttrFormat vf, uf glhf.AttrFormat
vs, fs string vs, fs string
@ -33,7 +33,7 @@ type gsUniformAttr struct {
} }
// reinitialize GLShader data and recompile the underlying gl shader object // reinitialize GLShader data and recompile the underlying gl shader object
func (gs *GLShader) update() { func (gs *glShader) update() {
gs.uf = nil gs.uf = nil
for _, u := range gs.uniforms { for _, u := range gs.uniforms {
gs.uf = append(gs.uf, glhf.Attr{ gs.uf = append(gs.uf, glhf.Attr{
@ -59,7 +59,7 @@ func (gs *GLShader) update() {
} }
// gets the uniform index from GLShader // gets the uniform index from GLShader
func (gs *GLShader) getUniform(Name string) int { func (gs *glShader) getUniform(Name string) int {
for i, u := range gs.uniforms { for i, u := range gs.uniforms {
if u.Name == Name { if u.Name == Name {
return i return i
@ -68,14 +68,14 @@ func (gs *GLShader) getUniform(Name string) int {
return -1 return -1
} }
// AddUniform appends a custom uniform name and value to the shader. // SetUniform appends a custom uniform name and value to the shader.
// if the uniform already exists, it will simply be overwritten. // if the uniform already exists, it will simply be overwritten.
// //
// example: // example:
// //
// utime := float32(time.Since(starttime)).Seconds()) // utime := float32(time.Since(starttime)).Seconds())
// mycanvas.shader.AddUniform("u_time", &utime) // mycanvas.shader.AddUniform("u_time", &utime)
func (gs *GLShader) AddUniform(Name string, Value interface{}) { func (gs *glShader) SetUniform(Name string, Value interface{}) {
t, p := getAttrType(Value) t, p := getAttrType(Value)
if loc := gs.getUniform(Name); loc > -1 { if loc := gs.getUniform(Name); loc > -1 {
gs.uniforms[loc].Name = Name gs.uniforms[loc].Name = Name
@ -94,18 +94,18 @@ func (gs *GLShader) AddUniform(Name string, Value interface{}) {
// Sets up a base shader with everything needed for a Pixel // Sets up a base shader with everything needed for a Pixel
// canvas to render correctly. The defaults can be overridden // canvas to render correctly. The defaults can be overridden
// by simply using the AddUniform function. // by simply using the SetUniform function.
func baseShader(c *Canvas) { func baseShader(c *Canvas) {
gs := &GLShader{ gs := &glShader{
vf: defaultCanvasVertexFormat, vf: defaultCanvasVertexFormat,
vs: defaultCanvasVertexShader, vs: defaultCanvasVertexShader,
fs: baseCanvasFragmentShader, fs: baseCanvasFragmentShader,
} }
gs.AddUniform("u_transform", &gs.uniformDefaults.transform) gs.SetUniform("u_transform", &gs.uniformDefaults.transform)
gs.AddUniform("u_colormask", &gs.uniformDefaults.colormask) gs.SetUniform("u_colormask", &gs.uniformDefaults.colormask)
gs.AddUniform("u_bounds", &gs.uniformDefaults.bounds) gs.SetUniform("u_bounds", &gs.uniformDefaults.bounds)
gs.AddUniform("u_texbounds", &gs.uniformDefaults.texbounds) gs.SetUniform("u_texbounds", &gs.uniformDefaults.texbounds)
c.shader = gs c.shader = gs
} }