// File: Lut99[P]RayTracer/src/lib/materials/Dielectric.cpp
// Function: RayTracer::Dielectric::scatter(RayTracer::Ray const&, RayTracer::HitRecord const&, RayTracer::Vec3&, RayTracer::Ray&) const
bool Dielectric::scatter(const Ray& ray_in, const HitRecord& record, Vec3& attenuation, Ray& ray_out) const {
    Vec3 outward_normal;
    double ni_over_nt;
    attenuation = this->albedo;
    Vec3 refracted;

    double reflect_prob;
    double cosine;

    // I think this decides on what side of the sphere we hit; in to out or out to in.
    if (dot(ray_in.direction, record.normal) > 0) {
        outward_normal = -record.normal;
        ni_over_nt = this->ref_idx;
        cosine = this->ref_idx * dot(ray_in.direction, record.normal) / ray_in.direction.length();
    } else {
        outward_normal = record.normal;
        ni_over_nt = 1.0 / this->ref_idx;
        cosine = -dot(ray_in.direction, record.normal) / ray_in.direction.length();
    }

    if (this->refract(ray_in.direction, outward_normal, ni_over_nt, refracted)) {
        reflect_prob = this->schlick(cosine);
    } else {
        reflect_prob = 1.0;
    }

    if (random_double() < reflect_prob) {
        ray_out = Ray(record.hitpoint, this->reflect(ray_in.direction, record.normal));
    } else {
        ray_out = Ray(record.hitpoint, refracted);
    }

   if (this->refract(ray_in.direction, outward_normal, ni_over_nt, refracted)) {
       ray_out = Ray(record.hitpoint, refracted);
   } else {
       ray_out = Ray(record.hitpoint, this->reflect(ray_in.direction, record.normal));
   }

    return true;
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movq %rdx, 0x200(%rsp)
movq %rcx, 0x1f8(%rsp)
movq %r8, 0x1f0(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x1d0(%rsp), %rdi
callq 0x154710
movq 0x30(%rsp), %rsi
addq $0x10, %rsi
movq 0x1f8(%rsp), %rdi
callq 0x1547a0
leaq 0x1a8(%rsp), %rdi
callq 0x154710
movq 0x208(%rsp), %rdi
addq $0x20, %rdi
movq 0x200(%rsp), %rsi
addq $0x38, %rsi
callq 0x155c70
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1678fe
movq 0x200(%rsp), %rsi
addq $0x38, %rsi
leaq 0x178(%rsp), %rdi
callq 0x1548b0
leaq 0x1d0(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x1547a0
movq 0x30(%rsp), %rax
movsd 0x30(%rax), %xmm0
movsd %xmm0, 0x1c8(%rsp)
movsd 0x30(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x208(%rsp), %rdi
addq $0x20, %rdi
movq 0x200(%rsp), %rsi
addq $0x38, %rsi
callq 0x155c70
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x208(%rsp), %rdi
addq $0x20, %rdi
callq 0x154e70
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x198(%rsp)
jmp 0x167993
movq 0x200(%rsp), %rsi
addq $0x38, %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x1547a0
movq 0x30(%rsp), %rax
movsd 0x871c(%rip), %xmm0     # 0x170040
divsd 0x30(%rax), %xmm0
movsd %xmm0, 0x1c8(%rsp)
movq 0x208(%rsp), %rdi
addq $0x20, %rdi
movq 0x200(%rsp), %rsi
addq $0x38, %rsi
callq 0x155c70
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x208(%rsp), %rdi
addq $0x20, %rdi
callq 0x154e70
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x198(%rsp)
movq 0x30(%rsp), %rdi
movq 0x208(%rsp), %rsi
addq $0x20, %rsi
movsd 0x1c8(%rsp), %xmm0
movq (%rdi), %rax
leaq 0x1d0(%rsp), %rdx
leaq 0x1a8(%rsp), %rcx
callq *0x38(%rax)
testb $0x1, %al
jne 0x1679c9
jmp 0x1679e8
movq 0x30(%rsp), %rdi
movsd 0x198(%rsp), %xmm0
movq (%rdi), %rax
callq *0x40(%rax)
movsd %xmm0, 0x1a0(%rsp)
jmp 0x1679f9
movsd 0x8650(%rip), %xmm0     # 0x170040
movsd %xmm0, 0x1a0(%rsp)
callq 0x15eca0
movaps %xmm0, %xmm1
movsd 0x1a0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x167a7d
movq 0x30(%rsp), %rsi
movq 0x200(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x10(%rsp)
movq 0x208(%rsp), %rdx
addq $0x20, %rdx
movq 0x200(%rsp), %rcx
addq $0x38, %rcx
movq (%rsi), %rax
leaq 0x118(%rsp), %rdi
callq *0x30(%rax)
movq 0x10(%rsp), %rsi
leaq 0x138(%rsp), %rdi
leaq 0x118(%rsp), %rdx
callq 0x139c00
movq 0x1f0(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x166910
jmp 0x167ab3
movq 0x200(%rsp), %rsi
addq $0x18, %rsi
leaq 0xd8(%rsp), %rdi
leaq 0x1a8(%rsp), %rdx
callq 0x139c00
movq 0x1f0(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x166910
movq 0x30(%rsp), %rdi
movq 0x208(%rsp), %rsi
addq $0x20, %rsi
movsd 0x1c8(%rsp), %xmm0
movq (%rdi), %rax
leaq 0x1d0(%rsp), %rdx
leaq 0x1a8(%rsp), %rcx
callq *0x38(%rax)
testb $0x1, %al
jne 0x167ae9
jmp 0x167b21
movq 0x200(%rsp), %rsi
addq $0x18, %rsi
leaq 0x98(%rsp), %rdi
leaq 0x1a8(%rsp), %rdx
callq 0x139c00
movq 0x1f0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x166910
jmp 0x167b80
movq 0x30(%rsp), %rsi
movq 0x200(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x8(%rsp)
movq 0x208(%rsp), %rdx
addq $0x20, %rdx
movq 0x200(%rsp), %rcx
addq $0x38, %rcx
movq (%rsi), %rax
leaq 0x38(%rsp), %rdi
callq *0x30(%rax)
movq 0x8(%rsp), %rsi
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x139c00
movq 0x1f0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x166910
movb $0x1, %al
andb $0x1, %al
addq $0x218, %rsp            # imm = 0x218
retq
nopl (%rax)