ICT290 / src / engine / light.cpp
light.cpp
Raw
#include "light.h"
#include "../glIncludes.h"

Light::Light(short id, const glm::vec3& position_) : m_lightID(id) {
    m_position[0] = position_.x;
    m_position[1] = position_.y;
    m_position[2] = position_.z;
}

Light::Light(short id, float x, float y, float z) : m_lightID(id) {
    m_position[0] = x;
    m_position[1] = y;
    m_position[2] = z;
}

void Light::enable() {
    assert(m_lightID >= 0 && m_lightID <= 7);
    glEnable(GL_LIGHT0 + m_lightID);

    m_enabled = true;

    if (m_isSpotLight) {
        glLightf(GL_LIGHT0 + m_lightID, GL_SPOT_EXPONENT, m_spotLight.exponent);
        glLightf(GL_LIGHT0 + m_lightID, GL_SPOT_CUTOFF, m_spotLight.cutoff);
        glLightfv(GL_LIGHT0 + m_lightID,
                  GL_SPOT_DIRECTION,
                  m_spotLight.direction);
    }

    glLightfv(GL_LIGHT0 + m_lightID, GL_POSITION, m_position);
    glLightfv(GL_LIGHT0 + m_lightID, GL_AMBIENT, m_ambient);
    glLightfv(GL_LIGHT0 + m_lightID, GL_DIFFUSE, m_diffuse);
    glLightfv(GL_LIGHT0 + m_lightID, GL_SPECULAR, m_specular);
}

void Light::disable() {
    m_enabled = false;
    glLightf(GL_LIGHT0 + m_lightID, GL_SPOT_CUTOFF, 180);
    glDisable(GL_LIGHT0 + m_lightID);
}

void Light::enableLighting() {
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}

void Light::disableLighting() {
    for (int i = 0; i < 8; ++i) {
        glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, 180);
        glDisable(GL_LIGHT0 + i);
    }
    glDisable(GL_LIGHTING);
    glDisable(GL_COLOR_MATERIAL);
}

void Light::setPosition(const glm::vec3& location) {
    m_position[0] = location.x;
    m_position[1] = location.y;
    m_position[2] = location.z;
}

void Light::setPosition(float x, float y, float z) {
    m_position[0] = x;
    m_position[1] = y;
    m_position[2] = z;
}

void Light::setSpotDirection(const glm::vec3& direction) {
    m_spotLight.direction[0] = direction.x;
    m_spotLight.direction[1] = direction.y;
    m_spotLight.direction[2] = direction.z;
}

void Light::setSpotDirection(float x, float y, float z) {
    m_spotLight.direction[0] = x;
    m_spotLight.direction[1] = y;
    m_spotLight.direction[2] = z;
}

void Light::setSpotDirectionToLocation(const glm::vec3& rotateTo) {
    glm::vec3 vec = rotateTo
                    - glm::vec3(m_position[0], m_position[1], m_position[2]);

    m_spotLight.direction[0] = vec.x;
    m_spotLight.direction[1] = vec.y;
    m_spotLight.direction[2] = vec.z;
}

void Light::draw() const {
    if (m_isSpotLight) {
        glLightf(GL_LIGHT0 + m_lightID, GL_SPOT_EXPONENT, m_spotLight.exponent);
        glLightf(GL_LIGHT0 + m_lightID, GL_SPOT_CUTOFF, m_spotLight.cutoff);
        glLightfv(GL_LIGHT0 + m_lightID,
                  GL_SPOT_DIRECTION,
                  m_spotLight.direction);
    }

    glLightfv(GL_LIGHT0 + m_lightID, GL_POSITION, m_position);
}

glm::vec3 Light::getPositionVec3() {
    return glm::vec3{m_position[0], m_position[1], m_position[2]};
}