// File: /workspace/github_cpp_makefile_star2_O2/alan0201tw[P]RayTracer/src/sphere.cpp
// Function: moving_sphere::hit(ray const&, float, float, hit_record&) const
bool moving_sphere::hit(const ray& _ray, float t_min, float t_max, hit_record& rec) const
{
    vec3 current_center = center(_ray.get_time());
    vec3 oc = _ray.origin() - current_center;
    float a = dot(_ray.direction(), _ray.direction());
    float b = dot(oc, _ray.direction());
    float c = dot(oc, oc) - radius * radius;
    float discriminant = b*b - a*c;

    if(discriminant > 0.0f)
    {
        float temp = (-b - sqrt(discriminant)) / a;

        if(temp < t_max && temp > t_min)
        {
            rec.t = temp;
            rec.hit_point = _ray.point_at_parameter(rec.t);
            // obtaining texture coordinate
            get_sphere_uv((rec.hit_point - current_center) / radius, rec.u, rec.v);
            //
            rec.normal = (rec.hit_point - current_center) / radius;
            rec.material_ref = moving_sphere_mat;
            return true;
        }

        temp = (-b + sqrt(discriminant)) / a;

        if(temp < t_max && temp > t_min)
        {
            rec.t = temp;
            rec.hit_point = _ray.point_at_parameter(rec.t);
            // obtaining texture coordinate
            get_sphere_uv((rec.hit_point - current_center) / radius, rec.u, rec.v);
            //
            rec.normal = (rec.hit_point - current_center) / radius;
            rec.material_ref = moving_sphere_mat;
            return true;
        }
    }
    
    return false;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x60, %rsp
movq %rdx, %rbx
movss %xmm1, 0xc(%rsp)
movss %xmm0, 0x8(%rsp)
movq %rsi, %r15
movq %rdi, %r14
movss 0x18(%rsi), %xmm0
callq 0x9460
movlps %xmm0, 0x10(%rsp)
movss %xmm1, 0x18(%rsp)
movsd (%r15), %xmm3
movss 0x8(%r15), %xmm2
subps %xmm0, %xmm3
movaps %xmm3, %xmm0
shufps $0x55, %xmm3, %xmm0     # xmm0 = xmm0[1,1],xmm3[1,1]
subss %xmm1, %xmm2
movsd 0xc(%r15), %xmm1
movss 0x14(%r15), %xmm6
movaps %xmm1, %xmm4
shufps $0x55, %xmm1, %xmm4     # xmm4 = xmm4[1,1],xmm1[1,1]
movaps %xmm0, %xmm5
mulss %xmm4, %xmm5
mulss %xmm4, %xmm4
movaps %xmm3, %xmm7
mulss %xmm1, %xmm7
mulss %xmm1, %xmm1
addss %xmm4, %xmm1
movaps %xmm2, %xmm4
mulss %xmm6, %xmm4
mulss %xmm6, %xmm6
addss %xmm1, %xmm6
addss %xmm5, %xmm7
addss %xmm4, %xmm7
mulss %xmm0, %xmm0
mulss %xmm3, %xmm3
addss %xmm0, %xmm3
mulss %xmm2, %xmm2
addss %xmm3, %xmm2
movss 0x28(%r14), %xmm0
mulss %xmm0, %xmm0
subss %xmm0, %xmm2
mulss %xmm6, %xmm2
movaps %xmm7, %xmm1
mulss %xmm7, %xmm1
subss %xmm2, %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x91a1
movaps 0x27192(%rip), %xmm2    # 0x30320
xorps %xmm7, %xmm2
ucomiss %xmm0, %xmm1
movaps %xmm7, 0x20(%rsp)
jb 0x91a8
sqrtss %xmm1, %xmm0
jmp 0x91d0
xorl %eax, %eax
jmp 0x92cc
movaps %xmm1, %xmm0
movss %xmm6, 0x4(%rsp)
movaps %xmm1, 0x50(%rsp)
movaps %xmm2, 0x40(%rsp)
callq 0x6750
movaps 0x40(%rsp), %xmm2
movaps 0x50(%rsp), %xmm1
movss 0x4(%rsp), %xmm6
subss %xmm0, %xmm2
divss %xmm6, %xmm2
movss 0xc(%rsp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x91f6
ucomiss 0x8(%rsp), %xmm2
jbe 0x91f6
movss %xmm2, (%rbx)
movq %r15, %rdi
movaps %xmm2, %xmm0
jmp 0x9248
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jb 0x9204
sqrtss %xmm1, %xmm0
jmp 0x9218
movaps %xmm1, %xmm0
movss %xmm6, 0x4(%rsp)
callq 0x6750
movss 0x4(%rsp), %xmm6
movaps 0x20(%rsp), %xmm1
subss %xmm1, %xmm0
divss %xmm6, %xmm0
xorl %eax, %eax
movss 0xc(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x92cc
ucomiss 0x8(%rsp), %xmm0
jbe 0x92cc
movss %xmm0, (%rbx)
movq %r15, %rdi
callq 0x8924
leaq 0xc(%rbx), %r15
movlps %xmm0, 0xc(%rbx)
movss %xmm1, 0x14(%rbx)
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x9449
movss 0x28(%r14), %xmm2
divss %xmm2, %xmm1
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
divps %xmm2, %xmm0
leaq 0x30(%rsp), %rdi
movlps %xmm0, (%rdi)
movss %xmm1, 0x8(%rdi)
leaq 0x4(%rbx), %rsi
leaq 0x8(%rbx), %rdx
callq 0x11f9f
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x9449
movss 0x28(%r14), %xmm2
divss %xmm2, %xmm1
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
divps %xmm2, %xmm0
movlps %xmm0, 0x18(%rbx)
movss %xmm1, 0x20(%rbx)
addq $0x30, %r14
addq $0x28, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x894e
movb $0x1, %al
addq $0x60, %rsp
popq %rbx
popq %r14
popq %r15
retq