// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %r8, 0x60(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdi, %rbx
cmpl $0x0, 0x20(%rsi)
je 0x3cd42
movq %rcx, %r15
movq %rsi, %r13
leaq 0x78(%rsi), %r14
movq %r14, %rdi
movq %rcx, %rsi
callq 0x4834c
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x3cd42
movq %rbx, 0x10(%rsp)
leaq 0x50(%r13), %rax
movq %rax, 0x38(%rsp)
addq $0x38, 0x18(%rsp)
leaq 0x30(%r13), %rax
movq %rax, 0x48(%rsp)
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %r14, 0x58(%rsp)
movq %r14, %rsi
callq 0x6c044
movq %rbx, %rdi
callq 0x6c17e
cltq
imulq $0x18, %rax, %rdx
addq 0xf150b(%rip), %rdx     # 0x12dfa8
leaq 0xe8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6c6e6
movq %r14, %rdi
callq 0x6c336
leaq 0xd0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6c6e6
movq %r12, %rdi
callq 0x6c336
leaq 0xfab23(%rip), %rsi     # 0x137600
leaq 0x88(%rsp), %rdi
callq 0x7c020
leaq 0x58(%r13), %rax
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0xb8(%rsp), %r12
leaq 0x118(%rsp), %r14
movq %r13, 0x40(%rsp)
cmpl $0x3, %ebp
jg 0x3cd56
callq 0x7dcd2
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd 0x889a8(%rip), %xmm0    # 0xc54d0
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
callq 0x7dcd2
addsd %xmm0, %xmm0
addsd 0x8898e(%rip), %xmm0    # 0xc54d0
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd (%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm2, %xmm1
ucomisd 0x884b9(%rip), %xmm1    # 0xc5018
ja 0x3cb0a
movq %r12, %rdi
movq 0x50(%rsp), %rsi
movsd %xmm0, 0x20(%rsp)
callq 0x6b884
movsd (%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %r14, %rdi
leaq 0xe8(%rsp), %rsi
callq 0x6c72a
movq %r12, %rdi
movq %r14, %rsi
callq 0x6bbc4
movsd 0x20(%rsp), %xmm0
mulsd 0x70(%r13), %xmm0
movq %r14, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x6c72a
movq %r12, %rdi
movq %r14, %rsi
callq 0x6bbc4
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x6ba51
movq %r14, %rdi
callq 0x6c336
movsd 0x28(%r13), %xmm0
movsd %xmm0, (%rsp)
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6c99a
movq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x6c2ae
mulsd (%rsp), %xmm0
leaq 0xf24ea(%rip), %rax     # 0x12f0f8
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3cda0
movapd %xmm0, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rbx
callq 0x48146
movsd %xmm0, 0x70(%rsp)
movsd 0x98(%r13), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0xa0(%r13), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0xa8(%r13), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd (%rbx), %xmm0
movapd %xmm0, 0x20(%rsp)
movq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x6c2ae
movsd (%rbx), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3cd3b
movsd 0x70(%rsp), %xmm3
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
mulsd 0x68(%rsp), %xmm1
movsd 0x80(%rsp), %xmm2
mulsd %xmm3, %xmm2
addsd 0x78(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd (%rsp), %xmm3
movapd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd 0x20(%rsp), %xmm4
cmpnltsd %xmm2, %xmm4
movapd %xmm4, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm3, %xmm4
orpd %xmm2, %xmm4
mulsd %xmm4, %xmm0
movq %r14, %r13
movl %ebp, %r14d
movq %r12, %rbp
movq %r15, %r12
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x3d3cb
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %r15
movq %rbp, %r12
movl %r14d, %ebp
movq %r13, %r14
movq 0x40(%rsp), %r13
movq 0x48(%rsp), %rdx
callq 0x3d360
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
callq 0x7c0ee
incl %ebp
jmp 0x3cb0a
leaq 0xfa8b7(%rip), %rsi     # 0x137600
movq %rbx, %rdi
callq 0x7c020
movq %rbx, %rax
jmp 0x3cdb7
movq 0x38(%rsp), %rdi
callq 0x5ee84
movsd %xmm0, (%rsp)
leaq 0xb8(%rsp), %rbx
leaq 0x88(%rsp), %rsi
movq %rbx, %rdi
callq 0x7c020
cvtsi2sd %ebp, %xmm0
movq %rbx, %rdi
callq 0x7c19a
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd (%rsp), %xmm0
callq 0x3d3cb
jmp 0x3cdb4
leaq 0xfa859(%rip), %rsi     # 0x137600
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x7c020
movq %r14, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop