Wednesday, September 6, 2017

sdl2 osx opengl transparent background

not sure how often this question comes up, but every now and then you'll have an sdl2 window with opengl rendering and want to make the background transparent (the alpha of glClear is ignored).

here's the osx solution (you need to make the sdl window transparent and also set surface opacity to 0) :
void setOSXWindowTransparent(void *window)
{
 NSWindow *win = (NSWindow *)window;
 [win setOpaque:NO];
 [win setHasShadow:NO];
 [win setBackgroundColor:[NSColor clearColor]];
}

void setSDLWindowTransparent(void * window)
{
 SDL_SysWMinfo info;
 if(SDL_GetWindowWMInfo(static_cast<SDL_Window *>(window),&info))
 {
  setOSXWindowTransparency(info.info.cocoa.window);
 }
}

void setOSXOGLSurfaceOpacity(void *context)
{
 GLint opacity = 0;
 NSOpenGLContext * nscontext = (NSOpenGLContext *)context;
 [nscontext setValues:&opacity forParameter:NSOpenGLCPSurfaceOpacity];
}


No comments:

Post a Comment