Vector2 Vector2::random(G3D::Random& r) { Vector2 result; do { result = Vector2(r.uniform(-1, 1), r.uniform(-1, 1)); } while (result.squaredLength() >= 1.0f); result.unitize(); return result; }
void App::trace(int x, int y) { Color3 sum = Color3::black(); if (m_currentRays == 1) { sum = rayTrace(m_debugCamera->worldRay(x + 0.5f, y + 0.5f, m_currentImage->rect2DBounds()), m_world); } // Don't calculate blur if either aperture or focal length are 0 else if(m_aperture == 0 || m_focalLength == 0) { for (int i = 0; i < m_currentRays; ++i) { sum += rayTrace(m_debugCamera->worldRay(x + rnd.uniform(), y + rnd.uniform(), m_currentImage->rect2DBounds()), m_world); } } else { for (int i = 0; i < m_currentRays; ++i) { // Save the original ray Ray oldRay = m_debugCamera->worldRay(x, y, m_currentImage->rect2DBounds()); // Calculate the origin of the new ray by multiplying the x and y values by a fraction of the aperture (keep z the same) Point3 newOrigin = Point3(oldRay.origin().x + (rnd.uniform() * m_aperture/20.0f), oldRay.origin().y + (rnd.uniform() * m_aperture/20.0f), oldRay.origin().z); // Determine the direction between the end point of the original ray and the new origin Vector3 newDirection = ((oldRay.origin() + oldRay.direction()* m_focalLength) - newOrigin); // Normalize the direction of the new ray newDirection /= newDirection.magnitude(); // Create a new ray from origin and direction, then add to sum Ray newRay = Ray(newOrigin, newDirection); sum += rayTrace(newRay, m_world); } } m_currentImage->set(x, y, sum / (float)m_currentRays); }