#include <array>
#include <Redacted3D/types/entity/camera.h>

std::array<GLfloat, 16> lookAt(const Vector3& eye, const Vector3& center, const Vector3& up) {
    Vector3 f = Vector3::Normalized((center - eye));
    Vector3 upN = Vector3::Normalized(up);
    Vector3 s = Vector3::Normalized(f.Cross(upN));
    Vector3 u = Vector3::Normalized(s.Cross(f));

    std::array<GLfloat, 16> result = {
            s.x,         u.x,         -f.x,        0.0f,
            s.y,         u.y,         -f.y,        0.0f,
            s.z,         u.z,         -f.z,        0.0f,
            -s.Dot(eye), -u.Dot(eye),  f.Dot(eye), 1.0f
    };
    return result;
}

void Camera::render() {
    glRotatef(getAngle().x,1.0f, 0.0f, 0.0f);
    glRotatef(getAngle().y,0.0f, 1.0f, 0.0f);
    glRotatef(getAngle().z,0.0f, 0.0f, 1.0f);

    glMultMatrixf(lookAt({position.x, position.y, position.z}, {position.x, position.y, engine->farPlane+position.z}, engine->world->upVector).data());
}

void Camera::post_render() {
    ticksAlive++;
}

/*
bool Camera::raycast(Entity *entity) {
    Vector3 rayOrigin = position;
    Vector3 rayAngle = getAngle();
}
*/