Board index FlightGear Development Nasal

Project 3D to 2D coordinates

Nasal is the scripting language of FlightGear.

Project 3D to 2D coordinates

Postby gordonshamway23 » Sun Nov 22, 2020 8:26 pm

Does anyone know if there is a way in nasal to transform 3D coordinates (cartesian or geographical) to 2D screen coordinates of the actual view / camera?
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Project 3D to 2D coordinates

Postby Thorsten » Mon Nov 23, 2020 7:18 am

Nasal doesn't conceptually have access to that kind of information, pretty much only the vertex shader can know screen coordinates and the fragment shader does for sure. The relevant transformation matrices are known in C++ space, but conceptually the shaders don't necessarily have to use them in that form.

What exactly are you trying to do?
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Project 3D to 2D coordinates

Postby gordonshamway23 » Mon Nov 23, 2020 7:29 pm

I'm trying to position a svg canvas element on the desktop canvas so that it is pointing to a certain 3d object or coordinate, like a pointer/marker or similar. (Example below)

Image

I was hoping maybe it is possible to reconstruct the model view projection matrix of the camera via some information in the property tree.
But i think it is not possible. Conceptually it would be nice if one had access from nasal to the cameras or at least some information of it like projection and view matrix.

Have a nice day.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Project 3D to 2D coordinates

Postby Hooray » Mon Nov 23, 2020 8:01 pm

Speaking in general, you might want to take a look at the Canvas HUD - we have previously covered different methods to add "virtual" elements to either the HUD or the scene:

search.php?st=0&sk=t&sd=d&sr=posts&keywords=canvas+hud+tunnel
search.php?st=0&sk=t&sd=d&sr=posts&keywords=canvas+kml

PS: Kudos for figuring out the Canvas desktop placement :wink:
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Project 3D to 2D coordinates

Postby gordonshamway23 » Mon Nov 23, 2020 8:53 pm

Thx, information about canvas desktop is'nt that hard to find :wink: , see http://wiki.flightgear.org/Canvas_Snippets#Accessing_the_Canvas_Desktop.

Concerning the HUD idea, I do not have much experience with huds. Can they be used indepentently from an aircraft and the aircraft's view direction?
I want my markers also to function in external views. Is there any documentation about virtual elements on hud canvas? I could not find something helpful for now.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Project 3D to 2D coordinates

Postby Hooray » Mon Nov 23, 2020 9:27 pm

Yeah, I am aware of where you must have found that info, since I wrote those docs ;-)

A HUD that is shown via the desktop placement should also work for exterior views, and even without any particular aircraft/FDM.

Regarding your other questions, I'd suggest to look up the responses in the threads that I linked to - a number of people have done similar things in the past, using either the Canvas or the model manager (to place actual 3d models in the scene) - or a combination of both:

http://wiki.flightgear.org/FlightGear_M ... Adventures
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Project 3D to 2D coordinates

Postby Thorsten » Tue Nov 24, 2020 8:18 am

I was hoping maybe it is possible to reconstruct the model view projection matrix of the camera via some information in the property tree.


Actually you can (sort of) do that using the view axis, your own position and the relative position to the object in question. As Hooray said, it's a pretty frequent problem in HUD design, so studying things like targeting recticles on HUDs will probably give you the required insight.
Thorsten
 
Posts: 12490
Joined: Mon Nov 02, 2009 9:33 am

Re: Project 3D to 2D coordinates

Postby Hooray » Tue Nov 24, 2020 8:59 am

Spending 30 minutes reading through various "Canvas + HUD + projection" related topics will probably provide what the OP needs.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Project 3D to 2D coordinates

Postby gordonshamway23 » Tue Nov 24, 2020 9:07 pm

First of all thanks for your tips. I will do some further investigation on the topic. I can post my findings if I find something helpful.

Is there maybe an aircraft in flightgear i could study, which has markers on the hud, targeting 3D objects? I can think of enemies marked on the hud in a fighter jet for example.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Project 3D to 2D coordinates

Postby Hooray » Tue Nov 24, 2020 9:20 pm

Like I said previously, running a forum search should provide sufficient pointers to get started easily:

Drawing lines in the sky/environment
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).
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Project 3D to 2D coordinates

Postby gordonshamway23 » Sat Nov 28, 2020 1:00 pm

I tried to solve the problem using the camera parameters from the property tree, constructing a projection / view matrix and projecting my geo coordinate to screen using nasal only.
Here is what I have come up with. As far as I have tested, it is working, at least in a single window scenerio.

Have a nice day.

Code: Select all
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};
}
Last edited by gordonshamway23 on Sat Nov 28, 2020 9:14 pm, edited 2 times in total.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Project 3D to 2D coordinates

Postby Hooray » Sat Nov 28, 2020 1:09 pm

You should consider documenting your journey in the form of a little tutorial, so that people interested in similar projects can follow your tutorial - feel free to use the wiki for that :wink:
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Project 3D to 2D coordinates

Postby gordonshamway23 » Sat Nov 28, 2020 7:07 pm

Sure, why not.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Re: Project 3D to 2D coordinates

Postby Hooray » Sat Nov 28, 2020 9:07 pm

BTW: Regarding your assert(), unless you have previously defined a "false" variable, I don't think that'll work as expected, since there are no boolean true/false values in Nasal. Usually, this is the kind of thing where die("error message") would be used.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Project 3D to 2D coordinates

Postby gordonshamway23 » Sat Nov 28, 2020 9:12 pm

Ah, you are right. Always stepping into this trap, I just can get used to missing true and false keywords. I will change it. Thanks for that.
gordonshamway23
 
Posts: 24
Joined: Sun Nov 22, 2020 8:15 pm

Next

Return to Nasal

Who is online

Users browsing this forum: No registered users and 3 guests