// File: alan0201tw[P]RayTracer/src/main.cpp
// Function: get_color(ray const&, std::shared_ptr<hitable>, int)
vec3 get_color(const ray& _ray, std::shared_ptr<hitable> _world, int depth)
{
    hit_record rec;
    if(_world->hit(_ray, 0.001f, MAXFLOAT, rec))
    {
        ray scattered;
        vec3 attenuation;
        vec3 emitted = rec.material_ref->emitted(rec.u, rec.v, rec.hit_point);

        if(depth < 50 && rec.material_ref->scatter(_ray, rec, attenuation, scattered))
        {
            return emitted + attenuation * get_color(scattered, _world, depth + 1);
        }
        else
        {
            return emitted;
        }
    }
    else // here is where the "clear color", or the "background color" lays
    {
        return vec3(0.0f, 0.0f, 0.0f);
        //return vec3(0.1f, 0.1f, 0.1f);

        // the color here will also provide "brightness" when returning to last recursion

        // vec3 unit_direction = unit_vector(_ray.direction());
        // float t = 0.5f * (unit_direction.y() + 1.0f);
        // return (1.0 - t) * vec3(1.0, 1.0, 1.0) + t * vec3(0.5, 0.7, 1.0);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rsi, -0x140(%rbp)
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x10540
movq -0x140(%rbp), %rdi
callq 0x105a0
movq -0x138(%rbp), %rdx
movq %rax, %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
movss 0x24727(%rip), %xmm0    # 0x7f1fc
movss 0x23833(%rip), %xmm1    # 0x7e310
callq *%rax
movb %al, -0x129(%rbp)
jmp 0x5aae7
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0x5aaf6
jmp 0x5ad5d
leaq -0x88(%rbp), %rdi
callq 0x73e00
jmp 0x5ab04
leaq -0x94(%rbp), %rdi
callq 0x10660
jmp 0x5ab12
leaq -0x38(%rbp), %rdi
callq 0x73e30
movq %rax, %rdi
movss -0x5c(%rbp), %xmm0
movss -0x58(%rbp), %xmm1
leaq -0x54(%rbp), %rsi
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
movss %xmm1, -0x154(%rbp)
movaps %xmm0, -0x150(%rbp)
jmp 0x5ab46
movss -0x154(%rbp), %xmm0
movaps -0x150(%rbp), %xmm1
movlpd %xmm1, -0xb0(%rbp)
movss %xmm0, -0xa8(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x98(%rbp)
cmpl $0x32, -0x24(%rbp)
jge 0x5ad3d
leaq -0x38(%rbp), %rdi
callq 0x73e30
movq %rax, %rdi
movq -0x18(%rbp), %rsi
movq (%rdi), %rax
movq (%rax), %rax
leaq -0x60(%rbp), %rdx
leaq -0x94(%rbp), %rcx
leaq -0x88(%rbp), %r8
callq *%rax
movb %al, -0x155(%rbp)
jmp 0x5abbb
movb -0x155(%rbp), %al
testb $0x1, %al
jne 0x5abca
jmp 0x5ad3d
movq -0x140(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x9f20
movq -0x180(%rbp), %rsi
movl -0x24(%rbp), %edx
incl %edx
leaq -0x88(%rbp), %rdi
callq 0x5aa80
movss %xmm1, -0x174(%rbp)
movaps %xmm0, -0x170(%rbp)
jmp 0x5ac0d
movss -0x174(%rbp), %xmm0
movaps -0x170(%rbp), %xmm1
movlpd %xmm1, -0xf0(%rbp)
movss %xmm0, -0xe8(%rbp)
movl -0xe8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x94(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x73e50
movss %xmm1, -0x194(%rbp)
movaps %xmm0, -0x190(%rbp)
jmp 0x5ac6a
movss -0x194(%rbp), %xmm0
movaps -0x190(%rbp), %xmm1
movlpd %xmm1, -0x100(%rbp)
movss %xmm0, -0xf8(%rbp)
movl -0xf8(%rbp), %eax
movl %eax, -0xb8(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xfa30
movss %xmm1, -0x1b4(%rbp)
movaps %xmm0, -0x1b0(%rbp)
jmp 0x5acc7
movss -0x1b4(%rbp), %xmm0
movaps -0x1b0(%rbp), %xmm1
movlpd %xmm1, -0x110(%rbp)
movss %xmm0, -0x108(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0xc(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0x4(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x8e00
movl $0x1, -0x114(%rbp)
jmp 0x5ad7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x5adb1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x8e00
jmp 0x5adb1
movq -0xa0(%rbp), %rax
movq %rax, -0xc(%rbp)
movl -0x98(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x114(%rbp)
jmp 0x5ad7b
leaq -0xc(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xf9b0
jmp 0x5ad71
movl $0x1, -0x114(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x10610
movl -0x4(%rbp), %eax
movl %eax, -0x120(%rbp)
movq -0xc(%rbp), %rax
movq %rax, -0x128(%rbp)
movsd -0x128(%rbp), %xmm0
movss -0x120(%rbp), %xmm1
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
leaq -0x60(%rbp), %rdi
callq 0x10610
movq -0x68(%rbp), %rdi
callq 0x7860
nopw %cs:(%rax,%rax)