Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GL Context for Quicklook generators



You do not need to create a window to create a hardware accelerated offscreen rendering context. CGL alone is sufficient, since Leopard you can create offscreen pbuffer contexts by using the kCGLPFARemotePBuffer pixel format attribute.

Sample code:

CGLError cglerr;

CGLPixelFormatAttribute attributes[] = {
kCGLPFAAccelerated,
kCGLPFANoRecovery,
kCGLPFADoubleBuffer,
kCGLPFAColorSize, 24,
kCGLPFAAlphaSize, 8,
kCGLPFADepthSize, 24,
kCGLPFASampleBuffers, 1,
kCGLPFASamples, 4,
kCGLPFAMultisample,
kCGLPFASampleAlpha,
0
};

CGLPixelFormatObj format;
GLint npix;
cglerr = CGLChoosePixelFormat(attributes, &format, &npix);
if (cglerr != kCGLNoError) {
fprintf(stderr, "CGLChoosePixelFormat failed: %s", CGLErrorString(cglerr));
exit(1);
}

CGLContextObj cgl_ctx;
cglerr = CGLCreateContext(format, NULL, &cgl_ctx);
if (cglerr != kCGLNoError) {
fprintf(stderr, "CGLCreateContext failed: %s", CGLErrorString(cglerr));
exit(1);
}

CGLSetCurrentContext(cgl_ctx);

GLuint fbo;
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

GLuint color0;
glGenTextures(1, &color0);
glBindTexture(GL_TEXTURE_2D, color0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, color0, 0);

GLuint depth_rb;
glGenRenderbuffersEXT(1, &depth_rb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth_rb);

glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, 1024, 1024);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_rb);

// render away


This is very brittle code, but should be a good starting point.

As mentioned by others, once you have a rendered content, readback the texture to main system memory (you want to use a PBO for that), and then use the Core Graphics API to draw to the CGContext provided to you by Quick Looks.

On May 14, 2008, at 12:13, Dario Accornero wrote:

The "modern" way to do offscreen rendering on OS X is with FBOs. Sticking to strictly C APIs, you might use CreateNewWindow to obtain a WindowRef from which a graphics port can be passed to aglCreateContext: this will give you the actual (A)GL context in which to draw. In this context, after making it current, you can create an FBO from which you'll end up reading actual texels. Note that you do not need, nor definitely want, to show the WindowRef to which the AGL context will be attached: you only need the window to make sure you get a hardware-rendering context.

HTH,
Dario

_______________________________________________ Do not post admin requests to the list. They will be ignored. Mac-opengl mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/mac-opengl/email@hidden

This email sent to email@hidden
References: 
 >GL Context for Quicklook generators (From: Heinrich Fink <email@hidden>)
 >Re: GL Context for Quicklook generators (From: Dario Accornero <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.