// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3AreaLight.cpp
// Function: gaps::R3AreaLight::DiffuseReflection(gaps::R3Brdf const&, gaps::R3Point const&, gaps::R3Vector const&) const
RNRgb R3AreaLight::
DiffuseReflection(const R3Brdf& brdf, 
    const R3Point& point, const R3Vector& normal) const
{
    // Parameter ????
    const int max_samples = 4;

    // Check if light is active
    if (!IsActive()) return RNblack_rgb;

    // Check if point is in front of light 
    if (R3SignedDistance(circle.Plane(), point) <= 0) return RNblack_rgb;

    // Get material properties
    const RNRgb& Dc = brdf.Diffuse();

    // Get light properties
    const RNRgb& Ic = Color();

    // Get circle axes
    R3Vector direction = circle.Normal();
    RNDimension dim = direction.MinDimension();
    R3Vector axis1 = direction % R3xyz_triad[dim];
    axis1.Normalize();
    R3Vector axis2 = direction % axis1;
    axis2.Normalize();

    // Sample points on light source
    int sample_count = 0;
    RNRgb sample_sum = RNblack_rgb;
    while (sample_count < max_samples) {
        // Sample point in circle
        RNScalar r1 = 2.0 * RNRandomScalar() - 1.0;
        RNScalar r2 = 2.0 * RNRandomScalar() - 1.0;
        if (r1*r1 + r2*r2 > 1) continue;
        R3Point sample_point = Position();
        sample_point += r1 * Radius() * axis1;
        sample_point += r2 * Radius() * axis2;
        sample_count++;
        
        // Compute direction at point
        R3Vector L = sample_point - point;
        L.Normalize();

        // Compute intensity at point
        RNScalar I = Intensity() * Direction().Dot(-L);
        if (RNIsNegativeOrZero(I)) return RNblack_rgb;
        RNLength d = R3Distance(point, sample_point);
        RNScalar denom = constant_attenuation;
        denom += d * linear_attenuation;
        denom += d * d * quadratic_attenuation;
        if (RNIsPositive(denom)) I /= denom;

        // Compute diffuse reflection from sample point
        RNScalar NL = normal.Dot(L);
        if (RNIsNegativeOrZero(NL)) continue;
        RNRgb diffuse = (I * NL) * Dc * Ic;

        // Add to result
        sample_sum += diffuse;
    }

    // Return diffuse reflection from area
    RNArea area = circle.Area();
    RNRgb sample_mean = sample_sum;
    if (sample_count > 0) sample_mean /= sample_count;
    return area * sample_mean;
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x28(%rsp)
movq %rdi, %rax
movq %rax, 0x30(%rsp)
movq %rdi, 0x1e0(%rsp)
movq %rsi, 0x1d8(%rsp)
movq %rdx, 0x1d0(%rsp)
movq %rcx, 0x1c8(%rsp)
movq %r8, 0x1c0(%rsp)
movq 0x1d8(%rsp), %rdi
movq %rdi, 0x38(%rsp)
movl $0x4, 0x1bc(%rsp)
callq 0x60940
cmpl $0x0, %eax
jne 0x5fb54
movq 0x28(%rsp), %rdi
leaq 0x1a003e(%rip), %rsi    # 0x1ffb88
callq 0xef300
jmp 0x60037
movq 0x38(%rsp), %rdi
addq $0x50, %rdi
callq 0x60950
movq %rax, %rdi
movq 0x1c8(%rsp), %rsi
callq 0x77d10
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jb 0x5fb94
movq 0x28(%rsp), %rdi
leaq 0x19fffe(%rip), %rsi    # 0x1ffb88
callq 0xef300
jmp 0x60037
movq 0x1d0(%rsp), %rdi
callq 0x48930
movq 0x38(%rsp), %rdi
movq %rax, 0x1b0(%rsp)
callq 0x2ba40
movq 0x38(%rsp), %rdi
movq %rax, 0x1a8(%rsp)
addq $0x50, %rdi
callq 0x4e7b0
movq %rax, %rsi
leaq 0x190(%rsp), %rdi
callq 0xcda10
leaq 0x190(%rsp), %rdi
callq 0xcdc30
movl %eax, 0x18c(%rsp)
movl 0x18c(%rsp), %esi
leaq 0x19ef8d(%rip), %rdi    # 0x1feb88
callq 0x60960
movq %rax, %rdx
leaq 0x170(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0xce810
leaq 0x170(%rsp), %rdi
callq 0xce130
leaq 0x158(%rsp), %rdi
leaq 0x190(%rsp), %rsi
leaq 0x170(%rsp), %rdx
callq 0xce810
leaq 0x158(%rsp), %rdi
callq 0xce130
movl $0x0, 0x154(%rsp)
leaq 0x138(%rsp), %rdi
leaq 0x19ff1f(%rip), %rsi    # 0x1ffb88
callq 0xef300
cmpl $0x4, 0x154(%rsp)
jge 0x5ffdf
callq 0xf2580
addsd %xmm0, %xmm0
movsd 0x12e923(%rip), %xmm1   # 0x18e5b0
addsd %xmm1, %xmm0
movsd %xmm0, 0x130(%rsp)
callq 0xf2580
addsd %xmm0, %xmm0
movsd 0x12e905(%rip), %xmm1   # 0x18e5b0
addsd %xmm1, %xmm0
movsd %xmm0, 0x128(%rsp)
movsd 0x130(%rsp), %xmm0
movsd 0x130(%rsp), %xmm2
movsd 0x128(%rsp), %xmm1
mulsd 0x128(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x12e324(%rip), %xmm1   # 0x18e010
ucomisd %xmm1, %xmm0
jbe 0x5fcf7
jmp 0x5fc6e
movq 0x38(%rsp), %rdi
callq 0x4e6e0
movq %rax, %rsi
leaq 0x110(%rsp), %rdi
callq 0xcc8b0
movq 0x38(%rsp), %rdi
movsd 0x130(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x4e720
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
leaq 0xf8(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x2b630
leaq 0x110(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0xccfb0
movq 0x38(%rsp), %rdi
movsd 0x128(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x4e720
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
leaq 0xe0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x2b630
leaq 0x110(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0xccfb0
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
movq 0x1c8(%rsp), %rdx
leaq 0xc8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xccbd0
leaq 0xc8(%rsp), %rdi
callq 0xce130
movq 0x38(%rsp), %rdi
callq 0x60980
movq 0x38(%rsp), %rdi
movsd %xmm0, 0x20(%rsp)
callq 0x4e700
movq %rax, 0x18(%rsp)
leaq 0xa8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xcecc0
movq 0x18(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xcdfe0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
leaq 0x196450(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x49e40
cmpl $0x0, %eax
je 0x5fe7c
movq 0x28(%rsp), %rdi
leaq 0x19fd16(%rip), %rsi    # 0x1ffb88
callq 0xef300
jmp 0x60037
movq 0x1c8(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x779c0
movq 0x38(%rsp), %rax
movsd %xmm0, 0xa0(%rsp)
movsd 0x98(%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd 0xa0(%rax), %xmm2
movsd 0x98(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0xa0(%rsp), %xmm0
mulsd 0xa0(%rsp), %xmm0
movsd 0xa8(%rax), %xmm2
movsd 0x98(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
leaq 0x196389(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x4a000
cmpl $0x0, %eax
je 0x5ff4c
movsd 0x98(%rsp), %xmm1
movsd 0xc0(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0x1c0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0xcdfe0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
leaq 0x19632e(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x49e40
cmpl $0x0, %eax
je 0x5ff8d
jmp 0x5fc6e
movsd 0xc0(%rsp), %xmm0
mulsd 0x90(%rsp), %xmm0
movq 0x1b0(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x60a00
movq 0x1a8(%rsp), %rdx
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x60990
leaq 0x138(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xef500
jmp 0x5fc6e
movq 0x38(%rsp), %rdi
addq $0x50, %rdi
callq 0xac230
movsd %xmm0, 0x58(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0xef300
cmpl $0x0, 0x154(%rsp)
jle 0x60022
cvtsi2sdl 0x154(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
callq 0xef640
movq 0x28(%rsp), %rdi
movsd 0x58(%rsp), %xmm0
leaq 0x40(%rsp), %rsi
callq 0x60a00
movq 0x30(%rsp), %rax
addq $0x1e8, %rsp            # imm = 0x1E8
retq
nopw %cs:(%rax,%rax)