Take normalized coords in DrawTextOpenGL

This commit is contained in:
bloeys
2022-07-01 10:43:45 +04:00
parent efe36f8c2b
commit 094cc1acdd
2 changed files with 15 additions and 28 deletions

View File

@ -76,10 +76,17 @@ func getGlyphsFromRanges(ranges [][2]rune) []rune {
return out
}
//DrawTextOpenGL prepares text that will be drawn on the next GlyphRend.Draw call.
//screenPos is in the range [0,1], where (0,0) is the bottom left.
//Color is RGBA in the range [0,1].
func (gr *GlyphRend) DrawTextOpenGL(text string, screenPos *gglm.Vec3, color *gglm.Vec4) {
screenWidthF32 := float32(gr.ScreenWidth)
screenHeightF32 := float32(gr.ScreenHeight)
screenPos.Set(screenPos.X()*screenWidthF32, screenPos.Y()*screenHeightF32, screenPos.Z())
//The projection matrix fits the screen size. This is needed so we can size and position characters correctly.
projMtx := gglm.Ortho(0, float32(gr.ScreenWidth), float32(gr.ScreenHeight), 0, 0.1, 10)
projMtx := gglm.Ortho(0, screenWidthF32, screenHeightF32, 0, 0.1, 10)
viewMtx := gglm.LookAt(gglm.NewVec3(0, 0, -1), gglm.NewVec3(0, 0, 0), gglm.NewVec3(0, 1, 0))
projViewMtx := projMtx.Clone().Mul(viewMtx)
@ -136,6 +143,10 @@ func (gr *GlyphRend) DrawTextOpenGL(text string, screenPos *gglm.Vec3, color *gg
func (gr *GlyphRend) Draw() {
if gr.GlyphCount == 0 {
return
}
gr.InstancedBuf.SetData(gr.GlyphVBO)
gr.InstancedBuf.Bind()
gr.GlyphMat.Bind()

30
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"time"
"github.com/bloeys/gglm/gglm"
@ -114,33 +113,10 @@ func (p *program) Update() {
func (p *program) Render() {
w, h := p.win.SDLWin.GetSize()
defer p.GlyphRend.Draw()
textColor := gglm.NewVec4(1, 1, 1, 1)
//Draw FPS
var fps float32
if frameTime.Milliseconds() > 0 {
fps = 1 / float32(frameTime.Milliseconds()) * 1000
}
startFromTop := float32(h) - float32(p.GlyphRend.Atlas.LineHeight)
p.GlyphRend.DrawTextOpenGL(fmt.Sprintf("FPS=%f", fps), gglm.NewVec3(float32(w)*0.7, startFromTop, 0), textColor)
//Draw point and texture sizes
startFromTop -= float32(p.GlyphRend.Atlas.LineHeight)
p.GlyphRend.DrawTextOpenGL(fmt.Sprintf("Point size=%d", p.FontSize), gglm.NewVec3(float32(w)*0.7, startFromTop, 0), textColor)
startFromTop -= float32(p.GlyphRend.Atlas.LineHeight)
p.GlyphRend.DrawTextOpenGL(fmt.Sprintf("Texture size=%d*%d", p.GlyphRend.Atlas.Img.Rect.Max.X, p.GlyphRend.Atlas.Img.Rect.Max.Y), gglm.NewVec3(float32(w)*0.7, startFromTop, 0), textColor)
//Draw all other
count := 1000
startFromBot := float32(p.GlyphRend.Atlas.LineHeight)
for i := 0; i < count; i++ {
p.GlyphRend.DrawTextOpenGL("Hello friend, how are you?\n", gglm.NewVec3(0, startFromBot, 0), textColor)
startFromBot += float32(p.GlyphRend.Atlas.LineHeight) * 2
}
p.GlyphRend.Draw()
p.GlyphRend.DrawTextOpenGL("Hello there, friend.", gglm.NewVec3(0, 0.9, 0), textColor)
}
func (p *program) FrameEnd() {