// RenderString must be called on the render thread. x and y are the initial position of the pen, // in screen coordinates, and height is the height of a full line of text, in screen coordinates. func (d *Dictionary) RenderString(str string, x, y, height float64) { if str == "" { return } // No synchronization necessary because everything is run serially on the render thread anyway. if d.strs == nil { d.strs = make(map[string]strData) } data, ok := d.strs[str] if !ok { data = d.bindString(str) d.strs[str] = data } render.EnableShader("glop.font") defer render.EnableShader("") gl.ActiveTexture(gl.TEXTURE0) gl.BindTexture(gl.TEXTURE_2D, d.atlas.texture) location, _ := render.GetUniformLocation("glop.font", "tex") gl.Uniform1i(location, 0) gl.BindSampler(0, d.atlas.sampler) location, _ = render.GetUniformLocation("glop.font", "height") gl.Uniform1f(location, float32(height)) var viewport [4]int32 gl.GetIntegerv(gl.VIEWPORT, &viewport[0]) location, _ = render.GetUniformLocation("glop.font", "screen") gl.Uniform2f(location, float32(viewport[2]), float32(viewport[3])) location, _ = render.GetUniformLocation("glop.font", "pen") gl.Uniform2f(location, float32(x)+float32(viewport[0]), float32(y)+float32(viewport[1])) location, _ = render.GetUniformLocation("glop.font", "textColor") gl.Uniform3f(location, d.color[0], d.color[1], d.color[2]) gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.BindVertexArray(data.varrays[0]) gl.DrawArrays(gl.TRIANGLES, 0, data.count) }
func (d *Dictionary) RenderString(s string, x, y, z, height float64, just Justification) { if len(s) == 0 { return } strbuf, ok := d.strs[s] if !ok { defer d.RenderString(s, x, y, z, height, just) } else { render.EnableShader("glop.font") diff := 20/math.Pow(height, 1.0) + 5*math.Pow(d.data.Scale, 1.0)/math.Pow(height, 1.0) if diff > 0.4 { // TODO: Need to come up with decent values here diff = 0.4 } render.SetUniformF("glop.font", "dist_min", float32(0.5-diff)) render.SetUniformF("glop.font", "dist_max", float32(0.5+diff)) defer render.EnableShader("") } size := unsafe.Sizeof(dictVert{}) scale := height / float64(d.data.Maxy-d.data.Miny) width := float32(d.figureWidth(s) * scale) x_pos := float32(x) switch just { case Center: x_pos -= width / 2 case Right: x_pos -= width } if ok { gl.PushMatrix() defer gl.PopMatrix() gl.Translated(gl.Double(x_pos), gl.Double(y), gl.Double(z)) gl.Scaled(gl.Double(scale), gl.Double(scale), 1) gl.PushAttrib(gl.COLOR_BUFFER_BIT) defer gl.PopAttrib() gl.Enable(gl.BLEND) gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.Enable(gl.TEXTURE_2D) gl.BindTexture(gl.TEXTURE_2D, gl.Uint(d.texture)) gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(strbuf.vbuffer)) gl.EnableClientState(gl.VERTEX_ARRAY) gl.VertexPointer(2, gl.FLOAT, gl.Sizei(size), nil) gl.EnableClientState(gl.TEXTURE_COORD_ARRAY) gl.TexCoordPointer(2, gl.FLOAT, gl.Sizei(size), gl.Pointer(unsafe.Offsetof(strbuf.vs[0].u))) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.Uint(strbuf.ibuffer)) gl.DrawElements(gl.TRIANGLES, gl.Sizei(len(strbuf.is)), gl.UNSIGNED_SHORT, nil) gl.DisableClientState(gl.VERTEX_ARRAY) gl.DisableClientState(gl.TEXTURE_COORD_ARRAY) gl.Disable(gl.TEXTURE_2D) return } x_pos = 0 var prev rune for _, r := range s { if _, ok := d.data.Kerning[prev]; ok { x_pos += float32(d.data.Kerning[prev][r]) } prev = r info := d.getInfo(r) xleft := x_pos + float32(info.Full_bounds.Min.X) xright := x_pos + float32(info.Full_bounds.Max.X) ytop := float32(info.Full_bounds.Max.Y) + float32(-d.data.Miny) ybot := float32(info.Full_bounds.Min.Y) + float32(-d.data.Miny) start := uint16(len(strbuf.vs)) strbuf.is = append(strbuf.is, start+0) strbuf.is = append(strbuf.is, start+1) strbuf.is = append(strbuf.is, start+2) strbuf.is = append(strbuf.is, start+0) strbuf.is = append(strbuf.is, start+2) strbuf.is = append(strbuf.is, start+3) strbuf.vs = append(strbuf.vs, dictVert{ x: xleft, y: ytop, u: float32(info.Pos.Min.X) / float32(d.data.Dx), v: float32(info.Pos.Max.Y) / float32(d.data.Dy), }) strbuf.vs = append(strbuf.vs, dictVert{ x: xleft, y: ybot, u: float32(info.Pos.Min.X) / float32(d.data.Dx), v: float32(info.Pos.Min.Y) / float32(d.data.Dy), }) strbuf.vs = append(strbuf.vs, dictVert{ x: xright, y: ybot, u: float32(info.Pos.Max.X) / float32(d.data.Dx), v: float32(info.Pos.Min.Y) / float32(d.data.Dy), }) strbuf.vs = append(strbuf.vs, dictVert{ x: xright, y: ytop, u: float32(info.Pos.Max.X) / float32(d.data.Dx), v: float32(info.Pos.Max.Y) / float32(d.data.Dy), }) x_pos += float32(info.Advance) // - float32((info.Full_bounds.Dx() - info.Bounds.Dx())) } gl.GenBuffers(1, (*gl.Uint)(&strbuf.vbuffer)) gl.BindBuffer(gl.ARRAY_BUFFER, gl.Uint(strbuf.vbuffer)) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(int(size)*len(strbuf.vs)), gl.Pointer(&strbuf.vs[0].x), gl.STATIC_DRAW) gl.GenBuffers(1, (*gl.Uint)(&strbuf.ibuffer)) gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.Uint(strbuf.ibuffer)) gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, gl.Sizeiptr(int(unsafe.Sizeof(strbuf.is[0]))*len(strbuf.is)), gl.Pointer(&strbuf.is[0]), gl.STATIC_DRAW) d.strs[s] = strbuf }
func SetFontColor(r, g, b, a float32) { render.EnableShader("glop.font") render.SetUniform4F("glop.font", "color", []float32{r, g, b, a}) render.EnableShader("") }