package me.eater.threedom.example.shader

import me.eater.threedom.gl.Shader
import me.eater.threedom.gl.ShaderPreProcessor
import me.eater.threedom.gl.ShaderProgram

class Plain private constructor(vertexShader: Shader, fragmentShader: Shader) :
    ShaderProgram(vertexShader, fragmentShader) {

    var objectColor by vec3()
    var lightColor by vec3()
    var lightPos by vec3()
    var viewPos by vec3()

    companion object {
        fun load(preProcessor: ShaderPreProcessor): Plain {
            val vertex = preProcessor.compile(
                this::class.java.getResourceAsStream("/shaders/plain/vertex.glsl").readAllBytes()
                    .toString(Charsets.UTF_8),
                Shader.ShaderType.Vertex
            )

            val fragment = preProcessor.compile(
                this::class.java.getResourceAsStream("/shaders/plain/fragment.glsl").readAllBytes()
                    .toString(Charsets.UTF_8),
                Shader.ShaderType.Fragment

            )
            return Plain(
                vertex,
                fragment
            ).also {
                vertex.delete()
                fragment.delete()
            }
        }

        fun lamp(preProcessor: ShaderPreProcessor): Plain {
            val vertex = preProcessor.compile(
                this::class.java.getResourceAsStream("/shaders/plain/vertex.glsl").readAllBytes()
                    .toString(Charsets.UTF_8),
                Shader.ShaderType.Vertex
            )

            val fragment = preProcessor.compile(
                this::class.java.getResourceAsStream("/shaders/plain/fragment_lamp.glsl").readAllBytes()
                    .toString(Charsets.UTF_8),
                Shader.ShaderType.Fragment

            )
            return Plain(
                vertex,
                fragment
            ).also {
                vertex.delete()
                fragment.delete()
            }
        }
    }
}
