Hello Guest

Render to PNG

  • 5 Replies
  • 7450 Views
*

Offline msx

  • *
  • 14
Render to PNG
« on: January 20, 2015, 16:49:30 »
Hello there :) It's nice to join you all!

So my problem is: i need to render and opengl frame to a file, without passing for a window. If i understood correctly, a possible path would be to use a framebuffer object to "render to texture", and then use direct access to the texture to retrieve the content and save it to a file.
The questions are:
- Will the tecnique work even if i don't open an opengl window? All examples use the generated texture in a regular opengl window, so i'm not sure it it works even without.
- All code for render-to-texture i found around are for older version of lwjgl. Do you have a bare bone example for current lwjgl version? Just need maybe rendering a triangle to a texture, the rest i can tackle, i think.

Thanks in advance.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Render to PNG
« Reply #1 on: January 20, 2015, 18:15:06 »
1) It will. OpenGL contexts without windows are a fairly standard thing for which I have no doubt that the LWJGL gurus have thought of before. As to how to do it, no idea.

2) All of the framebuffer code will be exactly the same in LWJGL 3 as in LWJGL 2. The only difference would be the window creation code and you're doing that differently anyway.

Edit: Found an example (Google is your friend) on this very forum which does very similar to what you want using PBuffers which can (apparently) be created outside of an OpenGL context and have their own context: http://forum.lwjgl.org/index.php?topic=3346.0
« Last Edit: January 20, 2015, 22:02:35 by quew8 »

Re: Render to PNG
« Reply #2 on: January 20, 2015, 19:26:14 »
Yes, rendering to a texture works if you don't render it into a window.
At least the last time I tried, it did.

What I don't know is if a display is needed for OpenGL to render at all.
After creating the correct othogonal matrices for the GUI and optimizing the RAM usage with a streaming engine, I usually fail to call the initialization function; As soon as that happens, you know I will search that stupid error for ten hours.

*

Offline msx

  • *
  • 14
Re: Render to PNG
« Reply #3 on: January 21, 2015, 09:18:37 »
Thanks! i'm almost there, i managed to render and save the texture. But i have a problem: if i remove the window creation altogether, it breaks with the message "There is no OpenGL context current in the current thread.".
I understood that a context must be created, which can be done with the windowId:

glfwMakeContextCurrent(windowID);
GLContext ctx = GLContext.createFromCurrent();

But how can i create a context without the window? I've read around that there's something known as PBuffer, and there was a PBuffer class in old lwjgl, but it's not there anymore. Is that the correct way to do?

Also, i've understood that glfw is a system that manage windows lifecycle. Should i remove it? What should i substitute glfwInit() with?

Thanks a lot!

*

Offline SHC

  • **
  • 94
    • GoHarsha.com
Re: Render to PNG
« Reply #4 on: January 21, 2015, 09:43:14 »
You can create an invisible window. Set the GLFW_VISIBLE hint to GL_FALSE to make the window hidden initially.

*

Offline msx

  • *
  • 14
Re: Render to PNG
« Reply #5 on: January 21, 2015, 10:50:28 »
Ok, that worked.. I hoped to avoid creating windows altogether but maybe it's not possible..

For everyone interested, you can find the minimal example i concocted here:

https://gist.github.com/anonymous/35f9a540a82ad369e4e3

not sure it's completely clean but it works.