// File: alan0201tw[P]RayTracer/src/material.cpp
// Function: dielectric::scatter(ray const&, hit_record const&, vec3&, ray&) const
bool dielectric::scatter(const ray& _incoming_ray, const hit_record& _record, vec3& _attenuation, ray& _scattered_ray) const
{
    vec3 reflected = reflect(_incoming_ray.direction(), _record.normal);
    
    float ni_over_nt;
    _attenuation = vec3(1.0f, 1.0f, 1.0f);
    
    vec3 outward_normal;
    float cosine;
    if(dot(_incoming_ray.direction(), _record.normal) > 0.0f)
    {
        outward_normal = -1 * _record.normal;
        ni_over_nt = refractive_index;
        cosine = refractive_index * dot(_incoming_ray.direction(), _record.normal) / _incoming_ray.direction().length();
    }
    else
    {
        outward_normal = _record.normal;
        ni_over_nt = 1.0f / refractive_index;
        cosine = -1 * dot(_incoming_ray.direction(), _record.normal) / _incoming_ray.direction().length();
    }
    
    vec3 refracted;
    float reflect_prob;
    if(refract(_incoming_ray.direction(), outward_normal, ni_over_nt, refracted))
    {
        reflect_prob = schlick(cosine, refractive_index);
    }
    else
    {
        reflect_prob = 1.0f;
    }

    if(drand48() < reflect_prob)
    {
        _scattered_ray = ray(_record.hit_point, reflected, _incoming_ray.get_time());
    }
    else
    {
        _scattered_ray = ray(_record.hit_point, refracted, _incoming_ray.get_time());
    }
    
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x50(%rbp)
movlpd %xmm0, -0x58(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq -0x48(%rbp), %rdi
callq 0x24840
movss %xmm1, -0x60(%rbp)
movlpd %xmm0, -0x68(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x78(%rbp), %rdi
movss 0x2886c(%rip), %xmm2    # 0x7e1fc
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xf9b0
movq -0x20(%rbp), %rax
movl -0x70(%rbp), %ecx
movl %ecx, 0x8(%rax)
movq -0x78(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x88(%rbp), %rdi
callq 0x10660
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xa0(%rbp)
movlpd %xmm0, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0x98(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x90(%rbp)
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq -0x98(%rbp), %rdi
callq 0x11900
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x55b38
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
movss 0x28a39(%rip), %xmm0    # 0x7e454
callq 0xfaa0
movq -0x1b8(%rbp), %rax
movss %xmm1, -0xc0(%rbp)
movlpd %xmm0, -0xc8(%rbp)
movl -0xc0(%rbp), %ecx
movl %ecx, -0xb0(%rbp)
movq -0xc8(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movl -0xb0(%rbp), %ecx
movl %ecx, -0x80(%rbp)
movq -0xb8(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movss 0x8(%rax), %xmm0
movss %xmm0, -0x6c(%rbp)
movss 0x8(%rax), %xmm0
movss %xmm0, -0x1c0(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0xe0(%rbp)
movlpd %xmm0, -0xe8(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, -0xd0(%rbp)
movq -0xe8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq -0xd8(%rbp), %rdi
callq 0x11900
movaps %xmm0, %xmm1
movss -0x1c0(%rbp), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, -0x1bc(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x100(%rbp)
movlpd %xmm0, -0x108(%rbp)
movq -0x108(%rbp), %rax
movq %rax, -0xf4(%rbp)
movl -0x100(%rbp), %eax
movl %eax, -0xec(%rbp)
leaq -0xf4(%rbp), %rdi
callq 0x12130
movaps %xmm0, %xmm1
movss -0x1bc(%rbp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x8c(%rbp)
jmp 0x55c1a
movq -0x1b8(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x20(%rcx), %edx
movl %edx, -0x80(%rbp)
movq 0x18(%rcx), %rcx
movq %rcx, -0x88(%rbp)
movss 0x8(%rax), %xmm1
movss 0x2869b(%rip), %xmm0    # 0x7e1fc
divss %xmm1, %xmm0
movss %xmm0, -0x6c(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x120(%rbp)
movlpd %xmm0, -0x128(%rbp)
movl -0x120(%rbp), %eax
movl %eax, -0x110(%rbp)
movq -0x128(%rbp), %rax
movq %rax, -0x118(%rbp)
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
leaq -0x118(%rbp), %rdi
callq 0x11900
movaps 0x28878(%rip), %xmm1    # 0x7e430
pxor %xmm1, %xmm0
movss %xmm0, -0x1c4(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x140(%rbp)
movlpd %xmm0, -0x148(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x134(%rbp)
movl -0x140(%rbp), %eax
movl %eax, -0x12c(%rbp)
leaq -0x134(%rbp), %rdi
callq 0x12130
movaps %xmm0, %xmm1
movss -0x1c4(%rbp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x8c(%rbp)
leaq -0x154(%rbp), %rdi
callq 0x10660
movq -0x10(%rbp), %rdi
callq 0xf8b0
movss %xmm1, -0x170(%rbp)
movlpd %xmm0, -0x178(%rbp)
movq -0x178(%rbp), %rax
movq %rax, -0x164(%rbp)
movl -0x170(%rbp), %eax
movl %eax, -0x15c(%rbp)
movss -0x6c(%rbp), %xmm0
leaq -0x164(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x154(%rbp), %rdx
callq 0x248d0
testb $0x1, %al
jne 0x55c7e
jmp 0x55ca1
movq -0x1b8(%rbp), %rax
movss -0x8c(%rbp), %xmm0
movss 0x8(%rax), %xmm1
callq 0x24b20
movss %xmm0, -0x158(%rbp)
jmp 0x55cb1
movss 0x28553(%rip), %xmm0    # 0x7e1fc
movss %xmm0, -0x158(%rbp)
callq 0x7480
movaps %xmm0, %xmm1
movss -0x158(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x55d29
movq -0x18(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x1d0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11ae0
movq -0x1d0(%rbp), %rsi
leaq -0x194(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x55430
movq -0x28(%rbp), %rax
movq -0x194(%rbp), %rcx
movq %rcx, (%rax)
movq -0x18c(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x184(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x17c(%rbp), %ecx
movl %ecx, 0x18(%rax)
jmp 0x55d88
movq -0x18(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x1d8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x11ae0
movq -0x1d8(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
leaq -0x154(%rbp), %rdx
callq 0x55430
movq -0x28(%rbp), %rax
movq -0x1b0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x1a8(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x1a0(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl -0x198(%rbp), %ecx
movl %ecx, 0x18(%rax)
movb $0x1, %al
andb $0x1, %al
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopw %cs:(%rax,%rax)