// Draw draws the Sprite object. func (sprite *Sprite) Draw(frame int) error { gl.BindBuffer(gl.ARRAY_BUFFER, sprite.shape.vertexBuffer) gl.VertexAttribPointer(gl.Uint(0), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0)) gl.BindAttribLocation(paunchEffect.program, gl.Uint(0), gl.GLString("position")) gl.BindBuffer(gl.ARRAY_BUFFER, 0) if sprite.texcoordBuffer != 0 { gl.ActiveTexture(gl.TEXTURE0) gl.BindBuffer(gl.ARRAY_BUFFER, sprite.texcoordBuffer) gl.VertexAttribPointer(gl.Uint(1), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0)) gl.BindAttribLocation(paunchEffect.program, gl.Uint(0), gl.GLString("texcoord")) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindTexture(gl.TEXTURE_2D, sprite.texture[frame]) gl.EnableVertexAttribArray(gl.Uint(1)) } gl.EnableVertexAttribArray(gl.Uint(0)) gl.DrawArrays(gl.TRIANGLES, 0, gl.Sizei(sprite.shape.size)) gl.DisableVertexAttribArray(gl.Uint(0)) gl.DisableVertexAttribArray(gl.Uint(1)) gl.BindTexture(gl.TEXTURE_2D, 0) return checkForErrors() }
func shinit() gl.Int { vs := loadShader(gl.VERTEX_SHADER, vss) var sfs string if *blend { sfs = bfss } else { sfs = fss } fs := loadShader(gl.FRAGMENT_SHADER, sfs) prg := gl.CreateProgram() gl.AttachShader(prg, vs) gl.AttachShader(prg, fs) gl.LinkProgram(prg) var l int if *blend { l = 6 } else { l = 3 } gl.UseProgram(prg) names := []string{"yt1", "cbt1", "crt1", "yt0", "cbt0", "crt0"} for i := 0; i < l; i++ { loc := gl.GetUniformLocation(prg, gl.GLString(names[i])) gl.Uniform1i(loc, gl.Int(i)) } return gl.GetUniformLocation(prg, gl.GLString("factor")) }
func loadTextFile(filename string) (*gl.Char, error) { text, err := ioutil.ReadFile(filename) if err != nil { return gl.GLString(""), err } return gl.GLString(string(text)), nil }
func loadShader(shtype gl.Enum, src string) gl.Uint { sh := gl.CreateShader(shtype) gsrc := gl.GLString(src) gl.ShaderSource(sh, 1, &gsrc, nil) gl.CompileShader(sh) return sh }
func (s *Shader) initUniform(name string, uni *gl.Int) { uni_name := gl.GLString(name) defer gl.GLStringFree(uni_name) *uni = gl.GetUniformLocation(s.program, uni_name) if *uni == -1 { fmt.Println("Error in getting uniform", gl.GoString(uni_name)) } }
func (s *Shader) initAttribute(name string, att *gl.Uint) { att_name := gl.GLString(name) defer gl.GLStringFree(att_name) att_tmp := gl.GetAttribLocation(s.program, att_name) if att_tmp == -1 { fmt.Println("Error in getting attribute ", gl.GoString(att_name)) } else { *att = gl.Uint(att_tmp) } }
// NewEffectFromStrings creates a new Effect object based on the given strings // containing GLSL shader script. The types values should correlate with the // type of shader the script is describing. func NewEffectFromStrings(text []string, types []ShaderType) (*Effect, error) { effect := &Effect{} effect.uniforms = make(map[string]gl.Int) programID := gl.CreateProgram() var vscript, fscript []*gl.Char for i, val := range text { if types[i] == Vertex { vscript = append(vscript, gl.GLString(val)) } else if types[i] == Fragment { fscript = append(fscript, gl.GLString(val)) } } vshaderID, vertErr := compileShader(Vertex, vscript) if vertErr != nil { return effect, vertErr } gl.AttachShader(programID, vshaderID) fshaderID, fragErr := compileShader(Fragment, fscript) if fragErr != nil { return effect, fragErr } gl.AttachShader(programID, fshaderID) gl.LinkProgram(programID) var status gl.Int gl.GetProgramiv(programID, gl.LINK_STATUS, &status) if status == gl.FALSE { return effect, errors.New("linking program") } effect.program = programID return effect, checkForErrors() }
// Draw draws the Shape object. func (shape *Shape) Draw() error { gl.BindBuffer(gl.ARRAY_BUFFER, shape.vertexBuffer) vertexAttribLoc := gl.GetAttribLocation(paunchEffect.program, gl.GLString("position")) gl.VertexAttribPointer(gl.Uint(vertexAttribLoc), 2, gl.FLOAT, gl.FALSE, 0, gl.Offset(nil, 0)) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.EnableVertexAttribArray(gl.Uint(vertexAttribLoc)) gl.DrawArrays(shape.mode, 0, gl.Sizei(shape.size/2)) gl.DisableVertexAttribArray(gl.Uint(vertexAttribLoc)) //gl.DisableVertexAttribArray(gl.Uint(1)) //gl.BindTexture(gl.TEXTURE_2D, 0) return checkForErrors() }
func (effect *Effect) checkUniformVariable(variable string) { if _, ok := effect.uniforms[variable]; !ok { effect.uniforms[variable] = gl.GetUniformLocation(effect.program, gl.GLString(variable)) } }