Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Using a Hidden View or Window

Starting in Mac OS X v10.2, you can use a hidden Carbon window or Cocoa view as a texture source. Cocoa supports this kind of offscreen drawing through the createTexture:fromView:internalFormat: method of the NSOpenGLContext class. Carbon provides the function aglSurfaceTexture.

Note: Although these routines provide a flexible way to render to an offscreen texture and then use that texture as a source, you should consider using pixel buffers and framebuffers instead. If your application provides support for Mac OS X v10.2, however, you must use hidden views and windows if you want accelerated offscreen drawing, because the other hardware-accelerated options are not available for Mac OS X v10.2

The crucial concept behind using hidden views and windows is that there are two rendering contexts involved, as shown in Figure 4-1: one that's bound to the hidden drawable object and the other that's bound to the destination window. You must make sure that the current rendering context is set to the appropriate context prior to drawing.


Figure 4-1  Using the content from a hidden window as a texture source

Using the content from a hidden window as a texture source

Drawing the texture content is just like drawing to an offscreen window. The content is treated as a texture only when you actually use it for the destination rendering context. You generate a texture name, bind it, and set the texture environment after you set the current context to the destination context. Once the texture is set, you call the routine that makes the texture content available to the destination window (either the createTexture:fromView:internalFormat: method of the NSOpenGLContext class or the AGL function aglSurfaceTexture). Then you can draw using the texture.

Figure 4-1 depicts the steps, explained below, that are required to use an offscreen window as a texture source:

  1. Create a window to use as the texture source. The window should specify a hidden attribute.

  2. Create a destination window to use the texture in.

  3. Set up each window as a drawable object attached to an AGL context. That is, set up buffer and renderer attributes, get a pixel format object, create an AGL context, and attach the window to the context. For details, see “Drawing to a Window or View.” The pixel format object for each context must be compatible, but the contexts do not need to be shared.

  4. Set the current rendering context to the texture source context and draw the texture. OpenGL draws the contents to the hidden window.

  5. Set the current rendering context to the destination window.

  6. Enable texturing by calling the function glEnable.

  7. Generate a texture name and bind the name to a texture target, using code similar to the following:

    glGenTextures (1, &mySurfaceTexName);
    glBindTexture (GL_TEXTURE_2D, mySurfaceTexName);
    glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    This code sets up a power-of-two texture. You can just as easily use a rectangular texture by providing the GL_TEXTURE_RECTANGLE_ARB option.

  8. Map the contents of the texture source window to the texture target bound in the destination window, using code similar to the following:

    aglSurfaceTexture (myDestinationContext,GL_TEXTURE_2D,
                GL_RGBA8, mySurfaceTextureContext);
  9. Draw to the destination window, using the texture just as you would any other texture.

  10. When you are done using the texture, unbind it by calling glBindTexture with the texture set to 0.

    glBindTexture (GL_TEXTURE_2D, 0);
  11. Flush the content to the destination window by calling the function aglSwapBuffers.



< Previous PageNext Page > Hide TOC


Last updated: 2008-06-09




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice