Drawing to NSView with OpenGL questions

  • Hello.

    Is it possible to somehow mix CoreGraphics and OpenGL drawing commands
    using a common NSView? What I'd like to do is render my own vertex/
    fragment shaders into a CGContext.

    1. Currently, I'm drawing to an NSOpenGLPixelBuffer with OpenGL, then
    copy the drawing with glReadPixels() to a scratch buffer, from which I
    copy byte rows to an NSBitmapImageRep, which I can draw to my
    CGContext or NSGraphicsContext. That's a lot of code moving pixels
    from VRAM to RAM to GPU, though I admit it works. Is there a more
    straight-forward way to draw with OpenGL to CGContext or, maybe, with
    CoreGraphics to an NSOpenGLView?

    2. I've written and tested several shaders both in OpenGL Shader
    Builder and in a test app inside an NSOpenGLView. They work just fine.
    However, I don't have any NSOpenGLViews in my app, so I guess it's the
    absence of glutCreateWindow() that causes glCreateShader() to return 0
    without errors. If I call glutCreateWindow(), I get "GLUT Fatal Error:
    redisplay needed for window 1, but no display callback." in the
    console, so I have to add display callbacks, but then I'm moving away
    from my NSView. What confuses me is why simple OpenGL commands works
    while shaders don't.

    What is the best practice of mixing CoreGraphics and OpenGL? Ideally,
    I'd want to avoid copying pixel data back to RAM because that slows
    down my animations a lot.

    Thanks in advance.

    Regards.
    Konstantin.
  • It is easy to overlay gl over quartz or quartz over gl, but it's
    tricky to interleave them.  Approaches for overlays include the
    following samples :  <http://developer.apple.com/cgi-bin/search.pl?q=gl+NSView&num=50&sit
    e=(samplecode)
    >

    If you must interleave gl and quartz, render quartz drawing into gl
    textures and draw with gl  You are correct that reading back from VRAM
    is seldom the right solution.  Instead of reading your gl images back
    from the card, write your quartz images to the card and do the mixing
    there.  This sample shows one approach: http://developer.apple.com/samplecode/CocoaGL/listing10.html

    Core Animation in Mac OS X 10.5 essentially draws with quartz into gl
    textures when views are layer backed.