Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 430 core
  2.  
  3. #define FAR_CLIP  10000.0f
  4. #define width 1080
  5. #define height 720
  6.  
  7. struct Triangle
  8. {
  9.     vec3 vA, vB, vC;
  10. };
  11. struct Light{
  12.     vec4    dir;
  13.     vec4    color;
  14.     vec4    attenuation;
  15. };
  16. struct Primitive{
  17.     vec4 A, B, C;
  18. };
  19. struct Camera{
  20.     vec3    pos, dir, yAxis, xAxis ;
  21.     float   tanFovY, tanFovX;
  22. };
  23. struct Ray{
  24.     vec3    origin;
  25.     vec3    dir;
  26. };
  27.  
  28. layout(binding = 0, rgba32f) uniform image2D framebuffer;
  29. layout(std430, binding = 1) buffer meshtriangles
  30.  {
  31.     Triangle tris[ ];
  32.  };
  33.  
  34. uniform int mNumTriangles;
  35. uniform sampler2D diffuse;
  36. uniform uint reflectionDepth;
  37. uniform Camera camera;
  38. uniform mat4 mvpMatrix;
  39. Light light;
  40.  
  41.  
  42.  
  43.  
  44.  
  45. float hitTriangle(Ray r, Primitive t)
  46. {
  47. vec3 v0 = t.A.xyz, v1 = t.B.xyz, v2 = t.C.xyz;
  48.  
  49.   vec3 v0v1 = v1 - v0;
  50.   vec3 v0v2 = v2 - v0;
  51.  
  52.   vec3 pvec = cross(r.dir, v0v2);
  53.  
  54.   float det = dot(v0v1, pvec);
  55.  
  56.   if (det < 0.000001)
  57.     return FAR_CLIP;
  58.  
  59.   float invDet = 1.0 / det;
  60.  
  61.   vec3 tvec = r.origin - v0;
  62.  
  63.   float u = dot(tvec, pvec) * invDet;
  64.  
  65.   if (u < 0 || u > 1)
  66.     return FAR_CLIP;
  67.  
  68.   vec3 qvec = cross(tvec, v0v1);
  69.  
  70.   float v = dot(r.dir, qvec) * invDet;
  71.  
  72.   if (v < 0 || u + v > 1)
  73.     return FAR_CLIP;
  74.  
  75.   return dot(v0v2, qvec) * invDet;
  76. }
  77.  
  78.  
  79. Ray initRay(uint x, uint y, Camera cam){
  80.     Ray r;
  81.     vec3 dir;
  82.     float a, b, halfWidth, halfHeight;
  83.        
  84.     halfWidth = float(width)/2.0f;
  85.     halfHeight = float(height)/2.0f;
  86.  
  87.     a = cam.tanFovX*( (float(x)-halfWidth+0.5f) / halfWidth);
  88.     b = cam.tanFovY*( (halfHeight - float(y)-0.5f) / halfHeight);
  89.  
  90.     dir = normalize( a*cam.xAxis.xyz + b*cam.yAxis.xyz + cam.dir.xyz);
  91.        
  92.     r.dir = dir;
  93.     r.origin = cam.pos.xyz;
  94.    
  95.     return r;
  96. }
  97.  
  98. layout (local_size_x = 25, local_size_y = 8) in;
  99. void main(void) {
  100.     uint x = gl_GlobalInvocationID.x;
  101.     uint y = gl_GlobalInvocationID.y;
  102.     light.dir = vec4(0, -1, 0, 0);
  103.     light.color = vec4(1, 1, 1, 1);
  104.     light.attenuation = vec4(1.0f);
  105.     if(x < width && y < height)
  106.     {
  107.         vec4 color = vec4(0.0f, 0.0f, 0.0f, 1.0f);
  108.         vec4 tempColor = vec4(0);
  109.  
  110.         //Initialize the ray
  111.         Ray r = initRay(x, y, camera);
  112.  
  113.         float t = FAR_CLIP, temp = FAR_CLIP;
  114.         int currentObject = -1;
  115.  
  116.  
  117.         for(uint n = 0; n < reflectionDepth; ++n)
  118.         {
  119.             for(int i = 0; i < mNumTriangles; i++)
  120.             {
  121.                
  122.                     Primitive t0;
  123.                     t0.A = vec4(tris[i].vA, 1);
  124.                     t0.B =  vec4(tris[i].vB, 1);
  125.                     t0.C =  vec4(tris[i].vC, 1);
  126.                     temp = hitTriangle(r, t0);
  127.                    
  128.                    
  129.                
  130.                 if(temp < t && temp >= -0.001f)
  131.                 {
  132.                 color = vec4(0, 1, 0,1);
  133.                     t = temp;
  134.                     currentObject = i;
  135.                 }
  136.             }
  137.  
  138.         }
  139.  
  140.         imageStore(framebuffer, ivec2(x, y), color);
  141.  
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement