Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. #version 430 core
  2. #extension GL_ARB_compute_shader : enable
  3. #extension GL_ARB_shader_image_load_store : enable
  4.  
  5. // output of compute shader is a TEXTURE
  6. layout(rgba32f, binding = 0) uniform image2D outTexture;
  7.  
  8.  
  9. // receive as inputs, rays from the centre of the camera to the frustum corners
  10. layout( location = 0 ) uniform vec4 ray00; // top left in world space
  11. layout( location = 1 ) uniform vec4 ray01; // top right in world space
  12. layout( location = 2 ) uniform vec4 ray10; // bottom left in world space
  13. layout( location = 3 ) uniform vec4 ray11; // bottom right in world space
  14.  
  15. // receive the camera position
  16. layout( location = 4 ) uniform vec3 camPos; // camera position
  17.  
  18. // receive how many primitives of each type
  19. // geomCount.x = number of spheres
  20. // geomCount.y = number of triangles
  21. // geomCount.z = number of planes
  22. // geomCount.w = number of obbs
  23. layout( location = 5 ) uniform ivec4 geomCount; // sphere, triangle, planes, obbs
  24.  
  25. // how many lights to process (how many elements in the array "light")
  26. layout( location = 6 ) uniform int lightCount;
  27.  
  28. // receive an ARRAY of light_type,
  29. struct light_type {
  30. vec4 position;
  31. vec4 colour;
  32. };
  33. layout(std430, binding = 2) buffer light
  34. {
  35. light_type lights_data[];
  36. };
  37.  
  38. struct sphere_type {
  39. vec4 centre_radius;
  40. vec4 colour;
  41. };
  42. // 8 floats per sphere: cx cy cz rad r g b a
  43. layout(std430, binding = 3) buffer sphere
  44. {
  45. sphere_type spheres_data[];
  46. };
  47.  
  48. struct plane_type {
  49. vec4 normal_d;
  50. vec4 colour;
  51. };
  52. // 8 floats per plane: nx ny nz d r g b a
  53. layout(std430, binding = 4) buffer plane
  54. {
  55. plane_type planes_data[];
  56. };
  57.  
  58. struct triangle_type {
  59. vec4 vtx0, vtx1, vtx2;
  60. vec4 colour;
  61. };
  62. layout(std430, binding = 5) buffer triangle
  63. {
  64. triangle_type triangles_data[];
  65. };
  66.  
  67. // 20 floats per obb: centre4, u3, hu, v3, hv, w3, hw, rgba4
  68. struct obb_type {
  69. vec4 centre, u_hu, v_hv, w_hw, colour;
  70. };
  71. layout(std430, binding = 6) buffer obb
  72. {
  73. obb_type obb_data[];
  74. };
  75.  
  76. layout (local_size_x = 20, local_size_y = 20) in;
  77.  
  78. #define SPHERE_TYPE 0
  79. #define LIGHT_TYPE 1
  80. #define PLANE_TYPE 2
  81. #define TRIANGLE_TYPE 3
  82. #define OBB_TYPE 4
  83.  
  84. // primitives tests forward declarations
  85. float sphereTest(vec3 rayDir, vec3 rayOrigin, vec4 centre_radius);
  86. float planeTest(vec3 rayDir, vec3 rayOrigin, vec4 plane_info);
  87. float triangleTest(vec3 rayDir, vec3 rayOrigin, triangle_type tri);
  88. float obbTest(vec3 rayDir, vec3 rayOrigin, obb_type obb);
  89.  
  90. // entire scene test forward declaration
  91. void sceneTest(in vec4 ray_dir, inout float lastT, inout int objIndex, inout int objType);
  92.  
  93. // shade a point of a surface (for any primitive) forward declaration
  94. vec4 shade(in vec3 pointOnSurface, in vec3 normal, in vec3 colour);
  95.  
  96. vec4 castRay(ivec2 pos)
  97. {
  98. // normalise values from [ 0,width; 0,height ] to [ 0,1; 0,1]
  99. vec2 interp = vec2(pos) / imageSize(outTexture);
  100.  
  101. // compute ray as interpolation of input rays (corners)
  102. vec4 ray_dir = normalize(
  103. mix(
  104. mix(ray00,ray01,interp.y), // left corners together
  105. mix(ray10,ray11,interp.y), // right corners together
  106. interp.x // join new interpolated rays into a final ray
  107. )
  108. );
  109.  
  110. // lastT is the last intersection found (will keep the closest value)
  111. float lastT=-1;
  112. // will remember which object index was hit if any.
  113. int objIndex = -1;
  114. // will remember which object type was hit if any.
  115. int objType = -1;
  116.  
  117. // do ray versus scene test
  118. sceneTest(ray_dir, lastT, objIndex, objType);
  119.  
  120. // set pixel to BACKGROUND colour
  121. vec4 finalPixelOut = vec4(0.2,0.,0.2, 1.0);
  122.  
  123. if (objIndex >= 0) {
  124. // did we hit something?
  125.  
  126. // IMPLEMENT HERE.
  127. // FIND POINT IN SPACE WHERE THE INTERSECTION HAPPENED.
  128. vec3 pointOnSurface = ray_dir.xyz * lastT + camPos;
  129.  
  130. if (objType==SPHERE_TYPE)
  131. {
  132. // IMPLEMENT HERE
  133. // COMPUTE FINAL PIXEL COLOUR USING SHADE() FUNCTION
  134. vec3 n = normalize(pointOnSurface - spheres_data[objIndex].centre_radius.xyz);
  135. finalPixelOut = shade (pointOnSurface, n, spheres_data[objIndex].colour.xyz);
  136. }
  137. else if (objType==LIGHT_TYPE)
  138. {
  139. // IMPLEMENT HERE.
  140. // LIGHTS ARE DRAWN AS SPHERES, JUST GIVE A CONSTANT COLOUR TO IT.
  141. // DO NOT SHADE WITH SHADE() FUNCTION.
  142. finalPixelOut = vec4(1.0, 1.0, 1.0, 1.0);
  143. }
  144. else if (objType == PLANE_TYPE)
  145. {
  146. // IMPLEMENT HERE
  147. // COMPUTE FINAL PIXEL COLOUR USING SHADE() FUNCTION
  148. finalPixelOut=shade(pointOnSurface, planes_data[objIndex].normal_d.xyz, planes_data[objIndex].colour.xyz);
  149. }
  150. else if (objType == TRIANGLE_TYPE)
  151. {
  152. // IMPLEMENT HERE
  153. // COMPUTE FINAL PIXEL COLOUR USING SHADE() FUNCTION
  154. vec3 u = triangles_data[objIndex].vtx1.xyz - triangles_data[objIndex].vtx0.xyz, v = triangles_data[objIndex].vtx2.xyz - triangles_data[objIndex].vtx0.xyz;
  155. vec3 n = cross(u , v);
  156. finalPixelOut = shade(pointOnSurface, n, triangles_data[objIndex].colour.xyz);
  157.  
  158. }
  159. else if (objType == OBB_TYPE)
  160. {
  161. // IMPLEMENT HERE
  162. // COMPUTE FINAL PIXEL COLOUR USING SHADE() FUNCTION
  163. float epsilon = 0.0001;
  164. obb_type obb = obb_data[objIndex];
  165. vec3 nor;
  166. bool found = false;
  167.  
  168. vec4 vecArr[3];
  169. vecArr[0] = obb.u_hu;
  170. vecArr[1] = obb.v_hv;
  171. vecArr[2] = obb.w_hw;
  172.  
  173. for(int i = 0; i < 3 && found == false; i++)
  174. {
  175. vec3 n = normalize(vecArr[i].xyz);
  176. vec3 centreToFace = n * vecArr[i].w;
  177. vec3 pointOnFace = obb.centre.xyz + centreToFace;
  178. if(abs(dot(pointOnSurface - centreToFace, vecArr[i].xyz)) < epsilon){
  179. found = true;
  180. nor = n;
  181. }
  182. }
  183. finalPixelOut = shade(pointOnSurface, nor, obb.colour.xyz);
  184. }
  185. }
  186. return finalPixelOut;
  187. }
  188.  
  189. layout (local_size_x = 20, local_size_y = 20) in;
  190. void main()
  191. {
  192. // pixel coordinate, x in [0,width-1], y in [0,height-1]
  193. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  194. vec4 finalPixel = castRay(pos);
  195. imageStore(outTexture, pos, finalPixel);
  196. return;
  197. };
  198.  
  199. void sceneTest(in vec4 ray_dir, inout float lastT, inout int objIndex, inout int objType)
  200. {
  201.  
  202. // use camPos to know where is the ray origin.
  203. // example:
  204. vec3 rayOrigin = camPos;
  205. float t;
  206. // test all spheres (in geomCount.x)
  207. for (int i=0;i< geomCount.x;i++)
  208. {
  209. sphere_type sphere = spheres_data[i];
  210. // IMPLEMENT HERE.
  211. // TEST SPHERE, UPDATE lastT, objIndex and objType if necessary.
  212. t = sphereTest(ray_dir.xyz, rayOrigin, sphere.centre_radius);
  213. if (t != -1 ){
  214. if(t < lastT || lastT == -1){
  215. lastT = t;
  216. objType = SPHERE_TYPE;
  217. objIndex = i;
  218. }
  219. }
  220. }
  221. // lights (we render lights as spheres)
  222. for (int i=0;i<lightCount;i++)
  223. {
  224. light_type light = lights_data[i];
  225.  
  226. t = sphereTest(ray_dir.xyz, rayOrigin, light.position);
  227. if(t != -1 ){
  228. if(t < lastT || lastT == -1){
  229. lastT = t;
  230. objType = LIGHT_TYPE;
  231. objIndex = i;
  232. }
  233. }
  234. // IMPLEMENT HERE.
  235. // TEST SPHERE (LIGHT), UPDATE lastT, objIndex and objType if necessary.
  236. }
  237. // planes
  238. for (int i = 0; i<geomCount.y; i++)
  239. {
  240. plane_type plane = planes_data[i];
  241. // IMPLEMENT HERE.
  242. // TEST PLANE, UPDATE lastT, objIndex and objType if necessary.
  243. t = planeTest(ray_dir.xyz, rayOrigin, plane.normal_d);
  244. if( t != -1){
  245. if(t < lastT || lastT == -1){
  246. lastT = t;
  247. objType = PLANE_TYPE;
  248. objIndex = i;
  249. }
  250. }
  251.  
  252. }
  253. // triangles
  254. for (int i = 0; i<geomCount.z; i++)
  255. {
  256. triangle_type triangle = triangles_data[i];
  257. // IMPLEMENT HERE.
  258. // TEST TRIANGLE, UPDATE lastT, objIndex and objType if necessary.
  259. t = triangleTest(ray_dir.xyz, rayOrigin, triangle);
  260. if(t != -1){
  261. if(t < lastT || lastT == -1){
  262. lastT = t;
  263. objType = TRIANGLE_TYPE;
  264. objIndex = i;
  265. }
  266. }
  267. }
  268. // OBBss
  269. for (int i = 0; i < geomCount.w; i++)
  270. {
  271. obb_type obb = obb_data[i];
  272. // IMPLEMENT HERE.
  273. // TEST OBB, UPDATE lastT, objIndex and objType if necessary.
  274. t = obbTest(ray_dir.xyz, rayOrigin, obb);
  275. if(t != -1){
  276. if(t < lastT || lastT == -1){
  277. lastT = t;
  278. objType = OBB_TYPE;
  279. objIndex = i;
  280. }
  281. }
  282. }
  283. }
  284.  
  285. float sphereTest(in vec3 rayDir, in vec3 rayOrigin, in vec4 centre_radius)
  286. {
  287. // IMPLEMENT HERE
  288. vec3 oc = rayOrigin - centre_radius.xyz;
  289. float p = dot(rayDir, oc);
  290. float q = dot(oc,oc)-(centre_radius.w * centre_radius.w);
  291. if(p*p - q < 0)
  292. return -1.0f;
  293. else{
  294. float t = min(-p + sqrt(p*p - q), -p - sqrt(p*p - q));
  295. if(t > 0)
  296. return min(-p + sqrt(p*p - q), -p - sqrt(p*p - q));
  297. }
  298. }
  299.  
  300. float planeTest(vec3 rayDir, vec3 rayOrigin, vec4 plane_info)
  301. {
  302. // IMPLEMENT HERE
  303. vec3 oc = rayOrigin - plane_info.xyz;
  304. if(dot(plane_info.xyz, rayDir) == 0)
  305. return -1;
  306. float t = (-plane_info.w - dot(plane_info.xyz, rayOrigin))/(dot(plane_info.xyz, rayDir));
  307. if (t>=0)
  308. return t;
  309.  
  310. return -1;
  311. }
  312.  
  313. float triangleTest(vec3 rayDir, vec3 rayOrigin, triangle_type tri)
  314. {
  315. vec3 v1,v2, s, tuv, temp;
  316. float epsilon = 0.0001;
  317. v1 = tri.vtx1.xyz - tri.vtx0.xyz;
  318. v2 = tri.vtx2.xyz - tri.vtx0.xyz;
  319. s = rayOrigin - tri.vtx0.xyz;
  320. vec3 q = cross(rayDir, v2);
  321. float a = dot(v1, q);
  322. float tReturn = -1;
  323. temp.x = determinant(mat3(s,v1,v2));
  324. temp.y = determinant(mat3(-rayDir,s,v2));
  325. temp.z = determinant(mat3(-rayDir,v1,s));
  326.  
  327. if(determinant(mat3(-rayDir,v1,v2)) > 0.0f){
  328. tuv = (1 / determinant(mat3(-rayDir,v1,v2)))*temp;
  329.  
  330. if(tuv.x >= 0.0){
  331. tReturn = tuv.x;
  332. }
  333.  
  334. if((a > -epsilon && a < epsilon) || (tuv.y < 0.0)|| ((tuv.z < 0.0) || (tuv.y + tuv.z > 1.0)))
  335. {
  336. tReturn = -1;
  337. }
  338. }
  339. if(tReturn >= 0){
  340. return tReturn;
  341. }
  342. else{
  343.  
  344. return -1;
  345. }
  346. }
  347.  
  348. float obbTest(vec3 rayDir, vec3 rayOrigin, obb_type o)
  349. {
  350. float epsilon = 0.0000001;
  351. float t_min= -1000000,
  352. t_max = 1000000 ;
  353. vec3 p = o.centre.xyz - rayOrigin;
  354.  
  355. float t,e,f,w,t1,t2;
  356. vec4 obbArr[3];
  357. obbArr[0] = o.u_hu;
  358. obbArr[1] = o.v_hv;
  359. obbArr[2] = o.w_hw;
  360.  
  361. for(int i = 0; i < 3; i++)
  362. {
  363. e = dot(obbArr[i].xyz, p);
  364. f = dot(obbArr[i].xyz, rayDir);
  365. if(abs(f) > epsilon)
  366. {
  367. w = obbArr[i].w;
  368. float t1 = (e+w)/f;
  369. float t2 = (e-w)/f;
  370. if(t1>t2){
  371. t=t1;
  372. t1=t2;
  373. t2=t;
  374. }
  375. if(t1 > t_min)
  376. t_min = t1;
  377.  
  378. if(t2 < t_max)
  379. t_max = t2;
  380.  
  381. if(t_min > t_max)
  382. return -1;
  383.  
  384. if(t_max < 0)
  385. return -1;
  386. }
  387. else if(-e-obbArr[i].w > 0.0 || -e+obbArr[i].w < 0.0)
  388. return -1;
  389. }
  390. if(t_min > 0.0)
  391. return t_min;
  392.  
  393. else
  394. return t_max;
  395.  
  396. };
  397.  
  398. // everythin in World Space
  399. vec4 shade(in vec3 pointOnSurface, in vec3 normal, in vec3 colour)
  400. {
  401. // ambient (combine with material color!)
  402. // 0.2 is arbitrary, that is the value used in the model solution.
  403. // ambient (combine with material color!)
  404. // 0.2 is arbitrary, that is the value used in the model solution.
  405. vec4 final_colour = vec4(0.2,0.2,0.2,1) * vec4(colour,1.0); //* vec4(colour,1);
  406. // diffuse, no attenuation.
  407. for (int i = 0; i < lightCount; i++)
  408. {
  409. vec4 light_pos = lights_data[i].position;
  410. vec4 light_colour = lights_data[i].colour;
  411. vec3 lightVec = normalize(light_pos.xyz - pointOnSurface);
  412. float diffuseFactor = dot(normalize(normal),lightVec);
  413. float lenght = distance(pointOnSurface, light_pos.xyz);
  414. vec3 colorObject = diffuseFactor*(light_colour.xyz*colour)*light_pos.w/lenght; //* (1.0/ abs(light_pos.xyz - pointOnSurface));
  415. final_colour += vec4(colorObject,1.0);
  416.  
  417. }
  418. final_colour = min( final_colour, vec4(1.0,1.0,1.0,1.0));
  419.  
  420. // UPDATE THIS LINE TO ACCOUNT FOR SATURATION (PIXEL COLOUR CANNOT GO OVER 1.0)
  421. return final_colour;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement