<commit_before>/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2018                                                               *
 *                                                                                       *
 * 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.                                         *
 ****************************************************************************************/

#include "PowerScaling/powerScaling_fs.hglsl"
#include "fragment.glsl"

in vec2 vs_st;
in vec4 vs_position;

uniform sampler1D discTexture;
uniform vec2 textureOffset;
uniform float opacity;
uniform float eccentricity;
uniform float semiMajorAxis;

Fragment getFragment() {
    // Moving the origin to the center
    vec2 st = (vs_st - vec2(0.5)) * 2.0;

    // The length of the texture coordinates vector is our distance from the center
    float outer;
    float inner;
    
    float E = eccentricity;
    float AU = semiMajorAxis;
    float BU = AU * sqrt(1.0 - pow(E, 2.0)); // semi-minor axis
    float CU = sqrt(pow(AU, 2.0) - pow(BU, 2.0));
    float apo = AU * (1 + E);

    float AL = AU - textureOffset.x * 149597870700.0 - textureOffset.y * 149597870700.0;
    float BL = AL * sqrt(1.0 - pow(E, 2.0));
    float CL = sqrt(pow(AL, 2.0) - pow(BL, 2.0));
    float apo_inner = AL * (1 + E);

    if(eccentricity <= 0.000000){
        outer = pow(st.x, 2.0) / pow(AU/apo, 2.0)  +  ( pow(st.y, 2.0) / (pow(BU/apo, 2.0)));
        inner = pow(st.x, 2.0) / pow(AL/apo, 2.0)  +  ( pow(st.y, 2.0) / (pow(BL/apo, 2.0)));
    }  
    else {
        outer = pow((st.x + CU/apo), 2.0) / pow(AU/apo, 2.0)  +  pow(st.y, 2.0) / (pow(BU/apo, 2.0));
        inner = pow((st.x + CL/apo), 2.0) / pow(AL/apo, 2.0)  +  pow(st.y, 2.0) / (pow(BL/apo, 2.0));
    }
    
    if (outer > 1.0) // point is outside outer ellipse
        discard;
    if (inner < 1.0) // point is inside inner ellipse
        discard;

    // Remapping the texture coordinates
    vec2 dir = normalize(st);

    // Find outer ellipse: where along the direction is the equation = 1
    float scale;
    if (eccentricity <= 0.000000) {
        scale = sqrt((pow((AU/apo) * (BU/apo), 2)) / ((pow((BU/apo) * dir.x, 2)) + (pow((AU/apo) * dir.y, 2))));
    }
    else {
        float first = -(pow(BU/apo, 2.0) * dir.x * (CU/apo)) / ( pow((BU/apo) * dir.x, 2.0) + pow((AU/apo) * dir.y, 2.0));
        float second = pow((pow(BU/apo, 2.0) * dir.x * (CU/apo))  /  (pow((BU/apo) * dir.x, 2.0) + pow((AU/apo) * dir.y, 2.0)), 2.0);
        float third = (pow( (BU/apo) * (CU/apo) , 2.0) - pow((AU/apo) * (BU/apo), 2.0)) / (pow((BU/apo) * dir.x, 2.0) + pow((AU/apo) * dir.y, 2.0));
        scale = first + sqrt(second - third);
    }
    
    vec2 max = dir * scale;
    vec2 min = max * (apo_inner/apo);

    float distance1 = distance(max, min);
    float distance2 = distance(max, st);
    float textureCoord = distance2 / distance1;

    vec4 diffuse = texture(discTexture, textureCoord);
    diffuse.a *= opacity;

    // The normal for the one plane depends on whether we are dealing
    // with a front facing or back facing fragment
    vec3 normal;
    // The plane is oriented on the xz plane
    // WARNING: This might not be the case for Uranus
    if (gl_FrontFacing) {
        normal = vec3(-1.0, 0.0, 0.0);
    }
    else {
        normal = vec3(1.0, 0.0, 0.0);
    }

    Fragment frag;
    frag.color = diffuse;
    frag.depth = vs_position.w;
    return frag;
}<commit_msg>Simplify shader and increase readability<commit_after>/*****************************************************************************************
 *                                                                                       *
 * OpenSpace                                                                             *
 *                                                                                       *
 * Copyright (c) 2014-2018                                                               *
 *                                                                                       *
 * 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.                                         *
 ****************************************************************************************/

#include "PowerScaling/powerScaling_fs.hglsl"
#include "fragment.glsl"

in vec2 vs_st;
in vec4 vs_position;

uniform sampler1D discTexture;
uniform vec2 textureOffset;
uniform float opacity;
uniform float eccentricity;
uniform float semiMajorAxis;

const float AstronomicalUnit = 149597870700.0; // m
const float Epsilon = 0.0000001;

// Compute semi minor axis from major axis, a, and eccentricity, e
float semiMinorAxis(float a, float e) {
    return a * sqrt(1.0 - pow(e, 2.0));
}

// If returned value <= 1, the point is insdie or on the ellipse specified by the input:
// a and b are the semi-major and semi-minor axes, respectively.
// cx is the displacement of the center of the ellipse along the x-axis (for an orbit,
// the y-displacement is always zero)
float ellipseTest(vec2 point, float a, float b, float cx) {
    float x = point.x;
    float y = point.y;
    return (pow(x - cx, 2.0) / (a*a)) + ((y*y) / (b*b));
}

Fragment getFragment() {
    // Moving the origin to the center
    vec2 st = (vs_st - vec2(0.5)) * 2.0;

    float AUpper = semiMajorAxis;
    float BUpper = semiMinorAxis(AUpper, eccentricity); 
    float CUpper = sqrt(AUpper*AUpper - BUpper*BUpper);
    float outerApoapsisDistance = AUpper * (1 + eccentricity); 

    float ALower = AUpper - AstronomicalUnit * (textureOffset.x + textureOffset.y);
    float BLower = semiMinorAxis(ALower, eccentricity);
    float CLower = sqrt(ALower*ALower - BLower*BLower);
    float innerApoapsisDistance = ALower * (1 + eccentricity); 

    // Normalize based on outer apoapsis distance (size of plane)
    float AU_n = AUpper / outerApoapsisDistance;
    float BU_n = BUpper / outerApoapsisDistance; 
    float CU_n = CUpper / outerApoapsisDistance;
    float AL_n = ALower / outerApoapsisDistance;
    float BL_n = BLower / outerApoapsisDistance;
    float CL_n = CLower / outerApoapsisDistance;

    if (eccentricity <= Epsilon) {
        CU_n = 0.0;
        CL_n = 0.0;
    }  

    float outer = ellipseTest(st, AU_n, BU_n, -CU_n); 
    float inner = ellipseTest(st, AL_n, BL_n, -CL_n);
    if (outer > 1.0 || inner < 1.0) { 
        // point is outside outer ellipse or inside inner eliipse
        discard;
    }

    // Remapping the texture coordinates
    vec2 dir = normalize(st);

    // Find outer ellipse: where along the direction does the equation == 1?
    float denominator = pow(BU_n * dir.x, 2.0) + pow(AU_n * dir.y, 2.0);
    float first = -(pow(BU_n, 2.0) * dir.x * CU_n) / denominator;
    float second = pow((pow(BU_n, 2.0) * dir.x * CU_n)  /  denominator, 2.0);
    float third = (pow(BU_n * CU_n, 2.0) - pow(AU_n * BU_n, 2.0)) / denominator;

    float scale = first + sqrt(second - third);
    
    vec2 max = dir * scale;
    vec2 min = max * (innerApoapsisDistance / outerApoapsisDistance);

    float distance1 = distance(max, min);
    float distance2 = distance(max, st);
    float textureCoord = distance2 / distance1;

    vec4 diffuse = texture(discTexture, textureCoord);
    diffuse.a *= opacity;

    // The normal for the one plane depends on whether we are dealing
    // with a front facing or back facing fragment
    vec3 normal;
    // The plane is oriented on the xz plane
    // WARNING: This might not be the case for Uranus
    if (gl_FrontFacing) {
        normal = vec3(-1.0, 0.0, 0.0);
    }
    else {
        normal = vec3(1.0, 0.0, 0.0);
    }

    Fragment frag;
    frag.color = diffuse;
    frag.depth = vs_position.w;
    return frag;
}
{'fraction_non_alphanumeric': 0.13224368499257058, 'fraction_numerical': 0.0237741456166419, 'mean_word_length': 1.8220649895178198, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4219782', 'n_tokens_mistral': 3229, 'n_tokens_neox': 2934, 'n_words': 1042}