Fix some coding style questions

This commit is contained in:
Alexander Bock
2025-08-27 13:19:59 +02:00
parent 792d65c941
commit eaa9e6fb39
5 changed files with 81 additions and 41 deletions
@@ -81,7 +81,11 @@ Fragment getFragment() {
float lerpFactor = dot(camPositionObj, sunPositionObj);
// Jon Colors:
//vec4 diffuse = mix(colorFwrd * vec4(1, 0.88, 0.82, 1.0), colorBckwrd * vec4(1, 0.88, 0.82, 1.0), lerpFactor);
// vec4 diffuse = mix(
// colorFwrd * vec4(1, 0.88, 0.82, 1.0),
// colorBckwrd * vec4(1, 0.88, 0.82, 1.0),
// lerpFactor
// );
vec4 diffuse = mix(colorFwrd, colorBckwrd, lerpFactor) * colorMult;
diffuse.a = colorFilterValue * transparency;
float colorValue = length(diffuse.rgb) / 0.57735026919;
@@ -91,7 +95,12 @@ Fragment getFragment() {
// Check if ray from fragment to sun intersects the ellipsoid (globe)
// This creates more accurate shadowing for rings
bool intersectsGlobe = rayIntersectsEllipsoid(posObj, sunPositionObj, vec3(0.0), ellipsoidRadii);
bool intersectsGlobe = rayIntersectsEllipsoid(
posObj,
sunPositionObj,
vec3(0.0),
ellipsoidRadii
);
// shadow == 1.0 means it is not in shadow
float shadow = intersectsGlobe ? 0.05 : 1.0;
+61 -35
View File
@@ -1,35 +1,61 @@
bool rayIntersectsEllipsoid(vec3 rayOrigin, vec3 rayDir, vec3 ellipsoidCenter, vec3 ellipsoidRadii) {
// Translate ray to ellipsoid's local coordinate system
vec3 oc = rayOrigin - ellipsoidCenter;
// Normalize by ellipsoid radii to convert to unit sphere problem
vec3 ocNorm = oc / ellipsoidRadii;
vec3 dirNorm = rayDir / ellipsoidRadii;
// Quadratic equation coefficients: At² + Bt + C = 0
float a = dot(dirNorm, dirNorm);
float b = dot(ocNorm, dirNorm); // Note: factor of 2 moved to discriminant calc
float c = dot(ocNorm, ocNorm) - 1.0;
// Calculate discriminant (optimized: b² - ac since we factored out the 2)
float discriminant = b * b - a * c;
// Early exit if no intersection
if (discriminant < 0.0) {
return false;
}
// Check if at least one intersection is in front of ray origin
// For quadratic At² + 2Bt + C = 0, if we want to check if any t >= 0:
// If C <= 0, ray origin is inside ellipsoid, so definitely intersects
if (c <= 0.0) {
return true;
}
// If both intersections exist and C > 0, check if the smaller root t1 >= 0
// t1 = (-b - sqrt(discriminant)) / a
// Since we need t1 >= 0: -b - sqrt(discriminant) >= 0
// This means: -b >= sqrt(discriminant), so b <= -sqrt(discriminant)
// Since sqrt(discriminant) >= 0, this means b <= 0
return b <= 0.0;
}
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2025 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
bool rayIntersectsEllipsoid(vec3 rayOrigin, vec3 rayDir, vec3 ellipsoidCenter,
vec3 ellipsoidRadii)
{
// Translate ray to ellipsoid's local coordinate system
vec3 oc = rayOrigin - ellipsoidCenter;
// Normalize by ellipsoid radii to convert to unit sphere problem
vec3 ocNorm = oc / ellipsoidRadii;
vec3 dirNorm = rayDir / ellipsoidRadii;
// Quadratic equation coefficients: A*t^2 + B*t + C = 0
float a = dot(dirNorm, dirNorm);
float b = dot(ocNorm, dirNorm); // Note: factor of 2 moved to discriminant calc
float c = dot(ocNorm, ocNorm) - 1.0;
// Calculate discriminant (optimized: b^2 - ac since we factored out the 2)
float discriminant = b * b - a * c;
// Early exit if no intersection
if (discriminant < 0.0) {
return false;
}
// Check if at least one intersection is in front of ray origin
// For quadratic A*t^2 + 2*B*t + C = 0, if we want to check if any t >= 0:
// If C <= 0, ray origin is inside ellipsoid, so definitely intersects
if (c <= 0.0) {
return true;
}
// If both intersections exist and C > 0, check if the smaller root t1 >= 0
// t1 = (-b - sqrt(discriminant)) / a
// Since we need t1 >= 0: -b - sqrt(discriminant) >= 0
// This means: -b >= sqrt(discriminant), so b <= -sqrt(discriminant)
// Since sqrt(discriminant) >= 0, this means b <= 0
return b <= 0.0;
}
+6 -1
View File
@@ -80,7 +80,12 @@ Fragment getFragment() {
// Check if ray from fragment to sun intersects the ellipsoid (globe)
// This creates more accurate shadowing for rings
bool intersectsGlobe = rayIntersectsEllipsoid(posObj, sunPositionObj, vec3(0.0), ellipsoidRadii);
bool intersectsGlobe = rayIntersectsEllipsoid(
posObj,
sunPositionObj,
vec3(0.0),
ellipsoidRadii
);
// shadow == 1.0 means it is not in shadow
float shadow = intersectsGlobe ? 0.05 : 1.0;
+2 -2
View File
@@ -850,8 +850,8 @@ glm::vec3 RingsComponent::camPositionObj() const {
return _camPositionObjectSpace;
}
void RingsComponent::setEllipsoidRadii(const glm::vec3& radii) {
_ellipsoidRadii = radii;
void RingsComponent::setEllipsoidRadii(glm::vec3 radii) {
_ellipsoidRadii = std::move(radii);
}
void RingsComponent::onReadinessChange(ReadinessChangeCallback callback) {
+1 -1
View File
@@ -84,7 +84,7 @@ public:
glm::vec3 sunPositionObj() const;
glm::vec3 camPositionObj() const;
void setEllipsoidRadii(const glm::vec3& radii);
void setEllipsoidRadii(glm::vec3 radii);
private:
void loadTexture();