Minimal Windowless OpenGL Context Initialization
Asked Answered
B

3

7

How can I initialize a windowless OpenGL context with the minimal amount of code?

I've read here that you can use wglCreateContextAttribsARB to create windowless context, however it doesn't explain how?

Boudreau answered 15/8, 2011 at 8:6 Comment(1)
Why do you need it ? To render into an image, or use compute shader for GPGPU ?Iredale
B
7

From the link:

Creating a context without a window, for off-screen rendering. This may not actually work.

That second sentence is important. From the WGL_ARB_create_context specification:

4) Should there be a way to make a context current without binding it to a window system drawable at the same time?

RESOLVED: Yes, but only in OpenGL 3.0 and later. This results in a context with an invalid default framebuffer, the meaning of which is defined in the OpenGL 3.0 specification.

NOTE: Apparently on Windows, opengl32.dll makes use of the drawable argument to identify the namespace of the driver, so we may not be able to work around it.

The specification doesn't allow you to create a context without a window, since it needs the pixel format that you set into the device context. But you theoretically can pass NULL for the HDC when making the context current, which causes OpenGL to not have a default framebuffer.

But, as noted above, this may not actually work. You can try it to see what happens, but I wouldn't get my hopes up.

Bevash answered 15/8, 2011 at 8:31 Comment(3)
Thanks for the answer. Then what is the most minimalistic way to create a windowless context?Boudreau
Another try would be the use the screen DC (passing NULL to GetDC routine).Prosecutor
@Luca: You must not set the PIXELFORMATDESCRIPTOR of the root/screen window! And the drawable a OpenGL context is bound to must have a compatible pixelformat. Hence your suggestion is very bad advice.Poulenc
P
6

The usual way to implement offscreen rendering is:

  1. Create a dummy window + OpenGL context to get access to extensions
  2. Obtain the functions to create a PBuffer
  3. Create a PBuffer DC
  4. Create an OpenGL context on the PBuffer
  5. Destroy the dummy window

Another option is using Framebuffer Objects (FBO) by which you create a regular OpenGL window, but instead of rendering to the main framebuffer you render to Framebuffer Object buffers, with the OpenGL window being made invisible.

Poulenc answered 15/8, 2011 at 18:4 Comment(1)
For a beginner would that be possible to have a code snippet?Andersonandert
F
-3

another stuff:

HDC  hdc = CreateDC(L"DISPLAY",NULL,NULL,NULL);

But ReleaseDC not work with it.DeleteDC does.

Fortdefrance answered 25/4, 2017 at 15:39 Comment(1)
What do you mean by "does not work"? ReleaseDC and DeleteDC are not equivalents for each other, they serve completely different purposes.Tybie

© 2022 - 2024 — McMap. All rights reserved.