// Implements Renderer interface. func (gc *opengl) BindShader(sh *data.Shader) (pref uint32, err error) { pref = gl.CreateProgram() // FUTURE: get rid of BindAttribLocation and use layout instead. // this needs GLSL 330 instead of 150 // eg: layout(location=0) in vec4 in_position; gl.BindAttribLocation(pref, 0, "in_v") // matches vattr in bindModel gl.BindAttribLocation(pref, 1, "in_n") // matches nattr in bindModel gl.BindAttribLocation(pref, 2, "in_t") // matches tattr in bindModel // compile and link the shader program. if glerr := gl.BindProgram(pref, sh.Vsh, sh.Fsh); glerr != nil { err = fmt.Errorf("Failed to create shader program: %s\n", glerr) return } // initialize the uniform references var errmsg string for label, _ := range sh.Uniforms { if uid := gl.GetUniformLocation(pref, label); uid >= 0 { sh.Uniforms[label] = uid } else { errnum := gl.GetError() errmsg += fmt.Sprintf("No %s uniform in shader %X\n", label, errnum) } } if len(errmsg) > 0 { err = errors.New(errmsg) } if glerr := gl.GetError(); glerr != gl.NO_ERROR { log.Printf("opengl:bindShader need to find and fix error %X", glerr) } return }
// initShader compiles shaders and links them into a shader program. func (tag *trtag) initShader() { shader := &data.Shader{} loader := data.NewLoader() loader.Load("basic", &shader) tag.shaders = gl.CreateProgram() if err := gl.BindProgram(tag.shaders, shader.Vsh, shader.Fsh); err != nil { fmt.Printf("Failed to create program: %s\n", err) } tag.mvpRef = gl.GetUniformLocation(tag.shaders, "Mvpm") if tag.mvpRef < 0 { fmt.Printf("No model-view-projection matrix in vertex shader\n") } }
// initShader compiles shaders and links them into a shader program. func (ld *ldtag) initShader() { shader := &data.Shader{} loader := data.NewLoader() loader.Load("monkey", &shader) ld.shaders = gl.CreateProgram() gl.BindAttribLocation(ld.shaders, 0, "inPosition") gl.BindAttribLocation(ld.shaders, 1, "inNormal") if err := gl.BindProgram(ld.shaders, shader.Vsh, shader.Fsh); err != nil { fmt.Printf("Failed to create program: %s\n", err) } ld.mvpref = gl.GetUniformLocation(ld.shaders, "modelViewProjectionMatrix") if ld.mvpref < 0 { fmt.Printf("No modelViewProjectionMatrix in vertex shader\n") } }
// initShader compiles shaders and links them into a shader program. func (tb *tbtag) initShader() { shader := &data.Shader{} loader := data.NewLoader() loader.Load("tuv", &shader) tb.shaders = gl.CreateProgram() gl.BindAttribLocation(tb.shaders, 0, "vertexPosition") gl.BindAttribLocation(tb.shaders, 2, "uvPoint") if err := gl.BindProgram(tb.shaders, shader.Vsh, shader.Fsh); err != nil { fmt.Printf("Failed to create program: %s\n", err) } tb.mvpref = gl.GetUniformLocation(tb.shaders, "Mvpm") tb.sampler = gl.GetUniformLocation(tb.shaders, "uvSampler") if tb.mvpref < 0 { fmt.Printf("No model-view-projection matrix in vertex shader\n") } }
// initScene is one time initialization that creates a single VAO func (sf *sftag) initScene() { sf.mvp32 = &render.M4{} sf.sTime = time.Now() sf.initData() // Bind the OpenGL calls and dump some version info. gl.Init() fmt.Printf("%s %s", gl.GetString(gl.RENDERER), gl.GetString(gl.VERSION)) fmt.Printf(" GLSL %s\n", gl.GetString(gl.SHADING_LANGUAGE_VERSION)) gl.GenVertexArrays(1, &sf.vao) gl.BindVertexArray(sf.vao) // vertex data. var vbuff uint32 gl.GenBuffers(1, &vbuff) gl.BindBuffer(gl.ARRAY_BUFFER, vbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(sf.points)*4), gl.Pointer(&(sf.points[0])), gl.STATIC_DRAW) var vattr uint32 = 0 gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(vattr) // faces data. var ebuff uint32 gl.GenBuffers(1, &ebuff) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(sf.faces)), gl.Pointer(&(sf.faces[0])), gl.STATIC_DRAW) // create texture and shaders after all the data has been set up. shader := &data.Shader{} loader := data.NewLoader() loader.Load("fire", &shader) sf.shaders = gl.CreateProgram() if err := gl.BindProgram(sf.shaders, shader.Vsh, shader.Fsh); err != nil { fmt.Printf("Failed to create program: %s\n", err) } sf.mvpref = gl.GetUniformLocation(sf.shaders, "Mvpm") sf.gTime = gl.GetUniformLocation(sf.shaders, "time") sf.sizes = gl.GetUniformLocation(sf.shaders, "resolution") sf.ortho = lin.NewOrtho(0, 4, 0, 4, 0, 10) // set some state that doesn't need to change during drawing. gl.ClearColor(0.0, 0.0, 0.0, 1.0) }