Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Exporting OpenGLes to Image : (6 Items)
   
Exporting OpenGLes to Image  
Hello All,

I'm currently working on generating an openGLes drawing on a Ptraw in photon and would like to export this "image drawn 
to a picture format like a png. I would like to avoid taking a screenshot with standard photon code, as this would cause
 the background to be the same as it is shown on the screen. It would be perfect if I could render one frame to an area 
in memory with a white background and then dump call another function to write this to a PNG.

I have almost no OPENGL experience, but I have found some documentation saying that it is possible in openGL, but it is 
not in OPENGLes, QNX's implementation of it.

Does anyone know if this is possible? A quick example or a list of the function calls would be perfect.

Thanks,
Kevin Raymond

Re: Exporting OpenGLes to Image  
Since you are using a PtRaw widget, I assume that you have a gf_surface_t of your eglContext. You can get the surfaces 
info by calling gf_surface_get_info(). Create an img_t structure based on the surface's info data (basically wrapping 
it). Now you can call img_write_file().
-Misha.
Re: Exporting OpenGLes to Image  
> Create an img_t structure based on the surface's info data (basically
> wrapping it). Now you can call img_write_file().

Thank you for your help, this is working nicely. However, the problem
we're running into is to save an img_t as a png with a transparent
background. Saving the surface as a png is easy enough, but we are not
able to get rid of the background.

Should this be happening on the surface or on the img_write_file()?
In other words, should the background be erased from the surface
or is the img library responsible for ignoring a specific color in the
surface?

I did try adding IMG_TRANSPARENCY to the img_t and specifying the
background color to no avail: it is always saved in the png. If you had
the time to provide a small example, I would be very grateful. 

QNX needs more love on google search.
Re: Exporting OpenGLes to Image  
Taking a quick look at the png codec, it does not appear to check the transparency flag on a write operation ... but do 
you really want transparency or alpha?

Could you not have a surface type of 8888 and write it out?
Re: Exporting OpenGLes to Image  
Thanks for the quick reply.

After doing more tests, I realized that the Photon Image Viewer doesn't
support transparent pngs. Loading my png with firefox gives a transparent
background while the image viewer gives me whatever background color I
had in the surface (from glClear()). ...am I making any sense here?

The last problem I'm having is that the same surface blitted to the PtRaw
gives a black background. It looks like the PgContextBlit() call doesn't
handle the alpha channel of the gf_surface. Do you have a hint on how to
do this?

For the record, here's what I have (error handling removed):

  gf_dev_t gf_dev;
  gf_dev_info_t info;

  gf_dev_attach(&gf_dev, 0, &info);

  gf_display_t gf_display;
  gf_display_info_t gf_display_info;

  gf_display_attach(&gf_display, gf_dev, 0, &gf_display_info);
  gf_surface_create(
    &g_gf_surface, gf_dev, 100, 100,
    gf_display_info.format, 0, 0);

  EGLDisplay display = eglGetDisplay(gf_dev);
  eglInitialize(display, 0, 0);

  EGLint attributes[] = {
    EGL_RED_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_BLUE_SIZE, 8,
    EGL_ALPHA_SIZE, 8,
    EGL_NONE
  };

  EGLConfig config;
  EGLint num_config = 0;

  eglChooseConfig(display, attributes, &config, 1, &num_config);

  EGLContext context =
    eglCreateContext(display, config, EGL_NO_CONTEXT, 0);
  EGLSurface surface =
    eglCreatePixmapSurface(display, config, g_gf_surface, 0);
  eglMakeCurrent(display, surface, surface, context);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glViewport(0, 0, 100, 100);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  glEnable(GL_LINE_SMOOTH);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE);

  glOrthox(fix(0), fix(100), fix(0), fix(100), fix(-1), fix(1));
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

After this, I create a PtWindow containing a PtRaw:

  PtInit(0);

  PtArg_t args[4];
  PhDim_t dim = {100, 100};

  PtSetArg(&args[0], Pt_ARG_DIM, &dim, 0);
  PtSetArg(&args[1], Pt_ARG_WINDOW_TITLE, "", 0);
  PtWidget_t* window =
    PtCreateWidget(PtWindow, Pt_NO_PARENT, 2, args);

  PtSetArg(&args[1], Pt_ARG_RAW_DRAW_F, raw_draw, 0);
  PtWidget_t* raw = PtCreateWidget(PtRaw, Pt_DFLT_PARENT, 2, args);

  // g_context is a PdOffscreenContext_t*
  g_context = PdCreateOffscreenContextGF(g_gf_surface);

Here's the draw function (where draw_rect() is a wrapper around
glDrawArrays with GL_TRIANGLE_STRIP):

  glColor4f(0.0, 0.0, 1.0, 0.5);
  draw_rect(20, 20, 70, 70);

  glColor4f(0.0, 1.0, 0.0, 0.5);
  draw_rect(30, 30, 90, 50);

  PhRect_t src_rect = {{0, 0}, {100, 100}};
  PhRect_t dest_rect = {{0, 0}, {100, 100}};
  PgContextBlit(g_context, &src_rect, PhDCGetCurrent(), &dest_rect);

  gf_surface_info_t info;
  gf_surface_get_info(g_gf_surface, &info);		

  img_t img;
  memset(&img, 0, sizeof(img_t));
  img.access.direct.data = info.vaddr;
  img.access.direct.stride = info.stride;
  img.w = info.w;
  img.h = info.h;
  img.format = gf_to_img_format(info.format);
  img.npalette = info.palette.ncolors;
  img.palette = info.palette.colors;
  img.flags =
    IMG_FORMAT | IMG_W | IMG_H | IMG_DIRECT | IMG_PALETTE;

  img_write_file(ilib, "test.png", 0, &img);
Re: Exporting OpenGLes to Image  
What display mode are you in, 8888?

For the blit operation, I believe you have to use PgSetAlpha().  If you don't turn on some sort of alpha operation, the 
pixels just get pounded into the frame buffer - i.e. they don't get "alpha'ed" with what is behind them.