How to implement custom blend mode in SDL2? - sdl

SDL2 provides by default 3 blend modes: alpha blend, additive, and multiply.
However I need to render filled rectangles using the Linear Light blend mode.
Is this possible to implement using OpenGL, or as a combination of the 3 built-in blend modes, or should I convert all my SDL_Textures to SDL_Surfaces and modify the pixel values manually?

SDL 2.0.6 introduced the function SDL_ComposeCustomBlendMode allowing you to create a new blend mode for 2D rendering.
Here is the doc entry: https://wiki.libsdl.org/SDL_ComposeCustomBlendMode

Related

How to make transparent lines in a mesh using Qt3D?

I am using Qt3D to draw lines and I use the QPerVertexColorMaterial approach for more efficiency (see here for code example : How to optimize point cloud rendering in Qt3D).
I defined a size of 4 vertex for the color attribute (rgba) since I would like to modify transparency on specific lines. But modifying the alpha value seems to have no effect on display.
Is it possible to do this with the QPerVertexColorMaterial implementation in Qt3D ?
Although you can provide color attributes with proper alpha values for the vertices the QPerVertexColorMaterial itself does not implement alpha blending.
Have a look at QPhongAlphaMaterial under qt/qt3d/src/extras/defaults/qphongalphamaterial.cpp to see what's needed to handle transparency.

With Metal (vs OpenGL), How does color/alpha blending work on single-channel render targets?

I'm porting some OpenGL code from a technical paper to use with Metal. In it, they use a render target with only one channel - a 16-bit float buffer. But then they set blending operations on it like this:
glBlendFunci(1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
With only one channel, does that mean that with OpenGL, the target defaults to being an alpha channel?
Does anyone know if it is the same with Metal? I am not seeing the results I expect and I am wondering if Metal differs, or if there is a setting that controls how single-channel targets are treated with regards to blending.
In OpenGL, image format channels are labeled explicitly with their channels. There is only one one-channel color format: GL_R* (obviously with different bitdepths and other info). That is, red-only. And while texture swizzling can make the red channel appear in other channels from a texture fetch, that doesn't work for framebuffer writes.
Furthermore, that blend function doesn't actually use the destination alpha. It only uses the source alpha, which has the value the FS gave it. So the fact that the framebuffer doesn't store an alpha is essentially irrelevant.

SDL grid color smoothing

Is it possible to smooth a grid of values displayed as color using methods such as bicubic or linear interpolation with SDL? Currently I use SDL_FillRect to fast fill rectangles. Here is a reference image of interpolation in MATLAB to get an idea of what I want to achieve:
There is no built-in system in SDL to do what you want. You could easily do this using a shader and OpenGL, but you mentioned using SDL_FillRect so I assume you are using an SDL_Surface which suggests you're not using OpenGL. In that case you can easily plot the pixels and calculate the colours however you like.

Get OpenGL histogram of masked texture

I can get the histogram of an opengl texture using the glGetHistogram() function.
Similar to the OpenCV histogram function, where a second OpenCV matrix can be given as a mask, I have an OpenGL Texture and a binary mask (either as alpha channel or as a separate texture), and I would like to get a histogram of all the pixels in the image that are not masked.
Is this possible somehow?
glGetHistogram is deprecated since OpenGL 3.1 anyway.
Using compute shaders or occlusion queries would be a better idea.

LWJGL set transparency color?

I've been playing with LWJGL a little, as a bit of a step up from Pygame. I'm trying to render a sprite and I was wondering if LWJGL has a function similar to Pygame's colorkey that lets you define a color in an image that will be rendered as transparent. Do you have to use an alpha channel in OpenGL?
OpenGL doesn't have any built in color keying support. You'll need to either manually swap your key for alpha on the CPU, or use a custom shader that replaces it on the fly.