// initScene is one time initialization that creates a single VAO func (tag *trtag) initScene() { tag.mvp64 = lin.NewM4() tag.mvp32 = &render.M4{} tag.persp = lin.NewPersp(60, float64(600)/float64(600), 0.1, 50) tag.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)) // Gather the one scene into this one vertex array object. gl.GenVertexArrays(1, &tag.vao) gl.BindVertexArray(tag.vao) // create shaders tag.initShader() gl.UseProgram(tag.shaders) // vertex data. var vbuff uint32 gl.GenBuffers(1, &vbuff) gl.BindBuffer(gl.ARRAY_BUFFER, vbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(tag.points)*4), gl.Pointer(&(tag.points[0])), gl.STATIC_DRAW) vattr := uint32(gl.GetAttribLocation(tag.shaders, "in_Position")) gl.EnableVertexAttribArray(vattr) gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0) // colour data. var cbuff uint32 gl.GenBuffers(1, &cbuff) gl.BindBuffer(gl.ARRAY_BUFFER, cbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(tag.colour)*4), gl.Pointer(&(tag.colour[0])), gl.STATIC_DRAW) cattr := uint32(gl.GetAttribLocation(tag.shaders, "in_Color")) gl.EnableVertexAttribArray(cattr) gl.VertexAttribPointer(cattr, 4, gl.FLOAT, false, 0, 0) // faces data. var ebuff uint32 gl.GenBuffers(1, &ebuff) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(tag.faces)), gl.Pointer(&(tag.faces[0])), gl.STATIC_DRAW) // set some state that doesn't need to change during drawing. gl.ClearColor(0.0, 0.0, 0.0, 1.0) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.CULL_FACE) gl.CullFace(gl.BACK) }
// Implements Renderer interface. func (gc *opengl) BindModel(mesh *data.Mesh) (err error) { if glerr := gl.GetError(); glerr != gl.NO_ERROR { log.Printf("opengl:bindModel need to find and fix prior error %X", glerr) } // Gather the one scene into this one vertex array object. gl.GenVertexArrays(1, &(mesh.Vao)) gl.BindVertexArray(mesh.Vao) // vertex data. var vbuff uint32 gl.GenBuffers(1, &vbuff) gl.BindBuffer(gl.ARRAY_BUFFER, vbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.V)*4), gl.Pointer(&(mesh.V[0])), gl.STATIC_DRAW) var vattr uint32 = 0 gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(vattr) // normal data. var nbuff uint32 gl.GenBuffers(1, &nbuff) gl.BindBuffer(gl.ARRAY_BUFFER, nbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.N)*4), gl.Pointer(&(mesh.N[0])), gl.STATIC_DRAW) var nattr uint32 = 1 gl.VertexAttribPointer(nattr, 3, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(nattr) // texture coordatinate, 2 float32's if len(mesh.T) > 0 { var tbuff uint32 gl.GenBuffers(1, &tbuff) gl.BindBuffer(gl.ARRAY_BUFFER, tbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.T)*4), gl.Pointer(&(mesh.T[0])), gl.STATIC_DRAW) var tattr uint32 = 2 gl.VertexAttribPointer(tattr, 2, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(tattr) } // faces data, uint16 in this case, so 2 bytes per element. var fbuff uint32 gl.GenBuffers(1, &fbuff) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, fbuff) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(mesh.F)*2), gl.Pointer(&(mesh.F[0])), gl.STATIC_DRAW) if glerr := gl.GetError(); glerr != gl.NO_ERROR { err = fmt.Errorf("Failed binding model %s\n", mesh.Name) } return }
// drawScene renders the 3D models consisting of one VAO func (tag *trtag) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) tag.checkError("gl.Clear") gl.UseProgram(tag.shaders) tag.checkError("gl.UseProgram") gl.BindVertexArray(tag.vao) tag.checkError("gl.BindVertexArray") // Use a modelview matrix and quaternion to rotate the 3D object. tag.mvp64.SetQ(lin.NewQ().SetAa(0, 1, 0, lin.Rad(-tag.rotateAngle))) tag.mvp64.TranslateMT(0, 0, -4) tag.mvp64.Mult(tag.mvp64, tag.persp) tag.mvp32 = renderMatrix(tag.mvp64, tag.mvp32) gl.UniformMatrix4fv(tag.mvpRef, 1, false, tag.mvp32.Pointer()) if err := gl.GetError(); err != 0 { fmt.Printf("gl.UniformMatrix error %d\n", err) } gl.DrawElements(gl.TRIANGLES, int32(len(tag.faces)), gl.UNSIGNED_BYTE, gl.Pointer(nil)) if err := gl.GetError(); err != 0 { fmt.Printf("gl.DrawElements error %d\n", err) } // cleanup gl.UseProgram(0) tag.checkError("gl.UseProgram-0") gl.BindVertexArray(0) tag.checkError("gl.BindVertexArray-0") // rotate based on time... not on how fast the computer runs. if time.Now().Sub(tag.lastTime).Seconds() > 0.01 { tag.rotateAngle += 1 tag.lastTime = time.Now() } }
// initScene is one time initialization that creates a single VAO func (tb *tbtag) initScene() { tb.mvp32 = &render.M4{} tb.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)) // Gather the one scene into this one vertex array object. gl.GenVertexArrays(1, &tb.vao) gl.BindVertexArray(tb.vao) // vertex data. var vbuff uint32 gl.GenBuffers(1, &vbuff) gl.BindBuffer(gl.ARRAY_BUFFER, vbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(tb.points)*4), gl.Pointer(&(tb.points[0])), gl.STATIC_DRAW) var vattr uint32 = 0 gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(vattr) // texture coordatinates var tbuff uint32 gl.GenBuffers(1, &tbuff) gl.BindBuffer(gl.ARRAY_BUFFER, tbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(tb.tcoords)*4), gl.Pointer(&(tb.tcoords[0])), gl.STATIC_DRAW) var tattr uint32 = 2 gl.VertexAttribPointer(tattr, 2, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(tattr) // faces data. var ebuff uint32 gl.GenBuffers(1, &ebuff) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(tb.faces)), gl.Pointer(&(tb.faces[0])), gl.STATIC_DRAW) // create texture and shaders after all the data has been set up. tb.initTexture() tb.initShader() tb.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) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.TEXTURE_2D) }
// initScene is called once on startup to load the 3D data. func (ld *ldtag) initScene() { ld.persp = lin.NewM4() ld.mvp64 = lin.NewM4() ld.mvp32 = &render.M4{} gl.Init() mesh := &data.Mesh{} loader := data.NewLoader() loader.Load("monkey", &mesh) ld.faceCount = int32(len(mesh.F)) // Gather the one scene into this one vertex array object. gl.GenVertexArrays(1, &ld.vao) gl.BindVertexArray(ld.vao) // vertex data. var vbuff uint32 gl.GenBuffers(1, &vbuff) gl.BindBuffer(gl.ARRAY_BUFFER, vbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.V)*4), gl.Pointer(&(mesh.V[0])), gl.STATIC_DRAW) var vattr uint32 = 0 gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(vattr) // normal data. var nbuff uint32 gl.GenBuffers(1, &nbuff) gl.BindBuffer(gl.ARRAY_BUFFER, nbuff) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.N)*4), gl.Pointer(&(mesh.N[0])), gl.STATIC_DRAW) var nattr uint32 = 1 gl.VertexAttribPointer(nattr, 3, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(nattr) // faces data, uint32 in this case, so 4 bytes per element. var ebuff uint32 gl.GenBuffers(1, &ebuff) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(mesh.F)*2), gl.Pointer(&(mesh.F[0])), gl.STATIC_DRAW) ld.initShader() gl.ClearColor(0.2, 0.2, 0.2, 1.0) gl.Enable(gl.DEPTH_TEST) gl.Enable(gl.CULL_FACE) // set the initial perspetive matrix. ld.resize(0, 0, 800, 600) }
// 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) }
// Implements Renderer interface. func (gc *opengl) BindGlyphs(mesh *data.Mesh) (err error) { if glerr := gl.GetError(); glerr != gl.NO_ERROR { log.Printf("opengl:bindGlyphs need to find and fix prior error %X", glerr) } // Reuse existing vertex array object, otherwise create one. if mesh.Vao == 0 { gl.GenVertexArrays(1, &(mesh.Vao)) } gl.BindVertexArray(mesh.Vao) // Reuse existing vertex buffer, otherwise create one. if mesh.Vbuf == 0 { gl.GenBuffers(1, &(mesh.Vbuf)) } gl.BindBuffer(gl.ARRAY_BUFFER, mesh.Vbuf) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.V)*4), gl.Pointer(&(mesh.V[0])), gl.DYNAMIC_DRAW) var vattr uint32 = 0 gl.VertexAttribPointer(vattr, 4, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(vattr) // Reuse existing texture buffer, otherwise create one. if mesh.Tbuf == 0 { gl.GenBuffers(1, &(mesh.Tbuf)) } gl.BindBuffer(gl.ARRAY_BUFFER, mesh.Tbuf) gl.BufferData(gl.ARRAY_BUFFER, int64(len(mesh.T)*4), gl.Pointer(&(mesh.T[0])), gl.DYNAMIC_DRAW) var tattr uint32 = 2 gl.VertexAttribPointer(tattr, 2, gl.FLOAT, false, 0, 0) gl.EnableVertexAttribArray(tattr) // Reuse existing faces buffer, otherwise create one. if mesh.Fbuf == 0 { gl.GenBuffers(1, &mesh.Fbuf) } gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, mesh.Fbuf) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(mesh.F)*2), gl.Pointer(&(mesh.F[0])), gl.DYNAMIC_DRAW) if glerr := gl.GetError(); glerr != gl.NO_ERROR { err = fmt.Errorf("Failed binding glyphs %s\n", mesh.Name) } return }
// Implements Renderer interface. func (gc *opengl) Render(v *Vis) { if v.Mesh != nil && v.Shader != nil { gl.BindVertexArray(v.Mesh.Vao) gl.UseProgram(v.Shader.Program) gc.bindShaderUniforms(v) gl.DrawElements(gl.TRIANGLES, int32(len(v.Mesh.F)), gl.UNSIGNED_SHORT, gl.Pointer(nil)) // cleanup. gl.ActiveTexture(gl.TEXTURE0) gl.UseProgram(0) gl.BindVertexArray(0) } }
// drawScene renders the shader-only scene. func (sf *sftag) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(sf.shaders) gl.BindVertexArray(sf.vao) timeSinceStart := time.Since(sf.sTime).Seconds() gl.Uniform1f(sf.gTime, float32(timeSinceStart)) gl.Uniform2f(sf.sizes, 500, 500) sf.mvp32 = renderMatrix(sf.ortho, sf.mvp32) gl.UniformMatrix4fv(sf.mvpref, 1, false, sf.mvp32.Pointer()) gl.DrawElements(gl.TRIANGLES, int32(len(sf.faces)), gl.UNSIGNED_BYTE, gl.Pointer(nil)) // cleanup gl.UseProgram(0) gl.BindVertexArray(0) }
// drawScene renders the scene consisting of one VAO. func (tb *tbtag) drawScene() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(tb.shaders) gl.Uniform1i(tb.sampler, 0) gl.ActiveTexture(gl.TEXTURE0 + 0) gl.BindTexture(gl.TEXTURE_2D, tb.texture.Tid) gl.BindVertexArray(tb.vao) tb.mvp32 = renderMatrix(tb.ortho, tb.mvp32) gl.UniformMatrix4fv(tb.mvpref, 1, false, tb.mvp32.Pointer()) gl.DrawElements(gl.TRIANGLES, int32(len(tb.faces)), gl.UNSIGNED_BYTE, gl.Pointer(nil)) // cleanup gl.ActiveTexture(0) gl.UseProgram(0) gl.BindVertexArray(0) }
// render draws the scene consisting of one VAO func (ld *ldtag) render() { gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.UseProgram(ld.shaders) gl.BindVertexArray(ld.vao) // use a model-view-projection matrix ld.mvp64.Set(lin.M4I).ScaleSM(0.5, 0.5, 0.5).TranslateMT(0, 0, -2) ld.mvp64.Mult(ld.mvp64, ld.persp) v3 := renderMatrix(ld.mvp64, ld.mvp32) gl.UniformMatrix4fv(ld.mvpref, 1, false, v3.Pointer()) gl.CullFace(gl.BACK) gl.DrawElements(gl.TRIANGLES, ld.faceCount, gl.UNSIGNED_SHORT, gl.Pointer(nil)) // cleanup gl.UseProgram(0) gl.BindVertexArray(0) }
// initTexture loads the texture and binds it to the graphics device. func (tb *tbtag) initTexture() { texture := &data.Texture{} loader := data.NewLoader() if loader.Load("image", &texture); texture != nil { tb.texture = texture gl.GenTextures(1, &texture.Tid) gl.BindTexture(gl.TEXTURE_2D, texture.Tid) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) // ensure image is in RGBA format b := texture.Img.Bounds() rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) draw.Draw(rgba, rgba.Bounds(), texture.Img, b.Min, draw.Src) width, height := int32(b.Dx()), int32(b.Dy()) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&(rgba.Pix[0]))) if glerr := gl.GetError(); glerr != gl.NO_ERROR { fmt.Printf("Failed binding texture image.png\n") } } else { fmt.Println("Could not load image.png file.") } }
// Implements Renderer interface. func (gc *opengl) BindTexture(texture *data.Texture) (err error) { if texture == nil { return } if glerr := gl.GetError(); glerr != gl.NO_ERROR { log.Printf("opengl:bindTexture need to find and fix prior error %X", glerr) } gl.GenTextures(1, &(texture.Tid)) gl.BindTexture(gl.TEXTURE_2D, texture.Tid) // ensure image is in RGBA format b := texture.Img.Bounds() rgba := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) draw.Draw(rgba, rgba.Bounds(), texture.Img, b.Min, draw.Src) width, height := int32(b.Dx()), int32(b.Dy()) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Pointer(&(rgba.Pix[0]))) gl.GenerateMipmap(gl.TEXTURE_2D) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR) if glerr := gl.GetError(); glerr != gl.NO_ERROR { err = fmt.Errorf("Failed binding texture %s\n", texture.Name) } return }