Author Topic: Render scene and save screenshot to file  (Read 4217 times)

neothemachine

  • Jr. Member
  • **
  • Posts: 5
Render scene and save screenshot to file
« on: March 12, 2012, 03:34:17 PM »
Hi,

Is it possible to render a simple scene (without animation) and save a screenshot of it (RGBA) without displaying any windows? It should behave like a command line application in my case. If it's not possible yet, can it be done? Which parts would have to be changed/extended?

Cheers
neo

jerome_j

  • Administrator
  • *****
  • Posts: 116
    • Bonzai Engine
Re: Render scene and save screenshot to file
« Reply #1 on: March 12, 2012, 07:53:05 PM »
This is already possible. Use offscreen rendering as show in tutorial 15 and especially with createAppOffscreen method, which is the only difference with windowed mode.

The tutorial also shows some helper to save frame in image and write in hard drive
« Last Edit: March 12, 2012, 07:54:56 PM by jerome_j »

neothemachine

  • Jr. Member
  • **
  • Posts: 5
Re: Render scene and save screenshot to file
« Reply #2 on: March 13, 2012, 10:42:08 AM »
Thanks for pointing me to that. I just setup everything but get the following error:


OpenGL api: JOGL2
Info: XInitThreads() called for concurrent Thread support
java.lang.RuntimeException: GPU Hardware error: OpenGL driver not available or Graphic card does not support PBuffer
Exception in thread "RenderLoop-0" java.lang.NullPointerException
   at jouvieje.scene.Scene.render(Unknown Source)
   at jouvieje.scene.target.jogl2.Offscreen_jogl2.onDisplay(Unknown Source)
   at jouvieje.opengl.RenderLoop$a.run(Unknown Source)
   at jouvieje.scene.target.jogl2.Offscreen_jogl2.a(Unknown Source)
   at jouvieje.scene.target.jogl2.Offscreen_jogl2.onStart(Unknown Source)
   at jouvieje.opengl.RenderLoop$a.run(Unknown Source)
Caused by: java.lang.IllegalStateException
   at jouvieje.scene.target.jogl2.Offscreen_jogl2.getGLApi(Unknown Source)
   ... 2 more


I have a recent Intel onboard graphics card. I think it's something else setup related, but what could it be?

neothemachine

  • Jr. Member
  • **
  • Posts: 5
Re: Render scene and save screenshot to file
« Reply #3 on: March 13, 2012, 10:59:18 AM »
I just tried using it with LWJGL like in the tutorial but then I get:

OpenGL api: LWJGL
Viewport=(0,0,512,512)
java.lang.UnsupportedOperationException
   at jouvieje.scene.target.lwjgl.Offscreen_lwjgl.addKeyListener(Unknown Source)
   at jouvieje.scene.Scene.init(Unknown Source)
   at OffscreenTest.init(OffscreenTest.java:81)
   at jouvieje.scene.target.lwjgl.Offscreen_lwjgl.onStart(Unknown Source)
   at jouvieje.opengl.RenderLoop$a.run(Unknown Source)
OpenGL version: 21
Viewport=(0,0,512,512)


I wanted to look at the source to understand where the error exactly is but couldn't find any source code. Is this a closed source project?

jerome_j

  • Administrator
  • *****
  • Posts: 116
    • Bonzai Engine
Re: Render scene and save screenshot to file
« Reply #4 on: March 13, 2012, 07:46:50 PM »
Thanks for the report, offscreen rendering is in fact broken with jogl, but it should work fine with LWJGL.

The UnsupportedOperationException is a warning log, ie no error (I'll remove it to have a cleaner output). For what I see everything is fine with your LWJGL log and the render loop should work fine (ie you should be able to render and copy the frame in a file).
Have you been able to render your model with lwjgl and access the framebuffer ?

Ps: yes the source is closed.

EDIT: offscreen support for JOGL is fixed in the build 1.2.4 13-03-2012
« Last Edit: March 13, 2012, 08:11:37 PM by jerome_j »

neothemachine

  • Jr. Member
  • **
  • Posts: 5
Re: Render scene and save screenshot to file
« Reply #5 on: March 15, 2012, 10:18:45 AM »
Thanks for fixing. It works with both APIs now. Is there a way to make the background transparent and output an RGBA png?

jerome_j

  • Administrator
  • *****
  • Posts: 116
    • Bonzai Engine
Re: Render scene and save screenshot to file
« Reply #6 on: March 15, 2012, 10:33:29 AM »
Yes, there is many ways:
  • 1) Request alpha channel in the frame buffer (use capabilities argument of ApplicationSettings) and set clearing color to black with a 0 alpha. The model will write alpha of 1 in the area of the model so you'll get transparent background when saving to rgba image.
  • 2) Set a clearing color to a specific value and when saving the frame buffer to image, remove pixels with this color by setting alpha to 0 (you don't need alpha channel in frame buffer)

If you look at the tutorial sources, one of them is a window with transparent background (window with transparent background is only supported with JOGL right now).

neothemachine

  • Jr. Member
  • **
  • Posts: 5
Re: Render scene and save screenshot to file
« Reply #7 on: March 15, 2012, 11:02:40 AM »
Ok, it works! Thanks.

I'm currently running on Linux with onboard Intel graphics where the driver doesn't support anti-aliasing yet. I soon can test it on a proper machine but in the mean time, can you tell me whether anti-aliasing works with offscreen rendering/screenshots and which effects this has with the clearing color? I mean, do I also get exported pixels which are half-transparent in the anti-aliased areas or is it just a hard cut?

Thanks for taking time
neo

jerome_j

  • Administrator
  • *****
  • Posts: 116
    • Bonzai Engine
Re: Render scene and save screenshot to file
« Reply #8 on: March 15, 2012, 10:17:12 PM »
Yes exactly, you should get alpha also 'antia-aliased' meaning value in between 0 and 1. That's what I get on card with multisampling support ;)

Not sure why but lwjgl fail to initialize the offscreen buffer with multisampling (which seems a bug in lwjgl), but works fine with jogl.

Edit: this was indeed a bug in LWJGL which has now been fixed.
« Last Edit: July 08, 2013, 09:55:34 AM by jerome_j »