Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
192
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 Camera camera;
  37. Light light;
  38.  
  39.  
  40.  
  41.  
  42.  
  43. float hitTriangle(Ray r, Primitive t)
  44. {
  45.     vec3 AB = t.B.xyz - t.A.xyz;
  46.     vec3 AC = t.C.xyz - t.A.xyz;
  47.  
  48.     float det = determinant( mat3(AB, AC, -1.0f*r.dir) );
  49.    
  50.     if(det == 0.0f){
  51.         return FAR_CLIP;
  52.     } else {
  53.         vec3 oA = r.origin - t.A.xyz;
  54.        
  55.         mat3 Di = inverse(mat3(AB, AC, -1.0f*r.dir));
  56.         vec3 solution = Di*oA;
  57.  
  58.         if(solution.x >= -0.0001 && solution.x <= 1.0001){
  59.             if(solution.y >= -0.0001 && solution.y <= 1.0001){
  60.                 if(solution.x + solution.y <= 1.0001){
  61.                     return solution.z;
  62.                 }
  63.             }
  64.         }
  65.         return FAR_CLIP;
  66.     }
  67. }
  68.  
  69.  
  70. Ray initRay(uint x, uint y, Camera cam){
  71.     Ray r;
  72.     vec3 dir;
  73.     float a, b, halfWidth, halfHeight;
  74.        
  75.     halfWidth = float(width)/2.0f;
  76.     halfHeight = float(height)/2.0f;
  77.  
  78.     a = cam.tanFovX*( (float(x)-halfWidth+0.5f) / halfWidth);
  79.     b = cam.tanFovY*( (halfHeight - float(y)-0.5f) / halfHeight);
  80.  
  81.     dir = normalize( a*cam.xAxis.xyz + b*cam.yAxis.xyz + cam.dir.xyz);
  82.        
  83.     r.dir = dir;
  84.     r.origin = cam.pos.xyz;
  85.    
  86.     return r;
  87. }
  88.  
  89. layout (local_size_x = 25, local_size_y = 8) in;
  90. void main(void) {
  91.     uint x = gl_GlobalInvocationID.x;
  92.     uint y = gl_GlobalInvocationID.y;
  93.  
  94.     //Hardcoded light values
  95.     light.dir = vec4(0, -1, 0, 0);
  96.     light.color = vec4(1, 1, 1, 1);
  97.     light.attenuation = vec4(1.0f);
  98.  
  99.  
  100.     if(x < width && y < height)
  101.     {
  102.  
  103.         //Color used to find out the final color.
  104.         vec4 color = vec4(0.0f, 0.0f, 0.0f, 1.0f);
  105.         vec4 tempColor = vec4(0);
  106.  
  107.         //Initialize the pixel/camera ray.
  108.         Ray r = initRay(x, y, camera);
  109.  
  110.         float t = FAR_CLIP, temp = FAR_CLIP;
  111.         int currentObject = -1;
  112.         for(int i = 0; i < mNumTriangles; i++)
  113.         {
  114.                
  115.             Primitive t0;
  116.             t0.A = vec4(tris[i].vA, 1);
  117.             t0.B =  vec4(tris[i].vB, 1);
  118.             t0.C =  vec4(tris[i].vC, 1);
  119.             //Basically if it finds something, it'll return something thats not FAR_CLIP
  120.             temp = hitTriangle(r, t0);
  121.                        
  122.             //If the ray hit something, then we need to shade it. Just  give it a color for now.
  123.             if(temp != t)
  124.             {
  125.                 color = vec4(0, 1, 0,1);
  126.                 t = temp;
  127.                 currentObject = i;
  128.             }
  129.         }
  130.  
  131.        
  132.  
  133.         imageStore(framebuffer, ivec2(x, y), color);
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement