Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Last active March 1, 2019 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwarren/1580572d9d641147c61caf65c383c3a4 to your computer and use it in GitHub Desktop.
Save mattwarren/1580572d9d641147c61caf65c383c3a4 to your computer and use it in GitHub Desktop.
// This is just the outline/summary, full source code here http://fabiensanglard.net/postcard_pathtracer/formatted_full.html
void main() {
int w = 960, h = 540, samplesCount = 2;
Vec position(-22, 5, 25);
Vec goal = !(Vec(-3, 4, 0) + position * -1);
Vec left = !Vec(goal.z, 0, -goal.x) * (1. / w);
// Cross-product to get the up vector
Vec up(goal.y * left.z - goal.z * left.y,
goal.z * left.x - goal.x * left.z,
goal.x * left.y - goal.y * left.x);
for (int y = h; y--;) {
for (int x = w; x--;) {
Vec color;
for (int p = samplesCount; p--;) {
Vec direction = !(goal + left * (x - w / 2 + randomVal()) + up * (y - h / 2 + randomVal()));
color = color + Trace(position, direction);
// Reinhard tone mapping
...
}
}
}
}
Vec Trace(Vec origin, Vec direction) {
...
for (int bounceCount = 3; bounceCount--;) {
int hitType = RayMarching(origin, direction, sampledPosition, normal);
...
}
}
int RayMarching(Vec origin, Vec direction, Vec &hitPos, Vec &hitNorm) {
...
// Signed distance marching
for (float total_d=0; total_d < 100; total_d += d)
if ((d = QueryDatabase(hitPos = origin + direction * total_d, hitType)) < .01
|| ++noHitCount > 99)
...
}
float QueryDatabase(Vec position, int &hitType) {
..
// Contains several calls to BoxTest(..)
// Also calls min, sqrtf, fabsf, powf
// and some of the Vec operators (%, +, etc)
}
float BoxTest(Vec position, Vec lowerLeft, Vec upperRight) {
// Contains several calls to min(..)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment