You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
3.5 KiB

  1. [vert.glsl]
  2. #version 130
  3. in vec3 in_Position;
  4. in vec3 in_Normal;
  5. in vec4 in_Color;
  6. uniform mat4 u_modelview;
  7. uniform mat4 u_view;
  8. uniform mat4 u_projection;
  9. uniform mat3 u_normalmat;
  10. out vec4 pass_vertex; /* View space */
  11. out vec3 pass_tnormal;
  12. out vec4 pass_color;
  13. void main(void)
  14. {
  15. vec4 vertex = u_modelview * vec4(in_Position, 1.0);
  16. vec3 tnorm = normalize(u_normalmat * in_Normal);
  17. pass_vertex = vertex;
  18. pass_tnormal = tnorm;
  19. pass_color = in_Color;
  20. gl_Position = u_projection * vertex;
  21. }
  22. [frag.glsl]
  23. #version 130
  24. #if defined GL_ES
  25. precision highp float;
  26. #endif
  27. in vec4 pass_vertex; /* View space */
  28. in vec3 pass_tnormal;
  29. in vec4 pass_color;
  30. uniform mat4 u_view;
  31. uniform mat4 u_inv_view;
  32. uniform vec4 u_lights[8 * 2];
  33. uniform float u_damage; /* FIXME: remove this */
  34. #if 0
  35. //Cos(45) = 0.70710678118
  36. //1.0 - Cos(45) = 0.29289321881
  37. const float cos_45 = 0.70710678118;
  38. const float inv_cos_45 = 0.29289321881;
  39. //Cube Light
  40. vec4 in_Light3_Pos = vec4(-10.0, 10.0, 5.0, 1.0);
  41. vec3 in_Light3_Size_Inner = vec3(3.0, 1.0, 3.0);
  42. vec3 in_Light3_Size_Outer = vec3(15.0, 15.0, 15.0);
  43. vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4);
  44. #endif
  45. void main(void)
  46. {
  47. vec3 tnormal = pass_tnormal;
  48. /* Material properties */
  49. vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
  50. float specular_power = 60.0;
  51. /* World properties */
  52. vec3 ambient = vec3(0.1, 0.1, 0.1);
  53. vec3 specular = vec3(0.0, 0.0, 0.0);
  54. vec3 diffuse = vec3(0.0, 0.0, 0.0);
  55. /* Light precalculations */
  56. vec3 v = normalize(-pass_vertex.xyz);
  57. /* Apply lighting */
  58. for (int i = 0; i < 8; i++)
  59. {
  60. vec4 pos = u_lights[i * 2];
  61. vec4 color = u_lights[i * 2 + 1];
  62. vec3 s, r, p;
  63. p = (u_view * pos).xyz;
  64. if (pos.w > 0.0)
  65. {
  66. /* Point light -- no attenuation yet */
  67. s = normalize(p - pass_vertex.xyz);
  68. }
  69. else
  70. {
  71. /* Directional light */
  72. s = normalize(-p);
  73. }
  74. r = reflect(-s, tnormal);
  75. float sdotn = max(dot(s, tnormal), 0.0);
  76. diffuse += color.xyz * sdotn;
  77. if (sdotn > 0.0)
  78. specular += color.xyz * specular_reflect
  79. * pow(max(dot(r, v), 0.0), specular_power);
  80. }
  81. #if 0
  82. //Light calculation for cube light
  83. vec3 specular_color = vec3(1.0, 1.0, 0.6);
  84. vec3 Local_Vertex = (u_inv_view * pass_vertex).xyz - (in_Light3_Pos).xyz;
  85. vec3 Proj_Vertex = clamp(Local_Vertex.xyz, -in_Light3_Size_Inner, in_Light3_Size_Inner);
  86. vec3 new_LightDir = Local_Vertex - Proj_Vertex;
  87. vec3 light_radius = max(vec3(0.0,0.0,0.0), vec3(1.0,1.0,1.0) - abs(new_LightDir / in_Light3_Size_Outer));
  88. float light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z));
  89. if (length(new_LightDir) == 0.0)
  90. sdotn = 1.0;
  91. else
  92. {
  93. new_LightDir = normalize((u_view * vec4(Proj_Vertex + in_Light3_Pos.xyz,1.0)).xyz - pass_vertex.xyz);
  94. sdotn = max(dot(new_LightDir, tnormal), 0.0);
  95. r = reflect(-new_LightDir, tnormal);
  96. if (sdotn > 0.0 && light_radius_mod > 0.0)
  97. specular += specular_color * min(specular_reflect, light_radius_mod)
  98. * pow(max(dot(r, v), 0.0), specular_power);
  99. }
  100. diffuse += in_Light3_diffuse * min(sdotn, light_radius_mod);
  101. //----------
  102. #endif
  103. vec3 light = ambient + diffuse + specular;
  104. vec4 real_color = mix(pass_color, vec4(1.2, 1.2, 1.2, 1.0), u_damage);
  105. gl_FragColor = real_color * vec4(light, 1.0);
  106. }