Printing OpenGL scenes using MFC and shaders

Hi there, I hope you are well.

I am currently building an application for Windows 10 using MFC and modern OpenGL as the inner renderer. What I am trying to do is print a portion of the screen on a printer using MFC and Win32 commands. It has proven to be a struggle.

I am aware there are two ways to do this, namely:

1. Read data from the framebuffer using something like glReadPixels() however the problem I foresee is that the resolution of my framebuffer is way smaller than the printing resolution. So this would ultimately lead to stretching the image, and hence not be the best solution.

2. I could render the image(s) to memory (a DIB or Bitmap image) using MFC, and I have gone into this quite extensively, but I cannot seem to render using my shaders. I can render simple primitives using glBegin() / glEnd() (URG!!) but when texturing, I again cannot get anything to run.

I believe my main issue is that I’m using modern OpenGL and shaders to do the rendering. All the tutorials, docs, and coding examples that I’ve found on this use old OpenGL 1.1 and no texturing - simply primitive shapes.

Re: Option 2, because I am rendering to a DIB location, a whole new OpenGL context has to be created and bound to the Windows device context. Within this new OpenGL context, I cannot seem to create any shader objects, buffer objects or texture objects, etc, so I assume modern OpenGL is not supported here.

I did not want to paste a whole lot of code in here, so if you think you can help, simply ask me for a section of the code I’m dealing with and I’ll paste it in.

I thank you for your time and assistance.

-Karsten

I believe that DIBs always use the software renderer, which is limited to OpenGL 1.1.

If you want to perform off-screen rendering with modern OpenGL, use a framebuffer object (FBO) with renderbuffers or textures of the appropriate size. FBOs require OpenGL 3.0 or the ARB_framebuffer_object extension.

If you’re using OpenGL 2.x and the extension isn’t supported, as a last resort you can use tiled rendering, rendering the scene multiple times with the projection matrix adjusted to render a portion of the image.

Thank you @GClements! That confirms that I wasn’t missing anything. I thought I was forgetting a flag setting of some kind.

I will look into FBOs - thanks for the tip. I am using OpenGL 4.x, so it should be all good.

Thank you for your help!