How can I copy an OpenGL Context to a CGContext?
The reason I need to do this is so I can print what OpenGL has rendered.
Based on some searching, I came up with this general procedure:
- use glReadPixels to obtain what has currently been rendered
- use CGDataProviderCreateWithData
- use CGImageCreate and pass in the CGDataProvider
- use CGContextDrawImage
However, I am having some trouble figuring out all the details on how to call
the various functions.
First, I am using the following attributes, passed to aglChoosePixelFormat:
{ AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 16, AGL_NONE }
Second, I am using:
aglSetInteger( mGLContext, AGL_BUFFER_RECT, mBufferRect );
aglEnable( mGLContext, AGL_BUFFER_RECT);
to specify the specific part of the window OpenGL should render to.
I tried to find some sample code, but was unable.
What I’ve got so far is:
aglSetCurrentContext( mGLContext );
glReadPixels( 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, image
);
dataProvider = CGDataProviderCreateWithData( NULL, image, imageSize, NULL );
CGImageCreate( width, height, 8, 8 * 4, rowBytes, …
It is this CGImageCreate function that is causing me the most trouble, assuming
I’ve got everything else correct.
Should glReadPixels be based on what is in mBufferRect?
Has anyone else tried this? Any pointers or sample code available?