// File: /workspace/github_cpp_makefile_star2_O3/soybin[P]idyll/src/renderer.cpp
// Function: renderer::updateRotationMatrix()
void renderer::updateRotationMatrix() {
	// determine camera's rotation so that it always looks
	// towards the center of the scene regardless of its
	// position

	// pitch
	double ry = 0.0;

	// yaw
	double rx = 0.0;
	
	double x = cameraPosition.x;
	double y = cameraPosition.y;
	double z = cameraPosition.z;

	// yaw
	// to calculate the yaw axis rotation we have to think
	// about the intersection of the sphere at y height
	// because the distance towards the center will decrease
	// as we get closer to the poles. i wasted a whole day 
	// because of this
	ry = std::asin(std::abs(z) / std::sqrt(x * x + z * z));
	if (x < 0.0) {
		if (z < 0.0) {
			ry = (PI / 2.0) + ry;
		} else if (z > 0.0) {
			ry = (PI / 2.0) - ry;
		} else {
			ry = 90.0;
		}
	} else if (x > 0.0) {
		if (z < 0.0) {
			ry = -((PI / 2.0) + ry);
		} else if (z > 0.0){
			ry = -((PI / 2.0) - ry);
		} else {
			ry = -(PI / 2.0);
		}
	} else {
		if (z < 0.0) {
			ry = -PI;
		} else if (z > 0.0) {
			ry = 0.0;
		}
	}

	// pitch
	rx = std::asin(y / std::sqrt(x * x + y * y + z * z));

	// trig
	double siny = std::sin(ry);
	double cosy = std::cos(ry);
	double sinx = std::sin(rx);
	double cosx = std::cos(rx);

	// rotation matrix. 
	// there's no explicit matrix implementation because there's
	// only one use case in the domain of this program. i've
	// implemented it by having a static array of vec3s with
	// precomputed values that are multiplied by each ray 
	// direction vector at runtime.
	// https://en.wikipedia.org/wiki/Rotation_matrix
	RMy = {
		math::vec3(cosy, 0.0, siny),
		math::vec3(0.0, 1.0, 0.0),
		math::vec3(-siny, 0.0, cosy)
	};

	RMx = {
		math::vec3(1.0, 0.0, 0.0),
		math::vec3(0.0, cosx, -sinx),
		math::vec3(0.0, sinx, cosx)
	};
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x90, %rsp
movq %rdi, %rbx
movsd 0x30(%rdi), %xmm1
movsd %xmm1, -0x58(%rbp)
movsd 0x38(%rdi), %xmm0
movsd %xmm0, -0x18(%rbp)
movsd 0x40(%rdi), %xmm2
movapd 0x7e5a(%rip), %xmm0     # 0xe840
andpd %xmm2, %xmm0
movapd %xmm2, -0x30(%rbp)
mulsd %xmm2, %xmm2
mulsd %xmm1, %xmm1
movsd %xmm1, -0x40(%rbp)
movapd %xmm2, -0x50(%rbp)
addsd %xmm2, %xmm1
sqrtsd %xmm1, %xmm1
divsd %xmm1, %xmm0
callq 0x3410
movsd -0x58(%rbp), %xmm3
xorpd %xmm1, %xmm1
ucomisd %xmm3, %xmm1
jbe 0x6a39
movapd -0x30(%rbp), %xmm2
ucomisd %xmm2, %xmm1
jbe 0x6a5c
addsd 0x7e3c(%rip), %xmm0     # 0xe870
jmp 0x6abe
movapd -0x30(%rbp), %xmm2
ucomisd %xmm1, %xmm3
jbe 0x6a70
ucomisd %xmm2, %xmm1
jbe 0x6a80
addsd 0x7e1e(%rip), %xmm0     # 0xe870
xorpd 0x7df6(%rip), %xmm0     # 0xe850
jmp 0x6abe
ucomisd %xmm1, %xmm2
jbe 0x6aa0
movsd 0x7e06(%rip), %xmm1     # 0xe870
subsd %xmm0, %xmm1
jmp 0x6a9a
ucomisd %xmm2, %xmm1
jbe 0x6aaa
movsd 0x7de2(%rip), %xmm0     # 0xe860
jmp 0x6abe
ucomisd %xmm1, %xmm2
jbe 0x6ab6
movsd 0x7de2(%rip), %xmm1     # 0xe870
subsd %xmm0, %xmm1
xorpd 0x7db6(%rip), %xmm1     # 0xe850
movapd %xmm1, %xmm0
jmp 0x6abe
movsd 0x7dd0(%rip), %xmm0     # 0xe878
jmp 0x6abe
ucomisd %xmm1, %xmm2
jbe 0x6abe
xorpd %xmm0, %xmm0
jmp 0x6abe
movsd 0x7daa(%rip), %xmm0     # 0xe868
movapd %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd -0x40(%rbp), %xmm2
addsd %xmm1, %xmm2
movapd -0x50(%rbp), %xmm1
addsd %xmm2, %xmm1
sqrtsd %xmm1, %xmm1
divsd %xmm1, %xmm0
callq 0x3410
movsd %xmm0, -0x18(%rbp)
movaps -0x30(%rbp), %xmm0
callq 0x3080
movaps %xmm0, -0x40(%rbp)
movaps -0x30(%rbp), %xmm0
callq 0x34e0
movsd %xmm0, -0x50(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x3080
movaps %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x34e0
movsd %xmm0, -0x18(%rbp)
leaq -0xa0(%rbp), %r14
xorpd %xmm1, %xmm1
movq %r14, %rdi
movsd -0x50(%rbp), %xmm0
movaps -0x40(%rbp), %xmm2
callq 0x61f4
leaq -0x88(%rbp), %rdi
movsd 0x7b21(%rip), %xmm1     # 0xe678
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
callq 0x61f4
leaq -0x70(%rbp), %rdi
movaps -0x40(%rbp), %xmm0
xorps 0x7cdf(%rip), %xmm0     # 0xe850
xorps %xmm1, %xmm1
movsd -0x50(%rbp), %xmm2
callq 0x61f4
leaq 0xa8(%rbx), %rdi
leaq -0x58(%rbp), %rdx
movq %r14, %rsi
callq 0x80a4
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r14, %rdi
movsd 0x7ad6(%rip), %xmm0     # 0xe678
callq 0x61f4
leaq -0x88(%rbp), %rdi
movaps 0x7c9b(%rip), %xmm2     # 0xe850
xorps -0x30(%rbp), %xmm2
xorps %xmm0, %xmm0
movsd -0x18(%rbp), %xmm1
callq 0x61f4
leaq -0x70(%rbp), %rdi
xorps %xmm0, %xmm0
movaps -0x30(%rbp), %xmm1
movsd -0x18(%rbp), %xmm2
callq 0x61f4
addq $0x90, %rbx
leaq -0x58(%rbp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x80a4
addq $0x90, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nop