Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/celengine/rendcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,15 @@ RenderContext::updateShader(const cmod::VertexDescription& desc, cmod::Primitive
GLSL_RenderContext::GLSL_RenderContext(Renderer* renderer,
const LightingState& ls,
float _objRadius,
float _geomAlbedo,
const Eigen::Quaternionf& orientation,
const Eigen::Matrix4f* _modelViewMatrix,
const Eigen::Matrix4f* _projectionMatrix) :
RenderContext(renderer),
lightingState(ls),
objRadius(_objRadius),
objScale(Eigen::Vector3f::Constant(_objRadius)),
geomAlbedo(_geomAlbedo),
objOrientation(orientation),
modelViewMatrix(_modelViewMatrix),
projectionMatrix(_projectionMatrix)
Expand All @@ -228,13 +230,15 @@ GLSL_RenderContext::GLSL_RenderContext(Renderer* renderer,
GLSL_RenderContext::GLSL_RenderContext(Renderer* renderer,
const LightingState& ls,
const Eigen::Vector3f& _objScale,
float _geomAlbedo,
const Eigen::Quaternionf& orientation,
const Eigen::Matrix4f* _modelViewMatrix,
const Eigen::Matrix4f* _projectionMatrix) :
RenderContext(renderer),
lightingState(ls),
objRadius(_objScale.maxCoeff()),
objScale(_objScale),
geomAlbedo(_geomAlbedo),
objOrientation(orientation),
modelViewMatrix(_modelViewMatrix),
projectionMatrix(_projectionMatrix)
Expand Down Expand Up @@ -461,7 +465,7 @@ GLSL_RenderContext::makeCurrent(const cmod::Material& m)
prog->shininess = m.specularPower;
if (shaderProps.lightModel == LightingModel::LunarLambertModel)
{
prog->lunarLambert = lunarLambert;
prog->setLunarLambertParameters(lunarLambert, geomAlbedo);
}

// Generally, we want to disable depth writes for blend because it
Expand Down
3 changes: 3 additions & 0 deletions src/celengine/rendcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ class GLSL_RenderContext : public RenderContext
GLSL_RenderContext(Renderer* r,
const LightingState& ls,
float _objRadius,
float _geomAlbedo,
const Eigen::Quaternionf& orientation,
const Eigen::Matrix4f *_modelViewMatrix,
const Eigen::Matrix4f *_projectionMatrix);
GLSL_RenderContext(Renderer* r,
const LightingState& ls,
const Eigen::Vector3f& _objScale,
float _geomAlbedo,
const Eigen::Quaternionf& orientation,
const Eigen::Matrix4f *_modelViewMatrix,
const Eigen::Matrix4f *_projectionMatrix);
Expand All @@ -124,6 +126,7 @@ class GLSL_RenderContext : public RenderContext
cmod::BlendMode blendMode{ cmod::BlendMode::InvalidBlend };
float objRadius;
Eigen::Vector3f objScale;
float geomAlbedo;
Eigen::Quaternionf objOrientation;

// extended material properties
Expand Down
5 changes: 4 additions & 1 deletion src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,7 @@ void Renderer::renderObject(const Vector3f& pos,
renderEllipsoid_GLSL(ri, ls,
atmosphere, cloudTexOffset,
scaleFactors,
obj.geomAlbedo,
textureResolution,
renderFlags,
obj.orientation,
Expand Down Expand Up @@ -2318,6 +2319,7 @@ void Renderer::renderObject(const Vector3f& pos,
ls,
obj.atmosphere,
geometryScale,
obj.geomAlbedo,
renderFlags,
obj.orientation,
astro::daysToSecs(now - astro::J2000),
Expand Down Expand Up @@ -2660,8 +2662,9 @@ void Renderer::renderPlanet(Body& body,
rp.atmosphere = bodyFeaturesManager->getAtmosphere(&body);
rp.rings = bodyFeaturesManager->getRings(&body);
rp.radius = body.getRadius();
rp.geometry = body.getGeometry();
rp.semiAxes = body.getSemiAxes() * (1.0f / rp.radius);
rp.geomAlbedo = body.getGeomAlbedo();
rp.geometry = body.getGeometry();
rp.geometryScale = body.getGeometryScale();

Quaterniond q = body.getRotationModel(now)->spin(now) *
Expand Down
3 changes: 2 additions & 1 deletion src/celengine/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ class Renderer
const Atmosphere* atmosphere{ nullptr };
RingSystem* rings{ nullptr };
float radius{ 1.0f };
float geometryScale{ 1.0f };
Eigen::Vector3f semiAxes{ Eigen::Vector3f::Ones() };
float geomAlbedo{ 0.5f };
ResourceHandle geometry{ InvalidResource };
float geometryScale{ 1.0f };
Eigen::Quaternionf orientation{ Eigen::Quaternionf::Identity() };
LightingState::EclipseShadowVector* eclipseShadows;
};
Expand Down
6 changes: 4 additions & 2 deletions src/celengine/renderglsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void renderEllipsoid_GLSL(const RenderInfo& ri,
Atmosphere* atmosphere,
float cloudTexOffset,
const Eigen::Vector3f& semiAxes,
float geomAlbedo,
TextureResolution textureRes,
RenderFlags renderFlags,
const Eigen::Quaternionf& planetOrientation,
Expand Down Expand Up @@ -305,7 +306,7 @@ void renderEllipsoid_GLSL(const RenderInfo& ri,
prog->eyePosition = ls.eyePos_obj;
prog->shininess = ri.specularPower;
if (util::is_set(shadprop.lightModel, LightingModel::LunarLambertModel))
prog->lunarLambert = ri.lunarLambert;
prog->setLunarLambertParameters(ri.lunarLambert, geomAlbedo);

if (util::is_set(shadprop.texUsage, TexUsage::RingShadowTexture))
{
Expand Down Expand Up @@ -369,6 +370,7 @@ void renderGeometry_GLSL(Geometry* geometry,
const LightingState& ls,
const Atmosphere* atmosphere,
float geometryScale,
float geomAlbedo,
RenderFlags renderFlags,
const Eigen::Quaternionf& planetOrientation,
double tsec,
Expand Down Expand Up @@ -441,7 +443,7 @@ void renderGeometry_GLSL(Geometry* geometry,
glDepthRange(range[0], range[1]);
}

GLSL_RenderContext rc(renderer, ls, geometryScale, planetOrientation, m.modelview, m.projection);
GLSL_RenderContext rc(renderer, ls, geometryScale, geomAlbedo, planetOrientation, m.modelview, m.projection);

if (util::is_set(renderFlags, RenderFlags::ShowAtmospheres))
{
Expand Down
2 changes: 2 additions & 0 deletions src/celengine/renderglsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void renderEllipsoid_GLSL(const RenderInfo& ri,
Atmosphere* atmosphere,
float cloudTexOffset,
const Eigen::Vector3f& semiAxes,
float geomAlbedo,
TextureResolution textureRes,
RenderFlags renderFlags,
const Eigen::Quaternionf& planetOrientation,
Expand All @@ -54,6 +55,7 @@ void renderGeometry_GLSL(Geometry* geometry,
const LightingState& ls,
const Atmosphere* atmosphere,
float geometryScale,
float geomAlbedo,
RenderFlags renderFlags,
const Eigen::Quaternionf& planetOrientation,
double tsec,
Expand Down
60 changes: 41 additions & 19 deletions src/celengine/shadermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,28 @@ AssignDiffuse(unsigned int lightIndex, const ShaderProperties& props)
}


std::string
LunarLambert(const ShaderProperties& props, unsigned int light)
{
std::string source;

// Rough match to the phase functions of non-Lambertian surfaces
source += "float phaseFunction = phaseFunctionAt0 * exp(-phaseAngle);\n";
// Normalize by the phase function
source += "float normFactor = mix(1.0, phaseFunction, lunarLambert);\n";
if (props.usesTangentSpaceLighting())
{
source += "l = mix(NL, (phaseFunction * NL / (max(NV, 0.001) + NL)), lunarLambert) / normFactor * clamp(" + LightDir_tan(light) + ".z * 8.0, 0.0, 1.0);\n";
}
else
{
source += AssignDiffuse(light, props) + " mix(NL, phaseFunction * NL / (max(NV, 0.001) + NL), lunarLambert) / normFactor;\n";
}

return source;
}


// Values used in generated shaders:
// N - surface normal
// V - view vector: the normalized direction from vertex to eye
Expand Down Expand Up @@ -481,12 +503,14 @@ AddDirectionalLightContrib(unsigned int i, const ShaderProperties& props)
{
source += TangentSpaceTransform(LightDir_tan(i), LightProperty(i, "direction"));
}
else if (util::is_set(props.lightModel, LightingModel::LunarLambertModel))
{
source += "float phaseAngle = acos(clamp(dot(" + LightProperty(i, "direction") + ", eyeDir), -1.0, 1.0));\n";
source += LunarLambert(props, i);
}
else if (props.hasSpecular())
{
if (util::is_set(props.lightModel, LightingModel::LunarLambertModel))
source += AssignDiffuse(i, props) + " mix(NL, NL / (max(NV, 0.001) + NL), lunarLambert);\n";
else
source += SeparateDiffuse(i) + " = NL;\n";
source += SeparateDiffuse(i) + " = NL;\n";
}
#if 0
else if (props.lightModel == LightingModel::OrenNayarModel)
Expand Down Expand Up @@ -514,10 +538,6 @@ AddDirectionalLightContrib(unsigned int i, const ShaderProperties& props)
}
}
#endif
else if (util::is_set(props.lightModel, LightingModel::LunarLambertModel))
{
source += AssignDiffuse(i, props) + " mix(NL, NL / (max(NV, 0.001) + NL), lunarLambert);\n";
}
else if (props.usesShadows())
{
// When there are shadows, we need to track the diffuse contributions
Expand Down Expand Up @@ -1619,7 +1639,8 @@ buildFragmentShader(const ShaderProperties& props)
if (util::is_set(props.lightModel, LightingModel::LunarLambertModel))
{
source += "NL = max(0.0, NL);\n";
source += "l = mix(NL, (NL / (max(NV, 0.001) + NL)), lunarLambert) * clamp(" + LightDir_tan(i) + ".z * 8.0, 0.0, 1.0);\n";
source += "float phaseAngle = acos(clamp(dot(" + LightDir_tan(i) + ", eyeDir_tan), -1.0, 1.0));\n";
source += LunarLambert(props, i);
}
else
{
Expand Down Expand Up @@ -2949,6 +2970,7 @@ CelestiaGLProgram::initParameters()
if (util::is_set(props.lightModel, LightingModel::LunarLambertModel))
{
lunarLambert = floatParam("lunarLambert");
phaseFunctionAt0 = floatParam("phaseFunctionAt0");
}

if (props.usePointSize())
Expand Down Expand Up @@ -3054,16 +3076,6 @@ CelestiaGLProgram::setLightParameters(const LightingState& ls,
Eigen::Vector3f lightColor = light.color.toVector3() * light.irradiance;
lights[i].color = light.color.toVector3();
lights[i].direction = light.direction_obj;

// Include a phase-based normalization factor to prevent planets from appearing
// too dim when rendered with non-Lambertian photometric functions.
float cosPhaseAngle = light.direction_obj.dot(ls.eyeDir_obj);
if (util::is_set(props.lightModel, LightingModel::LunarLambertModel))
{
float photometricNormFactor = std::max(1.0f, 1.0f + cosPhaseAngle * 0.5f);
lightColor *= photometricNormFactor;
}

lights[i].diffuse = lightColor.cwiseProduct(diffuseColor);
lights[i].brightness = lightColor.maxCoeff();
lights[i].specular = lightColor.cwiseProduct(specularColor);
Expand All @@ -3080,6 +3092,16 @@ CelestiaGLProgram::setLightParameters(const LightingState& ls,
}


void
CelestiaGLProgram::setLunarLambertParameters(float l,
float geomAlbedo)
{
lunarLambert = l;
// Derived from Equation 6 of Buratti and Veverka (1983)
phaseFunctionAt0 = std::max(0.001f, (2.0f * geomAlbedo - 4.0f / 3.0f * (1.0f - l)) / l);
}


/** Set GLSL shader constants for shadows from ellipsoid occluders; shadows from
* irregular objects are not handled yet.
* \param scaleFactors the scale factors of the object being shadowed
Expand Down
4 changes: 4 additions & 0 deletions src/celengine/shadermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class CelestiaGLProgram
Color materialDiffuse,
Color materialSpecular,
Color materialEmissive);
void setLunarLambertParameters(float l,
float geomAlbedo);
void setEclipseShadowParameters(const LightingState& ls,
const Eigen::Vector3f& scale,
const Eigen::Quaternionf& orientation);
Expand Down Expand Up @@ -245,6 +247,8 @@ class CelestiaGLProgram
// Mix of Lambertian and "lunar" (Lommel-Seeliger) photometric models.
// 0 = pure Lambertian, 1 = L-S
FloatShaderParameter lunarLambert;
// Surface phase function f(alpha) at 0°
FloatShaderParameter phaseFunctionAt0;

// Diffuse texture coordinate offset
FloatShaderParameter textureOffset;
Expand Down
Loading