【问题标题】:Cannot set Alpha channel to 0 in OpenGL + SDL2无法在 OpenGL + SDL2 中将 Alpha 通道设置为 0
【发布时间】:2023-03-31 19:07:01
【问题描述】:

我正在尝试将 OpenGL rendered screen 保存为具有透明背景的 PNG 图像。但我在第 4 个频道中得到的只是 255 值。

即使在屏幕上绘制任何东西之前,只是清除也无济于事。

我在OpenGL中为获得透明背景所做的初始化大致如下:

gWindow = SDL_CreateWindow("View", SDL_WINDOWPOS_UNDEFINED, 
         SDL_WINDOWPOS_UNDEFINED, width, height,
         SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
gContext = SDL_GL_CreateContext(gWindow);
GLfloat clear_col[4] = {0.0, 0.0, 0.0, 0.0};

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, width/(float)height, nearplane, farplane);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(clear_col[0], clear_col[1], clear_col[2], clear_col[3]);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glShadeModel(GL_FLAT);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
pixels = (unsigned char*) malloc(width*height*4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// checking the values
FILE*ffp = fopen("saved.bin", "wb");
fwrite(pixels, width*height*4, 1, ffp);
fclose(ffp);

提前致谢。

【问题讨论】:

    标签: c opengl transparency sdl-2


    【解决方案1】:

    默认情况下,SDL 不会明确请求带有 Alpha 通道的帧缓冲区。你必须打电话

    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);
    

    在创建窗口之前,请确保您确实获得了一个 alpha 缓冲区。请注意,这在概念上要求至少 1 位 alpha。通常,GL 实现提供像素格式/视觉效果,每个像素具有 0 或 8 位 alpha。如果您想确保获得至少 8 位,您也可以明确要求。

    【讨论】:

      猜你喜欢
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 2020-06-27
      相关资源
      最近更新 更多