gouraud.fs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #version 110
  2. #define INTENSITY_CORRECTION 0.6
  3. // normalized values for (-0.6/1.31, 0.6/1.31, 1./1.31)
  4. const vec3 LIGHT_TOP_DIR = vec3(-0.4574957, 0.4574957, 0.7624929);
  5. #define LIGHT_TOP_DIFFUSE (0.8 * INTENSITY_CORRECTION)
  6. #define LIGHT_TOP_SPECULAR (0.125 * INTENSITY_CORRECTION)
  7. #define LIGHT_TOP_SHININESS 20.0
  8. // normalized values for (1./1.43, 0.2/1.43, 1./1.43)
  9. const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074);
  10. #define LIGHT_FRONT_DIFFUSE (0.3 * INTENSITY_CORRECTION)
  11. #define INTENSITY_AMBIENT 0.3
  12. const vec3 ZERO = vec3(0.0, 0.0, 0.0);
  13. const vec3 GREEN = vec3(0.0, 0.7, 0.0);
  14. const vec3 YELLOW = vec3(0.5, 0.7, 0.0);
  15. const vec3 RED = vec3(0.7, 0.0, 0.0);
  16. const vec3 WHITE = vec3(1.0, 1.0, 1.0);
  17. const float EPSILON = 0.0001;
  18. const float BANDS_WIDTH = 10.0;
  19. struct SlopeDetection
  20. {
  21. bool actived;
  22. float normal_z;
  23. mat3 volume_world_normal_matrix;
  24. };
  25. uniform vec4 uniform_color;
  26. uniform SlopeDetection slope;
  27. #ifdef ENABLE_ENVIRONMENT_MAP
  28. uniform sampler2D environment_tex;
  29. uniform bool use_environment_tex;
  30. #endif // ENABLE_ENVIRONMENT_MAP
  31. varying vec3 clipping_planes_dots;
  32. // x = diffuse, y = specular;
  33. varying vec2 intensity;
  34. varying vec3 delta_box_min;
  35. varying vec3 delta_box_max;
  36. varying vec4 model_pos;
  37. varying float world_pos_z;
  38. varying float world_normal_z;
  39. varying vec3 eye_normal;
  40. uniform bool compute_triangle_normals_in_fs;
  41. void main()
  42. {
  43. if (any(lessThan(clipping_planes_dots, ZERO)))
  44. discard;
  45. vec3 color = uniform_color.rgb;
  46. float alpha = uniform_color.a;
  47. vec2 intensity_fs = intensity;
  48. vec3 eye_normal_fs = eye_normal;
  49. float world_normal_z_fs = world_normal_z;
  50. if (compute_triangle_normals_in_fs) {
  51. vec3 triangle_normal = normalize(cross(dFdx(model_pos.xyz), dFdy(model_pos.xyz)));
  52. #ifdef FLIP_TRIANGLE_NORMALS
  53. triangle_normal = -triangle_normal;
  54. #endif
  55. // First transform the normal into camera space and normalize the result.
  56. eye_normal_fs = normalize(gl_NormalMatrix * triangle_normal);
  57. // Compute the cos of the angle between the normal and lights direction. The light is directional so the direction is constant for every vertex.
  58. // Since these two are normalized the cosine is the dot product. We also need to clamp the result to the [0,1] range.
  59. float NdotL = max(dot(eye_normal_fs, LIGHT_TOP_DIR), 0.0);
  60. intensity_fs = vec2(0.0, 0.0);
  61. intensity_fs.x = INTENSITY_AMBIENT + NdotL * LIGHT_TOP_DIFFUSE;
  62. vec3 position = (gl_ModelViewMatrix * model_pos).xyz;
  63. intensity_fs.y = LIGHT_TOP_SPECULAR * pow(max(dot(-normalize(position), reflect(-LIGHT_TOP_DIR, eye_normal_fs)), 0.0), LIGHT_TOP_SHININESS);
  64. // Perform the same lighting calculation for the 2nd light source (no specular applied).
  65. NdotL = max(dot(eye_normal_fs, LIGHT_FRONT_DIR), 0.0);
  66. intensity_fs.x += NdotL * LIGHT_FRONT_DIFFUSE;
  67. // z component of normal vector in world coordinate used for slope shading
  68. world_normal_z_fs = slope.actived ? (normalize(slope.volume_world_normal_matrix * triangle_normal)).z : 0.0;
  69. }
  70. if (slope.actived && world_normal_z_fs < slope.normal_z - EPSILON) {
  71. color = vec3(0.7, 0.7, 1.0);
  72. alpha = 1.0;
  73. }
  74. // if the fragment is outside the print volume -> use darker color
  75. color = (any(lessThan(delta_box_min, ZERO)) || any(greaterThan(delta_box_max, ZERO))) ? mix(color, ZERO, 0.3333) : color;
  76. #ifdef ENABLE_ENVIRONMENT_MAP
  77. if (use_environment_tex)
  78. gl_FragColor = vec4(0.45 * texture2D(environment_tex, normalize(eye_normal_fs).xy * 0.5 + 0.5).xyz + 0.8 * color * intensity_fs.x, alpha);
  79. else
  80. #endif
  81. gl_FragColor = vec4(vec3(intensity_fs.y) + color * intensity_fs.x, alpha);
  82. }