Skip to content

Improve -p option to support true perspective projections #8816

Open

Description

@Esteban82

The -p option currently produces what seems to be an orthographic projection, where all objects appear with the same size regardless of their distance from the viewer. This projection maintains parallel lines and provides no sense of depth.

A true perspective projection would make distant objects appear smaller, with lines converging toward vanishing points — similar to how real cameras or rendering engines represent 3D scenes. You can see this link for some interactive examples.

Implementation:
I would be interested in working on this improvement myself. However, I have not been able to find where in the GMT source the code handling the -p option (projection and 3D transformations) is implemented. Could you please indicate which source file(s) contain this logic?

I believe some of the required functionality might already exist in GMT through the General Perspective projection, so this could potentially be adapted or reused for implementing a true perspective view.

Activity

  1. added
    feature request
    on Oct 22, 2025
  2. ES-Solar commented on Oct 22, 2025

    @ES-Solar
    Collaborator

    I'm still trying to learn my way around the GMT code. It looks to me like the -p option state is a boolean stored in GMT->common.p.active variable. That then triggers the appropriate projection configuration based on parsing the -p option modifiers in the gmt_init.c:gmtinit_parse_p_option() function. Otherwise, I only see the common.p.active variable used to throw an error for an inverse projection in mapproject.c

  3. joa-quim commented on Oct 22, 2025

    @joa-quim
    Member

    -p was not thought to be a projection parameter. What it controls is angle of rotation and view elevation. But perhaps a view distance or camera opening could be added too.

    What I do when searching where things are done is to use the all-files-search of Visual Studio Code (Shift+Ctrl+F). For that one needs to load the all src dir as as workspace. Than ... see what happens. -p for sure saves the angles of rotation & elevation (or their cos/sinus) somewhere in a struct. Than repeat the process, where do those angles are used. It will be in one of the plotting functions, but this is still to broad.

  4. Esteban82 commented on Oct 22, 2025

    @Esteban82
    MemberAuthor

    Thanks, I found -p here: (

    struct p { /* -p<az>[/<el>[/<z0>]]+wlon0/lat0[/z0]][+vx0[cip]/y0[cip]] */
    )

    struct p {	/* -p<az>[/<el>[/<z0>]]+wlon0/lat0[/z0]][+vx0[cip]/y0[cip]] */
    		bool active;
    		bool do_z_rotation;	/* true if rotating plot about a vertical axis */
    		double z_rotation;	/* Rotation of <angle> about vertical axis */
    	} p;
    

    Now, I would have to search for do_z_rotation, right?

    I found this in gmt_init.c and in gmt_plot.c. Am I the rigth path?

    if (k == 1) { GMT->common.p.do_z_rotation = true; el = 90.0;}

    if (GMT->common.p.do_z_rotation) { /* Need a rotation about z of the whole page */

  5. Esteban82 commented on Oct 22, 2025

    @Esteban82
    MemberAuthor

    -p was not thought to be a projection parameter. What it controls is angle of rotation and view elevation. But perhaps a view distance or camera opening could be added too.

    I have found this (https://github.com/carltheperson/blockamok) where (I think) only one parameter (FOV_ANGLE) is needed. Maybe other two parameters could be add.

  6. joa-quim commented on Oct 22, 2025

    @joa-quim
    Member

    Am I the rigth path?

    Possibly (didn't) check. But remembered while driving back home that some 2-3 years ago I made a modification to allow a negative elevations. That must have been in the interest zone. Try to find it (some PR of me).

  7. joa-quim commented on Oct 22, 2025

    @joa-quim
    Member

    I have found this (https://github.com/carltheperson/blockamok) where (I think) only one parameter (FOV_ANGLE) is needed.

    That thing has pretty heavy dependencies.

  8. Esteban82 commented on Oct 23, 2025

    @Esteban82
    MemberAuthor

    But remembered while driving back home that some 2-3 years ago I made a modification to allow a negative elevations.

    Thank. I found the issue #7349 which works with gmt_init.c.

  9. Esteban82 commented on Dec 16, 2025

    @Esteban82
    MemberAuthor

    Update on this topic

    GMT uses the PostScript (PS) language to generate figures. Native PostScript only supports affine transformations, which is why GMT’s current -p option is limited to affine 3D projections (rotation, translation, and scaling) and cannot produce true perspective projections by itself.

    To obtain real 3D perspective, there are essentially two options:

    1. Implement the perspective projection in C and pass the projected coordinates to PostScript.
    2. Implement the perspective projection directly in PostScript.

    Regarding the second option, I found the PostScript package ps3d.inc, developed by Bill Casselman and described in his book Mathematical Illustration (more info here: https://personal.math.ubc.ca/~cass/graphics/manual/). From what I have seen, it provides true 3D perspective capabilities, including real perspective projections, text rendered in perspective, and spheres and other regular polyhedra with shading. Examples can be found in Chapters 13 and 14 of the book.

    While performing the 3D calculations in C would likely be faster, I believe that approach would complicate features such as rendering text in true perspective. For this reason, my current idea is to continue exploring option 2, i.e., performing the perspective calculations in PostScript. Once this is implemented and evaluated, it should then be possible to assess whether performance becomes an issue and, if so, consider ways to optimize or partially move computations elsewhere.

    I contacted the author by email, and he explicitly authorized the inclusion of this package in GMT, provided proper attribution is given.

    I will continue investigating this direction. In the meantime, I would welcome any general comments or suggestions on how best to proceed.

  10. joa-quim commented on Dec 16, 2025

    @joa-quim
    Member

    I don't follow what you are trying to achieve. Do you want to modify existing PS files? I don't think we can do perspectives this way. That I know, not even full affine transforms can be done (we cannot do the shear part). Only way, that I can think of (but may be wrong), is to create a perspective transform (no idea how) in the GMT lib.

  11. Esteban82 commented on Dec 17, 2025

    @Esteban82
    MemberAuthor

    Correct. The built-in funcions in PS cannot do the shear part. The ps3d.inc package has all the functions to do the perspective transformation.

  12. joa-quim commented on Dec 17, 2025

    @joa-quim
    Member

    And how do you use it?

  13. Esteban82 commented on Dec 17, 2025

    @Esteban82
    MemberAuthor

    You have to include some commands in the header of the ps file. For example, this is the PS file that creates the image below

    %!PS-Adobe-2.0
    %%BoundingBox: 0 0 372 180
    
    
    /w 372 def
    /h 180 def
    
    (ps3d.inc) run
    (epsetup.inc) run
    (cube.inc) run
    
    % ------------------------------------------------
    
    [0 0 6 1] set-display
    0 0 -4 translate3d
    
    /light [0 1 0 0] def
    /floor [0 1 0 2] def
    
    /wd 3 def
    /dp 3 def
    /fl floor 3 get neg def
    
    /grid {
    wd neg 1 wd {
      /i exch def
      i fl dp neg moveto3d
      i fl dp     lineto3d
    } for
    dp neg 1 dp {
      /i exch def
      wd neg fl i moveto3d
      wd     fl i lineto3d
    } for
    stroke
    } def
    
    page-begin
    0 0.5 translate
    
    gsave
    0.8 0.8 0.8 setrgbcolor
    newpath
    grid
    grestore
    
    gsave3d
    % the shadow
    gsave
    0.6 0.6 0.6 setrgbcolor
    
    floor light plane-project
    
    [1 1 1] A rotate3d
    -0.5 -0.5 -0.5 translate3d
    
    cube {
      % [ n [P[0] P[1] ... P[n-1]]]
      1 get /f exch def
      /ell f length def
      newpath
      f ell 1 sub get 
      aload pop moveto3d
      f {
        aload pop lineto3d
      } forall
      fill
      
    } forall
    
    grestore
    grestore3d
    
    gsave3d
    
    [1 1 1] A rotate3d
    -0.5 -0.5 -0.5 translate3d
    /O origin cim3d transform3d def
    % O = origin in current coordinates
    /L light cim3d transform3d normalized def
    % L = light in current coordinates
    
    cube {
      % [ n [P[0] P[1] ... P[n-1]]]
      dup 
      0 get /n exch def
      1 get /f exch def
      % n = normal function
      /ell f length def
        n 
        O 
        dot-product 
      0 ge 
      {
        newpath
        f ell 1 sub get aload pop moveto3d
        f {
          aload pop lineto3d
        } forall
        gsave
          n normalized L 
        dot-product
        [0.40 0.60 0.80 1] 
        shade 0 0 setrgbcolor
        fill
        grestore
        stroke
      } if
    } forall
    
    grestore3d
    
    grestore 
    showpage
    

    Link: https://personal.math.ubc.ca/~cass/graphics/manual/code/cube5.txt

    `

    Image

    `

  14. joa-quim commented on Dec 17, 2025

    @joa-quim
    Member

    Yes, but you are creating a new PS file. Not modifying a GMT PS to show it in a perspective view. So again, I fail to see the interest of this in a GMT context.

  15. Esteban82 commented on Dec 17, 2025

    @Esteban82
    MemberAuthor

    Well, I still have to see how to modify a GMT PS to make a perspective view. But I know that it is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions