// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r8, %rbx
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
movsd 0xc(%rsi), %xmm0
movss 0x14(%rsi), %xmm1
leaq 0x18(%rsp), %rdi
movsd %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
leaq 0x18(%rdx), %r13
movq %r13, %rsi
callq 0x152e1
movaps %xmm0, 0x60(%rsp)
movss %xmm1, 0x14(%rsp)
movabsq $0x3f8000003f800000, %rax # imm = 0x3F8000003F800000
movq %rax, (%rbp)
movl $0x3f800000, 0x8(%rbp)  # imm = 0x3F800000
movsd 0xc(%r15), %xmm1
movss 0x14(%r15), %xmm0
movss 0x18(%r14), %xmm3
movss 0x1c(%r14), %xmm4
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
movaps %xmm2, %xmm5
mulss %xmm4, %xmm5
movaps %xmm1, %xmm6
mulss %xmm3, %xmm6
addss %xmm5, %xmm6
movss 0x20(%r14), %xmm5
movaps %xmm0, %xmm7
mulss %xmm5, %xmm7
addss %xmm6, %xmm7
xorps %xmm6, %xmm6
ucomiss %xmm6, %xmm7
jbe 0x2444d
movaps 0x17ed4(%rip), %xmm6    # 0x3c300
xorps %xmm6, %xmm5
unpcklps %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
xorps %xmm6, %xmm3
movlps %xmm3, 0x30(%rsp)
movss %xmm5, 0x38(%rsp)
movss 0x8(%r12), %xmm3
mulss %xmm3, %xmm7
jmp 0x24474
movl 0x8(%r13), %eax
movl %eax, 0x38(%rsp)
movq (%r13), %rax
movq %rax, 0x30(%rsp)
movss 0x17dc2(%rip), %xmm3    # 0x3c228
divss 0x8(%r12), %xmm3
xorps 0x17e8c(%rip), %xmm7    # 0x3c300
movaps %xmm7, 0x40(%rsp)
mulss %xmm2, %xmm2
mulss %xmm1, %xmm1
addss %xmm2, %xmm1
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x2449b
sqrtss %xmm0, %xmm0
jmp 0x244ac
movss %xmm3, 0x10(%rsp)
callq 0x6710
movss 0x10(%rsp), %xmm3
movss %xmm0, 0xc(%rsp)
movsd 0xc(%r15), %xmm0
movss 0x14(%r15), %xmm1
leaq 0x18(%rsp), %rdi
movsd %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
leaq 0x30(%rsp), %rsi
leaq 0x54(%rsp), %rdx
movaps %xmm3, %xmm0
callq 0x1533a
testb %al, %al
je 0x244ff
movaps 0x40(%rsp), %xmm0
divss 0xc(%rsp), %xmm0
movss 0x8(%r12), %xmm1
callq 0x154ed
cvtss2sd %xmm0, %xmm0
jmp 0x24507
movsd 0x18101(%rip), %xmm0    # 0x3c608
movsd %xmm0, 0x40(%rsp)
callq 0x63a0
movsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x24548
movss 0x18(%r15), %xmm0
movq 0xc(%r14), %rax
movl 0x14(%r14), %ecx
movl %ecx, 0x8(%rbx)
movq %rax, (%rbx)
movaps 0x60(%rsp), %xmm1
movlps %xmm1, 0xc(%rbx)
movss 0x14(%rsp), %xmm1
movss %xmm1, 0x14(%rbx)
jmp 0x24590
movss 0x18(%r15), %xmm0
movl 0x14(%r14), %eax
movl %eax, 0x20(%rsp)
movq 0xc(%r14), %rax
movq %rax, 0x18(%rsp)
movq 0x54(%rsp), %rax
movq %rax, 0x24(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x18(%rsp), %rax
movq %rax, (%rbx)
movl 0x20(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0x24(%rsp), %eax
movl %eax, 0xc(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rbx)
movss %xmm0, 0x18(%rbx)
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq