Float Turbulence(const Point3f &p, const Vector3f &dpdx, const Vector3f &dpdy, Float omega, int maxOctaves) { // Compute number of octaves for antialiased FBm Float len2 = std::max(dpdx.LengthSquared(), dpdy.LengthSquared()); Float foctaves = Clamp(-1.f - .5f * Log2(len2), 0, maxOctaves); int octaves = std::floor(foctaves); // Compute sum of octaves of noise for turbulence Float sum = 0.f, lambda = 1.f, o = 1.f; for (int i = 0; i < octaves; ++i) { sum += o * std::abs(Noise(lambda * p)); lambda *= 1.99f; o *= omega; } // Account for contributions of clamped octaves in turbulence Float partialOctave = foctaves - octaves; sum += o * Lerp(SmoothStep(.3f, .7f, partialOctave), 0.2, std::abs(Noise(lambda * p))); for (int i = octaves; i < maxOctaves; ++i) { sum += o * 0.2f; o *= omega; } return sum; }
Float FBm(const Point3f &p, const Vector3f &dpdx, const Vector3f &dpdy, Float omega, int maxOctaves) { // Compute number of octaves for antialiased FBm Float len2 = std::max(dpdx.LengthSquared(), dpdy.LengthSquared()); Float foctaves = Clamp(-1.f - .5f * Log2(len2), 0, maxOctaves); int octaves = std::floor(foctaves); // Compute sum of octaves of noise for FBm Float sum = 0.f, lambda = 1.f, o = 1.f; for (int i = 0; i < octaves; ++i) { sum += o * Noise(lambda * p); lambda *= 1.99f; o *= omega; } Float partialOctave = foctaves - octaves; sum += o * SmoothStep(.3f, .7f, partialOctave) * Noise(lambda * p); return sum; }
Float FBm(const Point3f &p, const Vector3f &dpdx, const Vector3f &dpdy, Float omega, int maxOctaves) { // Compute number of octaves for antialiased FBm Float len2 = std::max(dpdx.LengthSquared(), dpdy.LengthSquared()); Float n = Clamp(-1 - .5f * Log2(len2), 0, maxOctaves); int nInt = std::floor(n); // Compute sum of octaves of noise for FBm Float sum = 0, lambda = 1, o = 1; for (int i = 0; i < nInt; ++i) { sum += o * Noise(lambda * p); lambda *= 1.99f; o *= omega; } Float nPartial = n - nInt; sum += o * SmoothStep(.3f, .7f, nPartial) * Noise(lambda * p); return sum; }
Float ConvertDensity(const Vertex &cur, Float pdf, const Vertex &next) { // Return solid angle density if _next_ is an infinite area light if (next.IsInfiniteLight()) return pdf; Vector3f d = next.GetPosition() - cur.GetPosition(); Float invL2 = 1.f / d.LengthSquared(); if (next.IsOnSurface()) pdf *= AbsDot(next.GetGeoNormal(), d * std::sqrt(invL2)); return pdf * invL2; }
Spectrum G(const Scene &scene, Sampler &sampler, const Vertex &v0, const Vertex &v1) { Vector3f d = v0.p() - v1.p(); Float g = 1 / d.LengthSquared(); d *= std::sqrt(g); if (v0.IsOnSurface()) g *= AbsDot(v0.ns(), d); if (v1.IsOnSurface()) g *= AbsDot(v1.ns(), d); VisibilityTester vis(v0.GetInteraction(), v1.GetInteraction()); return g * vis.Tr(scene, sampler); }
Spectrum GeometryTerm(const Scene &scene, Sampler &sampler, const Vertex &v0, const Vertex &v1) { Vector3f d = v0.GetPosition() - v1.GetPosition(); Float G = 1.f / d.LengthSquared(); d *= std::sqrt(G); if (v0.IsOnSurface()) G *= Dot(v0.GetShadingNormal(), d); if (v1.IsOnSurface()) G *= Dot(v1.GetShadingNormal(), d); VisibilityTester vis(v0.GetInteraction(), v1.GetInteraction()); return std::abs(G) * vis.T(scene, sampler); }
void Movement::MoveToLocation() { Vector3f pos; bool isPosition = false; /// Check if we reached our destination? switch(location) { case Location::VECTOR: { // Adjust movement vector? pos = levelEntity->worldPosition + vec; isPosition = true; break; } case Location::LEFT_EDGE: { if (state == 0) { SetDirection(Vector3f(-1,0,0)); ++state; } else if (shipEntity->worldPosition.x < leftEdge && state == 1) { SetDirection(Vector2f()); ++state; } break; } case Location::RIGHT_EDGE: if (state == 0) { SetDirection(Vector3f(1,0,0)); ++state; } else if (shipEntity->worldPosition.x > rightEdge && state == 1) { SetDirection(Vector2f()); ++state; } break; case Location::UPPER_EDGE: { if (state == 0) { SetDirection(Vector3f(0,1,0)); ++state; } else if (shipEntity->worldPosition[1] > 20.f && state == 1) { SetDirection(Vector2f()); ++state; } break; } case Location::LOWER_EDGE: { if (state == 0) { SetDirection(Vector3f(0,-1,0)); ++state; } if (shipEntity->worldPosition[1] < 0.f && state == 1) { SetDirection(Vector2f()); ++state; } break; } case Location::CENTER: pos = levelEntity->worldPosition; isPosition = true; break; case Location::PLAYER: if (playerShip->entity) { pos = playerShip->entity->worldPosition; isPosition = true; } else { SetDirection(Vector3f()); return; } break; default: assert(false && "Movement unidentified"); } if (isPosition) { Vector3f toPos = pos - shipEntity->worldPosition; if (toPos.LengthSquared() > 1) toPos.Normalize(); SetDirection(toPos); } }
bool Triangle::Intersect(const Ray &ray, Float *tHit, SurfaceInteraction *isect, bool testAlphaTexture) const { ProfilePhase p(Prof::TriIntersect); ++nTests; // Get triangle vertices in _p0_, _p1_, and _p2_ const Point3f &p0 = mesh->p[v[0]]; const Point3f &p1 = mesh->p[v[1]]; const Point3f &p2 = mesh->p[v[2]]; // Perform ray--triangle intersection test // Transform triangle vertices to ray coordinate space // Translate vertices based on ray origin Point3f p0t = p0 - Vector3f(ray.o); Point3f p1t = p1 - Vector3f(ray.o); Point3f p2t = p2 - Vector3f(ray.o); // Permute components of triangle vertices and ray direction int kz = MaxDimension(Abs(ray.d)); int kx = kz + 1; if (kx == 3) kx = 0; int ky = kx + 1; if (ky == 3) ky = 0; Vector3f d = Permute(ray.d, kx, ky, kz); p0t = Permute(p0t, kx, ky, kz); p1t = Permute(p1t, kx, ky, kz); p2t = Permute(p2t, kx, ky, kz); // Apply shear transformation to translated vertex positions Float Sx = -d.x / d.z; Float Sy = -d.y / d.z; Float Sz = 1.f / d.z; p0t.x += Sx * p0t.z; p0t.y += Sy * p0t.z; p1t.x += Sx * p1t.z; p1t.y += Sy * p1t.z; p2t.x += Sx * p2t.z; p2t.y += Sy * p2t.z; // Compute edge function coefficients _e0_, _e1_, and _e2_ Float e0 = p1t.x * p2t.y - p1t.y * p2t.x; Float e1 = p2t.x * p0t.y - p2t.y * p0t.x; Float e2 = p0t.x * p1t.y - p0t.y * p1t.x; // Fall back to double precision test at triangle edges if (sizeof(Float) == sizeof(float) && (e0 == 0.0f || e1 == 0.0f || e2 == 0.0f)) { double p2txp1ty = (double)p2t.x * (double)p1t.y; double p2typ1tx = (double)p2t.y * (double)p1t.x; e0 = (float)(p2typ1tx - p2txp1ty); double p0txp2ty = (double)p0t.x * (double)p2t.y; double p0typ2tx = (double)p0t.y * (double)p2t.x; e1 = (float)(p0typ2tx - p0txp2ty); double p1txp0ty = (double)p1t.x * (double)p0t.y; double p1typ0tx = (double)p1t.y * (double)p0t.x; e2 = (float)(p1typ0tx - p1txp0ty); } // Perform triangle edge and determinant tests if ((e0 < 0 || e1 < 0 || e2 < 0) && (e0 > 0 || e1 > 0 || e2 > 0)) return false; Float det = e0 + e1 + e2; if (det == 0) return false; // Compute scaled hit distance to triangle and test against ray $t$ range p0t.z *= Sz; p1t.z *= Sz; p2t.z *= Sz; Float tScaled = e0 * p0t.z + e1 * p1t.z + e2 * p2t.z; if (det < 0 && (tScaled >= 0 || tScaled < ray.tMax * det)) return false; else if (det > 0 && (tScaled <= 0 || tScaled > ray.tMax * det)) return false; // Compute barycentric coordinates and $t$ value for triangle intersection Float invDet = 1 / det; Float b0 = e0 * invDet; Float b1 = e1 * invDet; Float b2 = e2 * invDet; Float t = tScaled * invDet; // Ensure that computed triangle $t$ is conservatively greater than zero // Compute $\delta_z$ term for triangle $t$ error bounds Float maxZt = MaxComponent(Abs(Vector3f(p0t.z, p1t.z, p2t.z))); Float deltaZ = gamma(3) * maxZt; // Compute $\delta_x$ and $\delta_y$ terms for triangle $t$ error bounds Float maxXt = MaxComponent(Abs(Vector3f(p0t.x, p1t.x, p2t.x))); Float maxYt = MaxComponent(Abs(Vector3f(p0t.y, p1t.y, p2t.y))); Float deltaX = gamma(5) * (maxXt + maxZt); Float deltaY = gamma(5) * (maxYt + maxZt); // Compute $\delta_e$ term for triangle $t$ error bounds Float deltaE = 2 * (gamma(2) * maxXt * maxYt + deltaY * maxXt + deltaX * maxYt); // Compute $\delta_t$ term for triangle $t$ error bounds and check _t_ Float maxE = MaxComponent(Abs(Vector3f(e0, e1, e2))); Float deltaT = 3 * (gamma(3) * maxE * maxZt + deltaE * maxZt + deltaZ * maxE) * std::abs(invDet); if (t <= deltaT) return false; // Compute triangle partial derivatives Vector3f dpdu, dpdv; Point2f uv[3]; GetUVs(uv); // Compute deltas for triangle partial derivatives Vector2f duv02 = uv[0] - uv[2], duv12 = uv[1] - uv[2]; Vector3f dp02 = p0 - p2, dp12 = p1 - p2; Float determinant = duv02[0] * duv12[1] - duv02[1] * duv12[0]; if (determinant == 0) { // Handle zero determinant for triangle partial derivative matrix CoordinateSystem(Normalize(Cross(p2 - p0, p1 - p0)), &dpdu, &dpdv); } else { Float invdet = 1 / determinant; dpdu = (duv12[1] * dp02 - duv02[1] * dp12) * invdet; dpdv = (-duv12[0] * dp02 + duv02[0] * dp12) * invdet; } // Compute error bounds for triangle intersection Float xAbsSum = (std::abs(b0 * p0.x) + std::abs(b1 * p1.x) + std::abs(b2 * p2.x)); Float yAbsSum = (std::abs(b0 * p0.y) + std::abs(b1 * p1.y) + std::abs(b2 * p2.y)); Float zAbsSum = (std::abs(b0 * p0.z) + std::abs(b1 * p1.z) + std::abs(b2 * p2.z)); Vector3f pError = gamma(7) * Vector3f(xAbsSum, yAbsSum, zAbsSum); // Interpolate $(u,v)$ parametric coordinates and hit point Point3f pHit = b0 * p0 + b1 * p1 + b2 * p2; Point2f uvHit = b0 * uv[0] + b1 * uv[1] + b2 * uv[2]; // Test intersection against alpha texture, if present if (testAlphaTexture && mesh->alphaMask) { SurfaceInteraction isectLocal(pHit, Vector3f(0, 0, 0), uvHit, -ray.d, dpdu, dpdv, Normal3f(0, 0, 0), Normal3f(0, 0, 0), ray.time, this); if (mesh->alphaMask->Evaluate(isectLocal) == 0) return false; } // Fill in _SurfaceInteraction_ from triangle hit *isect = SurfaceInteraction(pHit, pError, uvHit, -ray.d, dpdu, dpdv, Normal3f(0, 0, 0), Normal3f(0, 0, 0), ray.time, this); // Override surface normal in _isect_ for triangle isect->n = isect->shading.n = Normal3f(Normalize(Cross(dp02, dp12))); if (mesh->n || mesh->s) { // Initialize _Triangle_ shading geometry // Compute shading normal _ns_ for triangle Normal3f ns; if (mesh->n) { ns = (b0 * mesh->n[v[0]] + b1 * mesh->n[v[1]] + b2 * mesh->n[v[2]]); if (ns.LengthSquared() > 0) ns = Normalize(ns); else ns = isect->n; } else ns = isect->n; // Compute shading tangent _ss_ for triangle Vector3f ss; if (mesh->s) { ss = (b0 * mesh->s[v[0]] + b1 * mesh->s[v[1]] + b2 * mesh->s[v[2]]); if (ss.LengthSquared() > 0) ss = Normalize(ss); else ss = Normalize(isect->dpdu); } else ss = Normalize(isect->dpdu); // Compute shading bitangent _ts_ for triangle and adjust _ss_ Vector3f ts = Cross(ss, ns); if (ts.LengthSquared() > 0.f) { ts = Normalize(ts); ss = Cross(ts, ns); } else CoordinateSystem((Vector3f)ns, &ss, &ts); // Compute $\dndu$ and $\dndv$ for triangle shading geometry Normal3f dndu, dndv; if (mesh->n) { // Compute deltas for triangle partial derivatives of normal Vector2f duv02 = uv[0] - uv[2]; Vector2f duv12 = uv[1] - uv[2]; Normal3f dn1 = mesh->n[v[0]] - mesh->n[v[2]]; Normal3f dn2 = mesh->n[v[1]] - mesh->n[v[2]]; Float determinant = duv02[0] * duv12[1] - duv02[1] * duv12[0]; if (determinant == 0) dndu = dndv = Normal3f(0, 0, 0); else { Float invDet = 1 / determinant; dndu = (duv12[1] * dn1 - duv02[1] * dn2) * invDet; dndv = (-duv12[0] * dn1 + duv02[0] * dn2) * invDet; } } else dndu = dndv = Normal3f(0, 0, 0); isect->SetShadingGeometry(ss, ts, dndu, dndv, true); } // Ensure correct orientation of the geometric normal if (mesh->n) isect->n = Faceforward(isect->n, isect->shading.n); else if (reverseOrientation ^ transformSwapsHandedness) isect->n = isect->shading.n = -isect->n; *tHit = t; ++nHits; return true; }