我正在尝试创建一个显示带有地形的地球仪的程序,并在 GPU 上执行所有纬度/经度/Alt 到 XYZ (ECEF)。
我已经编写了一个工作顶点着色器,它将表示经度、纬度和高度的 XYZ(分别)转换为正确的 XYZ(使用 ECEF)。
我做不到的是灯光。
只要没有地形数据,我就可以使用定向灯正确地照亮每个顶点。地形数据未正确点亮,我无法获得不同的坡度以获得正确的阴影。
这是我正在使用的顶点着色器:
const float a = 6378137.0;
const float f = 0.003352810664747480719845528618;
varying vec3 Normal;
varying vec3 ecPos;
vec3 LatLonAltToEcef(in vec3 latLonAlt)
{
vec3 v = latLonAlt;
float cosLat = cos(radians(v.y));
float sinLat = sin(radians(v.y));
float nfs = (1.0 - f) * (1.0 - f);
float C = 1.0 / (sqrt(cosLat * cosLat + nfs * sinLat * sinLat));
float S = nfs * C;
float lon = radians(v.x);
float h = v.z;
v.x = (a * C + h) * cosLat * cos(lon) / a;
v.y = (a * C + h) * cosLat * sin(lon) / a;
v.z = (a * S + h) * sinLat / a;
return v;
}
vec4 LatLonAltToEcef(in vec4 latLonAlt)
{
vec3 ecef = LatLonAltToEcef(latLonAlt.xyz);
return vec4(ecef.x, ecef.y, ecef.z, latLonAlt.w);
}
void main(void)
{
vec4 v = LatLonAltToEcef(gl_Vertex); //x=lon, y=lat, z=alt
ecPos = vec3(gl_ModelViewMatrix * v);
Normal = normalize(gl_NormalMatrix * v.xyz);
vec4 lightPos = LatLonAltToEcef(gl_LightSource[0].position);
vec3 lightDir = normalize(gl_NormalMatrix * lightPos.xyz);
float NdotL = max(dot(Normal, lightDir), 0.0);
vec4 diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
gl_FrontColor = NdotL * diffuse;
gl_Position = gl_ModelViewProjectionMatrix * v;
}
为了绘制地球网格,需要执行以下操作:
for (float lat = -90; lat < 90; lat += 5)
{
glBegin(GL_LINE_LOOP);
for (float lon = -180; lon < 180; lon += 5)
glVertex3f(lon, lat, 0);
glEnd();
}
for (float lon = -180; lat < 180; lon += 5)
{
glBegin(GL_LINE_LOOP);
for (float lat = -90; lon < 90; lon += 5)
glVertex3f(lon, lat, 0);
glEnd();
}
谁能指导我如何正确遮蔽地形?