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 009b86d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ All images and shapes in readme were generated using this library.
- `glbuild`: Automatic shader generation interfaces and logic.
- `gleval`: SDF evaluation interfaces and facilities, both CPU and GPU bound.
- `glrender`: Triangle rendering logic which consumes gleval. STL generation.
- `forge`: Composed shape generation such as `threads` package for generating screw threads. Engineering applications.
- `forge`: Engineering applications. Composed of subpackages.
- `textsdf` package for text generation.
- `threads` package for generating screw threads.
- `gsdfaux`: High level helper functions to get users started up with `gsdf`. See [examples](./examples).


Expand Down Expand Up @@ -126,3 +128,4 @@ go run ./examples/fibonacci-showerhead -resdiv 350 36,16s user 0,76s system 100

![iso-screw](https://github.com/user-attachments/assets/6bc987b9-d522-42a4-89df-71a20c3ae7ff)
![array-triangles](https://github.com/user-attachments/assets/6a479889-2836-464c-b8ea-82109a5aad13)
![geb-book-cover](https://github.com/user-attachments/assets/1ed945fb-5729-4028-bed8-26e0de3073ab)
2 changes: 1 addition & 1 deletion gsdf2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type OpUnion2D struct {

// Union joins the shapes of several 2D SDFs into one. Is exact.
// Union aggregates nested Union results into its own.
func (Builder) Union2D(shaders ...glbuild.Shader2D) glbuild.Shader2D {
func (*Builder) Union2D(shaders ...glbuild.Shader2D) glbuild.Shader2D {
if len(shaders) < 2 {
panic("need at least 2 arguments to Union2D")
}
Expand Down
40 changes: 32 additions & 8 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 @@ -113,13 +117,18 @@ void main() {
yawSensitivity = 0.005
pitchSensitivity = 0.005
refresh = true
lastEdit = time.Now()
)

flagEdit := func() {
refresh = true
lastEdit = time.Now()
gl.Uniform1i(antialiasingUniform, 1)
}
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 +156,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 +169,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,6 +184,8 @@ void main() {
// Main render loop
previousTime := glfw.GetTime()
ctx := cfg.Context
gl.Uniform1i(antialiasingUniform, 3)
OUTER:
for !window.ShouldClose() {
if ctx != nil {
select {
Expand All @@ -200,6 +211,7 @@ void main() {
gl.Uniform1f(yawUniform, float32(yaw))
gl.Uniform1f(pitchUniform, float32(pitch))
gl.Uniform1f(charDistUniform, float32(camDist)+diag)

// Draw the quad
gl.BindVertexArray(vao)
gl.DrawArrays(gl.TRIANGLES, 0, 6)
Expand All @@ -213,6 +225,10 @@ void main() {
if refresh || window.ShouldClose() {
refresh = false
break
} else if !isMousePressed && time.Since(lastEdit) > 300*time.Millisecond {
gl.Uniform1i(antialiasingUniform, 3)
lastEdit = lastEdit.Add(time.Hour)
continue OUTER
}
}
}
Expand Down Expand Up @@ -249,6 +265,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 +298,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 +332,13 @@ 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

0 comments on commit 009b86d

Please sign in to comment.