// File: /workspace/github_cpp_makefile_star2_O3/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 0x8(%rsp), %rdi
movsd %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
leaq 0x18(%rdx), %r13
movq %r13, %rsi
callq 0x14b53
movaps %xmm0, 0x60(%rsp)
movss %xmm1, 0x4(%rsp)
movsd 0x18bb6(%rip), %xmm0    # 0x3c520
movsd %xmm0, (%rbp)
movl $0x3f800000, 0x8(%rbp)  # imm = 0x3F800000
movsd 0xc(%r15), %xmm3
movss 0x14(%r15), %xmm4
movsd 0x18(%r14), %xmm0
movaps %xmm3, %xmm1
mulps %xmm0, %xmm1
shufps $0x55, %xmm1, %xmm1     # xmm1 = xmm1[1,1,1,1]
movaps %xmm3, %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
movss 0x20(%r14), %xmm1
movaps %xmm4, %xmm5
mulss %xmm1, %xmm5
addss %xmm2, %xmm5
xorps %xmm2, %xmm2
ucomiss %xmm2, %xmm5
jbe 0x239db
movaps 0x18963(%rip), %xmm2    # 0x3c320
xorps %xmm2, %xmm0
xorps %xmm2, %xmm1
movlps %xmm0, 0x20(%rsp)
movss %xmm1, 0x28(%rsp)
movss 0x8(%r12), %xmm0
mulss %xmm0, %xmm5
jmp 0x23a02
movl 0x8(%r13), %eax
movl %eax, 0x28(%rsp)
movq (%r13), %rax
movq %rax, 0x20(%rsp)
movss 0x18810(%rip), %xmm0    # 0x3c204
divss 0x8(%r12), %xmm0
xorps 0x1891e(%rip), %xmm5    # 0x3c320
movaps %xmm5, 0x30(%rsp)
leaq 0x8(%rsp), %rdi
movlps %xmm3, (%rdi)
movss %xmm4, 0x8(%rdi)
leaq 0x20(%rsp), %rsi
leaq 0x44(%rsp), %rdx
movaps %xmm3, 0x50(%rsp)
movss %xmm4, (%rsp)
callq 0x14b9c
testb %al, %al
je 0x23a78
movaps 0x50(%rsp), %xmm1
movaps %xmm1, %xmm0
shufps $0x55, %xmm1, %xmm0     # xmm0 = xmm0[1,1],xmm1[1,1]
mulss %xmm0, %xmm0
mulss %xmm1, %xmm1
addss %xmm0, %xmm1
movss (%rsp), %xmm0
mulss %xmm0, %xmm0
addss %xmm1, %xmm0
xorps %xmm1, %xmm1
sqrtss %xmm0, %xmm1
movaps 0x30(%rsp), %xmm0
divss %xmm1, %xmm0
movss 0x8(%r12), %xmm1
callq 0x14ce5
cvtss2sd %xmm0, %xmm0
jmp 0x23a80
movsd 0x18d28(%rip), %xmm0    # 0x3c7a8
movsd %xmm0, 0x30(%rsp)
callq 0x63a0
addq $0xc, %r14
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x23ac4
movss 0x18(%r15), %xmm0
movq (%r14), %rax
movl 0x8(%r14), %ecx
movl %ecx, 0x8(%rbx)
movq %rax, (%rbx)
movaps 0x60(%rsp), %xmm1
movlps %xmm1, 0xc(%rbx)
movss 0x4(%rsp), %xmm1
movss %xmm1, 0x14(%rbx)
jmp 0x23b0b
movss 0x18(%r15), %xmm0
movl 0x8(%r14), %eax
movl %eax, 0x10(%rsp)
movq (%r14), %rax
movq %rax, 0x8(%rsp)
movq 0x44(%rsp), %rax
movq %rax, 0x14(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x8(%rsp), %rax
movq %rax, (%rbx)
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0x14(%rsp), %eax
movl %eax, 0xc(%rbx)
movq 0x18(%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
nop