48#define GLSL(src) "#version 150\n" #src
60 vert_color = vertColor;
62 gl_Position = mvp * vec4(vert + offset, 1.0);
68 out vec4 fragment_color;
71 fragment_color = vert_color;
88 vert_color = vertColor;
89 gl_Position = mvp * vec4(vert, 1.0);
95 out vec4 fragment_color;
98 fragment_color = vert_color;
104 layout(line_strip, max_vertices=2) out;
105 in vec4 vert_color[];
112 vec2 win_pos_0 = gl_in[0].gl_Position.xy;
113 vec2 win_pos_1 = gl_in[1].gl_Position.xy;
114 gl_Position = gl_in[0].gl_Position;
116 v_color = vert_color[0];
118 gl_Position = gl_in[1].gl_Position;
120 line_pos = psize * length(win_pos_1 - win_pos_0);
122 v_color = vert_color[1];
136 vert_color = vertColor;
137 gl_Position = vec4(vert, 1.0);
143 layout(line_strip, max_vertices=20) out;
144 in vec4 vert_color[];
148 uniform mat4 un_view;
149 uniform mat4 text_proj;
155 const
float PI = 3.14159265359;
157 mat4 translate_this (in vec3 coord)
172 mat4 n_mvp = (mvp * translate_this (coord));
173 vec4
res = n_mvp * vec4(vec3(0.0), 1.0);
180 return vec3 (
res.x*viewp.z+viewp.x,
res.y*viewp.w+viewp.y,
res.z);
184 return vec3 (0.0, 0.0, -1.0);
188 float angle2d (in vec2 at, in vec2 bt, in vec2 ct)
192 float theta = dot(ab,bc) / (length(ab) * length(bc));
195 return acos (-2.0 - theta);
197 else if (theta > 1.0)
199 return acos (2.0 - theta);
209 vec3 pa =
project (gl_in[0].gl_Position.xyz);
210 vec3 pb =
project (gl_in[1].gl_Position.xyz);
211 vec3 pc =
project (gl_in[2].gl_Position.xyz);
215 float alpha = angle2d (pa.xy, pb.xy, pd);
216 float beta = angle2d (pc.xy, pb.xy, pd);
217 float theta = angle2d (pa.xy, pb.xy, pc.xy);
218 if (pa.y > pb.y && pc.y > pb.y)
220 beta =
min (alpha, beta);
222 else if (pa.y < pb.y && pc.y < pb.y)
224 beta =
min (-alpha, -beta);
231 pe.y =
max(pa.y, pc.y);
244 beta = angle2d (pe, pb.xy, pd);
245 alpha = angle2d (pf, pb.xy, pd);
246 if (beta + alpha < PI)
253 float tan_factor = tan(theta/10.0);
254 float radial_factor = cos(theta/10.0);
255 float dist =
min(length(pb-pa), length(pb-pc)) / 3.0;
256 float x =
dist * cos(beta);
257 float y =
dist * sin(beta);
263 v_color = vert_color[0];
265 vec4 pos_s = mvp * gl_in[0].gl_Position;
266 vec4 pos_e = mvp * gl_in[1].gl_Position;
270 float line_save = psize * length(pos_e.xy - pos_s.xy);
271 line_pos = line_save;
278 for (i = 0;
i < 11;
i++)
281 gl_Position = text_proj * (vec4(pb, 1.0) + vec4(apos.x, apos.y, 0.0, 1.0));
285 line_pos = line_save;
290 x += tx * tan_factor;
291 y += ty * tan_factor;
295 line_pos += psize * length(bpos - apos) / 20.0;
298 line_save = line_pos;
303 line_pos = line_save;
306 pos_s = mvp * gl_in[2].gl_Position;
307 line_pos += depth * psize * length(pos_s.xy - pos_e.xy);
316 uniform uint pattern;
320 out vec4 fragment_color;
323 fragment_color = v_color;
324 if (line_pos == -1.0) discard;
325 uint bit = uint(round(line_pos/factor)) % 16U;
326 if ((pattern & (1U<<bit)) == 0U) discard;
332 uniform uint pattern;
336 out vec4 fragment_color;
339 fragment_color = v_color;
340 if (line_pos == -1.0) discard;
353 out vec4 surfaceColor;
354 out vec3 surfacePosition;
355 out vec3 surfaceNormal;
356 out vec3 surfaceToCamera;
359 surfaceColor = vertColor;
361 surfacePosition = vec3(m_view * vec4(vert, 1.0f));
362 surfaceNormal = mat3(m_view) * vertNormal;
363 surfaceToCamera = normalize (- surfacePosition);
364 gl_Position = mvp * vec4(vert, 1.0f);
379 out vec4 surfaceColor;
380 out vec3 surfacePosition;
381 out vec3 surfaceNormal;
382 out vec3 surfaceToCamera;
385 surfaceColor = vertColor;
386 vec4 pos = vec4 (radius*vert + offset, 1.0);
387 surfacePosition = vec3(m_view * pos);
388 surfaceNormal = mat3(m_view) * vert;
389 surfaceToCamera = normalize (- surfacePosition);
391 gl_Position = mvp * pos;
407 out vec4 surfaceColor;
408 out vec3 surfacePosition;
409 out vec3 surfaceNormal;
410 out vec3 surfaceToCamera;
412 vec3 rotate_this (in vec3 v, in vec4 quat)
414 vec3 u = vec3(quat.x, quat.y, quat.z);
416 return 2.0 * dot(u,v) * u + (s*s - dot(u,u)) * v + 2.0 * s *
cross (u,v);
421 surfaceColor = vertColor;
422 vec3 pos = vec3(radius*vert.x, radius*vert.y, height*vert.z);
423 vec3 norm = normalize (vec3(vert.x, vert.y, 0.0));
426 pos = rotate_this (pos, quat);
427 norm = rotate_this (norm, quat);
430 surfacePosition = vec3(m_view * vec4(pos,1.0));
431 surfaceNormal = mat3(m_view) * norm;
432 surfaceToCamera = normalize (- surfacePosition);
433 gl_Position = mvp * vec4(pos,1.0);
447 out vec4 surfaceColor;
448 out vec3 surfacePosition;
449 out vec3 surfaceNormal;
450 out vec3 surfaceToCamera;
452 vec3 rotate_this (in vec3 v, in vec4 quat)
454 vec3 u = vec3(quat.x, quat.y, quat.z);
456 return 2.0 * dot(u,v) * u + (s*s - dot(u,u)) * v + 2.0 * s *
cross (u,v);
461 surfaceColor = vertColor;
462 vec3 pos = vec3(radius*vert.x, radius*vert.y, height*vert.z);
464 float B = sqrt(radius*radius + height*height);
465 vec3 norm = vec3(height*vert.x/B, height*vert.y/B, radius/B);
468 pos = rotate_this (pos, quat);
469 norm = rotate_this (norm, quat);
472 surfacePosition = vec3(m_view * vec4(pos,1.0));
473 surfaceNormal = mat3(m_view) * norm;
474 surfaceToCamera = normalize (- surfacePosition);
475 gl_Position = mvp * vec4(pos,1.0);
488 out vec4 surfaceColor;
489 out vec3 surfacePosition;
490 out vec3 surfaceNormal;
491 out vec3 surfaceToCamera;
493 vec3 rotate_this (in vec3 v, in vec4 quat)
495 vec3 u = vec3(quat.x, quat.y, quat.z);
497 return 2.0 * dot(u,v) * u + (s*s - dot(u,u)) * v + 2.0 * s *
cross (u,v);
502 surfaceColor = vertColor;
503 vec3 pos = vec3(radius*vert.x, radius*vert.y, vert.z);
504 vec3 norm = vec3(0.0, 0.0, -1.0);
507 pos = rotate_this (pos, quat);
508 norm = rotate_this (norm, quat);
511 surfacePosition = vec3(m_view * vec4(pos,1.0));
512 surfaceNormal = mat3(m_view) * norm;
513 surfaceToCamera = normalize (- surfacePosition);
514 gl_Position = mvp * vec4(pos,1.0);
520 layout (triangle_strip, max_vertices=64) out;
525 uniform
float radius;
529 out vec4 surfaceColor;
530 out vec3 surfacePosition;
531 out vec3 surfaceNormal;
532 out vec3 surfaceToCamera;
534 float pi = 3.141592653;
536 vec3 create_perp (in vec3
axis)
538 vec3 u = vec3(0.0, 0.0, 1.0);
539 vec3 v = vec3(0.0, 1.0, 0.0);
541 if (length(
res) == 0.0)
550 vec3 v1 = gl_in[0].gl_Position.xyz;
551 vec3 v2 = gl_in[1].gl_Position.xyz;
552 vec3
axis = normalize(v2 - v1);
553 vec3 perp_x = create_perp (
axis);
555 float step = 2.0 *
pi / float(quality - 1);
556 for(
int i=0;
i<quality;
i++)
562 vec3 normal = normalize(ca*perp_x + sa*perp_y);
563 vec3
p1 = v1 + radius * normal;
564 vec3
p2 = v2 + radius * normal;
566 surfaceNormal = mat3(m_view) * normal;
567 gl_Position = mvp * vec4(p1, 1.0);
569 surfacePosition = vec3(m_view * vec4(p1, 1.0));
570 surfaceToCamera = normalize (- surfacePosition);
571 surfaceColor = vertCol[0];
574 gl_Position = mvp * vec4 (p2, 1.0);
575 surfacePosition = vec3(m_view * vec4(p2, 1.0));
576 surfaceToCamera = normalize (- surfacePosition);
577 surfaceColor = vertCol[1];
586 uniform mat4 un_view;
587 uniform mat4 text_proj;
589 uniform vec4 pos_shift;
595 out vec2 text_coords;
596 mat4 translate_this (in vec3 coord)
612 mat4 n_mvp = ((mvp * translate_this (coord)) * un_view) * translate_this (pos_shift.xyz);
613 vec4
res = n_mvp * vec4(vec3(0.0), 1.0);
620 return vec4 (
res.x*viewp.z+viewp.x,
res.y*viewp.w+viewp.y, pos_shift.w*
res.z, 1.0);
624 return vec4 (0.0, 0.0, -1.0, 0.0);
630 text_coords = tcoord;
631 vec4 pos =
project (offset) + vec4(vert, 0.0, 1.0);
632 gl_Position = text_proj * pos;
641 int COOK_BECKMANN = 4;
661 float ambient_occlusion;
674 uniform
Light AllLights[10];
677 uniform
int lights_on;
678 uniform
int numLights;
680 in vec4 surfaceColor;
681 in vec3 surfacePosition;
682 in vec3 surfaceNormal;
683 in vec3 surfaceToCamera;
685 out vec4 fragment_color;
687 const float PI = 3.14159265359;
688 const float EPS = 1e-5;
691 float saturate (in
float value)
693 return clamp(value, 0.0, 1.0);
697 float phong_diffuse()
704 vec3 fresnel_factor (in vec3 f0, in
float product)
706 return mix(f0, vec3(1.0), pow(1.01 - product, 5.0));
712 float D_blinn(in
float roughness, in
float NdH)
714 float m = roughness * roughness;
716 float n = 2.0 / m2 - 2.0;
717 return (n + 2.0) / (2.0 * PI) * pow(NdH, n);
720 float D_beckmann(in
float roughness, in
float NdH)
722 float m = roughness * roughness;
724 float NdH2 = NdH * NdH;
725 return exp((NdH2 - 1.0) / (m2 * NdH2)) / (PI * m2 * NdH2 * NdH2);
728 float D_GGX(in
float roughness, in
float NdH)
730 float m = roughness * roughness;
732 float d = (NdH * m2 - NdH) * NdH + 1.0;
733 return m2 / (PI * d * d);
736 float G_schlick(in
float roughness, in
float NdV, in
float NdL)
738 float k = roughness * roughness * 0.5;
739 float V = NdV * (1.0 -
k) + k;
740 float L = NdL * (1.0 -
k) + k;
741 return 0.25 / (V * L);
745 vec3 phong_specular(in vec3 V, in vec3 L, in vec3 N, in vec3 specular, in
float roughness)
747 vec3 R = reflect(-L, N);
748 float spec =
max(0.0, dot(V, R));
750 float k = 1.999 / (roughness * roughness);
752 return min(1.0, 3.0 * 0.0398 * k) * pow(spec,
min(10000.0, k)) * specular;
756 vec3 blinn_specular(in
float NdH, in vec3 specular, in
float roughness)
758 float k = 1.999 / (roughness * roughness);
760 return min(1.0, 3.0 * 0.0398 * k) * pow(NdH,
min(10000.0, k)) * specular;
764 vec3 cooktorrance_specular (in
int cook, in
float NdL, in
float NdV, in
float NdH, in vec3 specular, in
float roughness)
767 if (cook == COOK_BLINN)
769 D = D_blinn(roughness, NdH);
771 else if (cook == COOK_BECKMANN)
773 D = D_beckmann(roughness, NdH);
775 else if (cook == COOK_GGX)
777 D = D_GGX(roughness, NdH);
780 float G = G_schlick(roughness, NdV, NdL);
782 float rim = mix(1.0 - roughness * 0.9, 1.0, NdV);
784 return (1.0 / rim) * specular * G * D;
787 vec3 Apply_lighting_model (in
int model, in
Light light, in vec3 specular)
789 vec3 v_pos = surfacePosition;
797 L = normalize (-light.direction);
802 L = light.position - v_pos;
803 float dist = length (L);
805 A = 1.0 / (light.constant + light.linear*
dist + light.quadratic*
dist*
dist);
808 float theta = dot(L, normalize(light.position-light.direction));
809 if(theta > light.cone_angle)
811 float epsilon = light.spot_inner - light.spot_outer;
812 I = saturate((theta - light.spot_outer) / epsilon);
820 vec3 V = normalize(-v_pos);
821 vec3 H = normalize(L + V);
822 vec3 N = surfaceNormal;
825 float NdL =
max(0.0, dot(N, L));
826 float NdV =
max(0.001, dot(N, V));
827 float NdH =
max(0.001, dot(N, H));
828 float HdV =
max(0.001, dot(H, V));
829 float LdV =
max(0.001, dot(L, V));
838 specfresnel = fresnel_factor (specular, NdV);
839 specref = phong_specular (V, L, N, specfresnel, mat.roughness);
841 else if (
model == BLINN)
844 specfresnel = fresnel_factor (specular, HdV);
845 specref = blinn_specular (NdH, specfresnel, mat.roughness);
850 specfresnel = fresnel_factor(specular, HdV);
851 specref = cooktorrance_specular(
model, NdL, NdV, NdH, specfresnel, mat.roughness);
854 specref *= vec3(NdL);
857 vec3 diffref = (vec3(1.0) - specfresnel) * phong_diffuse() * NdL;
860 vec3 reflected_light = vec3(0);
861 vec3 diffuse_light = vec3(0);
864 vec3 light_color = light.intensity * A * I;
865 reflected_light += specref * light_color;
866 diffuse_light += diffref * light_color;
869 return diffuse_light * mix(mat.albedo, vec3(0.0), mat.metallic) + reflected_light;
872 vec3 Apply_fog (in vec3 lightColor)
876 float fogFactor = 0.0;
882 dist = abs (surfacePosition.z);
887 dist = length (surfacePosition);
892 fogFactor = (fog.depth.x -
dist)/(fog.depth.y - fog.depth.x);
894 else if (fog.mode == 2)
896 fogFactor = 1.0 / exp (
dist * fog.density);
900 fogFactor = 1.0 / exp((
dist * fog.density)* (
dist * fog.density));
902 fogFactor = saturate (fogFactor);
903 return mix (fog.color, lightColor, fogFactor);
906 float computeAO(vec3 N)
908 float ao = 0.5 + 0.5 * N.y;
910 ao *= mat.ambient_occlusion;
922 alpha = surfaceColor.w;
928 vec3 specular = mix(vec3(0.04), mat.albedo, mat.metallic);
930 for(
int i = 0;
i < numLights;
i++)
932 color += Apply_lighting_model (lights_on, AllLights[i], specular);
935 float ao = computeAO(surfaceNormal);
937 alpha = surfaceColor.w * mat.alpha;
942 vec3
col = Apply_fog(surfaceColor.rgb*
color);
943 fragment_color = vec4 (
col * alpha, alpha);
947 fragment_color = vec4 ((surfaceColor.rgb*
color) * alpha, alpha);
983 uniform
int view_is_ortho;
990 out vec4 surfaceColor;
991 out vec3 surfacePosition;
995 flat out
int form_type;
996 flat out
float clip_r_a;
997 flat out
float clip_r_b;
1001 surfaceColor = vertColor;
1004 vec3 center_vs = vec3(m_view * vec4(offset, 1.0));
1007 float r_vs = radius * length(mat3(m_view) * vec3(1.0, 0.0, 0.0));
1013 float inflation = (view_is_ortho == 0) ? 1.05 : 2.0;
1014 vec3 billboard_vs = center_vs + (inflation * r_vs) * vec3(vert.x, vert.y, 1.0);
1016 surfacePosition = billboard_vs;
1024 gl_Position = m_proj * vec4(billboard_vs, 1.0);
1025 gl_Position.z =
max(gl_Position.z, -gl_Position.w);
1035 uniform mat4 m_view;
1036 uniform mat4 m_proj;
1037 uniform
int view_is_ortho;
1048 in
float r_sphere_a;
1049 in
float r_sphere_b;
1051 out vec4 surfaceColor;
1052 out vec3 surfacePosition;
1056 flat out
int form_type;
1057 flat out
float clip_r_a;
1058 flat out
float clip_r_b;
1060 vec3 rotate_this (in vec3 v, in vec4 q)
1062 vec3 u = vec3(q.x, q.y, q.z);
1064 return 2.0 * dot(u, v) * u + (s*s - dot(u, u)) * v + 2.0 * s *
cross(u, v);
1069 surfaceColor = vertColor;
1072 vec3 local_p1 = vec3(0.0, 0.0, 0.5 * height);
1073 vec3 local_p2 = vec3(0.0, 0.0, -0.5 * height);
1076 local_p1 = rotate_this(local_p1, quat);
1077 local_p2 = rotate_this(local_p2, quat);
1079 vec3 p1_vs = vec3(m_view * vec4(offset + local_p1, 1.0));
1080 vec3 p2_vs = vec3(m_view * vec4(offset + local_p2, 1.0));
1083 float r_vs = radius * length(mat3(m_view) * vec3(1.0, 0.0, 0.0));
1086 float inflation = (view_is_ortho == 0) ? 1.05 : 2.0;
1087 float r_vs_bb = inflation * r_vs;
1090 float xmin =
min(p1_vs.x, p2_vs.x) - r_vs_bb;
1091 float xmax =
max(p1_vs.x, p2_vs.x) + r_vs_bb;
1092 float ymin =
min(p1_vs.y, p2_vs.y) - r_vs_bb;
1093 float ymax =
max(p1_vs.y, p2_vs.y) + r_vs_bb;
1094 float z_bb =
max(p1_vs.z, p2_vs.z) + r_vs_bb;
1096 float bx = mix(xmin,
xmax, (vert.x + 1.0) * 0.5);
1097 float by = mix(ymin,
ymax, (vert.y + 1.0) * 0.5);
1098 vec3 billboard_vs = vec3(bx, by, z_bb);
1100 surfacePosition = billboard_vs;
1108 float r_scale = length(mat3(m_view) * vec3(1.0, 0.0, 0.0));
1109 clip_r_a = r_sphere_a * r_scale;
1110 clip_r_b = r_sphere_b * r_scale;
1112 gl_Position = m_proj * vec4(billboard_vs, 1.0);
1113 gl_Position.z =
max(gl_Position.z, -gl_Position.w);
1123 uniform mat4 m_view;
1124 uniform mat4 m_proj;
1125 uniform
int view_is_ortho;
1134 out vec4 surfaceColor;
1135 out vec3 surfacePosition;
1139 flat out
int form_type;
1140 flat out
float clip_r_a;
1141 flat out
float clip_r_b;
1143 vec3 rotate_this (in vec3 v, in vec4 q)
1145 vec3 u = vec3(q.x, q.y, q.z);
1147 return 2.0 * dot(u, v) * u + (s*s - dot(u, u)) * v + 2.0 * s *
cross(u, v);
1152 surfaceColor = vertColor;
1155 vec3 local_apex = vec3(0.0, 0.0, 0.5 * height);
1156 vec3 local_base = vec3(0.0, 0.0, -0.5 * height);
1159 local_apex = rotate_this(local_apex, quat);
1160 local_base = rotate_this(local_base, quat);
1162 vec3 p_apex_vs = vec3(m_view * vec4(offset + local_apex, 1.0));
1163 vec3 p_base_vs = vec3(m_view * vec4(offset + local_base, 1.0));
1165 float r_vs = radius * length(mat3(m_view) * vec3(1.0, 0.0, 0.0));
1167 float inflation = (view_is_ortho == 0) ? 1.05 : 2.0;
1168 float r_vs_bb = inflation * r_vs;
1171 float xmin =
min(p_apex_vs.x, p_base_vs.x - r_vs_bb);
1172 float xmax =
max(p_apex_vs.x, p_base_vs.x + r_vs_bb);
1173 float ymin =
min(p_apex_vs.y, p_base_vs.y - r_vs_bb);
1174 float ymax =
max(p_apex_vs.y, p_base_vs.y + r_vs_bb);
1176 float z_bb =
max(p_apex_vs.z, p_base_vs.z) + r_vs_bb;
1180 float bx = mix(xmin,
xmax, (vert.x + 1.0) * 0.5);
1181 float by = mix(ymin,
ymax, (vert.y + 1.0) * 0.5);
1182 vec3 billboard_vs = vec3(bx, by, z_bb);
1184 surfacePosition = billboard_vs;
1194 gl_Position = m_proj * vec4(billboard_vs, 1.0);
1195 gl_Position.z =
max(gl_Position.z, -gl_Position.w);
1205 uniform mat4 m_view;
1206 uniform mat4 m_proj;
1207 uniform
int view_is_ortho;
1215 out vec4 surfaceColor;
1216 out vec3 surfacePosition;
1220 flat out
int form_type;
1221 flat out
float clip_r_a;
1222 flat out
float clip_r_b;
1224 vec3 rotate_this (in vec3 v, in vec4 q)
1226 vec3 u = vec3(q.x, q.y, q.z);
1228 return 2.0 * dot(u, v) * u + (s*s - dot(u, u)) * v + 2.0 * s *
cross(u, v);
1233 surfaceColor = vertColor;
1234 vec3 center_vs = vec3(m_view * vec4(offset, 1.0));
1237 vec3 norm_local = vec3(0.0, 0.0, -1.0);
1238 if (quat.w != 0.0) norm_local = rotate_this(norm_local, quat);
1239 vec3 norm_vs = normalize (mat3(m_view) * norm_local);
1241 float r_vs = radius * length(mat3(m_view) * vec3(1.0, 0.0, 0.0));
1242 float inflation = (view_is_ortho == 0) ? 1.05 : 2.0;
1245 vec3 billboard_vs = center_vs + (inflation * r_vs) * vec3(vert.x, vert.y, 1.0);
1247 surfacePosition = billboard_vs;
1256 gl_Position = m_proj * vec4(billboard_vs, 1.0);
1257 gl_Position.z =
max(gl_Position.z, -gl_Position.w);
1269 int COOK_BECKMANN = 4;
1289 float ambient_occlusion;
1307 uniform
Light AllLights[10];
1310 uniform
int lights_on;
1311 uniform
int numLights;
1312 uniform
int view_is_ortho;
1313 uniform mat4 m_proj;
1315 in vec4 surfaceColor;
1316 in vec3 surfacePosition;
1322 flat in
int form_type;
1328 flat in
float clip_r_a;
1329 flat in
float clip_r_b;
1331 out vec4 fragment_color;
1333 const float PI = 3.14159265359;
1334 const float EPS = 1e-5;
1337 float saturate (in
float value)
1339 return clamp(value, 0.0, 1.0);
1343 float phong_diffuse()
1350 vec3 fresnel_factor (in vec3 f0, in
float product)
1352 return mix(f0, vec3(1.0), pow(1.01 - product, 5.0));
1358 float D_blinn(in
float roughness, in
float NdH)
1360 float m = roughness * roughness;
1362 float n = 2.0 / m2 - 2.0;
1363 return (n + 2.0) / (2.0 * PI) * pow(NdH, n);
1366 float D_beckmann(in
float roughness, in
float NdH)
1368 float m = roughness * roughness;
1370 float NdH2 = NdH * NdH;
1371 return exp((NdH2 - 1.0) / (m2 * NdH2)) / (PI * m2 * NdH2 * NdH2);
1374 float D_GGX(in
float roughness, in
float NdH)
1376 float m = roughness * roughness;
1378 float d = (NdH * m2 - NdH) * NdH + 1.0;
1379 return m2 / (PI * d * d);
1382 float G_schlick(in
float roughness, in
float NdV, in
float NdL)
1384 float k = roughness * roughness * 0.5;
1385 float V = NdV * (1.0 -
k) + k;
1386 float L = NdL * (1.0 -
k) + k;
1387 return 0.25 / (V * L);
1391 vec3 phong_specular(in vec3 V, in vec3 L, in vec3 N, in vec3 specular, in
float roughness)
1393 vec3 R = reflect(-L, N);
1394 float spec =
max(0.0, dot(V, R));
1396 float k = 1.999 / (roughness * roughness);
1398 return min(1.0, 3.0 * 0.0398 * k) * pow(spec,
min(10000.0, k)) * specular;
1402 vec3 blinn_specular(in
float NdH, in vec3 specular, in
float roughness)
1404 float k = 1.999 / (roughness * roughness);
1406 return min(1.0, 3.0 * 0.0398 * k) * pow(NdH,
min(10000.0, k)) * specular;
1410 vec3 cooktorrance_specular (in
int cook, in
float NdL, in
float NdV, in
float NdH, in vec3 specular, in
float roughness)
1413 if (cook == COOK_BLINN)
1415 D = D_blinn(roughness, NdH);
1417 else if (cook == COOK_BECKMANN)
1419 D = D_beckmann(roughness, NdH);
1421 else if (cook == COOK_GGX)
1423 D = D_GGX(roughness, NdH);
1426 float G = G_schlick(roughness, NdV, NdL);
1428 float rim = mix(1.0 - roughness * 0.9, 1.0, NdV);
1430 return (1.0 / rim) * specular * G * D;
1433 vec3 Apply_lighting_model (in
int model, in
Light light, in vec3 specular, in vec3 v_pos, in vec3 N)
1439 if (light.type == 0)
1442 L = normalize (-light.direction);
1447 L = light.position - v_pos;
1448 float dist = length (L);
1450 A = 1.0 / (light.constant + light.linear*
dist + light.quadratic*
dist*
dist);
1451 if (light.type == 2)
1453 float theta = dot(L, normalize(light.position-light.direction));
1454 if(theta > light.cone_angle)
1456 float epsilon = light.spot_inner - light.spot_outer;
1457 I = saturate((theta - light.spot_outer) / epsilon);
1461 return vec3(0.0001);
1465 vec3 V = normalize(-v_pos);
1466 vec3 H = normalize(L + V);
1469 float NdL =
max(0.0, dot(N, L));
1470 float NdV =
max(0.001, dot(N, V));
1471 float NdH =
max(0.001, dot(N, H));
1472 float HdV =
max(0.001, dot(H, V));
1473 float LdV =
max(0.001, dot(L, V));
1482 specfresnel = fresnel_factor (specular, NdV);
1483 specref = phong_specular (V, L, N, specfresnel, mat.roughness);
1485 else if (
model == BLINN)
1488 specfresnel = fresnel_factor (specular, HdV);
1489 specref = blinn_specular (NdH, specfresnel, mat.roughness);
1494 specfresnel = fresnel_factor(specular, HdV);
1495 specref = cooktorrance_specular (
model, NdL, NdV, NdH, specfresnel, mat.roughness);
1498 specref *= vec3(NdL);
1501 vec3 diffref = (vec3(1.0) - specfresnel) * phong_diffuse() * NdL;
1504 vec3 reflected_light = vec3(0);
1505 vec3 diffuse_light = vec3(0);
1508 vec3 light_color = light.intensity * A * I;
1509 reflected_light += specref * light_color;
1510 diffuse_light += diffref * light_color;
1513 return diffuse_light * mix(mat.albedo, vec3(0.0), mat.metallic) + reflected_light;
1516 vec3 Apply_fog (in vec3 lightColor, in vec3 v_pos, in vec3 normal)
1520 float fogFactor = 0.0;
1526 dist = abs (v_pos.z);
1531 dist = length (v_pos);
1536 fogFactor = (fog.depth.x -
dist)/(fog.depth.y - fog.depth.x);
1538 else if (fog.mode == 2)
1540 fogFactor = 1.0 / exp (
dist * fog.density);
1544 fogFactor = 1.0 / exp((
dist * fog.density)* (
dist * fog.density));
1546 fogFactor = saturate (fogFactor);
1551 return mix (fog.color, lightColor, fogFactor);
1554 bool intersect_sphere(vec3 ro, vec3 rd, vec3 c,
float r, out Hit hit)
1557 vec3 cr =
cross(rd, oc);
1558 float d2 = dot(cr, cr);
1561 if (d2 > r2)
return false;
1563 float tca = -dot(oc, rd);
1564 float thc = sqrt(r2 - d2);
1566 float t = tca - thc;
1567 if (t < EPS)
t = tca + thc;
1568 if (t < EPS)
return false;
1570 hit.pos = ro + rd*
t;
1571 hit.normal = normalize(hit.pos - c);
1576 bool intersect_cylinder(vec3 ro, vec3 rd, vec3 pa, vec3 pb,
float ra, out Hit hit)
1581 float baba = dot(ba, ba);
1582 float bard = dot(ba, rd);
1583 float baoc = dot(ba, oc);
1585 vec3 vb =
cross(rd, ba);
1586 vec3 va =
cross(oc, ba);
1588 float k2 = dot(vb, vb);
1589 if (k2 < 1e-10)
return false;
1591 float k1 = dot(va, vb);
1592 float k0 = dot(va, va) - ra * ra * baba;
1594 float h = k1 * k1 - k2 * k0;
1595 if (h < 0.0)
return false;
1598 float t = (-k1 -
h) / k2;
1599 if (t < EPS)
t = (-k1 +
h) / k2;
1600 if (t < EPS)
return false;
1602 float y = baoc +
t * bard;
1603 if (
y > -EPS &&
y < baba + EPS)
1605 hit.pos = ro +
t * rd;
1606 hit.normal = normalize((hit.pos - pa) * baba - ba *
y);
1612 bool intersect_cone(vec3 ro, vec3 rd, vec3 pa, vec3 pb,
float ra, out Hit hit)
1617 float m0 = dot(ba,ba);
1618 float m1 = dot(oa,ba);
1619 float m2 = dot(rd,ba);
1621 if (m0 < 1e-6)
return false;
1623 float k = (ra*ra) / m0;
1624 float m = dot(rd,ba)/m0;
1625 float n = dot(oa,ba)/m0;
1626 float a = dot(rd,rd) - m2*m2*(1.0+
k)/m0;
1627 float b = dot(rd,oa) - m2*m1*(1.0+
k)/m0;
1628 float c = dot(oa,oa) - m1*m1*(1.0+
k)/m0;
1630 float h = b*b - a*
c;
1631 if (h < 0.0)
return false;
1635 float t1 = (-b -
h)/a;
1636 float t2 = (-b +
h)/a;
1639 if (t1 > EPS)
t = t1;
1640 if (t2 > EPS && t2 < t)
t = t2;
1642 if (t == 1e20)
return false;
1644 float y = m1 +
t*m2;
1646 if (
y > -EPS &&
y < m0 + EPS)
1648 hit.pos = ro +
t * rd;
1649 hit.normal = normalize(m0*(hit.pos-pa) - ba*(1.0+k)*
y);
1655 bool intersect_cap(vec3 ro, vec3 rd, vec3 center, vec3 normal,
float radius, out Hit hit)
1657 float denom = dot(normal, rd);
1658 if (abs(denom) < 1e-6)
return false;
1660 float t = dot(center - ro, normal) / denom;
1661 if (t < EPS)
return false;
1663 vec3
p = ro +
t * rd;
1664 vec3
v =
p - center;
1665 if (dot(v, v) > radius * radius)
return false;
1668 hit.normal = normal;
1670 if (dot(hit.normal, rd) > 0.0) hit.normal = -hit.normal;
1675 bool intersect_scene(vec3 ro, vec3 rd, out Hit hit)
1679 return intersect_sphere(ro, rd, imp_a, imp_r, hit);
1681 else if (form_type == 1)
1683 return intersect_cylinder(ro, rd, imp_a, imp_b, imp_r, hit);
1685 else if (form_type == 2)
1687 return intersect_cap(ro, rd, imp_a, imp_b, imp_r, hit);
1689 else if (form_type == 3)
1691 return intersect_cone(ro, rd, imp_a, imp_b, imp_r, hit);
1696 float computeAO(vec3 pos, vec3 N)
1698 const int AO_SAMPLES = 16;
1699 const float EPS_AO = 0.001;
1701 float maxDist = imp_r * 8.0;
1702 float occlusion = 0.0;
1703 vec3 tangent = normalize(abs(N.x) > 0.5 ?
cross(N, vec3(0,1,0)) :
cross(N, vec3(1,0,0)));
1704 vec3 bitangent =
cross(N, tangent);
1706 for(
int i = 0;
i < AO_SAMPLES;
i++)
1708 float u = float(i)/float(AO_SAMPLES);
1709 float v = fract(sin(
float(i)*91.345) * 47453.545);
1711 float phi = 2.0 * PI *
v;
1712 float cosTheta = sqrt(1.0 - u);
1713 float sinTheta = sqrt(u);
1714 vec3 dir = vec3(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
1717 dir = normalize(dir.x*tangent + dir.y*bitangent + dir.z*N);
1720 if(intersect_scene(pos + N * EPS_AO, dir, tmp))
1722 float dist = length(tmp.pos - pos);
1724 float weight = exp(-
dist * 2.0 / maxDist);
1725 occlusion += weight;
1729 float ao = 1.0 - (occlusion / float(AO_SAMPLES));
1730 ao = pow(ao, mat.gamma);
1732 return saturate(ao);
1735 float ray_computeAO(vec3 N)
1738 float ao = 0.5 + 0.5 * N.y;
1741 ao = pow(ao, mat.gamma);
1744 return saturate(ao);
1750 vec3 surfaceToCamera = normalize(-surfacePosition);
1751 vec3 ray_origin = surfacePosition;
1753 if (view_is_ortho == 0)
1755 ray_dir = vec3(0.0, 0.0, -1.0);
1759 ray_dir = normalize(surfacePosition);
1764 hit = intersect_scene (ray_origin, ray_dir, the_hit);
1767 if (dot(the_hit.normal, surfaceToCamera) < 0.0) the_hit.normal = -the_hit.normal;
1774 const float CLIP_MARGIN = 1.00f;
1777 float r_clip = clip_r_a * CLIP_MARGIN;
1778 if (dot(the_hit.pos - imp_a, the_hit.pos - imp_a) < r_clip * r_clip) discard;
1782 float r_clip = clip_r_b * CLIP_MARGIN;
1783 if (dot(the_hit.pos - imp_b, the_hit.pos - imp_b) < r_clip * r_clip) discard;
1786 vec4 hit_clip = m_proj * vec4(the_hit.pos, 1.0);
1787 if (hit_clip.w <= 0.0) discard;
1788 gl_FragDepth = clamp((hit_clip.z / hit_clip.w + 1.0) * 0.5, 0.0, 1.0);
1796 alpha = surfaceColor.w;
1802 vec3 specular = mix(vec3(0.04), mat.albedo, mat.metallic);
1804 vec3 diffuse = vec3(0.0);
1806 for(i = 0;
i < numLights;
i++)
1808 diffuse += Apply_lighting_model (lights_on, AllLights[i], specular, the_hit.pos, the_hit.normal);
1810 float ao = computeAO (the_hit.pos, the_hit.normal) * ray_computeAO (the_hit.normal);
1812 diffuse *= (ao * mat.ambient_occlusion);
1813 vec3 lit_color = pow(diffuse,vec3(1.0/mat.gamma));
1814 color = surfaceColor.rgb*lit_color;
1815 alpha = surfaceColor.w*mat.alpha;
1820 vec3
col = Apply_fog(
color, the_hit.pos, the_hit.normal);
1821 fragment_color = vec4 (
col * alpha, alpha);
1825 fragment_color = vec4 (
color * alpha, alpha);
1836 uniform mat4 un_view;
1837 uniform mat4 text_proj;
1839 uniform vec4 pos_shift;
1848 out vec2 text_coords;
1850 float angle2d (in vec2 at, in vec2 bt, in vec2 ct)
1854 float theta = dot(ab,bc) / (length(ab) * length(bc));
1857 return acos (-2.0 - theta);
1859 else if (theta > 1.0)
1861 return acos (2.0 - theta);
1865 return acos (theta);
1869 mat4 rotate_this_z (in
float theta)
1871 return mat4 ( vec4( cos(theta), sin(theta), 0.0, 0.0),
1872 vec4(-sin(theta), cos(theta), 0.0, 0.0),
1873 vec4( 0.0, 0.0, 1.0, 0.0),
1874 vec4( 0.0, 0.0, 0.0, 1.0));
1877 mat4 translate_this (in vec3 coord)
1880 translate[0] = vec4(1.0, 0.0, 0.0, 0.0);
1881 translate[1] = vec4(0.0, 1.0, 0.0, 0.0);
1882 translate[2] = vec4(0.0, 0.0, 1.0, 0.0);
1893 mat4 n_mvp = mvp * translate_this (coord) * un_view;
1894 vec4
res = n_mvp * vec4(vec3(0.0), 1.0);
1902 return vec4 (
res.x*viewp.z+viewp.x,
res.y*viewp.w+viewp.y,
shift.w*
res.z, 1.0);
1906 return vec4 (0.0, 0.0, -1.0, 0.0);
1912 text_coords = tcoord;
1913 float rot_angle = 0.0;
1914 vec4
shift = pos_shift;
1917 vec3 pos_a =
project (at_a, vec4(0.0)).xyz;
1918 vec3 pos_b =
project (at_b, vec4(0.0)).xyz;
1922 pa.y =
max(pos_a.y, pos_b.y);
1923 if (pa.y == pos_a.y)
1937 rot_angle = - angle2d (pa, pb, pc);
1940 rot_angle = -rot_angle;
1945 if (rot_angle != 0.0)
1947 pos =
project(offset,
shift) + vec4(vert, 0.0, 1.0) * rotate_this_z(rot_angle);
1953 gl_Position = text_proj * pos;
1959 uniform mat4 un_view;
1960 uniform mat4 text_proj;
1962 uniform vec4 pos_shift;
1972 const float PI = 3.14159265359;
1974 out vec2 text_coords;
1976 float angle2d (in vec2 at, in vec2 bt, in vec2 ct)
1980 float theta = dot(ab,bc) / (length(ab) * length(bc));
1983 return acos (-2.0 - theta);
1985 else if (theta > 1.0)
1987 return acos (2.0 - theta);
1991 return acos (theta);
1995 mat4 rotate_this_z (in
float theta)
1997 return mat4 ( vec4( cos(theta), sin(theta), 0.0, 0.0),
1998 vec4(-sin(theta), cos(theta), 0.0, 0.0),
1999 vec4( 0.0, 0.0, 1.0, 0.0),
2000 vec4( 0.0, 0.0, 0.0, 1.0));
2003 mat4 translate_this (in vec3 coord)
2006 translate[0] = vec4(1.0, 0.0, 0.0, 0.0);
2007 translate[1] = vec4(0.0, 1.0, 0.0, 0.0);
2008 translate[2] = vec4(0.0, 0.0, 1.0, 0.0);
2019 mat4 n_mvp = mvp * translate_this (coord) * un_view;
2020 vec4
res = n_mvp * vec4(vec3(0.0), 1.0);
2028 return vec4 (
res.x*viewp.z+viewp.x,
res.y*viewp.w+viewp.y,
shift.w*
res.z, 1.0);
2032 return vec4 (0.0, 0.0, -1.0, 0.0);
2038 text_coords = tcoord;
2039 float rot_angle = 0.0;
2040 vec4
shift = pos_shift;
2041 vec3 pos_a =
project (at_a, vec4(0.0)).xyz;
2042 vec3 pos_b =
project (at_b, vec4(0.0)).xyz;
2043 vec3 pos_c =
project (at_c, vec4(0.0)).xyz;
2048 float theta = angle2d (pa, pb, pc);
2049 pd.x = pb.x + 100.0;
2058 beta = angle2d (pa, pb, pd);
2059 alpha = angle2d (pc, pb, pd);
2060 if (pa.y > pb.y && pc.y > pb.y)
2062 gamma =
min (alpha, beta);
2064 else if (pa.y < pb.y && pc.y < pb.y)
2066 gamma =
min (-alpha, -beta);
2072 pe.y =
max(pa.y, pc.y);
2085 beta = angle2d (pe, pb, pd);
2087 alpha = angle2d (pf, pb, pd);
2088 if (beta + alpha < PI)
2093 rot_angle = PI/2.0 - gamma - theta/2.0;
2094 vec3 a = at_b + ((at_a - at_b)/3.0 + (at_c - at_b)/3.0)/2.0;
2096 float dist =
min(length(pb-pa), length(pb-pc)) / 3.0;
2097 float x = pb.x + (
shift.x+
dist) * sin(rot_angle);
2098 float y = pb.y + (
shift.y+
dist) * cos(rot_angle);
2099 if (pa.y < pb.y && pc.y < pb.y)
2101 gamma = -
max (alpha, beta);
2103 else if (pa.y > pb.y || pc.y > pb.y)
2107 rot_angle = PI/2.0 - gamma - theta/2.0;
2108 vec3
c = vec3(
x,
y, b.z);
2109 vec4 pos = vec4(c, 1.0);
2113 pos += vec4(vert, 0.0, 1.0) * rotate_this_z(rot_angle);
2117 pos += vec4(vert, 0.0, 1.0);
2119 gl_Position = text_proj * pos;
2124 uniform sampler2DRect tex;
2125 uniform vec4 vert_color;
2128 in vec2 text_coords;
2130 out vec4 fragment_color;
2133 vec2 coords = text_coords;
2134 vec4
color = vert_color * vec4(1.0, 1.0, 1.0, texture (tex, text_coords).r);
2140 uniform sampler2DRect tex;
2141 uniform vec4 vert_color;
2142 in vec2 text_coords;
2144 out vec4 fragment_color;
2147 vec4 sampled = vec4(1.0, 1.0, 1.0, texture (tex, text_coords).r);
2148 vec4
color = vert_color * sampled;
2156 out vec2 fragment_coord;
2159 fragment_coord = 0.5*vert + 0.5;
2160 gl_Position = vec4(vert, 0.0, 1.0);
2165 in vec2 fragment_coord;
2166 uniform vec4 first_color;
2167 uniform vec4 second_color;
2168 uniform
int gradient;
2171 out vec4 fragment_color;
2174 float horizontal = 1.0 - fragment_coord.x;
2175 float vertical = fragment_coord.y;
2181 factor = clamp ((horizontal -
position) * 2.0 + 0.5, 0.0, 1.0);
2185 factor = clamp ((0.5*(horizontal + vertical) -
position) * 2.0 + 0.5, 0.0, 1.0);
2189 factor = clamp ((0.5*(fragment_coord.x + vertical) -
position) * 2.0 + 0.5, 0.0, 1.0);
2193 factor = clamp ((vertical -
position) * 2.0 + 0.5, 0.0, 1.0);
2196 fragment_color = mix(first_color, second_color, factor);
2201 in vec2 fragment_coord;
2202 uniform vec4 first_color;
2203 uniform vec4 second_color;
2204 uniform
int gradient;
2207 out vec4 fragment_color;
2217 center = vec2 (0.0, 0.5);
2220 factor = clamp (
dist / 1.118, 0.0, 1.0);
2224 center = vec2 (1.0, 0.5);
2226 factor = clamp (
dist / 1.118, 0.0, 1.0);
2230 center = vec2 (0.5, 1.0);
2232 factor = clamp (
dist / 1.118, 0.0, 1.0);
2236 center = vec2 (0.5, 0.0);
2238 factor = clamp (
dist / 1.118, 0.0, 1.0);
2242 center = vec2 (1.0, 0.0);
2244 factor = clamp (
dist / 1.4142, 0.0, 1.0);
2248 center = vec2 (0.0, 0.0);
2250 factor = clamp (
dist / 1.4142, 0.0, 1.0);
2254 center = vec2 (1.0, 1.0);
2256 factor = clamp (
dist / 1.4142, 0.0, 1.0);
2260 center = vec2 (0.0, 1.0);
2262 factor = clamp (
dist / 1.4142, 0.0, 1.0);
2265 center = vec2 (0.5, 0.5);
2267 factor = clamp (
dist / 0.707, 0.0, 1.0);
2270 fragment_color = mix (first_color, second_color, factor);
void translate(project *this_proj, int status, int axis, vec3_t trans)
translate
Global variable declarations Global convenience function declarations Global data structure defin...
struct project project
data structure for the 'atomes' project
int main(int argc, char *argv[])
initialization of the atomes program
double precision, dimension(98), parameter c
double precision, dimension(3) rim
const GLchar * full_color
const GLchar * string_color_2d
const GLchar * string_vertex
const GLchar * line_stipple_color
const GLchar * cap_vertex_ray
const GLchar * angle_stipple
const GLchar * angle_vertex
const GLchar * background_circular
const GLchar * gs_cylinder_geom
const GLchar * background_vertex
const GLchar * cylinder_vertex
const GLchar * full_color_ray
const GLchar * string_color
const GLchar * cylinder_vertex_ray
const GLchar * full_vertex
const GLchar * line_color
const GLchar * line_stipple
const GLchar * cap_vertex
const GLchar * background_linear
const GLchar * angle_color
const GLchar * cone_vertex_ray
const GLchar * line_vertex
const GLchar * sphere_vertex
const GLchar * point_vertex
const GLchar * cone_vertex
const GLchar * degree_vertex
const GLchar * point_color
const GLchar * sphere_vertex_ray
const GLchar * angstrom_vertex