/
tessellation-inline-glsl.lisp
187 lines (152 loc) · 5.61 KB
/
tessellation-inline-glsl.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(in-package :cepl.examples+camera)
;;- - - - - - - - - - - - - - - - - -
(defparameter sphere-stream nil)
(defparameter brick nil)
(defstruct sphere
(pos (v! 0 0 -10))
(rot (q:identity)))
(defparameter sphere-a (make-sphere :pos (v! 0 0 -1.5)))
;;- - - - - - - - - - - - - - - - - -
(defun model->world (x)
(m4:* (m4:translation (sphere-pos x)) (q:to-mat4 (sphere-rot x))))
(defun world->clip (c)
(m4:* (cam->clip c) (world->cam c)))
(defun model->clip (m c)
(m4:* (world->clip c) (model->world m)))
;;- - - - - - - - - - - - - - - - - -
(defparameter bp (make-blending-params))
(defparameter camera (make-camera))
(defparameter factor 0)
;;- - - - - - - - - - - - - - - - - -
;; Inline glsl for vertex shaders is not currently supported
(defun-g sphere-vert ((vert g-pnt) &uniform (model->clip :mat4))
(values (* model->clip (v! (pos vert) 1))
(pos vert)))
(def-glsl-stage sphere-tess-con (("position" (:vec3 3))
&context :450 :tessellation-control
(:patch 3))
"
layout(vertices = 3) out;
void main()
{
float TessLevelInner = 5f;
float TessLevelOuter = 5f;
v_out[gl_InvocationID].pos = v_in[gl_InvocationID].position;
if (gl_InvocationID == 0) {
gl_TessLevelInner[0] = TessLevelInner;
gl_TessLevelOuter[0] = TessLevelOuter;
gl_TessLevelOuter[1] = TessLevelOuter;
gl_TessLevelOuter[2] = TessLevelOuter;
}
}
"
(("pos" :vec3)))
(def-glsl-stage sphere-tess-eval (("position" (:vec3 3))
&uniform ("model_to_clip" :mat4)
&context :450 :tessellation-evaluation
(:patch 3))
"
layout(triangles, equal_spacing, ccw) in;
void main()
{
vec3 p0 = gl_TessCoord.x * v_in[0].position;
vec3 p1 = gl_TessCoord.y * v_in[1].position;
vec3 p2 = gl_TessCoord.z * v_in[2].position;
vec3 pos = normalize(p0 + p1 + p2);
gl_Position = model_to_clip * vec4(pos, 1);
v_out.pos = pos;
v_out.patch_distance = gl_TessCoord;
}
"
(("pos" :vec3) ("patch_distance" :vec3)))
(def-glsl-stage sphere-geom (("position" (:vec3 3))
("patch_distance" (:vec3 3))
&uniform ("normal_mat" :mat3)
&context :450 :geometry)
"layout(triangle_strip, max_vertices = 3) out;
void main()
{
vec3 A = v_in[2].position - v_in[0].position;
vec3 B = v_in[1].position - v_in[0].position;
vec3 f_norm = normal_mat * normalize(cross(A, B));
gl_Position = gl_in[0].gl_Position;
v_out.patch_dist = v_in[0].patch_distance;
v_out.tri_dist = vec3(1, 0, 0);
v_out.facet_norm = f_norm;
EmitVertex();
gl_Position = gl_in[1].gl_Position;
v_out.patch_dist = v_in[1].patch_distance;
v_out.tri_dist = vec3(0, 1, 0);
v_out.facet_norm = f_norm;
EmitVertex();
gl_Position = gl_in[2].gl_Position;
v_out.patch_dist = v_in[2].patch_distance;
v_out.tri_dist = vec3(0, 0, 1);
v_out.facet_norm = f_norm;
EmitVertex();
EndPrimitive();
}
"
(("patch_dist" :vec3) ("facet_norm" :vec3) ("tri_dist" :vec3)))
(def-glsl-stage sphere-frag (("patch_distance" :vec3) ("facet_normal" :vec3)
("tri_distance" :vec3)
&context :450 :fragment)
"
float amplify(float d, float scale, float offset)
{
d = scale * d + offset;
d = clamp(d, 0, 1);
d = 1 - exp2(-2*d*d);
return d;
}
void main()
{
vec3 LightPosition = vec3(0, 1, 0);
vec3 DiffuseMaterial = vec3(1, 0, 0);
vec3 AmbientMaterial = vec3(0.2, 0.2, 0.2);
vec3 N = normalize(v_in.facet_normal);
vec3 L = LightPosition;
float df = abs(dot(N, L));
vec3 color = AmbientMaterial + df * DiffuseMaterial;
float d1 = min(min(v_in.tri_distance.x, v_in.tri_distance.y), v_in.tri_distance.z);
float d2 = min(min(v_in.patch_distance.x, v_in.patch_distance.y), v_in.patch_distance.z);
color = amplify(d1, 40, -0.5) * amplify(d2, 60, -0.5) * color;
out_color = vec4(color, 1.0);
}"
(("out_color" :vec4)))
(defpipeline-g draw-sphere ((:patch 3))
:vertex (sphere-vert g-pnt)
:tessellation-control (sphere-tess-con (:vec3 3))
:tessellation-evaluation (sphere-tess-eval (:vec3 3))
:geometry (sphere-geom (:vec3 3) (:vec3 3))
:fragment (sphere-frag :vec3 :vec3 :vec3))
;;- - - - - - - - - - - - - - - - - -
(defun step-demo ()
(incf factor 0.001)
(setf (sphere-rot sphere-a)
(q:from-axis-angle (v! (sin factor) (cos factor) 1) 10s0))
(clear)
(map-g #'draw-sphere sphere-stream
:model_to_clip (model->clip sphere-a camera))
(swap))
;;- - - - - - - - - - - - - - - - - -
(defun init ()
(destructuring-bind (d i) (dendrite.primitives:sphere-data
:lines-of-longitude 10
:lines-of-latitude 10)
(setf sphere-stream (make-buffer-stream
(make-gpu-array d :element-type 'g-pnt)
:index-array (make-gpu-array i :element-type :ushort))
brick (sample
(dirt:load-image-to-texture
(merge-pathnames "brick/col.png" *examples-dir*))))))
(let ((running nil))
(defun run-loop ()
(unless brick (init))
(setf running t)
(loop :while (and running (not (shutting-down-p))) :do
(continuable
(step-host)
(update-repl-link)
(step-demo))))
(defun stop-loop () (setf running nil)))