Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial sRGB-capable display with desktop GL #1941

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions shaders/galaxy_vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ void main(void)
return;
}

vec4 v = vec4(viewMat * vec3(in_TexCoord0.s * 2.0 - 1.0, in_TexCoord0.t * 2.0 - 1.0, 0.0) * s, 0.0);
vec4 v = vec4(viewMat * vec3(in_TexCoord0 * 2.0 - 1.0, 0.0) * s, 0.0);
float alpha = (0.1 - screenFrac) * in_Brightness * brightness;

color = vec4(texture2D(colorTex, vec2(in_ColorIndex, 0.0)).rgb, alpha);
float premultalpha = sqrt(alpha); // faster and no so presize srgb->linear
color = vec4(texture2D(colorTex, vec2(in_ColorIndex, 0.0)).rgb * premultalpha, alpha);
texCoord = in_TexCoord0;
set_vp(p + v);
}
12 changes: 11 additions & 1 deletion shaders/passthrough_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ varying vec2 texCoord;

uniform sampler2D tex;

const float gamma = 2.2;

float to_srgb(float v)
{
return v <= 0.0031308 ? 12.92 * v : 1.055 * pow(v, 1.0/2.4) - 0.055;
}

void main(void)
{
gl_FragColor = texture2D(tex, texCoord);
// gl_FragColor = texture2D(tex, texCoord);
vec3 color = texture2D(tex, texCoord).rgb;
// gl_FragColor = vec4(pow(color, vec3(1.0 / gamma)), 1.0);
gl_FragColor = vec4(to_srgb(color.r), to_srgb(color.g), to_srgb(color.b), 1.0);
}
8 changes: 4 additions & 4 deletions src/celengine/framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ FramebufferObject::generateColorTexture()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Set the texture dimensions
// Do we need to set GL_DEPTH_COMPONENT24 here?
#ifdef GL_ES
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_HALF_FLOAT_OES, nullptr);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
#endif

// Unbind the texture
Expand Down Expand Up @@ -128,7 +129,6 @@ FramebufferObject::generateDepthTexture()

// Set the texture dimensions
// Do we need to set GL_DEPTH_COMPONENT24 here?

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, GL_DEPTH_COMPONENT, CEL_DEPTH_FORMAT, nullptr);

// Unbind the texture
Expand Down
6 changes: 6 additions & 0 deletions src/celengine/glsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ namespace celestia::gl
CELAPI bool OES_vertex_array_object = false;
CELAPI bool OES_texture_border_clamp = false;
CELAPI bool OES_geometry_shader = false;
CELAPI bool EXT_color_buffer_half_float = false;
CELAPI bool EXT_sRGB = false;
#else
CELAPI bool ARB_vertex_array_object = false;
CELAPI bool ARB_framebuffer_object = false;
CELAPI bool ARB_color_buffer_float = false;
#endif
CELAPI bool ARB_shader_texture_lod = false;
CELAPI bool EXT_texture_compression_s3tc = false;
Expand Down Expand Up @@ -45,9 +48,12 @@ bool init(util::array_view<std::string> ignore) noexcept
OES_vertex_array_object = check_extension(ignore, "GL_OES_vertex_array_object");
OES_texture_border_clamp = check_extension(ignore, "GL_OES_texture_border_clamp") || check_extension(ignore, "GL_EXT_texture_border_clamp");
OES_geometry_shader = check_extension(ignore, "GL_OES_geometry_shader") || check_extension(ignore, "GL_EXT_geometry_shader");
EXT_color_buffer_half_float = check_extension(ignore, "EXT_color_buffer_half_float");
EXT_sRGB = check_extension(ignore, "EXT_sRGB");
#else
ARB_vertex_array_object = check_extension(ignore, "GL_ARB_vertex_array_object");
ARB_framebuffer_object = check_extension(ignore, "GL_ARB_framebuffer_object") || check_extension(ignore, "GL_EXT_framebuffer_object");
ARB_color_buffer_float = check_extension(ignore, "ARB_color_buffer_float");
#endif
ARB_shader_texture_lod = check_extension(ignore, "GL_ARB_shader_texture_lod");
EXT_texture_compression_s3tc = check_extension(ignore, "GL_EXT_texture_compression_s3tc");
Expand Down
5 changes: 4 additions & 1 deletion src/celengine/glsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define CELAPI __declspec(dllexport)
#endif
#else
#define CELAPI
#define CELAPI __attribute__((visibility("default")))
#endif
#include <epoxy/gl.h>

Expand Down Expand Up @@ -47,9 +47,12 @@ extern CELAPI bool MESA_pack_invert; //NOSONAR
extern CELAPI bool OES_vertex_array_object; //NOSONAR
extern CELAPI bool OES_texture_border_clamp; //NOSONAR
extern CELAPI bool OES_geometry_shader; //NOSONAR
extern CELAPI bool EXT_color_buffer_half_float; //NOSONAR
extern CELAPI bool EXT_sRGB; // NOSONAR
#else
extern CELAPI bool ARB_vertex_array_object; //NOSONAR
extern CELAPI bool ARB_framebuffer_object; //NOSONAR
extern CELAPI bool ARB_color_buffer_float; //NOSONAR
#endif
extern CELAPI GLint maxPointSize; //NOSONAR
extern CELAPI GLint maxTextureSize; //NOSONAR
Expand Down
167 changes: 117 additions & 50 deletions src/celengine/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,47 +155,49 @@ static const unsigned int OrbitCacheCullThreshold = 200;
// Age in frames at which unused orbit paths may be eliminated from the cache
static const uint32_t OrbitCacheRetireAge = 16;

Color Renderer::StarLabelColor (0.471f, 0.356f, 0.682f);
Color Renderer::PlanetLabelColor (0.407f, 0.333f, 0.964f);
Color Renderer::DwarfPlanetLabelColor (0.557f, 0.235f, 0.576f);
Color Renderer::MoonLabelColor (0.231f, 0.733f, 0.792f);
Color Renderer::MinorMoonLabelColor (0.231f, 0.733f, 0.792f);
Color Renderer::AsteroidLabelColor (0.596f, 0.305f, 0.164f);
Color Renderer::CometLabelColor (0.768f, 0.607f, 0.227f);
Color Renderer::SpacecraftLabelColor (0.93f, 0.93f, 0.93f);
Color Renderer::LocationLabelColor (0.24f, 0.89f, 0.43f);
Color Renderer::GalaxyLabelColor (0.0f, 0.45f, 0.5f);
Color Renderer::GlobularLabelColor (0.8f, 0.45f, 0.5f);
Color Renderer::NebulaLabelColor (0.541f, 0.764f, 0.278f);
Color Renderer::OpenClusterLabelColor (0.239f, 0.572f, 0.396f);
Color Renderer::ConstellationLabelColor (0.225f, 0.301f, 0.36f);
Color Renderer::EquatorialGridLabelColor(0.64f, 0.72f, 0.88f);
Color Renderer::PlanetographicGridLabelColor(0.8f, 0.8f, 0.8f);
Color Renderer::GalacticGridLabelColor (0.88f, 0.72f, 0.64f);
Color Renderer::EclipticGridLabelColor (0.72f, 0.64f, 0.88f);
Color Renderer::HorizonGridLabelColor (0.72f, 0.72f, 0.72f);

Color Renderer::StarOrbitColor (0.5f, 0.5f, 0.8f);
Color Renderer::PlanetOrbitColor (0.3f, 0.323f, 0.833f);
Color Renderer::DwarfPlanetOrbitColor (0.557f, 0.235f, 0.576f);
Color Renderer::MoonOrbitColor (0.08f, 0.407f, 0.392f);
Color Renderer::MinorMoonOrbitColor (0.08f, 0.407f, 0.392f);
Color Renderer::AsteroidOrbitColor (0.58f, 0.152f, 0.08f);
Color Renderer::CometOrbitColor (0.639f, 0.487f, 0.168f);
Color Renderer::SpacecraftOrbitColor (0.4f, 0.4f, 0.4f);
Color Renderer::SelectionOrbitColor (1.0f, 0.0f, 0.0f);

Color Renderer::ConstellationColor (0.0f, 0.24f, 0.36f);
Color Renderer::BoundaryColor (0.24f, 0.10f, 0.12f);
Color Renderer::EquatorialGridColor (0.28f, 0.28f, 0.38f);
Color Renderer::PlanetographicGridColor (0.8f, 0.8f, 0.8f);
Color Renderer::PlanetEquatorColor (0.5f, 1.0f, 1.0f);
Color Renderer::GalacticGridColor (0.38f, 0.38f, 0.28f);
Color Renderer::EclipticGridColor (0.38f, 0.28f, 0.38f);
Color Renderer::HorizonGridColor (0.38f, 0.38f, 0.38f);
Color Renderer::EclipticColor (0.5f, 0.1f, 0.1f);

Color Renderer::SelectionCursorColor (1.0f, 0.0f, 0.0f);
Color Renderer::StarLabelColor (0.471f, 0.356f, 0.682f);
Color Renderer::PlanetLabelColor (0.407f, 0.333f, 0.964f);
Color Renderer::DwarfPlanetLabelColor (0.557f, 0.235f, 0.576f);
Color Renderer::MoonLabelColor (0.231f, 0.733f, 0.792f);
Color Renderer::MinorMoonLabelColor (0.231f, 0.733f, 0.792f);
Color Renderer::AsteroidLabelColor (0.596f, 0.305f, 0.164f);
Color Renderer::CometLabelColor (0.768f, 0.607f, 0.227f);
Color Renderer::SpacecraftLabelColor (0.93f, 0.93f, 0.93f);
Color Renderer::LocationLabelColor (0.24f, 0.89f, 0.43f);
Color Renderer::GalaxyLabelColor (0.0f, 0.45f, 0.5f);
Color Renderer::GlobularLabelColor (0.8f, 0.45f, 0.5f);
Color Renderer::NebulaLabelColor (0.541f, 0.764f, 0.278f);
Color Renderer::OpenClusterLabelColor (0.239f, 0.572f, 0.396f);
Color Renderer::ConstellationLabelColor (0.225f, 0.301f, 0.36f);
Color Renderer::EquatorialGridLabelColor (0.64f, 0.72f, 0.88f);
Color Renderer::PlanetographicGridLabelColor (0.8f, 0.8f, 0.8f);
Color Renderer::GalacticGridLabelColor (0.88f, 0.72f, 0.64f);
Color Renderer::EclipticGridLabelColor (0.72f, 0.64f, 0.88f);
Color Renderer::HorizonGridLabelColor (0.72f, 0.72f, 0.72f);

Color Renderer::StarOrbitColor (0.5f, 0.5f, 0.8f);
Color Renderer::PlanetOrbitColor (0.3f, 0.323f, 0.833f);
Color Renderer::DwarfPlanetOrbitColor (0.557f, 0.235f, 0.576f);
Color Renderer::MoonOrbitColor (0.08f, 0.407f, 0.392f);
Color Renderer::MinorMoonOrbitColor (0.08f, 0.407f, 0.392f);
Color Renderer::AsteroidOrbitColor (0.58f, 0.152f, 0.08f);
Color Renderer::CometOrbitColor (0.639f, 0.487f, 0.168f);
Color Renderer::SpacecraftOrbitColor (0.4f, 0.4f, 0.4f);
Color Renderer::SelectionOrbitColor (1.0f, 0.0f, 0.0f);

Color Renderer::ConstellationColor (0.0f, 0.24f, 0.36f);
Color Renderer::BoundaryColor (0.24f, 0.10f, 0.12f);
Color Renderer::EquatorialGridColor (0.28f, 0.28f, 0.38f);
Color Renderer::PlanetographicGridColor (0.8f, 0.8f, 0.8f);
Color Renderer::PlanetEquatorColor (0.5f, 1.0f, 1.0f);
Color Renderer::GalacticGridColor (0.38f, 0.38f, 0.28f);
Color Renderer::EclipticGridColor (0.38f, 0.28f, 0.38f);
Color Renderer::HorizonGridColor (0.38f, 0.38f, 0.38f);
Color Renderer::EclipticColor (0.5f, 0.1f, 0.1f);

Color Renderer::SelectionCursorColor (1.0f, 0.0f, 0.0f);

bool Renderer::linearMode = false;

// Some useful unit conversions
inline float mmToInches(float mm)
Expand Down Expand Up @@ -236,7 +238,7 @@ Renderer::Renderer() :
#ifndef GL_ES
renderMode(GL_FILL),
#endif
labelMode(LocationLabels), //def. NoLabels
labelMode(NoLabels),
renderFlags(DefaultRenderFlags),
orbitMask(Body::Planet | Body::Moon | Body::Stellar),
brightnessBias(0.0f),
Expand Down Expand Up @@ -355,7 +357,9 @@ static void BuildGaussianDiscMipLevel(unsigned char* mipPixels,
float x = (float) j - size / 2;
float r2 = x * x + y * y;
float f = s * (float) exp(-r2 * isig2) * power;

#if GL_ES
f = f <= 0.04045f ? f * (1.0f/12.92f) : std::pow((f + 0.055f)/1.055f, 2.4f);
#endif
mipPixels[i * size + j] = (unsigned char) (255.99f * min(f, 1.0f));
}
}
Expand All @@ -377,6 +381,9 @@ static void BuildGlareMipLevel(unsigned char* mipPixels,
float x = (float) j - size / 2;
auto r = (float) sqrt(x * x + y * y);
auto f = (float) pow(base, r * scale);
#if GL_ES
f = f <= 0.04045f ? f * (1.0f/12.92f) : std::pow((f + 0.055f)/1.055f, 2.4f);
#endif
mipPixels[i * size + j] = (unsigned char) (255.99f * min(f, 1.0f));
}
}
Expand Down Expand Up @@ -414,7 +421,11 @@ static void BuildGlareMipLevel2(unsigned char* mipPixels,
static Texture* BuildGaussianDiscTexture(unsigned int log2size)
{
unsigned int size = 1 << log2size;
#if GL_ES
Image* img = new Image(PixelFormat::LUMINANCE, size, size, log2size + 1);
#else
Image* img = new Image(PixelFormat::SLUMINANCE, size, size, log2size + 1);
#endif

for (unsigned int mipLevel = 0; mipLevel <= log2size; mipLevel++)
{
Expand All @@ -438,7 +449,11 @@ static Texture* BuildGaussianDiscTexture(unsigned int log2size)
static Texture* BuildGaussianGlareTexture(unsigned int log2size)
{
unsigned int size = 1 << log2size;
#if GL_ES
Image* img = new Image(PixelFormat::LUMINANCE, size, size, log2size + 1);
#else
Image* img = new Image(PixelFormat::SLUMINANCE, size, size, log2size + 1);
#endif

for (unsigned int mipLevel = 0; mipLevel <= log2size; mipLevel++)
{
Expand Down Expand Up @@ -543,6 +558,51 @@ bool Renderer::init(int winWidth, int winHeight, const DetailOptions& _detailOpt
commonDataInitialized = true;
}

if (!linearMode)
{
StarLabelColor.linearize();
PlanetLabelColor.linearize();
DwarfPlanetLabelColor.linearize();
MoonLabelColor.linearize();
MinorMoonLabelColor.linearize();
AsteroidLabelColor.linearize();
CometLabelColor.linearize();
SpacecraftLabelColor.linearize();
LocationLabelColor.linearize();
GalaxyLabelColor.linearize();
GlobularLabelColor.linearize();
NebulaLabelColor.linearize();
OpenClusterLabelColor.linearize();
ConstellationLabelColor.linearize();
EquatorialGridLabelColor.linearize();
PlanetographicGridLabelColor.linearize();
GalacticGridLabelColor.linearize();
EclipticGridLabelColor.linearize();
HorizonGridLabelColor.linearize();

StarOrbitColor.linearize();
PlanetOrbitColor.linearize();
DwarfPlanetOrbitColor.linearize();
MoonOrbitColor.linearize();
MinorMoonOrbitColor.linearize();
AsteroidOrbitColor.linearize();
CometOrbitColor.linearize();
SpacecraftOrbitColor.linearize();
SelectionOrbitColor.linearize();

ConstellationColor.linearize();
BoundaryColor.linearize();
EquatorialGridColor.linearize();
PlanetographicGridColor.linearize();
PlanetEquatorColor.linearize();
GalacticGridColor.linearize();
EclipticGridColor.linearize();
HorizonGridColor.linearize();
EclipticColor.linearize();

SelectionCursorColor.linearize();
}

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

Expand Down Expand Up @@ -1605,7 +1665,7 @@ void Renderer::draw(const Observer& observer,
// Calculate saturation magnitude
satPoint = faintestMag - (1.0f - brightnessBias) / brightnessScale;

ambientColor = Color(ambientLightLevel, ambientLightLevel, ambientLightLevel);
ambientColor = Color::srgb(ambientLightLevel, ambientLightLevel, ambientLightLevel);

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Expand Down Expand Up @@ -2319,8 +2379,8 @@ void Renderer::renderObject(const Vector3f& pos,
ri.color = obj.surface->color;
}

ri.ambientColor = ambientColor;
ri.specularColor = obj.surface->specularColor;
ri.ambientColor = linearMode ? ambientColor : Color::srgb(ambientColor);
ri.specularColor = linearMode ? obj.surface->specularColor : Color::srgb(obj.surface->specularColor);
ri.specularPower = obj.surface->specularPower;
ri.lunarLambert = obj.surface->lunarLambert;

Expand Down Expand Up @@ -2366,8 +2426,7 @@ void Renderer::renderObject(const Vector3f& pos,
// If there's an atmosphere, we need to move the far plane
// out so that the clouds and atmosphere shell aren't clipped.
float atmosphereRadius = eradius + atmosphereHeight;
frustumFarPlane += (float) sqrt(square(atmosphereRadius) -
square(eradius));
frustumFarPlane += std::sqrt(square(atmosphereRadius) - square(eradius));
}
}
}
Expand Down Expand Up @@ -2509,8 +2568,16 @@ void Renderer::renderObject(const Vector3f& pos,
{
Eigen::Matrix4f modelView = celmath::rotate(getCameraOrientationf());
Matrices mvp = { m.projection, &modelView };
Atmosphere atm(*atmosphere);
if (!linearMode)
{
atm.lowerColor = Color::srgb(atm.lowerColor);
atm.upperColor = Color::srgb(atm.upperColor);
atm.skyColor = Color::srgb(atm.skyColor);
atm.sunsetColor = Color::srgb(atm.sunsetColor);
}
m_atmosphereRenderer->renderLegacy(
*atmosphere,
atm,
ls,
pos,
obj.orientation,
Expand Down Expand Up @@ -3053,7 +3120,7 @@ void Renderer::renderStar(const Star& star,
// Use atmosphere effect to give stars a fuzzy fringe
if (star.hasCorona() && rp.geometry == InvalidResource)
{
Color atmColor(color.red() * 0.5f, color.green() * 0.5f, color.blue() * 0.5f);
Color atmColor = color * 0.5f;
atmosphere.height = radius * CoronaHeight;
atmosphere.lowerColor = atmColor;
atmosphere.upperColor = atmColor;
Expand Down
2 changes: 2 additions & 0 deletions src/celengine/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,8 @@ class Renderer

static Color SelectionCursorColor;

static bool linearMode;

friend class PointStarRenderer;
};

Expand Down
2 changes: 2 additions & 0 deletions src/celengine/shadermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3635,6 +3635,7 @@ CelestiaGLProgram::setLightParameters(const LightingState& ls,
lights[i].color = light.color.toVector3();
lights[i].direction = light.direction_obj;

#if 0
// 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);
Expand All @@ -3643,6 +3644,7 @@ CelestiaGLProgram::setLightParameters(const LightingState& ls,
float photometricNormFactor = std::max(1.0f, 1.0f + cosPhaseAngle * 0.5f);
lightColor *= photometricNormFactor;
}
#endif

lights[i].diffuse = lightColor.cwiseProduct(diffuseColor);
lights[i].brightness = lightColor.maxCoeff();
Expand Down
Loading