[GRASS-dev] [GRASS GIS] #3606: m.nviz.image produces a screenshot

#3606: m.nviz.image produces a screenshot
----------------------+-------------------------
Reporter: epifanio | Owner: grass-dev@…
     Type: defect | Status: new
Priority: major | Milestone:
Component: Display | Version: svn-trunk
Keywords: | CPU: x86-64
Platform: Linux |
----------------------+-------------------------
Platform: Linux Ubuntu 18.04, 64 bit
GRASS version: 7.5 trunk revision #72985

Trying to run 'm.nviz.image' from sample location "nc_basic_spm_grass7"

returns a screenshot of the active window from whre I ran the command ...
I got a black ppm when running the above command from the terminal and I
got the attached image when running the same command from the WX gui
console.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------
Changes (by epifanio):

* Attachment "m.nviz.image.png" added.

This image (converted to png because of attachment size, original format:
ppm) has been generated by m.nviz.image by running the following command '
m.nviz.image elevation_map=elevation output=elev perspective=15' from the
WX Gui console.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hellik):

Replying to [ticket:3606 epifanio]:
> Platform: Linux Ubuntu 18.04, 64 bit
> GRASS version: 7.5 trunk revision #72985
>
>
> Trying to run 'm.nviz.image' from sample location "nc_basic_spm_grass7"
>
> returns a screenshot of the active window from whre I ran the command
...
> I got a black ppm when running the above command from the terminal and I
got the attached image when running the same command from the WX gui
console.
>
>

see #3600

this ticket seems to be a duplication? right?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:1&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

Hi, In my case, 'm.nviz.image' does produce a proper ppm file as output,
which I can open with an image viewer (e.g. gimp) but what is rendered in
that ppm is a screen grab of the software window from where the command
was issued. It seems that 'm.nviz.image' is acting like a software to make
screenshots ... (weird). In #606 the user experienced a non-readable ppm
output, the bug can be related, of course.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:2&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

If I remove line 480 in lib/ogsf/gsd_prim.c {{{glXSwapBuffers}}}, I'm
getting a similar result. Nviz draws to the back buffer and OGSF copies
the back buffer to the front before saving an image. You might be doing
double swapping for some reason. Please try the following patch.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:3&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------
Changes (by hcho):

* Attachment "lib-ogsf-gsd_prim.c.diff" added.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

Applied the patch on latest trunk rev. num.: 72990

print out the following output:

{{{
GRASS 7.5.svn (nc_basic_spm_grass7):~ > m.nviz.image
elevation_map=elevation output=elev perspective=15 --o
Loading raster map <elevation@PERMANENT>...
  100%
Loading raster map <elevation@PERMANENT>...
  100%
Translating colors from raster map <elevation@PERMANENT>...
  100%
buffer: both
buffer: both
buffer: both
buffer: both
buffer: both
buffer: both
buffer: both
buffer: swap
buffer: both
m.nviz.image complete. File <elev.ppm> created.

}}}

My grass_trunk is configured without X11 support:

{{{
GRASS is now configured for: x86_64-pc-linux-gnu

   Source directory: /home/epinux/dev/grass7_trunk
   Build directory: /home/epinux/dev/grass7_trunk
   Installation directory: ${prefix}/grass-7.5.svn
   Startup script in directory:${exec_prefix}/bin
   C compiler: gcc -g -O2
   C++ compiler: c++ -g -O2
   Building shared libraries: yes
   OpenGL platform: X11

   MacOSX application: no
   MacOSX architectures:
   MacOSX SDK:

   BLAS support: yes
   BZIP2 support: yes
   C++ support: yes
   Cairo support: yes
   DWG support: no
   FFTW support: yes
   FreeType support: yes
   GDAL support: yes
   GEOS support: yes
   LAPACK support: yes
   Large File support (LFS): yes
   libLAS support: yes
   MySQL support: no
   NetCDF support: yes
   NLS support: no
   ODBC support: yes
   OGR support: yes
   OpenCL support: no
   OpenGL support: yes
   OpenMP support: yes
   PDAL support: yes
   PNG support: yes
   POSIX thread support: yes
   PostgreSQL support: yes
   Readline support: yes
   Regex support: yes
   SQLite support: yes
   TIFF support: yes
   X11 support: no
   Zstandard support: yes

   using old PROJ 4 API

}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:4&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Replying to [comment:4 epifanio]:
> Applied the patch on latest trunk rev. num.: 72990
>
> print out the following output:
>
>
> {{{
> GRASS 7.5.svn (nc_basic_spm_grass7):~ > m.nviz.image
elevation_map=elevation output=elev perspective=15 --o
> Loading raster map <elevation@PERMANENT>...
> 100%
> Loading raster map <elevation@PERMANENT>...
> 100%
> Translating colors from raster map <elevation@PERMANENT>...
> 100%
> buffer: both
> buffer: both
> buffer: both
> buffer: both
> buffer: both
> buffer: both
> buffer: both
> buffer: swap
> buffer: both
> m.nviz.image complete. File <elev.ppm> created.
>
> }}}
>
> My grass_trunk is configured without X11 support:

This is my output:
{{{
buffer: back
buffer: both
buffer: back
buffer: both
buffer: back
buffer: both
buffer: back
buffer: swap
buffer: back
}}}

In your case, GS_set_draw(GSD_BACK) never worked. That's weird. Please add
{{{fprintf(stderr, "draw where: %d\n", where);}}} in line 2465 in
lib/ogsf/gs2.c. GSD_FRONT 1, GSD_BACK 2, and GSD_BOTH 3 are defined in
include/ogsf.h.

Anyway, I think you need X11 support because Nviz requires some Xlib
functions (XOpenDisplay, XCreatePixmap, XFree, ...) in lib/nviz/render.c.
Please try it again with X11 support enabled. Is there any specific reason
why you disabled it?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:5&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

Rebuilding with X support enabled didn't change the `m.nviz.image` output.
Adding `fprintf(stderr, "draw where: %d\n", where);`, as you instructed,
produce the following output:

{{{
(Mon Jul 16 14:19:48 2018)
m.nviz.image elevation_map=elevation output=elev perspective=15 --o
WARNING: File <elev.ppm> already exists and will be overwritten
Loading raster map <elevation@PERMANENT>...
Loading raster map <elevation@PERMANENT>...
Translating colors from raster map <elevation@PERMANENT>...
draw where: 2
buffer: both
draw where: 3
buffer: both
draw where: 2
buffer: both
draw where: 3
buffer: both
draw where: 2
buffer: both
draw where: 3
buffer: both
draw where: 2
buffer: both
buffer: swap
draw where: 2
buffer: both
m.nviz.image complete. File <elev.ppm> created.
(Mon Jul 16 14:19:48 2018) Command finished (0 sec)
}}}

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:6&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Replying to [comment:6 epifanio]:
> Rebuilding with X support enabled didn't change the `m.nviz.image`
output.
> Adding `fprintf(stderr, "draw where: %d\n", where);`, as you instructed,
produce the following output:
>
> {{{
> (Mon Jul 16 14:19:48 2018)
> m.nviz.image elevation_map=elevation output=elev perspective=15 --o
> WARNING: File <elev.ppm> already exists and will be overwritten
> Loading raster map <elevation@PERMANENT>...
> Loading raster map <elevation@PERMANENT>...
> Translating colors from raster map <elevation@PERMANENT>...
> draw where: 2
> buffer: both
> draw where: 3
> buffer: both
> draw where: 2
> buffer: both
> draw where: 3
> buffer: both
> draw where: 2
> buffer: both
> draw where: 3
> buffer: both
> draw where: 2
> buffer: both
> buffer: swap
> draw where: 2
> buffer: both
> m.nviz.image complete. File <elev.ppm> created.
> (Mon Jul 16 14:19:48 2018) Command finished (0 sec)
> }}}

Could you please attach your lib/ogsf/gs2.c, lib/ogsf/gsd_prim.c, and
include/ogsf.h? I know they must be the same as my trunk, but looks like
switch doesn't work at all? I'm confused.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:7&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

I made a gist of lib/ogsf/gs2.c, lib/ogsf/gsd_prim.c, and include/ogsf.h

https://gist.github.com/0a22197af301f2f64678efa408ea997f

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:8&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Replying to [comment:8 epifanio]:
> I made a gist of lib/ogsf/gs2.c, lib/ogsf/gsd_prim.c, and include/ogsf.h
>
> https://gist.github.com/0a22197af301f2f64678efa408ea997f

I think I know what happened. In gsd_prim.c, all gsd_bothbuffers,
gsd_frontbuffer, and gsd_backbuffer have "buffer: both\n". That's why I
was confused. Anyway, that's not the reason for your problem.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:9&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Maybe try size=100,100 and see what the output size is.

Not sure what it is, but you can also try to directly draw to the front
buffer without buffer swapping
1. Change line 223 in lib/nviz/draw.c to {{{GS_set_draw(GSD_FRONT);}}}
2. Comment out line 480 {{{glXSwapBuffers}}} from lib/ogsf/gsd_prim.c.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:10&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

@hco The two changes you proposed fixed the issue. I didn't test all the
`m.nviz.image` options, but I can now render a proper elevation map with
no errors.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:11&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

forgot to say, ... Thanks for your help!!!

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:12&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Replying to [comment:12 epifanio]:
> forgot to say, ... Thanks for your help!!!

You're welcome and thanks for testing, but it only solves your specific
problem. I still don't understand why drawing to the back buffer and
swapping it to the front doesn't work for you. Could you please try one
more thing?

1. Change line 223 in lib/nviz/draw.c to {{{GS_set_draw(GSD_BOTH);}}}
2. Uncomment line 480 {{{glXSwapBuffers}}} in lib/ogsf/gsd_prim.c

So the only different from trunk is {{{GS_set_draw(GSD_BOTH);}}} vs
{{{GS_set_draw(GSD_BACK);}}}.

Thanks.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:13&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by cmbarton):

When this is at a point where it seems to be working on Windows, let me
know and I'll recompile it for the Mac and test some more.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:14&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by epifanio):

Double checking I've the right changes applied to the right files, here is
a gist of the previous files:

https://gist.github.com/c6a7cb6019f2347add44f2dc6b35f4c7

With this configuration, I do have a proper PPM rendering the elevation
surface correctly.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:15&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Replying to [comment:15 epifanio]:
> Double checking I've the right changes applied to the right files, here
is a gist of the previous files:
>
> https://gist.github.com/c6a7cb6019f2347add44f2dc6b35f4c7
>
> With this configuration, I do have a proper PPM rendering the elevation
surface correctly.

I'm confused. Do you mean that the files in the gist work for you? That's
basically trunk + debug outputs.

In your last test, you drew to the front buffer without swapping
(different from your gist). Did they both work for you?

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:16&gt;
GRASS GIS <https://grass.osgeo.org>

#3606: m.nviz.image produces a screenshot
-----------------------+-------------------------
  Reporter: epifanio | Owner: grass-dev@…
      Type: defect | Status: new
  Priority: major | Milestone:
Component: Display | Version: svn-trunk
Resolution: | Keywords:
       CPU: x86-64 | Platform: Linux
-----------------------+-------------------------

Comment (by hcho):

Replying to [comment:14 cmbarton]:
> When this is at a point where it seems to be working on Windows, let me
know and I'll recompile it for the Mac and test some more.

r72997 works on Windows at least for me.

--
Ticket URL: <https://trac.osgeo.org/grass/ticket/3606#comment:17&gt;
GRASS GIS <https://grass.osgeo.org>