Skip to content

Commit 009b86d

Browse files
committed
add crisp draw to UI
1 parent e113fdd commit 009b86d

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ All images and shapes in readme were generated using this library.
4343
- `glbuild`: Automatic shader generation interfaces and logic.
4444
- `gleval`: SDF evaluation interfaces and facilities, both CPU and GPU bound.
4545
- `glrender`: Triangle rendering logic which consumes gleval. STL generation.
46-
- `forge`: Composed shape generation such as `threads` package for generating screw threads. Engineering applications.
46+
- `forge`: Engineering applications. Composed of subpackages.
47+
- `textsdf` package for text generation.
48+
- `threads` package for generating screw threads.
4749
- `gsdfaux`: High level helper functions to get users started up with `gsdf`. See [examples](./examples).
4850

4951

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

127129
![iso-screw](https://github.com/user-attachments/assets/6bc987b9-d522-42a4-89df-71a20c3ae7ff)
128130
![array-triangles](https://github.com/user-attachments/assets/6a479889-2836-464c-b8ea-82109a5aad13)
131+
![geb-book-cover](https://github.com/user-attachments/assets/1ed945fb-5729-4028-bed8-26e0de3073ab)

gsdf2d.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type OpUnion2D struct {
1818

1919
// Union joins the shapes of several 2D SDFs into one. Is exact.
2020
// Union aggregates nested Union results into its own.
21-
func (Builder) Union2D(shaders ...glbuild.Shader2D) glbuild.Shader2D {
21+
func (*Builder) Union2D(shaders ...glbuild.Shader2D) glbuild.Shader2D {
2222
if len(shaders) < 2 {
2323
panic("need at least 2 arguments to Union2D")
2424
}

gsdfaux/ui.go

+32-8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ void main() {
6767
1.0, 1.0,
6868
}
6969
gl.BufferData(gl.ARRAY_BUFFER, 4*len(vertices), gl.Ptr(vertices), gl.STATIC_DRAW)
70+
antialiasingUniform, err := prog.UniformLocation("uAA\x00")
71+
if err != nil {
72+
return err
73+
}
7074
charDistUniform, err := prog.UniformLocation("uCharDist\x00")
7175
if err != nil {
7276
return err
@@ -113,13 +117,18 @@ void main() {
113117
yawSensitivity = 0.005
114118
pitchSensitivity = 0.005
115119
refresh = true
120+
lastEdit = time.Now()
116121
)
117-
122+
flagEdit := func() {
123+
refresh = true
124+
lastEdit = time.Now()
125+
gl.Uniform1i(antialiasingUniform, 1)
126+
}
118127
window.SetCursorPosCallback(func(w *glfw.Window, xpos float64, ypos float64) {
119128
if !isMousePressed {
120129
return
121130
}
122-
refresh = true
131+
flagEdit()
123132
if firstMouseMove {
124133
lastMouseX = xpos
125134
lastMouseY = ypos
@@ -147,7 +156,7 @@ void main() {
147156
})
148157

149158
window.SetScrollCallback(func(w *glfw.Window, xoff, yoff float64) {
150-
refresh = true
159+
flagEdit()
151160
camDist -= yoff * (camDist*.1 + .01)
152161
if camDist < minZoom {
153162
camDist = minZoom // Minimum zoom level
@@ -160,7 +169,7 @@ void main() {
160169
window.SetMouseButtonCallback(func(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
161170
switch button {
162171
case glfw.MouseButtonLeft:
163-
refresh = true
172+
flagEdit()
164173
if action == glfw.Press {
165174
isMousePressed = true
166175
firstMouseMove = true
@@ -175,6 +184,8 @@ void main() {
175184
// Main render loop
176185
previousTime := glfw.GetTime()
177186
ctx := cfg.Context
187+
gl.Uniform1i(antialiasingUniform, 3)
188+
OUTER:
178189
for !window.ShouldClose() {
179190
if ctx != nil {
180191
select {
@@ -200,6 +211,7 @@ void main() {
200211
gl.Uniform1f(yawUniform, float32(yaw))
201212
gl.Uniform1f(pitchUniform, float32(pitch))
202213
gl.Uniform1f(charDistUniform, float32(camDist)+diag)
214+
203215
// Draw the quad
204216
gl.BindVertexArray(vao)
205217
gl.DrawArrays(gl.TRIANGLES, 0, 6)
@@ -213,6 +225,10 @@ void main() {
213225
if refresh || window.ShouldClose() {
214226
refresh = false
215227
break
228+
} else if !isMousePressed && time.Since(lastEdit) > 300*time.Millisecond {
229+
gl.Uniform1i(antialiasingUniform, 3)
230+
lastEdit = lastEdit.Add(time.Hour)
231+
continue OUTER
216232
}
217233
}
218234
}
@@ -249,6 +265,7 @@ vec3 calcNormal(vec3 pos) {
249265
}
250266
251267
uniform float uCamDist; // Distance from the target. Controlled by mouse scroll (zoom).
268+
uniform int uAA; // Anti aliasing.
252269
253270
void main() {
254271
vec2 fragCoord = vTexCoord * uResolution;
@@ -281,7 +298,13 @@ void main() {
281298
vec3 vv = cross(uu, ww); // Up vector
282299
283300
// Pixel coordinates
284-
vec2 p = (2.0 * fragCoord - uResolution) / uResolution.y;
301+
vec3 tot = vec3(0.0); // Total color accumulation.
302+
303+
for (int m = 0; m < uAA; m++)
304+
for (int n = 0; n < uAA; n++)
305+
{
306+
vec2 o = vec2(float(m), float(n)) / float(uAA) - 0.5;
307+
vec2 p = (2.0 * (fragCoord+o) - uResolution) / uResolution.y;
285308
286309
// Create view ray
287310
vec3 rd = normalize(p.x * uu + p.y * vv + 1.5 * ww);
@@ -309,12 +332,13 @@ void main() {
309332
float amb = 0.5 + 0.5 * dot(nor, vec3(0.0, 1.0, 0.0));
310333
col = vec3(0.2, 0.3, 0.4) * amb + vec3(0.8, 0.7, 0.5) * dif;
311334
col = sqrt(col);
335+
tot += col;
312336
}
337+
}
338+
tot /= float(uAA*uAA);
313339
314340
// Gamma correction
315-
316-
317-
fragColor = vec4(col, 1.0);
341+
fragColor = vec4(tot, 1.0);
318342
}
319343
`)
320344
buf.WriteByte(0)

0 commit comments

Comments
 (0)