// File: /workspace/github_cpp_makefile_star2_O2/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 0x644e(%rip), %xmm0     # 0xc880
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 0x3400
movsd -0x58(%rbp), %xmm3
xorpd %xmm1, %xmm1
ucomisd %xmm3, %xmm1
jbe 0x6485
movapd -0x30(%rbp), %xmm2
ucomisd %xmm2, %xmm1
jbe 0x64a8
addsd 0x6430(%rip), %xmm0     # 0xc8b0
jmp 0x650a
movapd -0x30(%rbp), %xmm2
ucomisd %xmm1, %xmm3
jbe 0x64bc
ucomisd %xmm2, %xmm1
jbe 0x64cc
addsd 0x6412(%rip), %xmm0     # 0xc8b0
xorpd 0x63ea(%rip), %xmm0     # 0xc890
jmp 0x650a
ucomisd %xmm1, %xmm2
jbe 0x64ec
movsd 0x63fa(%rip), %xmm1     # 0xc8b0
subsd %xmm0, %xmm1
jmp 0x64e6
ucomisd %xmm2, %xmm1
jbe 0x64f6
movsd 0x63d6(%rip), %xmm0     # 0xc8a0
jmp 0x650a
ucomisd %xmm1, %xmm2
jbe 0x6502
movsd 0x63d6(%rip), %xmm1     # 0xc8b0
subsd %xmm0, %xmm1
xorpd 0x63aa(%rip), %xmm1     # 0xc890
movapd %xmm1, %xmm0
jmp 0x650a
movsd 0x63c4(%rip), %xmm0     # 0xc8b8
jmp 0x650a
ucomisd %xmm1, %xmm2
jbe 0x650a
xorpd %xmm0, %xmm0
jmp 0x650a
movsd 0x639e(%rip), %xmm0     # 0xc8a8
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 0x3400
movsd %xmm0, -0x18(%rbp)
movaps -0x30(%rbp), %xmm0
callq 0x3090
movaps %xmm0, -0x40(%rbp)
movaps -0x30(%rbp), %xmm0
callq 0x34a0
movsd %xmm0, -0x50(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x3090
movaps %xmm0, -0x30(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x34a0
movsd %xmm0, -0x18(%rbp)
leaq -0xa0(%rbp), %r14
xorpd %xmm1, %xmm1
movq %r14, %rdi
movsd -0x50(%rbp), %xmm0
movaps -0x40(%rbp), %xmm2
callq 0x5c46
leaq -0x88(%rbp), %rdi
movsd 0x60d5(%rip), %xmm1     # 0xc678
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
callq 0x5c46
leaq -0x70(%rbp), %rdi
movaps -0x40(%rbp), %xmm0
xorps 0x62d3(%rip), %xmm0     # 0xc890
xorps %xmm1, %xmm1
movsd -0x50(%rbp), %xmm2
callq 0x5c46
leaq 0xa8(%rbx), %rdi
leaq -0x58(%rbp), %rdx
movq %r14, %rsi
callq 0x78a2
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r14, %rdi
movsd 0x608a(%rip), %xmm0     # 0xc678
callq 0x5c46
leaq -0x88(%rbp), %rdi
movaps 0x628f(%rip), %xmm2     # 0xc890
xorps -0x30(%rbp), %xmm2
xorps %xmm0, %xmm0
movsd -0x18(%rbp), %xmm1
callq 0x5c46
leaq -0x70(%rbp), %rdi
xorps %xmm0, %xmm0
movaps -0x30(%rbp), %xmm1
movsd -0x18(%rbp), %xmm2
callq 0x5c46
addq $0x90, %rbx
leaq -0x58(%rbp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x78a2
addq $0x90, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nop