I'm writing a plugin(i.e. a .dll) for graphics engine that is written in OpenGL. My goal is to mirror what is on that window on a second window. The good news is the graphics engine exposes handles to the device context(hDC), rendering context(hRC), and the rendering window (hWnd). The other good news (I think) is that since my plugin is loaded as a .dll, it is loaded into the same address space, so there wouldn't be any access violations with what I want to achieve.
My goal is to capture the pixels from the graphics engine and draw them into a separate window and thus mirror the display(e.g. SDL2, glfw, or even just a regular Win32 window). My understanding is that I need to use the context of the graphics engine, and then I can then I could do something like read from the framebuffer of the graphics engine to mirror the pixels on my second window in a separate texture.
But how do I go about sharing the context so that I can access the textures and such?
I'm looking at this which may have some useful functions (wglmakecurrent, wglGetCurrentContext, etc.) for achieving this task.
Can anyone confirm I am on the right track, or point me to some resources explaining this idea?
Other Relevant stuff:
- Working in Windows 10
- Programming in C and C++
EDIT
Here is what I tried in the .dll I laoded in order to capture pixels from the main applications framebuffer:
// Make the current context available
wglMakeCurrent(mainApplication.hDC, mainApplication.hRC);
glReadBuffer(GL_BACK);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, 400, 400, GL_RGBA, GL_UNSIGNED_BYTE, framebufferCapture);
Unfortunately I just get garbage data back. Am I on the right track? I tried creating a texture in the next context just to see if it's value was greater than 1, but it was not, which I think is telling me the context is not being shared (otherwise a unique texture id would have been returned).