我有我一直在使用的代码来加载 SDL_Surface
并将其转换为 OpenGL 纹理,但是我意识到它们只适用于 RGB(A) 表面。我需要将支持扩展到索引模式图像(有或没有透明度)。
最初,我正在研究 ::SDL_SetColorKey()
,但它似乎只适用于 SDL blits。我已经阅读了 SDL_Surface
、SDL_PixelFormat
和 SDL_Color
,然后开始草拟以下内容(//# 是伪代码):
SDL_Surface *pSurf(::IMG_Load(image));
::SDL_LockSurface(pSurf);
Uint32 bytesPerPixel(pSurf->format->bytesPerPixel);
GLenum pixelFormat;
Uint8 *pixelData(pSurf->pixels);
bool allocated(false); // pixelData isn't allocated
if(pSurf->format->palette != 0) // indexed mode image
{
//# Determine transparency. // HOW?
//# bytesPerPixel = 3 or 4 depending on transparency being present;
//# pixelFormat = GL_RGB or GL_RGBA depending on bytesPerPixel;
Uint32 blockSize(pSurf->w * pSurf->h * bytesPerPixel);
pixelData = new Uint8[blockSize];
allocated = true;
//# traverse pSurf->pixels, look up pSurf->format->palette references and copy
// colors into pixelData;
}
else
{
//# Determine pixelFormat based on bytesPerPixel and pSurf->format->Rmask
// (GL_RGB(A) or GL_BGR(A)).
}
//# Pass bytesPerPixel, pixelFormat and pixelData to OpenGL (generate texture,
// set texture parameters, glTexImage2D etc).
if(allocated)
{
delete[] pixelData;
pixelData = 0;
}
::SDL_UnlockSurface(pSurf);
::SDL_FreeSurface(pSurf);
因此,问题是:如何确定传递给此例程的索引模式图像是否具有透明度?
最佳答案
索引模式的典型做法是拥有一个完整的 32 位 RGBA 调色板,这样每个索引颜色槽就有 8 位 alpha。或者,您可以只将某个(范围的)调色板索引定义为透明。
OpenGL 支持后者,通过 GL_PIXEL_MAP_I_TO_A
通过 <a href="http://www.talisman.org/opengl-1.1/Reference/glPixelMap.html" rel="noreferrer noopener nofollow">glPixelMap()</a>
访问的表.参见 <a href="http://www.talisman.org/opengl-1.1/Reference/glPixelTransfer.html" rel="noreferrer noopener nofollow">glPixelTransfer()</a>
用于翻译逻辑的描述。
关于c++ - 如何确定索引模式 SDL_Surface 是否具有透明度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1846856/