Reputation: 11506
I have a task to implement offscreen OpenGL renderer both for Window and Linux in C++.I have such a version already written in Java using LWJGL lib.There I used PBuffer object ,which under hood creates Pbuffers based on the used OS.First I thought to re-implement the full PBuffer creation logic just as it i done in native source of LWJGL.Then I read this post on StackOverflow.com where it is suggested using the standard context creation ,let's say using GLFW (which is cross platform) but not to create the actual window.Is it the right way to go? What are pros and cons vs using Pbuffer in such a case?
Update: I just want to emphasize that I use FBOs to render the frames so my problem here is not how to render in offscreen mode but how to create a context without window both in Windows and Linux OSs.
Upvotes: 9
Views: 5453
Reputation: 29866
I'd highly recommend not to use PBuffers anymore but to use Frame Buffer Objects (FBOs) instead. FBOs offer much better performance as using them does not require a context switch and they have several other advantages.
LWJGL supports FBOs, but GLFW is "just" for cross-platform setup of OpenGL and not for rendering. For convenient cross-platform FBO usage I'd recommend to use a library like OGLplus on top of GLFW. See here for a render-to-texture example.
Upvotes: 6
Reputation: 494
The Simple DirectMedia Layer (SDL) library is worth a try. It simplifies cross-platform OpenGL context creation, with the ability to use memory surfaces for off-screen rendering.
The only thing you would have to do extra, is to include your OpenGL and SDL headers from different locations, depending on your platform. This can be done with simple pre-processor directives.
Upvotes: 2
Reputation: 3901
As far as I know there is no cross-platform way to create contexts, you will have to create your own abstraction and then implement it for each platform.
On windows I have used the following code to create a second context to do loading of content in a background thread (this program used GLFW but that should not matter):
void Program::someFunction()
{
HDC hdc = wglGetCurrentDC();
HGLRC hglrc = wglGetCurrentContext();
HGLRC hglrc_new = wglCreateContext(hdc);
wglShareLists(hglrc, hglrc_new);
loadThread = boost::thread(&Program::loadFunc, this, hdc, hglrc_new);
}
/**
* Imports all our assets. Supposed to run in its own thread with its own OpenGL context.
* @param hdc The current device context.
* @param hglrc A OpenGL context thats shares its display list with the main rendering context.
*/
void Program::loadFunc(HDC hdc, HGLRC hglrc)
{
wglMakeCurrent(hdc, hglrc);
//Do stuff...
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
}
Upvotes: 0