PDA

View Full Version : Offscreen rendering ?



Thallius
01-23-2012, 07:19 AM
Hi,

I am very new in OpenGL but i develop software in C,C++ for over 25 years now. I am a specialist in developing digital video and photo software. Until now i only developed software with 2D graphics, but i want to go a step further and make 3D graphics which should be combined with video and photos.

For this i need a good 3D lib which let me render 3D worlds in a frame buffer, because i have to work with the result of the renderer for example to create a new video from it.

So first i need to now is:

Can I render images with OpenGL without opening a display window ? Just setup a 3D Model and let it render into a buffer and save this buffer as an image?

I know that there is something like FBO in OpenGL, but all i read about it was only to create textures which are used in the online window rendering. That's not what I'm looking for.

Thanks for your help.

Thallius

Alfonse Reinheart
01-23-2012, 07:56 AM
Can I render images with OpenGL without opening a display window ?

In general, no. You must at least create a window.

Whether you use the window is up to you. You don't have to render to it, after all. You can create the window, minimize it, and then just render to a Framebuffer Object (http://www.opengl.org/wiki/Framebuffer_Object).


I know that there is something like FBO in OpenGL, but all i read about it was only to create textures which are used in the online window rendering. That's not what I'm looking for.

Those examples all show you everything you need to know. They show you how to set up an FBO as a render target. They show you how to attach images to the target. They show you how to render to it.

Yes, they also show you how to use that rendered texture in a different scene to render to the default framebuffer. But just don't do that part. You don't have to copy-and-paste all of the example code into your project.

Thallius
01-23-2012, 08:05 AM
Hi Alfonse

thanks for your explanation. Just one question left. When i render into the FBO, how can i retrieve the image data for example to save it to disk ?

Regards

Thallius

thokra
01-23-2012, 08:46 AM
Check out http://www.opengl.org/sdk/docs/man4/xhtml/glReadPixels.xml

This reads an area of pixels from the currently bound read buffer and puts it into a memory segment pointed to by data.

You can switch the read buffer with glReadBuffer(GL_COLOR_ATACHMENTi). Also note that if you don't bind the FBO as a GL_READ_FRAMEBUFFER then you'll copy pixels from the default framebuffer. So the algorithm is:

a) Render to FBO
b) Set the color attachment you want to read from (if you've got only one attachment at COLOR_ATTACHMENT0 you don't need to call glReadBuffer() since it defaults to that value)
c) Bind the FBO as either GL_FRAMEBUFFER or GL_READ_FRAMEBUFFER (notice, GL_DRAW_FRAMEBUFFER and GL_READ_FRAMEBUFFER are explicit bindings while GL_FRAMEBUFFER binds the FBO as both)
d) ReadPixels from the FBO

Edit: Also make sure that you allocate enough data in client memory for the GL to write to. For instance, reading 4 RGBA pixels of GL_FLOAT means you'll have to allocate at least 4 * 4 * sizeof(GLfloat) in data (4 pixels, 4 components each, 4 bytes each).

Thallius
01-23-2012, 09:06 AM
Hi Thokra,

thanks for the help

Thallius

Dark Photon
01-23-2012, 05:35 PM
Can I render images with OpenGL without opening a display window ?
In general, no. You must at least create a window.
Not necessarily. Depends on your platform. See also: this thread (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=304434#Post3044 34).

Alfonse Reinheart
01-23-2012, 05:59 PM
That's why I said "In general."

thokra
01-24-2012, 01:38 AM
Thallius, I forgot to mention that readback operations such as glReadPixels are costly and should not be taken lightly when striving for high performance.

Bruce Wheaton
01-24-2012, 10:02 AM
That's why I said "In general."

AFAIK, Windows is the only platform that requires you to make an actual 'window'. Mac and iOS just needs a context, Linux and derivatives can use pbuffers (including Android, apparently).

So in general, no, you don't need an actual window.

Bruce