Skip to content

Commit

Permalink
add crisp draw to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 24, 2024
1 parent e113fdd commit 707cd4f
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions gsdfaux/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 707cd4f

Please sign in to comment.