Advertisement
Xellug

GLSL RayTracer

Apr 1st, 2021
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Texture at iChannel0
  2. float deg1 = 3.14159265359 / 180.;
  3. vec4 tri[3];
  4. mat4 RotMat, ScalMat;
  5. mat4 MovMat;
  6. vec3 sphNormal;
  7.  
  8.  
  9. // ray starts at 'ro' and directed to normalized 'rd'
  10. vec3 triIntersect(vec3 ro, vec3 rd, vec3 v0, vec3 v1, vec3 v2 ){
  11.     vec3 v1v0 = v1 - v0;
  12.     vec3 v2v0 = v2 - v0;
  13.     vec3 rov0 = ro - v0;
  14.     vec3  n = cross( v1v0, v2v0 );
  15.     vec3  q = cross( rov0, rd );
  16.     float d = 1.0/dot( rd, n );
  17.     float u = d*dot( -q, v2v0 );
  18.     float v = d*dot(  q, v1v0 );
  19.     float t = d*dot( -n, rov0 );
  20.     if( u<0.0 || u>1.0 || v<0.0 || (u+v)>1.0 ) t = -1.0;
  21.     return vec3( t, u, v );
  22. }
  23.  
  24. // sphere of size 'ra' centered at point 'ce'
  25. vec3 sphIntersect(vec3 ro, vec3 rd, vec3 ce, float ra ){
  26.     vec3 oc = ro - ce;
  27.     float b = dot(oc, rd);
  28.     float c = dot(oc, oc) - ra*ra;
  29.     float h = b*b - c;
  30.     if(h<0.) return vec3(-1.); // no intersection
  31.     h = sqrt(h);
  32.     float nearLen = -b-h;
  33.     float farLen = -b+h;
  34.     vec3 near = rd*nearLen;
  35.     sphNormal = normalize(near-ce);
  36.     float coinc = dot(normalize(ro-near), sphNormal);
  37.     return vec3(nearLen, farLen, coinc); // Near point length, Far point length, Normal coincidence
  38. }
  39.  
  40. float plaIntersect( in vec3 ro, in vec3 rd, in vec4 p )
  41. {
  42.     return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz);
  43. }
  44.  
  45. void triCreate(float r, float angle){
  46.     float alpha1 = 0.;
  47.     for (int i=0; i<3; i++){
  48.         tri[i].x = r*cos((alpha1 + angle)*deg1);
  49.         tri[i].y = r*sin((alpha1 + angle)*deg1);
  50.         tri[i].z = 0.;
  51.         tri[i].w = 1.;
  52.         alpha1 += 120.;
  53.     }
  54. }
  55.  
  56. void rotate(vec3 angle){
  57.     mat4 RotX = mat4(1., 0.,           0.,            0.,
  58.                      0., cos(angle.x), -sin(angle.x), 0.,
  59.                      0., sin(angle.x), cos(angle.x),  0.,
  60.                      0., 0.,           0.,            1.);
  61.  
  62.     mat4 RotY = mat4(cos(angle.y),  0., sin(angle.y), 0.,
  63.                      0.,            1., 0.,           0.,
  64.                      -sin(angle.y), 0., cos(angle.y), 0.,
  65.                      0.,            0., 0.,           1.);
  66.  
  67.     mat4 RotZ = mat4(cos(angle.z), -sin(angle.z), 0., 0.,
  68.                      sin(angle.z), cos(angle.z),  0., 0.,
  69.                      0.,           0.,            1., 0.,
  70.                      0.,           0.,            0., 1.);
  71.     RotMat = RotX * RotY * RotZ;
  72. }
  73.  
  74. void move(vec3 dest){
  75.     MovMat = mat4(1.,     0.,     0.,     0.,
  76.                   0.,     1.,     0.,     0.,
  77.                   0.,     0.,     1.,     0.,
  78.                   dest.x, dest.y, dest.z, 1.);
  79. }
  80.  
  81. void mainImage(out vec4 fragColor, in vec2 fragCoord){
  82.     vec3 col;
  83.     float alpha = 0.;
  84.     /* Res correct: if uvmul = 1  =>  x=(-0,89; 0,89)  y=(-0.5; 0.5) */
  85.     float uvmul = 2.;
  86.     vec2 uv = fragCoord/iResolution.xy*uvmul;
  87.     uv = (uv - uvmul/2.) * iResolution.xy / iResolution.y;
  88.  
  89.     vec3 rayOrigin = vec3(0., 0., 0.); // Don't touch
  90.     vec3 rayDirection = normalize(vec3(uv.xy, 1.)); // Don't touch
  91.    
  92.     vec3 lightOrigin = vec3(3., 3., -1.);
  93.     vec3 sphereOrigin = vec3(0., 0., sin(iTime)*sin(iTime)*2.5+7.);
  94.     float sphereSize = 3.;
  95.    
  96.     triCreate(2., 0.);
  97.  
  98.     for(int i=0; i<3; i++) {            // Triangle move
  99.         rotate(vec3(.5,.3,0.1)*iTime);
  100.         move(vec3(sin(iTime)*2.,cos(iTime)*2.,cos(iTime)*1.5+5.5));
  101.         tri[i] = RotMat * tri[i];
  102.         tri[i] = MovMat * tri[i];
  103.     }
  104.    
  105.     vec3 tritex = triIntersect(rayOrigin, rayDirection, tri[0].xyz, tri[1].xyz, tri[2].xyz);
  106.     vec3 spher = sphIntersect(rayOrigin, rayDirection, sphereOrigin, sphereSize);
  107.     col = vec3(plaIntersect( rayOrigin, rayDirection, vec4( normalize(vec3(0.,1.,0.)),1.0) )/10.);
  108.     if (tritex.x > 0.) col = vec3(texture(iChannel0 , tritex.gb));
  109.    
  110.     if ( (spher.x > 0.)&&( (spher.x < tritex.r)||(tritex.r < 0.) ) ) {
  111.         vec3 refStart = spher.r*rayDirection;           // Glare
  112.         vec3 refDir = reflect(refStart, sphNormal);
  113.         float light = sphIntersect(refStart, refDir, lightOrigin, 0.1).x;
  114.         if (light > 0.){
  115.             col = vec3(1.);
  116.         }
  117.         else col = vec3(spher.z,0.,0.);
  118.     }
  119.     fragColor = vec4(col, 1.);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement