Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. #version 130
  2. uniform vec4 eye;
  3. uniform vec4 ambient;
  4. uniform vec4[20] objects;
  5. uniform vec4[20] objColors;
  6. uniform vec4[10] lightsDirection;
  7. uniform vec4[10] lightsIntensity;
  8. uniform vec4[10] lightsPosition;
  9. uniform vec4 sizes; //{number of objects , number of lights , num of reflective objects, hight}
  10. uniform vec2[10] objReflective;
  11. in vec3 position0; //Pixels in the picture
  12. const float FPI_PRECISION = 0.001;
  13. const int RECURSION_COUNTER = 5;
  14. vec3 recursive_colorCalc(vec4 view_point, vec3 ray);
  15.  
  16. struct rec_help{
  17. vec4 view_point;
  18. vec3 ray;
  19. vec3 color;
  20. bool rec_flag;
  21. };
  22.  
  23. //------------------------------------------------Intersection Logic START----------------------------------------------
  24.  
  25. vec4 calc_intersection_with_plane(vec3 sourcePoint, vec3 direction, vec4 plane){
  26. vec4 intersection_point = vec4(0,0,0,-1);
  27. vec3 norm_direction = normalize(direction);
  28. vec3 norm_plane = normalize(plane.xyz);
  29. float denominator = dot(norm_plane, norm_direction);
  30. float t = 0.0;
  31. float nominator = 0.0;
  32. if (denominator > FPI_PRECISION){
  33. nominator = dot(sourcePoint, norm_plane) + plane.w/length(plane.xyz);
  34. t = -nominator/denominator;
  35. if (t > FPI_PRECISION){
  36. intersection_point = vec4(sourcePoint + t*norm_direction, 0);
  37. }
  38. }
  39. return (intersection_point);
  40. }
  41.  
  42. vec4 calc_intersection_with_sphere(vec3 sourcePoint, vec3 direction, vec4 sphere){
  43. float t1,t2, sol;
  44. vec3 norm_direction = normalize(direction);
  45. vec4 returned_point = vec4(0,0,0,-1);
  46. float a = 1.0;
  47. float b = dot(2*norm_direction, (sourcePoint - sphere.xyz));
  48. float c = pow(length(sourcePoint - sphere.xyz), 2) - pow(sphere.w, 2);
  49. float b24ac = pow(b, 2) - 4*a*c;
  50. if (b24ac < FPI_PRECISION){
  51. return returned_point;
  52. }
  53. b24ac = sqrt(b24ac);
  54. t1 = (-b + b24ac)/(2*a);
  55. t2 = (-b - b24ac)/(2*a);
  56. if (t1 < FPI_PRECISION){
  57. if (t2 < FPI_PRECISION){
  58. return returned_point;
  59. }
  60. else {
  61. sol = t2;
  62. }
  63. } else {
  64. if (t2 < FPI_PRECISION){
  65. sol = t1;
  66. }
  67. else {
  68. sol = (t1 > t2) ? t2 : t1;
  69. }
  70. }
  71. if (sol <= FPI_PRECISION){
  72. return returned_point;
  73. }
  74. returned_point.xyz = sourcePoint + norm_direction*sol;
  75. if (!(distance(returned_point.xyz, sphere.xyz) - sphere.w >= 0.0001)){
  76. returned_point.w = 0;
  77. }
  78. return returned_point;
  79. }
  80.  
  81. vec4 intersection(vec4 sourcePoint, vec3 v, int index) {
  82. float min_dist = 100000; //dist is 1-0.
  83. vec4 intersection_point = vec4(0);
  84. vec4 intersection_and_index = vec4(0,0,0,-1);
  85. int index_of_obj = -1;
  86. for(int i = 0; i < sizes[0]; i++){ //Iterate through objects to find the min intersection.
  87. if (objects[i].w < 0){
  88. //plane
  89. intersection_point = calc_intersection_with_plane(sourcePoint.xyz, v, objects[i]);
  90. } else {
  91. //sphere
  92. intersection_point = calc_intersection_with_sphere(sourcePoint.xyz, v, objects[i]);
  93. }
  94. if (intersection_point.w > -1 && distance(sourcePoint.xyz, intersection_point.xyz) < min_dist){
  95. if (distance(sourcePoint.xyz, intersection_point.xyz) > FPI_PRECISION){
  96. min_dist = distance(sourcePoint.xyz, intersection_point.xyz);
  97. intersection_and_index = vec4(intersection_point.xyz, i);
  98. }
  99. }
  100. }
  101. return intersection_and_index;
  102. }
  103.  
  104. //------------------------------------------------Intersection Logic END----------------------------------------------
  105.  
  106.  
  107. //------------------------------------------------Light Logic START----------------------------------------------
  108.  
  109.  
  110. vec4 calc_normal(vec4 intersectionPoint, bool reflective) {
  111. vec4 normal = vec4(0,0,0,1);
  112. if (objects[int(intersectionPoint.w)].w > 0){
  113. normal.xyz = normalize(intersectionPoint.xyz - objects[int(intersectionPoint.w)].xyz);
  114.  
  115. } else { //A plane
  116. normal.xyz = -normalize(objects[int(intersectionPoint.w)].xyz);
  117. if ((intersectionPoint.x < 0 && intersectionPoint.y > 0) || (intersectionPoint.x > 0 && intersectionPoint.y < 0)){
  118. if ((mod(int(1.5*intersectionPoint.x),2) != mod(int(1.5*intersectionPoint.y),2)) ){
  119. normal.w = 0.5;
  120. }
  121. } else if ((mod(int(1.5*intersectionPoint.x),2) == mod(int(1.5*intersectionPoint.y),2)) ){
  122. normal.w = 0.5;
  123. }
  124. } //Calculate normal to a plane.
  125. return normal;
  126. }
  127.  
  128. vec4 calc_vec_to_light(vec4 intersectionPoint, vec4 light, vec4 light_pos){
  129. vec4 L_vec_to_light = vec4(0,0,0,1);
  130. if (light.w != 0.0){ //Checks if the light is positional or sun-like
  131. L_vec_to_light.xyz = normalize(light_pos.xyz - intersectionPoint.xyz);
  132. if (dot(normalize(light.xyz), -L_vec_to_light.xyz) <= light_pos.w){
  133. L_vec_to_light.w = 0;
  134. } else {}
  135. } else {
  136. L_vec_to_light.xyz = -normalize(light.xyz);
  137. }
  138. return L_vec_to_light;
  139. }
  140.  
  141.  
  142. rec_help colorCalc(vec4 view_point, vec3 current_ray, int recursion_counter) {
  143. float S_I = 1;
  144. int positional_light_index = 0;
  145. vec3 color = vec3(0);
  146. vec3 I_E = vec3(0);
  147. vec3 Sigma = vec3(0);
  148. vec3 Temp_Sigma = vec3(0);
  149. vec3 K_S = vec3(0.7, 0.7, 0.7);
  150. vec3 L_vec_to_light = vec3(0);
  151. vec4 L_vec_to_light_container = vec4(0);
  152. vec3 R_reflected = vec3(0);
  153. vec3 K_A = vec3(0);
  154. vec4 intersectionPoint = intersection(view_point, current_ray, -1);
  155. vec4 normal = vec4(calc_normal(intersectionPoint, false));
  156. rec_help ANS;
  157. if (intersectionPoint.w > -1){
  158. for (int i = 0; i < sizes[2]; i++){
  159. if (int(intersectionPoint.w) == objReflective[i].x && recursion_counter < RECURSION_COUNTER){ //In case the object with hit is reflective.
  160. ANS.rec_flag = true;
  161. ANS.view_point = intersectionPoint;
  162. ANS.ray = normalize(reflect(current_ray, normal.xyz));
  163. return ANS;
  164. }
  165. }
  166. } else { //We didn't intersect with any object.
  167. ANS.rec_flag = false;
  168. ANS.color = vec3(0);
  169. return ANS;
  170. }
  171. K_A = vec3(objColors[int(intersectionPoint.w)]);
  172. normal = vec4(calc_normal(intersectionPoint, false));
  173. vec3 K_D = vec3(K_A)*normal.w;
  174. color += I_E + K_A*ambient.xyz; //I = I_E + K_A*I_A
  175. for (int i = 0; i < sizes[1]; i++){
  176. S_I = 1.0;
  177. L_vec_to_light_container = calc_vec_to_light(intersectionPoint, lightsDirection[i],
  178. lightsPosition[positional_light_index++]);
  179. L_vec_to_light = L_vec_to_light_container.xyz;
  180. S_I *= L_vec_to_light_container.w;
  181. //Checks if a given point is obstructed.
  182. vec4 partial_intersection = vec4(intersectionPoint.xyz, 2);
  183. vec4 intersection_check = intersection(partial_intersection, L_vec_to_light, int(intersectionPoint.w));
  184. if (intersection_check.w > -1){ //Stops a positional light from being obstructed from a plane behind it.
  185. if(lightsDirection[i].w == 1.0 &&
  186. (distance(intersectionPoint.xyz,lightsPosition[positional_light_index - 1].xyz) -
  187. distance(intersectionPoint.xyz,intersection_check.xyz) > FPI_PRECISION )){
  188. S_I = 0;
  189. }
  190. else {
  191. if(lightsDirection[i].w == 0.0){
  192. if (objects[int(intersection_check.w)].w > 0){//If a shadow from directional light sources
  193. S_I = 0; //Hits a plane, it doesn't cast a shadow.
  194. }
  195. }
  196. }
  197.  
  198. }
  199. R_reflected = normalize(reflect(L_vec_to_light, normal.xyz));
  200. Temp_Sigma += clamp(K_D*(dot(normal.xyz, L_vec_to_light)), 0, 1);
  201. Temp_Sigma += clamp(K_S*(pow(dot(normalize(intersectionPoint.xyz-view_point.xyz), R_reflected),
  202. objColors[int(intersectionPoint.w)].w)), 0, 1);
  203. Temp_Sigma *= lightsIntensity[i].xyz;
  204. Temp_Sigma *= S_I;
  205. Sigma += clamp(Temp_Sigma,0 ,1);
  206. }
  207. color += clamp(Sigma, 0, 1);
  208. ANS.rec_flag = false;
  209. ANS.color = clamp(color, 0, 1);
  210. return ANS;
  211. }
  212.  
  213.  
  214.  
  215. vec3 recursive_colorCalc(vec4 view_point, vec3 ray){
  216. rec_help Current_iter;
  217. Current_iter.view_point = view_point;
  218. Current_iter.ray = ray;
  219. Current_iter.color = vec3(0);
  220. Current_iter.rec_flag = false;
  221. for (int i = 0; i <= RECURSION_COUNTER; i++){
  222. Current_iter = colorCalc(Current_iter.view_point, Current_iter.ray, i);
  223. if (Current_iter.rec_flag == false){
  224. break;
  225. }
  226. Current_iter.ray;
  227. }
  228. return Current_iter.color;
  229. }
  230.  
  231.  
  232.  
  233. //------------------------------------------------Light Logic END----------------------------------------------
  234.  
  235. void main() {
  236. vec3 color = vec3(0);
  237. vec4 intersectionPoint = intersection(eye, normalize(position0 - eye.xyz), -1);
  238. vec3 initial_ray = vec3(normalize(position0 - eye.xyz));
  239. color = recursive_colorCalc(eye, initial_ray);
  240. gl_FragColor = vec4(color, 1);
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement