Ronen Ness
Ronen Ness

Reputation: 10760

SDL2 - render on texture alpha channels

I'm looking for a way to render specifically on the alpha channels of a texture. the idea is to create a "mask" in runtime, i.e. to render a texture that will act as the alpha channels of another texture.

note: I know how to use alpha channels and color key so please don't explain about those :) what I'm looking for is a way to generate a mask dynamically in runtime, using other textures.

I prefer a solution that takes an advantage of the graphic card, and not something involving getting the pixels buffer and manipulate them in the cpu.

is that possible?

Upvotes: 3

Views: 3468

Answers (2)

Ronen Ness
Ronen Ness

Reputation: 10760

answering myself for others who might be interested:

  1. the mask I use is a black and white texture: black is opaque, white is transparent. mask can be generated at runtime by rendering to texture.
  2. when the mask texture is ready I copied it to another texture and inverted all the pixels. lets call this new texture "inverted mask".
  3. before rendering the texture I wanted to mask, I render at the same position and size the inverted mask texture with additive blending. this will create a white silhouette at the shape of the mask I want to apply (since its additive the black pixels are transparent).
  4. I then render the mask itself on the texture I want to mask, with additive blending. this will turn the background white (the pixels that are supposed to be transparent by the mask are now white. since its additive, the black pixels do no effect)
  5. and finally I render the texture on screen with mod blending, exactly on the white silhouette I rendered at phase 3. the background white pixels will be transparent (because its mod) and what was the white silhouette will now be the texture, masked.

note: this is not ideal method, it involved with several renderings and manipulating texture pixels. but it was sufficient to my specific case and its pure SDL so good enough for me.

Upvotes: 4

kdyz
kdyz

Reputation: 1560

Sorry if I get this wrong, but since you said "on the alpha channel" then it isn't possible, in terms of graphics, a channel is just a subgroup of AN image, along with the rgb channel, bw channel, sound channel(for depth,etc), etc.

But what I think you want to say is put ANOTHER image on top of the original mask, and the image on top is supposed to look like its covering the image below (then ajdust-adjust the transparency and viola), thus, giving off the image of a "mask". EG: A bride wearing a veil (the bride's face is the original image while the veil is translucent. By looking, you'd think of both as just one image.

There are two ways to do this, 1 being the easiest:

  1. Edit the image that will act as a mask and change its opacity in an image manipulation program, remember to save it in png. After doing so, render the first image to texture then render the opacity edited image afterwards.

  2. same as the first one but don't edit the second image, instead, just edit its alpha channel in SDL2. (With this method, you can control it dynamically along with time and rendering it to a texture takes advantage of the graphics card.)

Both would give of an image of 2 photos merged, with the second one acting as a mask.

Upvotes: 2

Related Questions