Compatibility, when creating window from DLL

I have an old application with an internal 3D view that is implemented with a viewer library (dll) in C. Now this library shall also be able to open an external window and show an additional 3D view on another screen. With GLFW I manage to open another window but I can’t draw the content. It seems as if the OpenGL legacy functions are disabled.

I can clear and swap the buffer, but the OpenGL V1.4 functions like glBegin(GL_TRIANGLES) doesn’t seem to work.

Additionally, once I create a window using GLFW, I seem to lose access to the existing internal OpenGL-Plane that was initialized with wgl. I can’t update the internal view anymore.

I appended this to the header:

...
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
extern GLFWwindow* ExtWindow;

source:

// many includes ...
GLFWwindow* ExtWindow = NULL;
...
createExtWindow() {
  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  ExtWindow = glfwCreateWindow(600, 400, "Test Window", NULL, NULL);
}

extWindowLegacyTest() {
  glfwMakeContextCurrent(ExtWindow);
  glClearColor(0, 0, 0, 0);
  glClear(GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT);
  // draw basic triangle with glBegin() is not working
  glFlush();
  glfwSwapBuffer(ExtWindow);
}

EDIT: added a few more details to the source code.

There are a number of examples in GLFW which use the legacy OpenGL API, so I recommend you try these first, for example take a look at boing.c.

It’s likely that the reason the wgl window OpenGL functions no longer work is because the context for that window is not current.

1 Like

Thanks a lot, Doug.

I will take a very close look at the example.

Regarding the wgl panel:
When I’m trying to make that context current like I used to, it doesn’t work.

wglMakeCurrent(hDeviceContext, hRenderingContext);

I’ve also thought about this:

GLFWwindow* MainWindow = NULL;
// after initializing wgl Window, before initializing glfw:
MainWindow = glfwGetCurrentContext();
glfwMakeContextCurrent(MainWindow);

But it doesn’t work either. glfwGetCurrentContext returns NULL when wgl context is current.

It turned out, that I didn’t see the actual problem. I assumed it was a problem with those legacy functions that this old library is using, but I was wrong. It was just that I haven’t set up the DisplayPort correctly (silly me). That was the reason why glClear seemed to work but content didn’t show.

I didn’t manage to find a better solution to the problem that I was losing access to the OpenGL-Plane that was initialized with wgl, so I’ve decided to use GLFW only to create the window and then create the OpenGL context for that window with wgl as well.

createExtWindow() {
  glfwInit();
  glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  ExtWindow = glfwCreateWindow(600, 400, "Test View", NULL, NULL);
  HWND hWindow2 = glfwGetWin32Window(ExtWindow);
  // initialize OpenGL context using wgl
  ...
}

This solution worked for me to solve both issues. It’s probably not recommendable for new projects but I think it was the least effort to add a new feature to this old project.