// File: logicx24[P]RayTracer/raytracer.cpp
// Function: Scene::traceRay(Ray&, int)
vec3 traceRay(Ray &testRay, int depth) {
		Polygon *hitObject = NULL;
    	float min_dist = MAXFLOAT;
		vec3 minIntersection;
    	Ray ray;
		for (int i = 0; i < objects.size(); i++) {
			ray = objects[i]->transformRay(testRay);
			float t = objects[i]->intersection(ray, eps);
			vec3 intersection = ray.pointAt(t);
			intersection = objects[i]->transformIntersection(intersection);
			float d = distance(ray.pos, intersection);
            if (t != MAXFLOAT && d < min_dist) {
                min_dist = d;
                hitObject = objects[i];
                minIntersection = intersection;
		    }
		}
		if (hitObject != NULL) {
			//minIntersection = hitObject->inverseTransform * minIntersection;
            vec3 normal = hitObject->normal(hitObject->inverseTransform * minIntersection);
            normal = hitObject->transformNormal(normal);
            normal.normalize();
            vec3 reflectionVec = ray.vec - (2*(ray.vec * normal))*normal;
            reflectionVec.normalize();
            Ray reflected = Ray(minIntersection, reflectionVec);
            if (depth == 0) {
            	return phongShade(ray, hitObject, normal, minIntersection);
            } else {
        	    return phongShade(ray, hitObject, normal, minIntersection)
        	           + multiplyComponents(traceRay(reflected, depth - 1), hitObject->mat.kr);
		    }
		} else {
		    return vec3(0, 0, 0);

        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x228(%rbp)
movq %rdi, %rax
movq %rax, -0x220(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x218(%rbp)
movq $0x0, -0x28(%rbp)
movss 0x1748a(%rip), %xmm0    # 0x25030
movss %xmm0, -0x2c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x8300
leaq -0x80(%rbp), %rdi
callq 0xe1e0
movl $0x0, -0x84(%rbp)
movq -0x218(%rbp), %rdi
movslq -0x84(%rbp), %rax
movq %rax, -0x230(%rbp)
addq $0xa0, %rdi
callq 0x9390
movq %rax, %rcx
movq -0x230(%rbp), %rax
cmpq %rcx, %rax
jae 0xdd6b
movq -0x218(%rbp), %rdi
addq $0xa0, %rdi
movslq -0x84(%rbp), %rsi
callq 0x93e0
movq (%rax), %rsi
movq -0x18(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0xe250
leaq -0x80(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0xe360
movq -0x218(%rbp), %rdi
addq $0xa0, %rdi
movslq -0x84(%rbp), %rsi
callq 0x93e0
movq %rax, %rcx
movq -0x218(%rbp), %rax
movq (%rcx), %rdi
movss 0x114(%rax), %xmm0
movq (%rdi), %rax
leaq -0x80(%rbp), %rsi
callq *0x8(%rax)
movss %xmm0, -0xc4(%rbp)
movss -0xc4(%rbp), %xmm0
leaq -0xe0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xaf60
movq -0x218(%rbp), %rdi
addq $0xa0, %rdi
movslq -0x84(%rbp), %rsi
callq 0x93e0
movq (%rax), %rsi
leaq -0xf8(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
callq 0xe3b0
leaq -0xe0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x8590
leaq -0x80(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x37a0
movss %xmm0, -0xfc(%rbp)
movss -0xc4(%rbp), %xmm0
movss 0x17334(%rip), %xmm1    # 0x25030
ucomiss %xmm1, %xmm0
jne 0xdd05
jp 0xdd05
jmp 0xdd55
movss -0xfc(%rbp), %xmm1
movss -0x2c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xdd55
movq -0x218(%rbp), %rdi
movss -0xfc(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
addq $0xa0, %rdi
movslq -0x84(%rbp), %rsi
callq 0x93e0
movq (%rax), %rax
movq %rax, -0x28(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x8590
jmp 0xdd57
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xdbc7
cmpq $0x0, -0x28(%rbp)
je 0xdf34
movq -0x28(%rbp), %rax
movq %rax, -0x240(%rbp)
movq -0x28(%rbp), %rsi
addq $0xf0, %rsi
leaq -0x130(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0xe430
movq -0x240(%rbp), %rsi
movq (%rsi), %rax
leaq -0x118(%rbp), %rdi
leaq -0x130(%rbp), %rdx
callq *0x10(%rax)
movq -0x28(%rbp), %rsi
leaq -0x148(%rbp), %rdi
leaq -0x118(%rbp), %rdx
callq 0xe490
leaq -0x118(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0x8590
leaq -0x118(%rbp), %rdi
callq 0xaeb0
leaq -0x80(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x238(%rbp)
leaq -0x80(%rbp), %rdi
addq $0x18, %rdi
leaq -0x118(%rbp), %rsi
callq 0xaee0
movaps %xmm0, %xmm1
movsd 0x1720d(%rip), %xmm0    # 0x25028
mulsd %xmm1, %xmm0
leaq -0x178(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0xb0d0
movq -0x238(%rbp), %rsi
leaq -0x160(%rbp), %rdi
leaq -0x178(%rbp), %rdx
callq 0xa040
leaq -0x160(%rbp), %rdi
callq 0xaeb0
leaq -0x1b0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0xe170
cmpl $0x0, -0x1c(%rbp)
jne 0xdea0
movq -0x218(%rbp), %rsi
movq -0x228(%rbp), %rdi
movq -0x28(%rbp), %rcx
leaq -0x80(%rbp), %rdx
leaq -0x118(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0xe510
jmp 0xdf49
movq -0x218(%rbp), %rsi
movq -0x28(%rbp), %rcx
leaq -0x1c8(%rbp), %rdi
leaq -0x80(%rbp), %rdx
leaq -0x118(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0xe510
movq -0x218(%rbp), %rsi
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
leaq -0x1f8(%rbp), %rdi
leaq -0x1b0(%rbp), %rdx
callq 0xdb60
movq -0x28(%rbp), %rsi
addq $0x8, %rsi
addq $0x48, %rsi
leaq -0x210(%rbp), %rdi
callq 0x7e40
leaq -0x1e0(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
leaq -0x210(%rbp), %rdx
callq 0x3890
movq -0x228(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
leaq -0x1e0(%rbp), %rdx
callq 0xb070
jmp 0xdf49
movq -0x228(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x6860
movq -0x220(%rbp), %rax
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
nopl (%rax)