Jump to content

Caustics - not able to accumulate light contribution because of Parallelism?

Recommended Posts

Hi, I try to accumulate refracted light through a water surface on the ground with a detail vex wrangle.

My code so far is this, with the ground being input 0 and the water surface being input 1:

vector sunDir = {0, -1, 0};
float ior = 1.33;
int numpt1 = npoints(1);
vector hitPoint;
float u, v;

for(int i = 0; i<numpt1; i++)
  vector Psurf = point(1,"P", i);
  vector Nsurf = normalize(point(1,"N", i));
  vector refractRay = refract(sunDir, Nsurf, ior);
  int hitPrim = intersect(0, Psurf, refractRay, hitPoint, u, v);
  int nearPoint = nearpoint(0, hitPoint);
  vector cdTemp = pointattrib(0, "Cd", nearPoint, 0);
  cdTemp += 0.1;
  setpointattrib(0, "Cd", nearPoint, cdTemp, "set");

While I do get the caustic pattern on the ground, it doesn't seem to accumulate the light as I intend to do with the last 3 lines.

Instead of adding 0.1 to the current Cd value each time a point gets hit in a loop step, it only ever outputs 0 or 0.1 as final color value.

I suspect this being due to some parallel execution when I read from/write to points on the second input.

I also tried a foreach loop as well as collecting the hit points in a detail array and iterate over that, with the same result.

When I did a nested loop to run over every point on the second input for each hit point it does yield the desired result, but becomes really slow at a grid resolution of about 100x100. Which is understandable as it runs 10.000 times 10.000  = 100 Million times according to my understanding.


Would anyone know how I can set this up so I can accumulate the color values efficiently on the hit points of the ground surface?




Share this post

Link to post
Share on other sites

Thanks a lot Yunus!

I guess you take a much more elegant approach and shuffled the inputs around a bit compared to mine.

Your technique seems to be proven and part of your course, so I'll give it a try understanding, thanks a lot again.

Btw, I am am intrigued of how you embedded the video, as the first image before starting shows the code, yet that image is nowhere to be found in the video :) 

Share this post

Link to post
Share on other sites

Oh yes I think when I made the video I realized I didn't show the code, but thought it would be good to show it and so made the thumbnail from a screenshot outside the video :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now