// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb0, %rsp
movq %rdi, %rbx
movsd 0x30(%rdi), %xmm2
movsd 0x38(%rdi), %xmm0
movsd %xmm0, -0x50(%rbp)
movsd 0x40(%rdi), %xmm1
movapd 0x7de3(%rip), %xmm3     # 0xe840
andpd %xmm1, %xmm3
movapd %xmm1, -0x30(%rbp)
mulsd %xmm1, %xmm1
movsd %xmm2, -0x18(%rbp)
movapd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, -0x60(%rbp)
addsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm1, -0x40(%rbp)
jb 0x6a95
sqrtsd %xmm0, %xmm0
jmp 0x6aa4
movapd %xmm3, -0x70(%rbp)
callq 0x3220
movapd -0x70(%rbp), %xmm3
divsd %xmm0, %xmm3
movapd %xmm3, %xmm0
callq 0x3410
movapd %xmm0, %xmm2
movsd -0x18(%rbp), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x6ae1
movapd -0x30(%rbp), %xmm3
ucomisd %xmm3, %xmm1
movapd -0x40(%rbp), %xmm0
jbe 0x6b09
addsd 0x7d94(%rip), %xmm2     # 0xe870
jmp 0x6b6b
ucomisd %xmm1, %xmm0
movapd -0x40(%rbp), %xmm0
movapd -0x30(%rbp), %xmm3
jbe 0x6b1d
ucomisd %xmm3, %xmm1
jbe 0x6b2d
addsd 0x7d71(%rip), %xmm2     # 0xe870
xorpd 0x7d49(%rip), %xmm2     # 0xe850
jmp 0x6b6b
ucomisd %xmm1, %xmm3
jbe 0x6b4d
movsd 0x7d59(%rip), %xmm1     # 0xe870
subsd %xmm2, %xmm1
jmp 0x6b47
ucomisd %xmm3, %xmm1
jbe 0x6b57
movsd 0x7d35(%rip), %xmm2     # 0xe860
jmp 0x6b6b
ucomisd %xmm1, %xmm3
jbe 0x6b63
movsd 0x7d35(%rip), %xmm1     # 0xe870
subsd %xmm2, %xmm1
xorpd 0x7d09(%rip), %xmm1     # 0xe850
movapd %xmm1, %xmm2
jmp 0x6b6b
movsd 0x7d23(%rip), %xmm2     # 0xe878
jmp 0x6b6b
ucomisd %xmm1, %xmm3
jbe 0x6b6b
xorpd %xmm2, %xmm2
jmp 0x6b6b
movsd 0x7cfd(%rip), %xmm2     # 0xe868
movapd %xmm2, -0x30(%rbp)
movsd -0x50(%rbp), %xmm1
mulsd %xmm1, %xmm1
movsd -0x60(%rbp), %xmm2
addsd %xmm1, %xmm2
addsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x6b96
sqrtsd %xmm0, %xmm0
jmp 0x6b9b
callq 0x3220
movsd -0x50(%rbp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x3410
movsd %xmm0, -0x18(%rbp)
movaps -0x30(%rbp), %xmm0
callq 0x3080
movaps %xmm0, -0x60(%rbp)
movaps -0x30(%rbp), %xmm0
callq 0x34e0
movsd %xmm0, -0x40(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x3080
movaps %xmm0, -0x50(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x34e0
movsd %xmm0, -0x30(%rbp)
leaq -0xb8(%rbp), %r14
xorpd %xmm1, %xmm1
movq %r14, %rdi
movsd -0x40(%rbp), %xmm0
movaps -0x60(%rbp), %xmm2
callq 0x61cc
leaq -0xa0(%rbp), %rdi
movsd 0x7a63(%rip), %xmm1     # 0xe678
xorps %xmm0, %xmm0
xorps %xmm2, %xmm2
callq 0x61cc
leaq -0x88(%rbp), %rdi
movaps -0x60(%rbp), %xmm0
xorps 0x7c1e(%rip), %xmm0     # 0xe850
xorps %xmm1, %xmm1
movsd -0x40(%rbp), %xmm2
callq 0x61cc
leaq 0xa8(%rbx), %rdi
leaq -0x70(%rbp), %rdx
movq %r14, %rsi
callq 0x8248
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r14, %rdi
movsd 0x7a15(%rip), %xmm0     # 0xe678
callq 0x61cc
leaq -0xa0(%rbp), %rdi
movaps 0x7bda(%rip), %xmm2     # 0xe850
xorps -0x50(%rbp), %xmm2
xorps %xmm0, %xmm0
movsd -0x30(%rbp), %xmm1
callq 0x61cc
leaq -0x88(%rbp), %rdi
xorps %xmm0, %xmm0
movaps -0x50(%rbp), %xmm1
movsd -0x30(%rbp), %xmm2
callq 0x61cc
addq $0x90, %rbx
leaq -0x70(%rbp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8248
addq $0xb0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
nop