void PBuffer::enable() { GemCGLint vs; cglReportError (CGLSetCurrentContext (data->context)); cglReportError (CGLGetVirtualScreen ( data->old_context, &vs )); cglReportError( CGLSetPBuffer( data->context, data->pbuffer, 0, 0, vs) ); debug ("enable Context (0x%X) Renderer: %s\n",CGLGetCurrentContext(), glGetString (GL_RENDERER)); debug ("pBuffer Context (0x%X) Renderer: %s\n",data->context, glGetString (GL_RENDERER)); return; }
/* * constructor (APPLE specific) */ PBuffer::PBuffer(int width, int height, int flag) : width(width), height(height) { OSStatus err = noErr; CGLPixelFormatAttribute *att,attrib[64]; GemCGLint vs, npf; // setup offscreen context att=attrib; *att++=kCGLPFANoRecovery; *att++=kCGLPFAAccelerated; *att++=kCGLPFAWindow; // *att++=kCGLPFAPBuffer; *att++=kCGLPFAColorSize; *att++=(CGLPixelFormatAttribute)32; if (flag & GEM_PBUFLAG_DEPTH){ *att++=kCGLPFADepthSize; //*att++=(CGLPixelFormatAttribute)24; *att++=(CGLPixelFormatAttribute)16; } if (flag & GEM_PBUFLAG_STENCIL){ *att++=kCGLPFADepthSize; *att++=(CGLPixelFormatAttribute)8; } if (flag & GEM_PBUFLAG_FLOAT){ // *att++=kCGLPFADepthSize; // *att++=(CGLPixelFormatAttribute)8; } //*att++=kCGLPFADoubleBuffer; // *att++=kCGLPFADisplayMask; // *att++=kCGLPFAAllRenderers; *att=(CGLPixelFormatAttribute)0; data = new PBuffer_data; data->old_context = CGLGetCurrentContext(); err = CGLGetVirtualScreen(data->old_context, &vs); verbose (2, "Target Context (0x%X) Renderer: %s\n",data->old_context, glGetString (GL_RENDERER)); cglReportError(CGLChoosePixelFormat (attrib, &data->pixfmt, &npf)); cglReportError(CGLCreateContext (data->pixfmt, data->old_context, &(data->context))); verbose (2, "pBuffer Context (0x%X) Renderer: %s\n",data->context, glGetString (GL_RENDERER)); /* if (float_buffer) cglReportError( CGLCreatePBuffer ( width, height, GL_TEXTURE_2D, GL_FLOAT, 0, &(data->pbuffer) ) ); else */ cglReportError( CGLCreatePBuffer ( width, height, GL_TEXTURE_RECTANGLE_EXT, GL_RGBA, 0, &(data->pbuffer) )); cglReportError( CGLSetCurrentContext( data->context ) ); cglReportError( CGLGetVirtualScreen(data->old_context, &vs) ); cglReportError( CGLSetPBuffer(data->context, data->pbuffer, 0, 0, vs) ); verbose (2, "pbuffer (0x%X) Renderer: %s\n",data->pbuffer, glGetString (GL_RENDERER)); }
//----------------------------------------------------------------------------// void OpenGLApplePBTextureTarget::declareRenderSize(const Size& sz) { // exit if current size is enough if ((d_area.getWidth() >= sz.d_width) && (d_area.getHeight() >= sz.d_height)) return; setArea(Rect(d_area.getPosition(), d_owner.getAdjustedTextureSize(sz))); // dump any previous pbuffer if (d_pbuffer) { CGLDestroyPBuffer(d_pbuffer); d_pbuffer = 0; } CGLError err; if (err = CGLCreatePBuffer(d_area.getWidth(), d_area.getHeight(), GL_TEXTURE_2D, GL_RGBA, 0, &d_pbuffer)) { CEGUI_THROW(RendererException( "OpenGLApplePBTextureTarget::declareRenderSize " "- CGLCreatePBuffer failed: " + String(CGLErrorString(err)))); } if (err = CGLSetPBuffer(d_context, d_pbuffer, 0, 0, d_screen)) CEGUI_THROW(RendererException( "OpenGLApplePBTextureTarget::declareRenderSize " "- CGLSetPBuffer failed: " + String(CGLErrorString(err)))); clear(); // make d_texture use the pbuffer as it's data source // save old texture binding GLuint old_tex; glGetIntegerv(GL_TEXTURE_BINDING_2D, reinterpret_cast<GLint*>(&old_tex)); glBindTexture(GL_TEXTURE_2D, d_texture); err = CGLTexImagePBuffer(CGLGetCurrentContext(), d_pbuffer, GL_FRONT); // restore previous texture binding. glBindTexture(GL_TEXTURE_2D, old_tex); if (err) CEGUI_THROW(RendererException( "OpenGLApplePBTextureTarget::declareRenderSize " "- CGLTexImagePBuffer failed: " + String(CGLErrorString(err)))); // ensure CEGUI::Texture is wrapping real GL texture and has correct size d_CEGUITexture->setOpenGLTexture(d_texture, d_area.getSize()); }
static int setup(int width, int height) { #if USE_OSMESA const size_t size = width * height * 4; pbuffer = malloc(size); memset(pbuffer, 0x21, size); context = OSMesaCreateContextExt(GL_RGBA, 24, 0, 0, 0); OSMesaMakeCurrent(context, (void *)pbuffer, GL_UNSIGNED_BYTE, width, height); return 0; #elif USE_CGL /* OpenGL PBuffer initialization: OSX specific */ CGLPixelFormatAttribute pfattr[] = { kCGLPFAPBuffer, (CGLPixelFormatAttribute)0 }; CGLPixelFormatObj pixformat; GLint npixels; int e; e = CGLChoosePixelFormat(pfattr, &pixformat, &npixels); if (e != kCGLNoError) { fprintf(stderr, "CGLChoosePixelFormat failed, err %d\n", e); return e; } e = CGLCreateContext(pixformat, 0, &context); if (e != kCGLNoError) { fprintf(stderr, "CGLChoosePixelFormat failed, err %d\n", e); return e; } e = CGLDestroyPixelFormat(pixformat); if (e != kCGLNoError) { fprintf(stderr, "CGLDestroyPixelFormat failed, err %d\n", e); return e; } e = CGLSetCurrentContext(context); if (e != kCGLNoError) { fprintf(stderr, "CGLSetCurrentContext failed, err %d\n", e); return e; } e = CGLCreatePBuffer(width, height, GL_TEXTURE_2D, GL_RGB, 0, &pbuffer); if (e != kCGLNoError) { fprintf(stderr, "CGLCreatePBuffer failed, err %d\n", e); return e; } e = CGLSetPBuffer(context, pbuffer, 0, 0, 0); if (e != kCGLNoError) { fprintf(stderr, "CGLSetPBuffer failed, err %d\n", e); return e; } return (int)kCGLNoError; #elif USE_GLX int result = (-1); char *glxversion; int screen; GLXFBConfig *fbConfigs = NULL; GLXFBConfig chosenFBConfig; GLXFBConfig fbconfig = 0; GLXPbuffer pbuffer = None; int nConfigs; int fbconfigid; int fbAttribs[] = { GLX_RENDER_TYPE, GLX_RGBA_BIT, GLX_DEPTH_SIZE, 1, GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT | GLX_PBUFFER_BIT, None }; int pbAttribs[] = { GLX_PBUFFER_WIDTH, 0, GLX_PBUFFER_HEIGHT, 0, GLX_LARGEST_PBUFFER, False, GLX_PRESERVED_CONTENTS, False, None }; /* Open the X display */ display = XOpenDisplay(NULL); if (!display) { fprintf(stderr, "Error: couldn't open default X display.\n"); goto end; } /* Get default screen */ screen = DefaultScreen(display); glxversion = (char *) glXGetClientString(display, GLX_VERSION); if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4"))) { goto end; } glxversion = (char *) glXQueryServerString(display, screen, GLX_VERSION); if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4"))) { goto end; } /* Create Pbuffer */ pbAttribs[1] = width; pbAttribs[3] = height; fbConfigs = glXChooseFBConfig(display, screen, fbAttribs, &nConfigs); if (0 == nConfigs || !fbConfigs) { fprintf(stderr, "Error: glxChooseFBConfig failed\n"); XFree(fbConfigs); goto end; } chosenFBConfig = fbConfigs[0]; glXGetFBConfigAttrib(display, chosenFBConfig, GLX_FBCONFIG_ID, &fbconfigid); /* Create the pbuffer using first fbConfig in the list that works. */ pbuffer = glXCreatePbuffer(display, chosenFBConfig, pbAttribs); if (pbuffer) { fbconfig = chosenFBConfig; } XFree(fbConfigs); if (pbuffer == None) { fprintf(stderr, "Error: couldn't create pbuffer\n"); goto end; } /* Create GLX context */ context = glXCreateNewContext(display, fbconfig, GLX_RGBA_TYPE, NULL, True); if (!context) { fprintf(stderr, "Error: Couldn't create GLXContext\n"); goto end; } /* Bind context to pbuffer */ if (!glXMakeCurrent(display, pbuffer, context)) { fprintf(stderr, "Error: glXMakeCurrent failed\n"); goto end; } result = 0; end: if (fbConfigs) XFree(fbConfigs); if (display) XCloseDisplay(display); return result; #else /* TODO: pbuffer initialization */ return 0; #endif }