QVector<quint16> PhongPixelProcessor::IlluminatePixel()
{
    qreal temp;
    quint8 channel = 0;
    const quint8 totalChannels = 3; // The 4th is alpha and we'll fill it with a nice 0xFFFF
    qreal computation[] = {0, 0, 0};
    QVector<quint16> finalPixel(4, 0xFFFF);

    if (lightSources.size() == 0)
        return finalPixel;

    // PREPARE ALGORITHM HERE

    for (int i = 0; i < size; i++) {
        light_vector = lightSources.at(i).lightVector;

        for (channel = 0; channel < totalChannels; channel++) {
            Ia = lightSources.at(i).RGBvalue.at(channel) * Ka;
            computation[channel] += Ia;
        }
        if (diffuseLightIsEnabled) {
            temp = Kd * QVector3D::dotProduct(normal_vector, light_vector);
            for (channel = 0; channel < totalChannels; channel++) {
                Id = lightSources.at(i).RGBvalue.at(channel) * temp;
                if (Id < 0)     Id = 0;
                if (Id > 1)     Id = 1;
                computation[channel] += Id;
            }
        }

        if (specularLightIsEnabled) {
            reflection_vector = (2 * pow(QVector3D::dotProduct(normal_vector, light_vector), shiny_exp)) * normal_vector - light_vector;
            temp = Ks * QVector3D::dotProduct(vision_vector, reflection_vector);
            for (channel = 0; channel < totalChannels; channel++) {
                Is = lightSources.at(i).RGBvalue.at(channel) * temp;
                if (Is < 0)     Is = 0;
                if (Is > 1)     Is = 1;
                computation[channel] += Is;
            }
        }
    }

    for (channel = 0; channel < totalChannels; channel++) {
        if (computation[channel] > 1)
            computation[channel] = 1;
        if (computation[channel] < 0)
            computation[channel] = 0;
    }

    //RGBA actually uses the BGRA order of channels, hence the disorder
    finalPixel[2] = quint16(computation[0] * 0xFFFF);
    finalPixel[1] = quint16(computation[1] * 0xFFFF);
    finalPixel[0] = quint16(computation[2] * 0xFFFF);
    
    return finalPixel;
 }
QVector<quint16> PhongPixelProcessor::IlluminatePixelFromHeightmap(quint32 posup, quint32 posdown, quint32 posleft, quint32 posright)
{
    QVector<quint16> finalPixel(4, 0xFFFF);

    if (lightSources.size() == 0)
        return finalPixel;

    // Algorithm begins, Phong Illumination Model
    normal_vector.setX(- realheightmap[posright] + realheightmap[posleft]);
    normal_vector.setY(- realheightmap[posup] + realheightmap[posdown]);
    normal_vector.setZ(8);
    normal_vector.normalize();

    // PREPARE ALGORITHM HERE

    finalPixel = IlluminatePixel();
    
    return finalPixel;
 }
QVector<quint16> PhongPixelProcessor::IlluminatePixelFromNormalmap(qreal r, qreal g, qreal b)
 {
    QVector<quint16> finalPixel(4, 0xFFFF);

    if (lightSources.size() == 0)
        return finalPixel;
    
    //  if ()
    // Algorithm begins, Phong Illumination Model
    normal_vector.setX(r*2-1.0);
    normal_vector.setY(-(g*2-1.0));
    normal_vector.setZ(b*2-1.0);
    //normal_vector.normalize();

    // PREPARE ALGORITHM HERE

    finalPixel = IlluminatePixel();
    
    return finalPixel;
 }
Exemple #4
0
QVector<quint16> PhongPixelProcessor::testingHeightmapIlluminatePixel(quint32 posup, quint32 posdown, quint32 posleft, quint32 posright)
{
    qreal temp;
    quint8 channel = 0;
    const quint8 totalChannels = 3; // The 4th is alpha and we'll fill it with a nice 0xFFFF
    qreal computation[] = {0, 0, 0};
    //QColor pixelColor(0, 0, 0);
    QVector<quint16> finalPixel(4, 0xFFFF);

    if (lightSources.size() == 0)
        return finalPixel;

    // Algorithm begins, Phong Illumination Model
    normal_vector.setX(- realheightmap[posright] + realheightmap[posleft]);
    normal_vector.setY(- realheightmap[posup] + realheightmap[posdown]);
    normal_vector.setZ(8);
    normal_vector.normalize();

    /*
    for (int i = 0; i < size; i++) {
        temp = QVector3D::dotProduct(normal_vector, lightSources.at(i).lightVector);
        for (int channel = 0; channel < totalChannels; channel++) {
            // I = each RGB value
            Id = fastLight.RGBvalue[channel] * temp;
            if (Id < 0)     Id = 0;
            if (Id > 1)     Id = 1;
            computation[channel] += Id;
        }
    }
    */
    // PREPARE ALGORITHM HERE

    for (int i = 0; i < size; i++) {
        light_vector = lightSources.at(i).lightVector;

        for (channel = 0; channel < totalChannels; channel++) {
            Ia = lightSources.at(i).RGBvalue.at(channel) * Ka;
            computation[channel] += Ia;
        }
        if (diffuseLightIsEnabled) {
            temp = Kd * QVector3D::dotProduct(normal_vector, light_vector);
            for (channel = 0; channel < totalChannels; channel++) {
                Id = lightSources.at(i).RGBvalue.at(channel) * temp;
                if (Id < 0)     Id = 0;
                if (Id > 1)     Id = 1;
                computation[channel] += Id;
            }
        }

        if (specularLightIsEnabled) {
            reflection_vector = (2 * pow(QVector3D::dotProduct(normal_vector, light_vector), shiny_exp)) * normal_vector - light_vector;
            temp = Ks * QVector3D::dotProduct(vision_vector, reflection_vector);
            for (channel = 0; channel < totalChannels; channel++) {
                Is = lightSources.at(i).RGBvalue.at(channel) * temp;
                if (Is < 0)     Is = 0;
                if (Is > 1)     Is = 1;
                computation[channel] += Is;
            }
        }
    }

    for (channel = 0; channel < totalChannels; channel++) {
        if (computation[channel] > 1)
            computation[channel] = 1;
        if (computation[channel] < 0)
            computation[channel] = 0;
    }

    //RGBA actually uses the BGRA order of channels, hence the disorder
    finalPixel[2] = quint16(computation[0] * 0xFFFF);
    finalPixel[1] = quint16(computation[1] * 0xFFFF);
    finalPixel[0] = quint16(computation[2] * 0xFFFF);
    
    return finalPixel;
    /*
    pixelColor.setRedF(computation[0]);
    pixelColor.setGreenF(computation[1]);
    pixelColor.setBlueF(computation[2]);

    return pixelColor.rgb();
    */
}