#include "SkyProgram.hpp"

#include "../Renderer.hpp"

using namespace pass;

SkyProgram::SkyProgram(): Program(), CubeBuffer(render::gl::Shape::SKY_CUBE) {

    std::vector<std::string> flags;
    std::vector<Shader*> shaders;
    shaders.push_back(loadShader(GL_VERTEX_SHADER, flags));
    shaders.push_back(loadShader(GL_FRAGMENT_SHADER, flags));
    load(shaders);

    ViewMatrixID = glGetUniformLocation(ProgramID, "View");
    ProjectionMatrixID = glGetUniformLocation(ProgramID, "Projection");
    TextureID = glGetUniformLocation(ProgramID, "Texture");
}

SkyProgram::~SkyProgram() { }

std::string SkyProgram::getName() const {
    return "Sky";
}
void SkyProgram::start(render::gl::Renderer *renderer) {
    const auto fixedView = glm::mat4(glm::mat3(renderer->getViewMatrix()));
    setView(&fixedView[0][0]);
    setProjection(&renderer->getProjectionMatrix()[0][0]);

    bindTexture(renderer->getSkyTexture());
}
void SkyProgram::draw(render::gl::Renderer *renderer) {
    useIt();
    start(renderer);
    CubeBuffer.draw(GL_TRIANGLES);
}

void SkyProgram::setView(const GLfloat *matrix) {
    glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, matrix);
}
void SkyProgram::setProjection(const GLfloat *matrix) {
    glUniformMatrix4fv(ProjectionMatrixID, 1, GL_FALSE, matrix);
}

void SkyProgram::bindTexture(GLuint textureID) {
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    glUniform1i(TextureID, 0);
}
