2

I've been playing with Derelict3&glfw to use OpenGL in D according to this, if I want to use extensions, I need to create a context first, and this is done by creating a window with glfw and set it as the current context. After the context is created and set, I need to use DerelictGL3.reload() to load all the extensions.

Now, I want to do all the preparations before I create the window. One of those preparations is to load and compile all the shader programs. But this required the shader extension, which required Derelict3GL.reload(), which refuses to run without a context...

So, I've used this hackish hack:

auto tmpWindow=glfwCreateWindow(1,1,"",null,null);
glfwMakeContextCurrent(tmpWindow);
DerelictGL3.reload();
glfwDestroyWindow(tmpWindow);

This works - I can now load and compile the shader programs and only then open the real window. But this seems a bit too hackish to me. Is there a proper way to fake a context, or to load the extensions without a context?

Idan Arye
  • 12,402
  • 5
  • 49
  • 68
  • Perhaps a better way would be to create the window first, but not show it. Then load your extensions and compile your shaders, and after that show the window. I'm not sure why you want to postpone creating the window, so I also don't know if this solution fits your purpose. – Kevin May 02 '13 at 08:19
  • The main problem is that it doesn't match my object design. I want to be able to create multiple windows, each with it's own context. That means I need to separate the creation of my `Window` object from the initialization of all the libraries. – Idan Arye May 02 '13 at 10:07
  • Then indeed the mentioned solution is probably the best. – Kevin May 02 '13 at 10:41

1 Answers1

3

Is there a proper way to fake a context, or to load the extensions without a context?

That depends on the plattform:

With Windows: Doing it through the intermediary window (that doesn't have to be mapped visibly on the screen) is the only way to load extensions reliably on Windows.

With X11/GLX: Extension function pointer can be loaded immediately using glXGetProcAddress ad the extension functions are part of the GLX client library and common to all contexts. However an actual OpenGL context may not support all of the functions that can be validly obtained with glXProcAddress.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Well, I certainly don't want to load the extension functions one-by-one with with `glXGetProcAddress`... But making my tmpWindow invisible sounds like a good idea! Currently it doesn't show up, but I don't know it that's because I destroy it too fast or because I don't call `Thread.sleep` before I close it, but it's better to set it to hidden explicitly... – Idan Arye May 01 '13 at 23:57
  • Absolutely, you can load extensions through a context associated with an invisible window. – ponce May 02 '13 at 07:12
  • @ponce: Technically you need the window only, to have something with a explicitly set PIXELFORMATDESCRIPTOR you can attach the OpenGL context to so that it's current in the active thread. X11/GLX offers you a few more choices than just windows for that. The Windows operating system is rather limited in that regard. – datenwolf May 02 '13 at 09:54
  • OK, turns out my design was bad to begin with(or maybe I should blame the OpenGL design...). Textures and shaders sit on the context that create them, so I need to load them **after** creating the window. So, I need to use @Kevin's suggestion, of hiding the window until I've loaded everything... – Idan Arye May 02 '13 at 19:21
  • @IdanArye: Encapsulating things like textures and shaders is the reason, why there are OpenGL contexts in the first place; this is a deliberate design element of OpenGL. However it is possible to share some kinds of objects between contexts (wglShareLists, or the share parameter of glXCreateContext); textures for example can be shared. – datenwolf May 02 '13 at 19:31
  • @IdanArye: But if all you're after is simply not showing the window until assets haven't been fully loaded, well, simply don't show the window until then. – datenwolf May 02 '13 at 19:32