1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-14 09:04:31 +02:00

fixed #133 fix ambient and main light calculation

This commit is contained in:
XProger
2019-01-29 22:58:25 +03:00
parent 5cf8edb59f
commit 7f20c44cdf
3 changed files with 39 additions and 27 deletions

View File

@@ -1268,39 +1268,47 @@ struct Controller {
} }
void updateLights(bool lerp = true) { void updateLights(bool lerp = true) {
TR::Room::Light sunLight;
const TR::Room &room = getLightRoom(); const TR::Room &room = getLightRoom();
if (getModel()) { targetLight = NULL;
vec3 center = getBoundingBox().center(); int ambient = room.ambient;
float maxAtt = 0.0f;
/* if (getEntity().intensity == -1) {
if (room.flags.sky) { // TODO trace rooms up for sun light, add direct light projection
sunLight.x = int32(center.x); if (room.lightsCount && getModel()) {
sunLight.y = int32(center.y) - 8192; ambient = 0x1FFF - ambient;
sunLight.z = int32(center.z); int maxValue = 0;
sunLight.color = Color32(255, 255, 255, 255);
sunLight.radius = 1000 * 1024; vec3 center = getBoundingBox().center();
targetLight = &sunLight;
} else { int x = int(center.x);
*/ int y = int(center.y);
{ int z = int(center.z);
for (int i = 0; i < room.lightsCount; i++) { for (int i = 0; i < room.lightsCount; i++) {
TR::Room::Light &light = room.lights[i]; TR::Room::Light &light = room.lights[i];
if ((light.color.r | light.color.g | light.color.b) == 0) continue;
vec3 dir = vec3(float(light.x), float(light.y), float(light.z)) - center; int dx = x - light.x;
float att = max(0.0f, 1.0f - dir.length2() / SQR(light.radius)) * ((light.color.r + light.color.g + light.color.b) / (3.0f * 255.0f)); int dy = y - light.y;
int dz = z - light.z;
if (att > maxAtt) { int D = (SQR(dx) + SQR(dy) + SQR(dz)) >> 12;
maxAtt = att; int R = SQR(light.radius >> 1) >> 12;
targetLight = &light;
int value = (light.intensity * R) / (D + R) + ambient;
if (maxValue < value) {
targetLight = &room.lights[i];
maxValue = value;
} }
} }
ambient = (maxValue + ambient) / 2;
ambient = 0x1FFF - ambient;
} }
} else
targetLight = NULL; intensity = intensityf(ambient);
}
if (targetLight == NULL) { if (targetLight == NULL) {
mainLightPos = vec3(0); mainLightPos = vec3(0);

View File

@@ -1347,6 +1347,7 @@ namespace TR {
struct Light { struct Light {
int32 x, y, z; int32 x, y, z;
uint32 radius; uint32 radius;
int32 intensity;
Color32 color; Color32 color;
} *lights; } *lights;
@@ -3255,6 +3256,7 @@ namespace TR {
int value = clamp((intensity > 0x1FFF) ? 0 : (intensity >> 5), 0, 255); int value = clamp((intensity > 0x1FFF) ? 0 : (intensity >> 5), 0, 255);
light.color.r = light.color.g = light.color.b = value; light.color.r = light.color.g = light.color.b = value;
light.color.a = 0; light.color.a = 0;
light.intensity = intensity;
light.radius = stream.readBE32() * 2; light.radius = stream.readBE32() * 2;
} }
@@ -4382,6 +4384,8 @@ namespace TR {
light.color.a = 0; light.color.a = 0;
} }
light.intensity = intensity;
if (version == VER_TR3_PSX) if (version == VER_TR3_PSX)
light.radius >>= 2; light.radius >>= 2;

View File

@@ -1664,14 +1664,14 @@ struct Level : IGame {
type = Shader::MIRROR; type = Shader::MIRROR;
if (isModel) { // model if (isModel) { // model
float intensity = controller->intensity < 0.0f ? intensityf(room.ambient) : controller->intensity; ASSERT(controller->intensity >= 0.0f);
setMainLight(controller); setMainLight(controller);
setRoomParams(roomIndex, type, 1.0f, intensity, controller->specular, 1.0f, mesh->transparent == 1); setRoomParams(roomIndex, type, 1.0f, controller->intensity, controller->specular, 1.0f, mesh->transparent == 1);
vec3 pos = controller->getPos(); vec3 pos = controller->getPos();
if (ambientCache) { if (ambientCache) {
if (!controller->getEntity().isDoor() && !controller->getEntity().isBlock()) { // no advanced ambient lighting for secret (all) doors and blocks if (!entity.isDoor() && !entity.isBlock()) { // no advanced ambient lighting for secret (all) doors and blocks
AmbientCache::Cube cube; AmbientCache::Cube cube;
ambientCache->getAmbient(roomIndex, pos, cube); ambientCache->getAmbient(roomIndex, pos, cube);
if (cube.status == AmbientCache::Cube::READY) if (cube.status == AmbientCache::Cube::READY)