// File: GUI-Graphics[P]cpuraytracing/dielectric.cpp
// Function: gui::Dielectric::scatter(gui::Ray const&, gui::Intersection const&, gui::Vector3&, gui::Ray&) const
bool Dielectric::scatter(
		const Ray& in,
		const Intersection& intersection,
		Vector3& attenuation,
		Ray& scattered) const {

		attenuation = Vector3(1);

		float niOverNt;
		Vector3 normal;

		if (in.direction.dot(intersection.normal) > 0) {
			niOverNt = ior;
			normal = -intersection.normal;
		} else {
			niOverNt = 1 / ior;
			normal = intersection.normal;
		}

		float cosine = -in.direction.dot(normal) / in.direction.length();

		Vector3 refracted;
		Vector3 reflected = in.direction.reflect(normal);

		if (in.direction.refract(normal, niOverNt, refracted)) {
			if (Math::random() < schlick(cosine, niOverNt)) {
				scattered.direction = reflected;
			} else {
				scattered.direction = refracted;
			}
		} else {
			scattered.direction = reflected;
		}

		scattered.origin = intersection.position + scattered.direction * 0.0001f;

		return true;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq -0x34(%rbp), %rdi
movss 0x188e(%rip), %xmm0     # 0x8004
callq 0x3070
movq -0x20(%rbp), %rax
movq -0x34(%rbp), %rcx
movq %rcx, (%rax)
movl -0x2c(%rbp), %ecx
movl %ecx, 0x8(%rax)
leaq -0x44(%rbp), %rdi
callq 0x47c0
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
movq -0x18(%rbp), %rsi
addq $0x10, %rsi
callq 0x6000
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x67f8
movq -0xd0(%rbp), %rax
movss 0x8(%rax), %xmm0
movss %xmm0, -0x38(%rbp)
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
callq 0x6aa0
movss %xmm1, -0x58(%rbp)
movlpd %xmm0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x44(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x6823
movq -0xd0(%rbp), %rax
movss 0x17fd(%rip), %xmm0     # 0x8004
divss 0x8(%rax), %xmm0
movss %xmm0, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq %rcx, -0x44(%rbp)
movl 0x18(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
leaq -0x44(%rbp), %rsi
movq %rsi, -0xd8(%rbp)
callq 0x6000
movaps 0x18ee(%rip), %xmm1     # 0x8130
pxor %xmm1, %xmm0
movss %xmm0, -0xdc(%rbp)
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
callq 0x48a0
movaps %xmm0, %xmm1
movss -0xdc(%rbp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x64(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x47c0
movq -0xd8(%rbp), %rsi
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
callq 0x6620
movss %xmm1, -0x88(%rbp)
movlpd %xmm0, -0x90(%rbp)
movq -0x90(%rbp), %rax
movq %rax, -0x7c(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
movss -0x38(%rbp), %xmm0
leaq -0x44(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x6b00
testb $0x1, %al
jne 0x68d0
jmp 0x6928
callq 0x2e90
movq -0xd0(%rbp), %rdi
movss %xmm0, -0xe0(%rbp)
movss -0x64(%rbp), %xmm0
movss -0x38(%rbp), %xmm1
callq 0x69f0
movss -0xe0(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x6914
movq -0x28(%rbp), %rax
movq -0x7c(%rbp), %rcx
movq %rcx, 0xc(%rax)
movl -0x74(%rbp), %ecx
movl %ecx, 0x14(%rax)
jmp 0x6926
movq -0x28(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, 0xc(%rax)
movl -0x68(%rbp), %ecx
movl %ecx, 0x14(%rax)
jmp 0x693a
movq -0x28(%rbp), %rax
movq -0x7c(%rbp), %rcx
movq %rcx, 0xc(%rax)
movl -0x74(%rbp), %ecx
movl %ecx, 0x14(%rax)
movq -0x18(%rbp), %rax
addq $0x4, %rax
movq %rax, -0xe8(%rbp)
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
movss 0x17cf(%rip), %xmm0     # 0x8128
callq 0x47d0
movq -0xe8(%rbp), %rdi
movss %xmm1, -0xb0(%rbp)
movlpd %xmm0, -0xb8(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rsi
callq 0x5970
movss %xmm1, -0xc0(%rbp)
movlpd %xmm0, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x9c(%rbp)
movl -0xc0(%rbp), %eax
movl %eax, -0x94(%rbp)
movq -0x28(%rbp), %rax
movq -0x9c(%rbp), %rcx
movq %rcx, (%rax)
movl -0x94(%rbp), %ecx
movl %ecx, 0x8(%rax)
movb $0x1, %al
andb $0x1, %al
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)