// File: alan0201tw[P]RayTracer/src/texture.cpp
// Function: image_texture::value_at_uv(float, float, vec3 const&) const
vec3 image_texture::value_at_uv(float _u, float _v, const vec3& _hit_point) const
{
    // do some repeating and fraction
    // _u *= 5.0f;
    // _v *= 5.0f;

    // _u = _u -(long)_u;
    // _v = _v -(long)_v;

    int width_index = _u * width;
    //int height_index = _v * height;
    int height_index = ( 1 - _v ) * height - 0.001f;

    width_index = std::clamp(width_index, 0, width - 1);
    height_index = std::clamp(height_index, 0, height - 1);

    float normalized_r = int(data[ height_index * width * 3 + width_index * 3 + 0 ]) / 255.0f;
    float normalized_g = int(data[ height_index * width * 3 + width_index * 3 + 1 ]) / 255.0f;
    float normalized_b = int(data[ height_index * width * 3 + width_index * 3 + 2 ]) / 255.0f;

    //std::cout << "input uv = " << _u << " , " << _v << std::endl;
    //std::cout << "width_index = " << width_index << " , height_index = " << height_index << std::endl;

    return vec3(normalized_r, normalized_g, normalized_b);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movss %xmm1, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movss -0x1c(%rbp), %xmm0
cvtsi2ssl 0x10(%rax), %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movl %ecx, -0x2c(%rbp)
movss -0x20(%rbp), %xmm1
movss 0x6e268(%rip), %xmm0    # 0x7e1fc
subss %xmm1, %xmm0
cvtsi2ssl 0x14(%rax), %xmm1
mulss %xmm1, %xmm0
movss 0x6e283(%rip), %xmm1    # 0x7e22c
addss %xmm1, %xmm0
cvttss2si %xmm0, %ecx
movl %ecx, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl 0x10(%rax), %eax
decl %eax
movl %eax, -0x38(%rbp)
leaq -0x2c(%rbp), %rdi
leaq -0x34(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x101b0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x2c(%rbp)
movl $0x0, -0x3c(%rbp)
movl 0x14(%rax), %eax
decl %eax
movl %eax, -0x40(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x101b0
movq %rax, %rcx
movq -0x68(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0x30(%rbp)
movq 0x8(%rax), %rcx
movl -0x30(%rbp), %esi
movl 0x10(%rax), %edx
imull %edx, %esi
movl %esi, %edx
leal (%rdx,%rdx,2), %edx
movl -0x2c(%rbp), %edi
movl %edi, %esi
leal (%rsi,%rsi,2), %esi
addl %esi, %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
cvtsi2ss %ecx, %xmm0
movss 0x6e1f5(%rip), %xmm1    # 0x7e230
divss %xmm1, %xmm0
movss %xmm0, -0x44(%rbp)
movq 0x8(%rax), %rdx
movl -0x30(%rbp), %esi
movl 0x10(%rax), %ecx
imull %ecx, %esi
movl %esi, %ecx
leal (%rcx,%rcx,2), %ecx
movl -0x2c(%rbp), %edi
movl %edi, %esi
leal (%rsi,%rsi,2), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movzbl 0x1(%rcx,%rdx), %ecx
cvtsi2ss %ecx, %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x48(%rbp)
movq 0x8(%rax), %rcx
movl -0x30(%rbp), %edx
movl 0x10(%rax), %eax
imull %eax, %edx
movl %edx, %eax
leal (%rax,%rax,2), %eax
movl -0x2c(%rbp), %esi
movl %esi, %edx
leal (%rdx,%rdx,2), %edx
addl %edx, %eax
cltq
movzbl 0x2(%rax,%rcx), %eax
cvtsi2ss %eax, %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x4c(%rbp)
movss -0x44(%rbp), %xmm0
movss -0x48(%rbp), %xmm1
movss -0x4c(%rbp), %xmm2
leaq -0xc(%rbp), %rdi
callq 0xf9b0
movl -0x4(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0xc(%rbp), %rax
movq %rax, -0x60(%rbp)
movsd -0x60(%rbp), %xmm0
movss -0x58(%rbp), %xmm1
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)