@@ -67,6 +67,10 @@ void main() {
67
67
1.0 , 1.0 ,
68
68
}
69
69
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
+ }
70
74
charDistUniform , err := prog .UniformLocation ("uCharDist\x00 " )
71
75
if err != nil {
72
76
return err
@@ -113,13 +117,18 @@ void main() {
113
117
yawSensitivity = 0.005
114
118
pitchSensitivity = 0.005
115
119
refresh = true
120
+ lastEdit = time .Now ()
116
121
)
117
-
122
+ flagEdit := func () {
123
+ refresh = true
124
+ lastEdit = time .Now ()
125
+ gl .Uniform1i (antialiasingUniform , 1 )
126
+ }
118
127
window .SetCursorPosCallback (func (w * glfw.Window , xpos float64 , ypos float64 ) {
119
128
if ! isMousePressed {
120
129
return
121
130
}
122
- refresh = true
131
+ flagEdit ()
123
132
if firstMouseMove {
124
133
lastMouseX = xpos
125
134
lastMouseY = ypos
@@ -147,7 +156,7 @@ void main() {
147
156
})
148
157
149
158
window .SetScrollCallback (func (w * glfw.Window , xoff , yoff float64 ) {
150
- refresh = true
159
+ flagEdit ()
151
160
camDist -= yoff * (camDist * .1 + .01 )
152
161
if camDist < minZoom {
153
162
camDist = minZoom // Minimum zoom level
@@ -160,7 +169,7 @@ void main() {
160
169
window .SetMouseButtonCallback (func (w * glfw.Window , button glfw.MouseButton , action glfw.Action , mods glfw.ModifierKey ) {
161
170
switch button {
162
171
case glfw .MouseButtonLeft :
163
- refresh = true
172
+ flagEdit ()
164
173
if action == glfw .Press {
165
174
isMousePressed = true
166
175
firstMouseMove = true
@@ -175,6 +184,8 @@ void main() {
175
184
// Main render loop
176
185
previousTime := glfw .GetTime ()
177
186
ctx := cfg .Context
187
+ gl .Uniform1i (antialiasingUniform , 3 )
188
+ OUTER:
178
189
for ! window .ShouldClose () {
179
190
if ctx != nil {
180
191
select {
@@ -200,6 +211,7 @@ void main() {
200
211
gl .Uniform1f (yawUniform , float32 (yaw ))
201
212
gl .Uniform1f (pitchUniform , float32 (pitch ))
202
213
gl .Uniform1f (charDistUniform , float32 (camDist )+ diag )
214
+
203
215
// Draw the quad
204
216
gl .BindVertexArray (vao )
205
217
gl .DrawArrays (gl .TRIANGLES , 0 , 6 )
@@ -213,6 +225,10 @@ void main() {
213
225
if refresh || window .ShouldClose () {
214
226
refresh = false
215
227
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
216
232
}
217
233
}
218
234
}
@@ -249,6 +265,7 @@ vec3 calcNormal(vec3 pos) {
249
265
}
250
266
251
267
uniform float uCamDist; // Distance from the target. Controlled by mouse scroll (zoom).
268
+ uniform int uAA; // Anti aliasing.
252
269
253
270
void main() {
254
271
vec2 fragCoord = vTexCoord * uResolution;
@@ -281,7 +298,13 @@ void main() {
281
298
vec3 vv = cross(uu, ww); // Up vector
282
299
283
300
// 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;
285
308
286
309
// Create view ray
287
310
vec3 rd = normalize(p.x * uu + p.y * vv + 1.5 * ww);
@@ -309,12 +332,13 @@ void main() {
309
332
float amb = 0.5 + 0.5 * dot(nor, vec3(0.0, 1.0, 0.0));
310
333
col = vec3(0.2, 0.3, 0.4) * amb + vec3(0.8, 0.7, 0.5) * dif;
311
334
col = sqrt(col);
335
+ tot += col;
312
336
}
337
+ }
338
+ tot /= float(uAA*uAA);
313
339
314
340
// Gamma correction
315
-
316
-
317
- fragColor = vec4(col, 1.0);
341
+ fragColor = vec4(tot, 1.0);
318
342
}
319
343
` )
320
344
buf .WriteByte (0 )
0 commit comments