// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %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 0x11de8
movaps %xmm0, 0x50(%rsp)
movss %xmm1, 0x4(%rsp)
movsd 0x10dde(%rip), %xmm0    # 0x30530
movsd %xmm0, (%rbp)
movl $0x3f800000, 0x8(%rbp)  # imm = 0x3F800000
movsd 0xc(%r15), %xmm1
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
movss 0x14(%r15), %xmm2
movss 0x1c(%r14), %xmm0
mulss %xmm3, %xmm0
movss 0x18(%r14), %xmm4
mulss %xmm1, %xmm4
addss %xmm0, %xmm4
movss 0x20(%r14), %xmm5
mulss %xmm2, %xmm5
addss %xmm4, %xmm5
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm5
jbe 0x1f805
movss 0x10ba9(%rip), %xmm0    # 0x30350
movq %r13, %rdi
callq 0x8980
movlps %xmm0, 0x20(%rsp)
movss %xmm1, 0x28(%rsp)
movss 0x8(%r12), %xmm0
movsd 0xc(%r15), %xmm1
movaps %xmm1, %xmm3
shufps $0x55, %xmm1, %xmm3     # xmm3 = xmm3[1,1],xmm1[1,1]
movss 0x14(%r15), %xmm2
movss 0x1c(%r14), %xmm4
mulss %xmm3, %xmm4
movss 0x18(%r14), %xmm5
mulss %xmm1, %xmm5
addss %xmm4, %xmm5
movss 0x20(%r14), %xmm4
mulss %xmm2, %xmm4
addss %xmm5, %xmm4
mulss %xmm0, %xmm4
movaps %xmm4, 0x30(%rsp)
jmp 0x1f831
movl 0x8(%r13), %eax
movl %eax, 0x28(%rsp)
movq (%r13), %rax
movq %rax, 0x20(%rsp)
movss 0x109e6(%rip), %xmm0    # 0x30204
divss 0x8(%r12), %xmm0
xorps 0x10af4(%rip), %xmm5    # 0x30320
movaps %xmm5, 0x30(%rsp)
mulss %xmm3, %xmm3
movaps %xmm1, %xmm4
mulss %xmm1, %xmm4
addss %xmm3, %xmm4
movaps %xmm2, %xmm3
mulss %xmm2, %xmm3
addss %xmm4, %xmm3
movss %xmm3, (%rsp)
leaq 0x8(%rsp), %rdi
movlps %xmm1, (%rdi)
movss %xmm2, 0x8(%rdi)
leaq 0x20(%rsp), %rsi
leaq 0x44(%rsp), %rdx
callq 0x11e23
testb %al, %al
je 0x1f890
sqrtss (%rsp), %xmm1
movaps 0x30(%rsp), %xmm0
divss %xmm1, %xmm0
movss 0x8(%r12), %xmm1
callq 0x11f3b
cvtss2sd %xmm0, %xmm0
jmp 0x1f898
movsd 0x10c18(%rip), %xmm0    # 0x304b0
movsd %xmm0, 0x30(%rsp)
callq 0x63e0
addq $0xc, %r14
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1f8dc
movss 0x18(%r15), %xmm0
movq (%r14), %rax
movl 0x8(%r14), %ecx
movl %ecx, 0x8(%rbx)
movq %rax, (%rbx)
movaps 0x50(%rsp), %xmm1
movlps %xmm1, 0xc(%rbx)
movss 0x4(%rsp), %xmm1
movss %xmm1, 0x14(%rbx)
jmp 0x1f923
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 $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop