#include <Inventor/SoOffscreenRenderer.h>
Public Types | |
enum | Components { LUMINANCE = 1, LUMINANCE_TRANSPARENCY = 2, RGB = 3, RGB_TRANSPARENCY = 4 } |
Public Member Functions | |
SoOffscreenRenderer (const SbViewportRegion &viewportregion) | |
SoOffscreenRenderer (SoGLRenderAction *action) | |
~SoOffscreenRenderer () | |
void | setComponents (const Components components) |
Components | getComponents (void) const |
void | setViewportRegion (const SbViewportRegion ®ion) |
const SbViewportRegion & | getViewportRegion (void) const |
void | setBackgroundColor (const SbColor &color) |
const SbColor & | getBackgroundColor (void) const |
void | setGLRenderAction (SoGLRenderAction *action) |
SoGLRenderAction * | getGLRenderAction (void) const |
SbBool | render (SoNode *scene) |
SbBool | render (SoPath *scene) |
unsigned char * | getBuffer (void) const |
SbBool | writeToRGB (FILE *fp) const |
SbBool | writeToPostScript (FILE *fp) const |
SbBool | writeToPostScript (FILE *fp, const SbVec2f &printsize) const |
SbBool | writeToRGB (const char *filename) const |
SbBool | writeToPostScript (const char *filename) const |
SbBool | writeToPostScript (const char *filename, const SbVec2f &printsize) const |
SbBool | isWriteSupported (const SbName &filetypeextension) const |
int | getNumWriteFiletypes (void) const |
void | getWriteFiletypeInfo (const int idx, SbList< SbName > &extlist, SbString &fullname, SbString &description) |
SbBool | writeToFile (const SbString &filename, const SbName &filetypeextension) const |
Static Public Member Functions | |
float | getScreenPixelsPerInch (void) |
SbVec2s | getMaximumResolution (void) |
If you want to render to a memory buffer instead of an on-screen OpenGL context, use this class. Rendering to a memory buffer can be used to generate texture maps on-the-fly, or for saving snapshots of the scene to disk files (as pixel bitmaps or as Postscript files for sending to a Postscript-capable printer).
Here's a dead simple usage example:
SoOffscreenRenderer * myRenderer = new SoOffscreenRenderer(vpregion); SoNode * root = myViewer->getSceneManager()->getSceneGraph(); SbBool ok = myRenderer->render(root); // [then use image buffer in a texture, or write it to file, or whatever]
Offscreen rendering is internally done with either GLX (i.e. OpenGL on X11) or WGL (i.e. OpenGL on Win32) or AGL (i.e. OpenGL on the Mac OS). The pixeldata is fetched from the OpenGL buffer with glReadPixels(), with the format and type arguments set to GL_RGBA and GL_UNSIGNED_BYTE, respectively. This means that the maximum resolution is 32 bits, 8 bits for each of the R/G/B/A components.
One particular usage of the SoOffscreenRenderer is to make it render frames to be used for the construction of movies. The general technique for doing this is to iterate over the following actions:
realTime
global field (see explanation below) ..then you use some external tool or library to construct the movie file, for instance in MPEG format, from the set of files dumped to disk from the iterative process above.
The code would go something like the following (pseudo-code style). First we need to stop the Coin library itself from doing any automatic updating of the realTime
field, so your application initialization for Coin should look something like:
[...] = SoQt::init([...]); // or SoWin::init() or SoDB::init() // ..and then immediately: // Control realTime field ourselves, so animations within the scene // follows "movie-time" and not "wallclock-time". SoDB::enableRealTimeSensor(FALSE); SoSceneManager::enableRealTimeUpdate(FALSE); SoSFTime * realtime = SoDB::getGlobalField("realTime"); realtime->setValue(0.0);
Note that it is important that the realTime
field is initialized to your start-time before setting up any engines or other entities in the system that uses the realTime
field.
Then for the rendering loop, something like:
for (int i=0; i < NRFRAMES; i++) { // [...reposition camera here, if necessary...] // render offscreenrend->render(root); // dump to file SbString framefile; framefile.sprintf("frame%06d.rgb", i); offscreenrend->writeToRGB(framefile); // advance "current time" by the frames-per-second value, which // is 24 fps in this example realtime->setValue(realtime.getValue() + 1/24.0); }
When making movies you need to write your application control code to take care of moving the camera along the correct trajectory yourself, and to explicitly control the global realTime
field. The latter is so you're able to "step" with appropriate time units for each render operation (e.g. if you want a movie that has a 24 FPS refresh rate, first render with realTime=0
.0, then add 1/24s to the realTime
field, render again to a new frame, add another 1/24s to the realTime
field, render, and so on).
For further information about how to control the realTime
field, see documentation of SoDB::getGlobalField(), SoDB::enableRealTimeSensor(), and SoSceneManager::enableRealTimeUpdate().
|
Enumerated values for the available image formats.
|
|
Constructor. Argument is the viewportregion we should use when rendering. An internal SoGLRenderAction will be constructed. |
|
Constructor. Argument is the action we should apply to the scene graph when rendering the scene. Information about the viewport is extracted from the action. |
|
Destructor. |
|
Returns the screen pixels per inch resolution of your monitor. |
|
Get maximum dimensions (width, height) of the offscreen buffer. |
|
Sets the component format of the offscreen buffer.
If set to
The default format to render to is This will invalidate the current buffer, if any. The buffer will not contain valid data until another call to SoOffscreenRenderer::render() happens. |
|
Returns the component format of the offscreen buffer.
|
|
Sets the viewport region. This will invalidate the current buffer, if any. The buffer will not contain valid data until another call to SoOffscreenRenderer::render() happens. |
|
Returns the viewerport region. |
|
Sets the background color. The buffer is cleared to this color before rendering. |
|
Returns the background color. |
|
Sets the render action. Use this if you have special rendering needs. |
|
Returns the rendering action currently used. |
|
Render the scenegraph rooted at scene into our internal pixel buffer. Important note: make sure you pass in a scene node pointer which has both a camera and at least one lightsource below it -- otherwise you are likely to end up with just a blank or black image buffer. This mistake is easily made if you use an SoOffscreenRenderer on a scenegraph from one of the standard viewer components, as you will often just leave the addition of a camera and a headlight lightsource to the viewer to set up. This camera and lightsource are then part of the viewer's private "super-graph" outside of the scope of the scenegraph passed in by the application programmer. To make sure the complete scenegraph (including the viewer's "private parts" (*snicker*)) are passed to this method, you can get the scenegraph root from the viewer's internal SoSceneManager instance instead of from the viewer's own getSceneGraph() method, like this:
If you do this and still get a blank buffer, another common problem is to have a camera which is not actually pointing at the scene geometry you want a snapshot of. If you suspect that could be the cause of problems on your end, take a look at SoCamera::pointAt() and SoCamera::viewAll() to see how you can make a camera node guaranteed to be directed at the scene geometry.
|
|
Render the scene path into our internal memory buffer. |
|
Returns the offscreen memory buffer. |
|
Writes the buffer in SGI RGB format by appending it to the already open file. Returns Important note: do not use this method when the Coin library has been compiled as an MSWindows DLL, as passing FILE* instances back or forth to DLLs is dangerous and will most likely cause a crash. This is an intrinsic limitation for MSWindows DLLs. |
|
Writes the buffer in Postscript format by appending it to the already open file. Returns Important note: do not use this method when the Coin library has been compiled as an MSWindows DLL, as passing FILE* instances back or forth to DLLs is dangerous and will most likely cause a crash. This is an intrinsic limitation for MSWindows DLLs. |
|
Writes the buffer to a file in Postscript format, with printsize dimensions. Important note: do not use this method when the Coin library has been compiled as an MSWindows DLL, as passing FILE* instances back or forth to DLLs is dangerous and will most likely cause a crash. This is an intrinsic limitation for MSWindows DLLs. |
|
Opens a file with the given name and writes the offscreen buffer in SGI RGB format to the new file. If the file already exists, it will be overwritten (if permitted by the filesystem).
Returns |
|
Opens a file with the given name and writes the offscreen buffer in Postscript format to the new file. If the file already exists, it will be overwritten (if permitted by the filesystem).
Returns |
|
Opens a file with the given name and writes the offscreen buffer in Postscript format with printsize dimensions to the new file. If the file already exists, it will be overwritten (if permitted by the filesystem).
Returns |
|
Returns Examples of possibly supported extensions are: "jpg", "png", "tiff", "gif", "bmp", etc. The extension match is not case sensitive. Which formats are actually supported depends on the capabilities of Coin's support library for handling import and export of pixel-data files: the simage library. If the simage library is not installed on your system, no extension output formats will be supported. Also, note that it is possible to build and install a simage library that lacks support for most or all of the file formats it is capable of supporting. This is so because the simage library depends on other, external 3rd party libraries -- in the same manner as Coin depends on the simage library for added file format support. The two built-in formats that are supported through the SoOffscreenRenderer::writeToRGB() and SoOffscreenRenderer::writeToPostScript() methods (for SGI RGB format and for Adobe Postscript files, respectively) are not considered by this method, as those two formats are guaranteed to always be supported through those functions. So if you want to be guaranteed to be able to export a screenshot in your wanted format, you will have to use either one of the above mentioned method for writing SGI RGB or Adobe Postscript directly, or make sure the Coin library has been built and is running on top of a version of the simage library (that you have preferably built yourself) with the file format(s) you want support for. This method is an extension versus the original SGI Open Inventor API.
|
|
Returns the number of available exporters. Detailed information about the exporters can then be found using getWriteFiletypeInfo(). See SoOffscreenRenderer::isWriteSupported() for information about which file formats you can expect to be present. Note that the two built-in export formats (SGI RGB and Adobe Postscript) are not counted. This method is an extension versus the original SGI Open Inventor API.
|
|
Returns information about an image exporter. extlist is a list of filenameextensions for a file format. E.g. for JPEG it is legal to use both jpg and jpeg. fullname is the full name of the image format. description is an optional string with more information about the file format. See SoOffscreenRenderer::isWriteSupported() for information about which file formats you can expect to be present. This method is an extension versus the original SGI Open Inventor API. Here is a stand-alone, complete code example that shows how you can check exactly which output formats are supported:
|
|
Saves the buffer to filename, in the filetype specified by filetypeextensions. Note that you must still specify the full filename for the first argument, i.e. the second argument will not automatically be attached to the filename -- it is only used to decide the filetype. This method is an extension versus the orignal SGI Open Inventor API.
|