Alpha blending is not working on SDL_RenderGeometry.

The documentation for SDL_RenderGeometry says that “optionally using a texture and indices into the vertex array Color and alpha modulation is done per vertex.” and since SDL_SetTextureAlphaMod and SDL_SetTextureColorMod are ignored I assumed I could change the transparency of the rendered geometry like this:

                vertices[0].color.a = 55;
                vertices[1].color.a = 55;
                vertices[2].color.a = 55;
                SDL_RenderGeometry(renderer, texture, vertices, 3, NULL, 0);

but nothing happens. Is it possible to change the color and opacity of the rendered geometry along with the optionally added texture?
Or am I misunderstanding the documentation? Sorry for not asking this over on https://discourse.libsdl.org/ but the website is taking ages to load. Here is the code I wrote to try it out in full. Thank you for any help in advance!

#include <SDL.h>
#include <SDL_image.h>

static SDL_Window   *window   = NULL;
static SDL_Renderer *renderer = NULL;

static SDL_bool is_running = SDL_TRUE;

static SDL_Event event = {0};

static SDL_Vertex vertex_1 = {{100.5, 100.5}, {255, 255, 255, 255}, {1, 1}};
static SDL_Vertex vertex_2 = {{200.5, 100.5}, {255, 255, 255, 255}, {1, 1}};
static SDL_Vertex vertex_3 = {{100.5, 200.5}, {255, 255, 255, 255}, {1, 1}};

static SDL_Vertex vertices[3] = {0};

static SDL_Surface *surface = NULL;
static SDL_Texture *texture = NULL;

int main(void) {
        int flags = SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC;
        vertices[0] = vertex_1;
        vertices[1] = vertex_2;
        vertices[2] = vertex_3;

        SDL_Init(SDL_INIT_VIDEO);

        window   = SDL_CreateWindow("", 0, 0, 1280, 720, SDL_WINDOW_SHOWN);
        renderer = SDL_CreateRenderer(window, -1, flags);

        surface = IMG_Load("foo.png");
        texture = SDL_CreateTextureFromSurface(renderer, surface);

        while(is_running) {
                SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

                SDL_RenderClear(renderer);

                while(SDL_PollEvent(&event)) {
                        switch(event.type) {
                                case SDL_QUIT:
                                        is_running = SDL_FALSE;
                                        break;
                        }
                }

                vertices[0].color.a = 55;
                vertices[1].color.a = 55;
                vertices[2].color.a = 55;
                SDL_RenderGeometry(renderer, texture, vertices, 3, NULL, 0);

                SDL_RenderPresent(renderer);
        }

        SDL_FreeSurface(surface);
        SDL_DestroyTexture(texture);
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
}

Hi)
Maybe you should also set a blend mode for your texture? See SDL2/SDL_SetTextureBlendMode - SDL Wiki. In your case you need SDL_BLENDMODE_BLEND.

2 Likes

Thank you so much! I could have sworn I tried this, but I guess not. :sweat_smile:

1 Like