// File: /workspace/github_cpp_makefile_star2_O1/soybin[P]idyll/src/renderer.cpp
// Function: renderer::render(double, double)
math::vec3 renderer::render(double y, double x) {

	// get ray direction relative to the pixel being rendered
	// coordinates and rotate it
	math::vec3 dir = calculateRayDirection(x, y);
	math::vec3 color(0.0);

	//
	// render same pixel multiple times
	//
	for (int i = 0; i < SAMPLES; ++i) {

		//
		// data per sample
		//
		math::ray r(cameraPosition, dir);
		math::vec3 colorLeft(1.0);
		math::vec3 colorAccumulated(0.0);

		//
		// path tracing
		//
		double fdist = 0.0;
		for (int i = 0; i < BOUNCES; ++i) {

			//
			// get distance from fractal marching the ray's direction
			//
			double distance = march(r);
			if (distance == -1.0) {
				if (i == 0) {
					colorAccumulated = renderSky(y, x);
				}
				break;
			}

			//
			// record initial distance
			//
			if (i == 0) {
				fdist = distance;
			}

			//
			// get current position and normal
			//
			math::vec3 point = r.origin + r.direction * distance;
			math::vec3 normal = f->calculateNormal(point);	

			//
			// get fractal surface color
			//
			math::vec3 colorAtPoint = f->calculateColor(point);

			math::vec3 colorLighting(0.0);
			//
			// directional light
			//
			double dl = std::max(0.0, math::dot(lightDirection, normal));
			double dlShadow = 1.0;
			if (dl > 0.0) {
				dlShadow = f->calculateShadow({r.origin + r.direction * MIN_DIST, lightDirection});
			}
			colorLighting += lightColor * dl * dlShadow;

			//
			// sky light
			//
			double skyShadow = f->calculateShadow({r.origin + r.direction * MIN_DIST, brdf(r.direction, normal)});
			colorLighting += skyColor * skyShadow;

			//
			// add bounce color to sample color
			//
			colorLeft *= colorAtPoint;
			colorAccumulated += colorLeft * colorLighting;

			//
			// bounce ray
			//
			r.origin = point;
			r.direction = brdf(r.direction, normal);
		}

		//
		// scaling / grading
		//
		double ff = std::exp(-0.01 * fdist * fdist);
		colorAccumulated *= ff;
		colorAccumulated += math::vec3(0.9, 1.0, 1.0) * (1.0 - ff) *0.05;

		//
		// add clamped sample color to average color
		//
		color += math::clamp(colorAccumulated, 0.0, 1.0);
	}
	//get average of paths colors
	color /= (double)SAMPLES;

	// apply gamma correction
	color.x = pow(color.x, 0.45);
	color.y = pow(color.y, 0.45);
	color.z = pow(color.z, 0.45);

	return color * 255.0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movapd %xmm0, %xmm2
movq %rsi, %r12
movq %rdi, -0x88(%rbp)
leaq -0x208(%rbp), %rdi
movapd %xmm1, %xmm0
movsd %xmm2, -0x98(%rbp)
movapd %xmm2, %xmm1
callq 0x6cc2
leaq -0x1f0(%rbp), %rdi
xorpd %xmm0, %xmm0
callq 0x61b8
movl 0xc(%r12), %eax
testl %eax, %eax
jle 0x813f
leaq 0x30(%r12), %rax
movq %rax, -0x90(%rbp)
leaq -0xc8(%rbp), %rdx
leaq 0x48(%r12), %rax
movq %rax, -0x50(%rbp)
leaq 0x60(%r12), %rax
movq %rax, -0xa8(%rbp)
leaq 0x78(%r12), %rax
movq %rax, -0xa0(%rbp)
xorl %ecx, %ecx
movsd 0x6a7d(%rip), %xmm0     # 0xe678
leaq -0xf8(%rbp), %rbx
movl %ecx, -0x44(%rbp)
movq -0x90(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, -0xd0(%rbp)
movups (%rcx), %xmm1
movaps %xmm1, -0xe0(%rbp)
movq -0x1f8(%rbp), %rax
movq %rax, 0x10(%rdx)
movupd -0x208(%rbp), %xmm1
movupd %xmm1, (%rdx)
leaq -0x238(%rbp), %rdi
callq 0x61b8
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq 0x61b8
xorpd %xmm1, %xmm1
cmpl $0x0, 0x10(%r12)
jle 0x8062
xorpd %xmm0, %xmm0
movl $0x1, %r14d
movsd %xmm0, -0x30(%rbp)
movapd -0xe0(%rbp), %xmm0
movaps -0xd0(%rbp), %xmm1
movaps -0xc0(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
movq %r12, %rdi
callq 0x783a
movapd %xmm0, %xmm2
ucomisd 0x6b90(%rip), %xmm0     # 0xe838
jne 0x7cb0
jnp 0x8009
movapd %xmm2, %xmm0
movq %r14, -0xb0(%rbp)
cmpl $0x1, %r14d
leaq -0xc8(%rbp), %r15
je 0x7ccd
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
leaq -0x68(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movsd %xmm2, -0x40(%rbp)
movapd %xmm2, %xmm0
callq 0x627c
leaq -0x80(%rbp), %rdi
leaq -0xe0(%rbp), %r13
movq %r13, %rsi
movq %rbx, %rdx
callq 0x61e0
movq 0xc0(%r12), %rsi
movq -0x70(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x80(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8f7e
movq 0xc0(%r12), %rsi
movq -0x70(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x80(%rbp), %xmm0
movups %xmm0, (%rsp)
leaq -0x1d8(%rbp), %rdi
callq 0x8f06
xorps %xmm0, %xmm0
leaq -0x1c0(%rbp), %rdi
callq 0x61b8
movq -0x58(%rbp), %rax
movq %rax, 0x28(%rsp)
movups -0x68(%rbp), %xmm0
movups %xmm0, 0x18(%rsp)
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x10(%rsp)
movupd (%rcx), %xmm0
movupd %xmm0, (%rsp)
callq 0x64f5
movapd %xmm0, %xmm1
ucomisd 0x6af2(%rip), %xmm0     # 0xe880
movsd 0x68e2(%rip), %xmm0     # 0xe678
jbe 0x7e3c
movq 0xc0(%r12), %rbx
leaq -0x220(%rbp), %r14
movq %r14, %rdi
movq %r15, %rsi
movsd 0x6af7(%rip), %xmm0     # 0xe8b0
movsd %xmm1, -0x38(%rbp)
callq 0x627c
leaq -0x1a8(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x61e0
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rax
leaq -0x148(%rbp), %rdx
movq %rax, 0x10(%rdx)
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq -0x198(%rbp), %rax
movq %rax, -0x150(%rbp)
movups -0x1a8(%rbp), %xmm0
movaps %xmm0, -0x160(%rbp)
movaps -0x160(%rbp), %xmm0
movaps -0x150(%rbp), %xmm1
movapd -0x140(%rbp), %xmm2
movupd %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x8d74
movsd -0x38(%rbp), %xmm1
movsd %xmm0, -0x38(%rbp)
maxsd 0x6a37(%rip), %xmm1     # 0xe880
leaq -0x250(%rbp), %rbx
movq %rbx, %rdi
movq -0xa8(%rbp), %rsi
movapd %xmm1, %xmm0
callq 0x627c
leaq -0x220(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd -0x38(%rbp), %xmm0
callq 0x627c
movq %r13, %rbx
movq %r12, %r13
leaq -0x1c0(%rbp), %rdi
movq %r14, %rsi
callq 0x63a0
movq 0xc0(%r12), %r12
movq %r14, %rdi
movq %r15, %rsi
movsd 0x6a0b(%rip), %xmm0     # 0xe8b0
callq 0x627c
leaq -0x190(%rbp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x61e0
movq -0x58(%rbp), %rax
movq %rax, 0x28(%rsp)
movups -0x68(%rbp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
leaq -0x178(%rbp), %rdi
movq %r13, %rsi
callq 0x7926
movq -0x168(%rbp), %rax
leaq -0x118(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups -0x178(%rbp), %xmm0
movups %xmm0, (%rcx)
movups -0x190(%rbp), %xmm0
movaps %xmm0, -0x130(%rbp)
movq -0x180(%rbp), %rax
movq %rax, -0x120(%rbp)
movaps -0x130(%rbp), %xmm0
movaps -0x120(%rbp), %xmm1
movaps -0x110(%rbp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0x8d74
movq %r14, %rdi
movq -0xa0(%rbp), %rsi
callq 0x627c
leaq -0x1c0(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x63a0
leaq -0x238(%rbp), %rbx
movq %rbx, %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x6400
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %r12
callq 0x6248
leaq -0xf8(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x63a0
leaq -0xe0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x6310
movq -0x58(%rbp), %rax
movq %rax, 0x28(%rsp)
movups -0x68(%rbp), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movupd (%r15), %xmm0
movupd %xmm0, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x7926
movq %r15, %rdi
movq %r14, %rsi
callq 0x6310
movq -0xb0(%rbp), %r14
movsd -0x30(%rbp), %xmm1
movsd -0x40(%rbp), %xmm2
jmp 0x8040
cmpl $0x1, %r14d
jne 0x803b
leaq -0x80(%rbp), %r15
movq %r15, %rdi
movq %r12, %rsi
movsd -0x98(%rbp), %xmm0
movsd %xmm2, -0x40(%rbp)
callq 0x6e92
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6310
movsd -0x40(%rbp), %xmm2
movsd -0x30(%rbp), %xmm1
ucomisd 0x67f0(%rip), %xmm2     # 0xe838
jne 0x804c
jnp 0x8062
leal 0x1(%r14), %eax
movapd %xmm1, %xmm0
cmpl 0x10(%r12), %r14d
movl %eax, %r14d
jl 0x7c6a
movapd %xmm1, %xmm0
mulsd 0x686a(%rip), %xmm0     # 0xe8d8
mulsd %xmm1, %xmm0
callq 0x34a0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x6430
leaq -0x1d8(%rbp), %r13
movq %r13, %rdi
movsd 0x684a(%rip), %xmm0     # 0xe8e0
movsd 0x65da(%rip), %xmm1     # 0xe678
movapd %xmm1, %xmm2
callq 0x61cc
movsd 0x65c9(%rip), %xmm0     # 0xe678
subsd -0x30(%rbp), %xmm0
leaq -0x68(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x627c
leaq -0x80(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movsd 0x6813(%rip), %xmm0     # 0xe8e8
callq 0x627c
movq %rbx, %rdi
movq %r13, %rsi
callq 0x63a0
movq -0xe8(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xf8(%rbp), %xmm0
movups %xmm0, (%rsp)
xorps %xmm0, %xmm0
movq %r13, %rdi
movsd 0x656e(%rip), %xmm1     # 0xe678
callq 0x66e8
leaq -0x1f0(%rbp), %rdi
movq %r13, %rsi
callq 0x63a0
movsd 0x6552(%rip), %xmm0     # 0xe678
movl -0x44(%rbp), %ecx
incl %ecx
movl 0xc(%r12), %eax
cmpl %eax, %ecx
leaq -0xc8(%rbp), %rdx
jl 0x7c02
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
leaq -0x1f0(%rbp), %rbx
movq %rbx, %rdi
callq 0x648a
movsd (%rbx), %xmm0
movsd 0x678f(%rip), %xmm1     # 0xe8f0
callq 0x3480
movsd %xmm0, (%rbx)
movsd 0x8(%rbx), %xmm0
movsd 0x6779(%rip), %xmm1     # 0xe8f0
callq 0x3480
movsd %xmm0, 0x8(%rbx)
movsd 0x10(%rbx), %xmm0
movsd 0x6762(%rip), %xmm1     # 0xe8f0
callq 0x3480
movsd %xmm0, 0x10(%rbx)
movsd 0x6758(%rip), %xmm0     # 0xe8f8
movq -0x88(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x627c
movq %r14, %rax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop