Example #1
0
void ApplyTileLights(EERIEPOLY * ep, const Vec2s & pos)
{

    Color3f lightInfraFactor = Color3f::white;
    if(player.m_improve) {
        lightInfraFactor.r = 4.f;
    }

    TILE_LIGHTS * tls = &tilelights[pos.x][pos.y];
    size_t nbvert = (ep->type & POLY_QUAD) ? 4 : 3;

    for(size_t j = 0; j < nbvert; j++) {

        if(tls->el.empty()) {
            ep->tv[j].color = ep->v[j].color;
            continue;
        }

        Color3f tempColor;
        Color c = Color::fromRGBA(ep->v[j].color);
        tempColor.r = c.r;
        tempColor.g = c.g;
        tempColor.b = c.b;

        Vec3f & position = ep->v[j].p;
        Vec3f & normal = ep->nrml[j];

        for(size_t i = 0; i < tls->el.size(); i++) {
            EERIE_LIGHT * light = tls->el[i];

            Vec3f vLight = glm::normalize(light->pos - position);

            float cosangle = glm::dot(normal, vLight);

            if(cosangle > 0.f) {
                float distance = fdist(light->pos, position);

                if(distance <= light->fallstart) {
                    cosangle *= light->intensity * GLOBAL_LIGHT_FACTOR;
                } else {
                    float p = ((light->fallend - distance) * light->falldiffmul);

                    if(p <= 0.f)
                        cosangle = 0.f;
                    else
                        cosangle *= p * (light->intensity * GLOBAL_LIGHT_FACTOR);
                }
                cosangle *= 0.5f;

                tempColor += light->rgb255 * lightInfraFactor * cosangle;
            }
        }

        u8 ir = clipByte255(tempColor.r);
        u8 ig = clipByte255(tempColor.g);
        u8 ib = clipByte255(tempColor.b);
        ep->tv[j].color = Color(ir, ig, ib, 255).toRGBA();
    }
}
ColorRGBA ApplyLight(const ShaderLight lights[],
                     const int lightsCount,
                     const glm::quat & quat,
                     const Vec3f & position,
                     const Vec3f & normal,
                     const ColorMod & colorMod,
                     float materialDiffuse
) {
	Color3f tempColor = colorMod.ambientColor;
	
	glm::quat inv = glm::inverse(quat);
	
	// Dynamic lights
	for(int l = 0; l != lightsCount; l++) {
		const ShaderLight & light = lights[l];
		
		Vec3f vLight = glm::normalize(light.pos - position);
		
		Vec3f Cur_vLights = inv * vLight;
		
		float cosangle = glm::dot(normal, Cur_vLights);
		
		// If light visible
		if(cosangle > 0.f) {
			float distance = fdist(position, light.pos);
			
			// Evaluate its intensity depending on the distance Light<->Object
			if(distance <= light.fallstart) {
				cosangle *= light.intensity * GLOBAL_LIGHT_FACTOR;
			} else {
				float p = ((light.fallend - distance) * light.falldiffmul);
				
				if(p <= 0.f)
					cosangle = 0.f;
				else
					cosangle *= p * (light.intensity * GLOBAL_LIGHT_FACTOR);
			}
			
			cosangle *= materialDiffuse;

			tempColor += light.rgb255 * cosangle;
		}
	}

	tempColor *= colorMod.factor;
	tempColor += colorMod.term;

	u8 ir = clipByte255(tempColor.r);
	u8 ig = clipByte255(tempColor.g);
	u8 ib = clipByte255(tempColor.b);

	return Color(ir, ig, ib, 255).toRGBA();
}