package me.eater.threedom.gl.dom

import me.eater.threedom.dom.IDocument
import me.eater.threedom.dom.Node
import org.joml.Matrix4d
import org.joml.Matrix4dc

class PerspectiveCamera(document: IDocument?) : Node<PerspectiveCamera>(document), ICamera<PerspectiveCamera> {
    override fun cloneSelf(): PerspectiveCamera =
        PerspectiveCamera(document)

    var fieldOfView: Double = 45.0
        set(value) {
            field = value
            updateProjection()
        }

    var far: Double = 100.0
        set(value) {
            field = value
            updateProjection()
        }

    var near: Double = 0.1
        set(value) {
            field = value
            updateProjection()
        }

    override var width: Double = 800.0
        set(value) {
            field = value
            updateProjection()
        }

    override var height: Double = 600.0
        set(value) {
            field = value
            updateProjection()
        }

    private fun updateProjection() {
        projection = makeProjection()
    }

    private fun makeProjection(): Matrix4dc = Matrix4d().perspective(fieldOfView, width / height, near, far)

    override var projection: Matrix4dc = makeProjection()
        private set

    override val view: Matrix4dc
        get() = absolute
}
