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

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

    // Get material properties
    const RNRgb& Sc = brdf.Specular();
    RNScalar s = brdf.Shininess();

    // 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;
    for (int i = 0; i < max_samples; i++) {
        // 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 intensity at point
        RNScalar I = Intensity();
        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 direction at point
        R3Vector L = sample_point - point;
        L.Normalize();

        // Compute specular reflection from sample_point
        RNScalar NL = normal.Dot(L);
        if (RNIsNegativeOrZero(NL)) return RNblack_rgb;
        R3Vector R = (2.0 * NL) * normal - L;
        R3Vector V = eye - point;
        V.Normalize();
        RNScalar VR = V.Dot(R);
        if (RNIsNegativeOrZero(VR)) continue;

        // Return specular component of reflection
        sample_sum += (I * pow(VR,s)) * Sc * Ic;
    }

    // Return specular 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 $0x228, %rsp            # imm = 0x228
movq %rdi, 0x18(%rsp)
movq %rdi, %rax
movq %rax, 0x20(%rsp)
movq %rdi, 0x220(%rsp)
movq %rsi, 0x218(%rsp)
movq %rdx, 0x210(%rsp)
movq %rcx, 0x208(%rsp)
movq %r8, 0x200(%rsp)
movq %r9, 0x1f8(%rsp)
movq 0x218(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movl $0x8, 0x1f4(%rsp)
callq 0x60940
cmpl $0x0, %eax
jne 0x600cc
movq 0x18(%rsp), %rdi
leaq 0x19fac6(%rip), %rsi    # 0x1ffb88
callq 0xef300
jmp 0x605ff
movq 0x210(%rsp), %rdi
callq 0x48980
movq %rax, 0x1e8(%rsp)
movq 0x210(%rsp), %rdi
callq 0x48ae0
movq 0x28(%rsp), %rdi
movsd %xmm0, 0x1e0(%rsp)
callq 0x2ba40
movq 0x28(%rsp), %rdi
movq %rax, 0x1d8(%rsp)
addq $0x50, %rdi
callq 0x4e7b0
movq %rax, %rsi
leaq 0x1c0(%rsp), %rdi
callq 0xcda10
leaq 0x1c0(%rsp), %rdi
callq 0xcdc30
movl %eax, 0x1bc(%rsp)
movl 0x1bc(%rsp), %esi
leaq 0x19ea3f(%rip), %rdi    # 0x1feb88
callq 0x60960
movq %rax, %rdx
leaq 0x1a0(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0xce810
leaq 0x1a0(%rsp), %rdi
callq 0xce130
leaq 0x188(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
leaq 0x1a0(%rsp), %rdx
callq 0xce810
leaq 0x188(%rsp), %rdi
callq 0xce130
movl $0x0, 0x184(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x19f9d1(%rip), %rsi    # 0x1ffb88
callq 0xef300
movl $0x0, 0x164(%rsp)
cmpl $0x8, 0x164(%rsp)
jge 0x605a7
callq 0xf2580
addsd %xmm0, %xmm0
movsd 0x12e3ca(%rip), %xmm1   # 0x18e5b0
addsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
callq 0xf2580
addsd %xmm0, %xmm0
movsd 0x12e3ac(%rip), %xmm1   # 0x18e5b0
addsd %xmm1, %xmm0
movsd %xmm0, 0x150(%rsp)
movsd 0x158(%rsp), %xmm0
movsd 0x158(%rsp), %xmm2
movsd 0x150(%rsp), %xmm1
mulsd 0x150(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x12ddcb(%rip), %xmm1   # 0x18e010
ucomisd %xmm1, %xmm0
jbe 0x60250
jmp 0x60591
movq 0x28(%rsp), %rdi
callq 0x4e6e0
movq %rax, %rsi
leaq 0x138(%rsp), %rdi
callq 0xcc8b0
movq 0x28(%rsp), %rdi
movsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x4e720
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
leaq 0x120(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x2b630
leaq 0x138(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0xccfb0
movq 0x28(%rsp), %rdi
movsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x4e720
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
leaq 0x108(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x2b630
leaq 0x138(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0xccfb0
movq 0x28(%rsp), %rdi
movl 0x184(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x184(%rsp)
callq 0x60980
movsd %xmm0, 0x100(%rsp)
movq 0x200(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x779c0
movq 0x28(%rsp), %rax
movsd %xmm0, 0xf8(%rsp)
movsd 0x98(%rax), %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf8(%rsp), %xmm0
movsd 0xa0(%rax), %xmm2
movsd 0xf0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf8(%rsp), %xmm0
mulsd 0xf8(%rsp), %xmm0
movsd 0xa8(%rax), %xmm2
movsd 0xf0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm0
leaq 0x195ed7(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x4a000
cmpl $0x0, %eax
je 0x603fe
movsd 0xf0(%rsp), %xmm1
movsd 0x100(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
movq 0x200(%rsp), %rdx
leaq 0xd8(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0xccbd0
leaq 0xd8(%rsp), %rdi
callq 0xce130
movq 0x1f8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0xcdfe0
movsd %xmm0, 0xd0(%rsp)
movsd 0xd0(%rsp), %xmm0
leaq 0x195e52(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x49e40
cmpl $0x0, %eax
je 0x6047a
movq 0x18(%rsp), %rdi
leaq 0x19f718(%rip), %rsi    # 0x1ffb88
callq 0xef300
jmp 0x605ff
movsd 0x12dbce(%rip), %xmm0   # 0x18e050
mulsd 0xd0(%rsp), %xmm0
movq 0x1f8(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x2b630
leaq 0xb8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
callq 0xcedc0
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
leaq 0x88(%rsp), %rdi
callq 0xccbd0
leaq 0x88(%rsp), %rdi
callq 0xce130
leaq 0x88(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0xcdfe0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
leaq 0x195d93(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm1
callq 0x49e40
cmpl $0x0, %eax
je 0x60525
jmp 0x60591
movsd 0x100(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x80(%rsp), %xmm0
movsd 0x1e0(%rsp), %xmm1
callq 0x23260
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x1e8(%rsp), %rsi
leaq 0x50(%rsp), %rdi
callq 0x60a00
movq 0x1d8(%rsp), %rdx
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x60990
leaq 0x168(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xef500
movl 0x164(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x164(%rsp)
jmp 0x601c7
movq 0x28(%rsp), %rdi
addq $0x50, %rdi
callq 0xac230
movsd %xmm0, 0x48(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0xef300
cmpl $0x0, 0x184(%rsp)
jle 0x605ea
cvtsi2sdl 0x184(%rsp), %xmm0
leaq 0x30(%rsp), %rdi
callq 0xef640
movq 0x18(%rsp), %rdi
movsd 0x48(%rsp), %xmm0
leaq 0x30(%rsp), %rsi
callq 0x60a00
movq 0x20(%rsp), %rax
addq $0x228, %rsp            # imm = 0x228
retq
nopl (%rax)