簡體   English   中英

SDL2紋理的像素格式和表面顏色蒙版

[英]SDL2 texture's pixel format and surface color mask

我正在嘗試創建用於像素操作的SDL_Surface,但是在設置表面的顏色蒙版時確實出錯了,因為填充顏色緩沖區時顏色不正確(請參見注釋,我試圖將u8vec4解釋為RGBA顏色):

const int win_width = 640;
const int win_height = 480;
std::vector<glm::u8vec4> colors{ win_width * win_height, glm::u8vec4{ 0, 0, 0, 0 } };

SDL_Surface* render_surface = SDL_CreateRGBSurfaceFrom(&colors[0], win_width, win_height, 32,
    sizeof(glm::u8vec4) * win_width, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);

SDL_Texture* render_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, 
    SDL_TEXTUREACCESS_STREAMING, win_width, win_height);

// { 255, 0, 0, 255 } = Red
// { 255, 0, 0, 100 } = Darker red, alpha seems to be working
// { 0, 255, 0, 255 } = Purple!? Should be green?
// { 0, 0, 255, 255 } = Yellow, expecting blue
std::fill(colors.begin(), colors.end(), glm::u8vec4{ 0, 0, 0, 255 }); // Red
SDL_UpdateTexture(render_texture, nullptr, render_surface->pixels, render_surface->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, render_texture, nullptr, nullptr);
SDL_RenderPresent(renderer);

您應該嘗試按照字節序設置位。 這是我的方法:

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    const uint32_t R_MASK = 0xff000000;
    const uint32_t G_MASK = 0x00ff0000;
    const uint32_t B_MASK = 0x0000ff00;
    const uint32_t A_MASK = 0x000000ff;
#else
    const uint32_t R_MASK = 0x000000ff;
    const uint32_t G_MASK = 0x0000ff00;
    const uint32_t B_MASK = 0x00ff0000;
    const uint32_t A_MASK = 0xff000000;
#endif

在您的情況下,您已經以相反的方式獲得了顏色蒙版。


我認為Jongware是正確的,格式為ABGR。

// { 255, 0, 0, 100 } = Darker red, alpha seems to be working

我猜這里的Alpha似乎是正確的,因為背景是黑色的,並且通過將紅色的數量減少到100,它自然會與背景融合在一起。

我還認為您尚未設置SDL_BlendMode

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM