Merge branch 'restoringthelightfacingthedark' into 'master'

Lighting fixes, non-exciting part

See merge request OpenMW/openmw!4126
esm4-texture
psi29a 7 months ago
commit 16572109fc

@ -41,6 +41,7 @@ namespace SceneUtil
if (mType == LT_Normal) if (mType == LT_Normal)
{ {
light->setDiffuse(mDiffuseColor); light->setDiffuse(mDiffuseColor);
light->setSpecular(mSpecularColor);
traverse(node, nv); traverse(node, nv);
return; return;
} }
@ -65,10 +66,10 @@ namespace SceneUtil
mPhase = mPhase <= 0.5f ? 1.f : 0.25f; mPhase = mPhase <= 0.5f ? 1.f : 0.25f;
} }
osg::Vec4f result = mDiffuseColor * mBrightness * node->getActorFade(); const float result = mBrightness * node->getActorFade();
light->setDiffuse(result); light->setDiffuse(mDiffuseColor * result);
light->setSpecular(result); light->setSpecular(mSpecularColor * result);
traverse(node, nv); traverse(node, nv);
} }
@ -78,4 +79,9 @@ namespace SceneUtil
mDiffuseColor = color; mDiffuseColor = color;
} }
void LightController::setSpecular(const osg::Vec4f& color)
{
mSpecularColor = color;
}
} }

@ -27,12 +27,14 @@ namespace SceneUtil
void setType(LightType type); void setType(LightType type);
void setDiffuse(const osg::Vec4f& color); void setDiffuse(const osg::Vec4f& color);
void setSpecular(const osg::Vec4f& color);
void operator()(SceneUtil::LightSource* node, osg::NodeVisitor* nv); void operator()(SceneUtil::LightSource* node, osg::NodeVisitor* nv);
private: private:
LightType mType; LightType mType;
osg::Vec4f mDiffuseColor; osg::Vec4f mDiffuseColor;
osg::Vec4f mSpecularColor;
float mPhase; float mPhase;
float mBrightness; float mBrightness;
double mStartTime; double mStartTime;

@ -1160,6 +1160,7 @@ namespace SceneUtil
auto* light = transform.mLightSource->getLight(frameNum); auto* light = transform.mLightSource->getLight(frameNum);
light->setDiffuse(light->getDiffuse() * fade); light->setDiffuse(light->getDiffuse() * fade);
light->setSpecular(light->getSpecular() * fade);
transform.mLightSource->setLastAppliedFrame(frameNum); transform.mLightSource->setLastAppliedFrame(frameNum);
} }

@ -52,11 +52,15 @@ namespace SceneUtil
static const bool useConstant = Fallback::Map::getBool("LightAttenuation_UseConstant"); static const bool useConstant = Fallback::Map::getBool("LightAttenuation_UseConstant");
static const bool useLinear = Fallback::Map::getBool("LightAttenuation_UseLinear"); static const bool useLinear = Fallback::Map::getBool("LightAttenuation_UseLinear");
static const bool useQuadratic = Fallback::Map::getBool("LightAttenuation_UseQuadratic"); static const bool useQuadratic = Fallback::Map::getBool("LightAttenuation_UseQuadratic");
static const float constantValue = Fallback::Map::getFloat("LightAttenuation_ConstantValue"); // User file might provide nonsense values
static const float linearValue = Fallback::Map::getFloat("LightAttenuation_LinearValue"); // Clamp these settings to prevent badness (e.g. illegal OpenGL calls)
static const float quadraticValue = Fallback::Map::getFloat("LightAttenuation_QuadraticValue"); static const float constantValue = std::max(Fallback::Map::getFloat("LightAttenuation_ConstantValue"), 0.f);
static const float linearRadiusMult = Fallback::Map::getFloat("LightAttenuation_LinearRadiusMult"); static const float linearValue = std::max(Fallback::Map::getFloat("LightAttenuation_LinearValue"), 0.f);
static const float quadraticRadiusMult = Fallback::Map::getFloat("LightAttenuation_QuadraticRadiusMult"); static const float quadraticValue = std::max(Fallback::Map::getFloat("LightAttenuation_QuadraticValue"), 0.f);
static const float linearRadiusMult
= std::max(Fallback::Map::getFloat("LightAttenuation_LinearRadiusMult"), 0.f);
static const float quadraticRadiusMult
= std::max(Fallback::Map::getFloat("LightAttenuation_QuadraticRadiusMult"), 0.f);
static const int linearMethod = Fallback::Map::getInt("LightAttenuation_LinearMethod"); static const int linearMethod = Fallback::Map::getInt("LightAttenuation_LinearMethod");
static const int quadraticMethod = Fallback::Map::getInt("LightAttenuation_QuadraticMethod"); static const int quadraticMethod = Fallback::Map::getInt("LightAttenuation_QuadraticMethod");
static const bool outQuadInLin = Fallback::Map::getBool("LightAttenuation_OutQuadInLin"); static const bool outQuadInLin = Fallback::Map::getBool("LightAttenuation_OutQuadInLin");
@ -68,7 +72,7 @@ namespace SceneUtil
{ {
linearAttenuation = linearMethod == 0 ? linearValue : 0.01f; linearAttenuation = linearMethod == 0 ? linearValue : 0.01f;
float r = radius * linearRadiusMult; float r = radius * linearRadiusMult;
if (r && (linearMethod == 1 || linearMethod == 2)) if (r > 0.f && (linearMethod == 1 || linearMethod == 2))
linearAttenuation = linearValue / std::pow(r, linearMethod); linearAttenuation = linearValue / std::pow(r, linearMethod);
} }
@ -76,10 +80,14 @@ namespace SceneUtil
{ {
quadraticAttenuation = quadraticMethod == 0 ? quadraticValue : 0.01f; quadraticAttenuation = quadraticMethod == 0 ? quadraticValue : 0.01f;
float r = radius * quadraticRadiusMult; float r = radius * quadraticRadiusMult;
if (r && (quadraticMethod == 1 || quadraticMethod == 2)) if (r > 0.f && (quadraticMethod == 1 || quadraticMethod == 2))
quadraticAttenuation = quadraticValue / std::pow(r, quadraticMethod); quadraticAttenuation = quadraticValue / std::pow(r, quadraticMethod);
} }
// If the values are still nonsense, try to at least prevent UB and disable attenuation
if (constantAttenuation == 0.f && linearAttenuation == 0.f && quadraticAttenuation == 0.f)
constantAttenuation = 1.f;
light->setConstantAttenuation(constantAttenuation); light->setConstantAttenuation(constantAttenuation);
light->setLinearAttenuation(linearAttenuation); light->setLinearAttenuation(linearAttenuation);
light->setQuadraticAttenuation(quadraticAttenuation); light->setQuadraticAttenuation(quadraticAttenuation);
@ -117,19 +125,23 @@ namespace SceneUtil
configureLight(light, radius, isExterior); configureLight(light, radius, isExterior);
osg::Vec4f diffuse = esmLight.mColor; osg::Vec4f diffuse = esmLight.mColor;
osg::Vec4f specular = esmLight.mColor; // ESM format doesn't provide specular
if (esmLight.mNegative) if (esmLight.mNegative)
{ {
diffuse *= -1; diffuse *= -1;
diffuse.a() = 1; diffuse.a() = 1;
// Using specular lighting for negative lights is unreasonable
specular = osg::Vec4f();
} }
light->setDiffuse(diffuse); light->setDiffuse(diffuse);
light->setAmbient(ambient); light->setAmbient(ambient);
light->setSpecular(diffuse); // ESM format doesn't provide specular light->setSpecular(specular);
lightSource->setLight(light); lightSource->setLight(light);
osg::ref_ptr<SceneUtil::LightController> ctrl(new SceneUtil::LightController); osg::ref_ptr<SceneUtil::LightController> ctrl(new SceneUtil::LightController);
ctrl->setDiffuse(light->getDiffuse()); ctrl->setDiffuse(light->getDiffuse());
ctrl->setSpecular(light->getSpecular());
if (esmLight.mFlicker) if (esmLight.mFlicker)
ctrl->setType(SceneUtil::LightController::LT_Flicker); ctrl->setType(SceneUtil::LightController::LT_Flicker);
if (esmLight.mFlickerSlow) if (esmLight.mFlickerSlow)

@ -106,8 +106,6 @@ float lcalcRadius(int lightIndex)
float lcalcIllumination(int lightIndex, float dist) float lcalcIllumination(int lightIndex, float dist)
{ {
float illumination = 1.0 / (lcalcConstantAttenuation(lightIndex) + lcalcLinearAttenuation(lightIndex) * dist + lcalcQuadraticAttenuation(lightIndex) * dist * dist); float illumination = 1.0 / (lcalcConstantAttenuation(lightIndex) + lcalcLinearAttenuation(lightIndex) * dist + lcalcQuadraticAttenuation(lightIndex) * dist * dist);
// FIXME: FFP doesn't do this
illumination = clamp(illumination, 0.0, 1.0);
#if @lightingMethodPerObjectUniform || @lightingMethodUBO #if @lightingMethodPerObjectUniform || @lightingMethodUBO
// Fade illumination between the radius and the radius doubled to diminish pop-in // Fade illumination between the radius and the radius doubled to diminish pop-in
illumination *= 1.0 - quickstep((dist / lcalcRadius(lightIndex)) - 1.0); illumination *= 1.0 - quickstep((dist / lcalcRadius(lightIndex)) - 1.0);

Loading…
Cancel
Save