From 707cd4f7e124f5d4f4c29f785d7721ebd596f6da Mon Sep 17 00:00:00 2001 From: soypat Date: Sun, 24 Nov 2024 17:52:14 -0300 Subject: [PATCH] add crisp draw to UI --- gsdfaux/ui.go | 74 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/gsdfaux/ui.go b/gsdfaux/ui.go index e5e4971..5097659 100644 --- a/gsdfaux/ui.go +++ b/gsdfaux/ui.go @@ -67,6 +67,10 @@ void main() { 1.0, 1.0, } gl.BufferData(gl.ARRAY_BUFFER, 4*len(vertices), gl.Ptr(vertices), gl.STATIC_DRAW) + antialiasingUniform, err := prog.UniformLocation("uAA\x00") + if err != nil { + return err + } charDistUniform, err := prog.UniformLocation("uCharDist\x00") if err != nil { return err @@ -103,6 +107,7 @@ void main() { minZoom := float64(diag * 0.00001) maxZoom := float64(diag * 10) var ( + lastEdit time.Time yaw float64 pitch float64 lastMouseX float64 @@ -114,12 +119,15 @@ void main() { pitchSensitivity = 0.005 refresh = true ) - + flagEdit := func() { + refresh = true + lastEdit = time.Now() + } window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) { if !isMousePressed { return } - refresh = true + flagEdit() if firstMouseMove { lastMouseX = xpos lastMouseY = ypos @@ -147,7 +155,7 @@ void main() { }) window.SetScrollCallback(func(w *glfw.Window, xoff, yoff float64) { - refresh = true + flagEdit() camDist -= yoff * (camDist*.1 + .01) if camDist < minZoom { camDist = minZoom // Minimum zoom level @@ -160,7 +168,7 @@ void main() { window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) { switch button { case glfw.MouseButtonLeft: - refresh = true + flagEdit() if action == glfw.Press { isMousePressed = true firstMouseMove = true @@ -175,14 +183,9 @@ void main() { // Main render loop previousTime := glfw.GetTime() ctx := cfg.Context - for !window.ShouldClose() { - if ctx != nil { - select { - case <-ctx.Done(): - return ctx.Err() - default: - } - } + + draw := func(aa int32) { + println("start draw") width, height := window.GetSize() currentTime := glfw.GetTime() elapsedTime := currentTime - previousTime @@ -200,22 +203,42 @@ void main() { gl.Uniform1f(yawUniform, float32(yaw)) gl.Uniform1f(pitchUniform, float32(pitch)) gl.Uniform1f(charDistUniform, float32(camDist)+diag) + gl.Uniform1i(antialiasingUniform, aa) // Draw the quad gl.BindVertexArray(vao) gl.DrawArrays(gl.TRIANGLES, 0, 6) // Swap buffers and poll events window.SwapBuffers() + println("end draw") + } - // Limit frame rate + for !window.ShouldClose() { + println("window poll") + if ctx != nil { + println("ads") + select { + case <-ctx.Done(): + return ctx.Err() + default: + } + } + draw(1) + + crisp := false for { time.Sleep(time.Second / 60) glfw.PollEvents() if refresh || window.ShouldClose() { refresh = false break + } else if !isMousePressed && !crisp && time.Since(lastEdit) > 300*time.Millisecond { + draw(3) // draw crisp image and stop rendering on end of mouse movement. + crisp = true + println("crisp") } } } + println("exiting") return nil } @@ -249,6 +272,7 @@ vec3 calcNormal(vec3 pos) { } uniform float uCamDist; // Distance from the target. Controlled by mouse scroll (zoom). +uniform int uAA; // Anti aliasing. void main() { vec2 fragCoord = vTexCoord * uResolution; @@ -281,7 +305,13 @@ void main() { vec3 vv = cross(uu, ww); // Up vector // Pixel coordinates - vec2 p = (2.0 * fragCoord - uResolution) / uResolution.y; + vec3 tot = vec3(0.0); // Total color accumulation. + + for (int m = 0; m < uAA; m++) + for (int n = 0; n < uAA; n++) + { + vec2 o = vec2(float(m), float(n)) / float(uAA) - 0.5; + vec2 p = (2.0 * (fragCoord+o) - uResolution) / uResolution.y; // Create view ray vec3 rd = normalize(p.x * uu + p.y * vv + 1.5 * ww); @@ -309,12 +339,15 @@ void main() { float amb = 0.5 + 0.5 * dot(nor, vec3(0.0, 1.0, 0.0)); col = vec3(0.2, 0.3, 0.4) * amb + vec3(0.8, 0.7, 0.5) * dif; col = sqrt(col); + tot += col; } + } + tot /= float(uAA*uAA); // Gamma correction - fragColor = vec4(col, 1.0); + fragColor = vec4(tot, 1.0); } `) buf.WriteByte(0) @@ -342,5 +375,14 @@ func startGLFW(width, height int) (window *glfw.Window, term func(), err error) if err := gl.Init(); err != nil { log.Fatalln("Failed to initialize OpenGL:", err) } - return window, glfw.Terminate, err + term = func() { + glfw.Terminate() + return + println("destroy") + window.Destroy() + println("terminate") + + println("destroy terminate done") + } + return window, term, err }