Fix renderer crash when enabling earth curvature

This commit is contained in:
James Seibel
2023-08-20 18:11:12 -05:00
parent 35f5bce149
commit d5f3ac9fb8
2 changed files with 22 additions and 34 deletions
+20 -34
View File
@@ -8,6 +8,8 @@ out vec4 vertexColor;
out vec3 vertexWorldPos;
out float vertexYPos;
uniform bool whiteWorld;
uniform mat4 combinedMatrix;
uniform vec3 modelOffset;
uniform float worldYOffset;
@@ -19,6 +21,8 @@ uniform float mircoOffset;
uniform float earthRadius;
/**
* TODO in the future this and standard.vert should be merged together to prevent inconsistencies between the two
*
* Vertex Shader
*
* author: James Seibel
@@ -51,43 +55,25 @@ void main()
vertexWorldPos.x += mx;
vertexWorldPos.y += my;
vertexWorldPos.z += mz;
#if 0
// Old (disabled) vertex transformation logic - Leetom
// Calculate the vertex pos due to curvature of the earth
// We use spherical coordinates to calculate the vertex position
//if (vertexWorldPos.x == 0.0 && vertexWorldPos.z == 0.0)
//{
// // In the center. No curvature needed
//}
//else
//{
float theta = atan(vertexWorldPos.z, vertexWorldPos.x); // in radians (-pi, pi)
float trueY = earthRadius + vertexWorldPos.y; // true Y position, or height
float phi = sqrt(vertexWorldPos.z * vertexWorldPos.z + vertexWorldPos.x * vertexWorldPos.x) / trueY;
// Convert spherical coordinates to cartesian coordinates
vertexWorldPos.x = trueY * sin(phi) * cos(theta);
vertexWorldPos.z = trueY * sin(phi) * sin(theta);
vertexWorldPos.y = trueY * cos(phi) - earthRadius;
//}
#else
// new vertex transformation logic - stduhpf
float localRadius = earthRadius + vertexYPos;// vertexWorldPos.y + cameraPosition.y - Center_Y;
float phi = length(vertexWorldPos.xz) / localRadius;
vertexWorldPos.y += (cos(phi) - 1.0) * localRadius;
vertexWorldPos.xz = vertexWorldPos.xz * sin(phi) / phi;
#endif
// vertex transformation logic - stduhpf
float localRadius = earthRadius + vertexYPos;
float phi = length(vertexWorldPos.xz) / localRadius;
vertexWorldPos.y += (cos(phi) - 1.0) * localRadius;
vertexWorldPos.xz = vertexWorldPos.xz * sin(phi) / phi;
uint lights = meta & 0xFFu;
float light2 = (mod(float(lights), 16.0) + 0.5) / 16.0;
float light = (float(lights / 16u) + 0.5) / 16.0;
vertexColor = color * vec4(texture(lightMap, vec2(light, light2)).xyz, 1.0);
vertexColor = vec4(texture(lightMap, vec2(light, light2)).xyz, 1.0);
if (!whiteWorld)
{
vertexColor *= color;
}
gl_Position = combinedMatrix * vec4(vertexWorldPos, 1.0);
}
@@ -20,6 +20,8 @@ uniform float mircoOffset;
/**
* TODO in the future this and curve.vert should be merged together to prevent inconsistencies between the two
*
* Vertex Shader
*
* author: James Seibel