I was hoping maybe it is possible to reconstruct the model view projection matrix of the camera via some information in the property tree.
TheTom wrote:If you want to do something similar like a HUD, you could place a transparent canvas in front of the pilots viewpoint and draw the lines onto it. If you calculate the direction from the pilots viewpoint to the according position in the environment and intersect an imaginary line between this two points with the canvas you can draw it exactly where the point is located in the environment. You just need the two angles describing the direction to the point (up/down and left/right).
var mat_vec_mult = func(m,v)
{
var sm=size(m);
var sv=size(v);
if(sm==16 and sv==4)
{
#4x4 matrix multiplied by 4 component vector
return
[
m[0]*v[0] + m[1]*v[1] + m[2]*v[2] + m[3]*v[3] ,
m[4]*v[0] + m[5]*v[1] + m[6]*v[2] + m[7]*v[3] ,
m[8]*v[0] + m[9]*v[1] + m[10]*v[2] + m[11]*v[3] ,
m[12]*v[0] + m[13]*v[1] + m[14]*v[2] + m[15]*v[3]
];
}
die("Only multiplication of 4x4 matrix with 4 component vector supported. ");
}
var quat_conj = func(q)
{
#quaternion conjugate
return [q[0],-q[1],-q[2],-q[3]];
}
var quat_mult = func(q1,q2)
{
#quaternion multiplication, hamilton product
return [q1[0]*q2[0] - q1[1]*q2[1] - q1[2]*q2[2] - q1[3]*q2[3],
q1[0]*q2[1] + q1[1]*q2[0] - q1[2]*q2[3] + q1[3]*q2[2],
q1[0]*q2[2] + q1[1]*q2[3] + q1[2]*q2[0] - q1[3]*q2[1],
q1[0]*q2[3] - q1[1]*q2[2] + q1[2]*q2[1] + q1[3]*q2[0]];
}
var rot_vec3_by_quat = func(v,q)
{
#rotate 3 component vector by quaternion
var qv = [0, v[0], v[1], v[2]];
var qr=quat_mult(quat_mult(q,qv),quat_conj(q));
return [qr[1], qr[2], qr[3]];
}
var create_projection_matrix_oglu = func(fovy, aspect, znear, zfar)
{
# perspective projection matrix like in gluPerspective
# fovy must be in radians
var f=1.0/math.tan(fovy/2.0);
var zdiff=znear-zfar;
return [f/aspect, 0.0, 0.0, 0.0,
0.0, f, 0.0, 0.0,
0.0, 0.0, (zfar+znear)/zdiff, 2.0*zfar*znear/zdiff,
0.0, 0.0, -1.0, 0.0];
}
#input is a coordinate created with geo.Coord class (see geo.nas)
var geo_unproject_to_screen = func(geocoord)
{
var screen_w=getprop("sim/startup/xsize");
var screen_h=getprop("sim/startup/ysize");
var aspect=screen_w/screen_h;
var fovx=getprop("sim/current-view/field-of-view")*D2R;#in radians
var fovy = 2.0 * math.atan2(math.tan(fovx / 2.0) / aspect,1.0 );
var znear=getprop("sim/rendering/camera-group/znear");
var zfar=getprop("sim/rendering/camera-group/zfar");
var proj_mat = create_projection_matrix_oglu(fovy, aspect, znear, zfar);
var geocoord_pos = geocoord.xyz();
var viewer_pos = [getprop("sim/current-view/viewer-x-m"), getprop("sim/current-view/viewer-y-m"), getprop("sim/current-view/viewer-z-m")];
var quat_viewer_rot = [getprop("sim/current-view/raw-orientation[0]"), getprop("sim/current-view/raw-orientation[1]"), getprop("sim/current-view/raw-orientation[2]"), getprop("sim/current-view/raw-orientation[3]")];
var diff_vec = [geocoord_pos[0]-viewer_pos[0], geocoord_pos[1]-viewer_pos[1], geocoord_pos[2]-viewer_pos[2]];
var vec_rel_to_view=rot_vec3_by_quat(diff_vec,quat_viewer_rot);
append(vec_rel_to_view,1.0);
var vec_proj=mat_vec_mult(proj_mat,vec_rel_to_view);
var device_coords_xy = [vec_proj[0]/vec_proj[3],vec_proj[1]/vec_proj[3]];
var screen_coords_xy = [(device_coords_xy[0]+1.0)*0.5*screen_w, (1.0-(device_coords_xy[1]+1.0)*0.5)*screen_h];
return {screen_xy:screen_coords_xy, is_behind: vec_rel_to_view[2]>0.0};
}
Users browsing this forum: No registered users and 3 guests