It not so simple as Z is only upwards in world space at latitude/longitude (90,0) it seems.

And in model space Z only points upwards if the aircraft is on a horizontal runway with 0 roll/pitch and the model in which the current triangle is in has not been rotated in any way from aircraft origin.

However its a lot of code, so its expensive to run every fragment. There must be a simpler way, have anyone done something similar before?

I noticed there are defined some uniforms called fg_ViewMatrixInverse and fg_ViewMatrix.

I have a hunch I can use those somehow, but I am not sure how they work, does anyone know?

Here is the code:

This needs to be defined:

- Code: Select all
`#extension GL_ARB_gpu_shader5 : enable`

Notice the 2 first methods and how to use them is shamelessly copied from ubershader:

- Code: Select all
`mat3 rotY(in float angle)`

{

mat3 rotmat = mat3(

cos(angle), 0.0, sin(angle),

0.0, 1.0, 0.0,

-sin(angle), 0.0, cos(angle)

);

return rotmat;

}

mat3 rotZ(in float angle)

{

mat3 rotmat = mat3(

cos(angle), -sin(angle), 0.0,

sin(angle), cos(angle), 0.0,

0.0, 0.0, 1.0

);

return rotmat;

}

vec3 upInViewSpace(float lat_deg, float lon_deg) {

float latRad = radians(90-lat_deg);

float lonRad = radians(lon_deg);

mat3 WorldFGInverse = inverse(rotY(latRad) * rotZ(lonRad));

vec3 up = vec3(0.0, 0.0, 1.0);// up in FG

up = WorldFGInverse * up;// up in world space

up = normalize(osg_ViewMatrix * vec4(up, 0.0)).xyz;// up in view space (as in away from ground)

return up;

}