Jump to content
  • Advertisement
Sign in to follow this  
penetrator

GLSL: blending two images

This topic is 4893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i would like to blend two images (the first is the base image, the second is a b/w image), and let the b/w image to act as a alpha map. Any suggestion ?

Share this post


Link to post
Share on other sites
Advertisement
so you want the black parts to be fully transparent and the white parts to be fully opaque? Use a fragment program and multiply the two color values together.

use two samplers (uniform sampler2D texture_n), read the color values ( texture2D(texture_n, texCoord_n); ), multiply them (color0 = color0 * color1;) and then set the resulting color with gl_FragColor = ...

HTH, this is off the top of my head so i might have made a mistake in the syntax or logic, looks like it'll work tho ;)

Share this post


Link to post
Share on other sites
if you use masking. it will slow your render rate.. its not good for you.

coz it must use use 2 steps

glBlendFunc(GL_DST_COLOR,GL_ZERO)
and
glBlendFunc(GL_ONE,GL_ONE)

i'll xplain a litle about glBlendFunc()

glBlendFunc(sFactor,dFactor);

Buffer = src*sFactor + buffer*dFactor

try to use

glBlendFunc(GL_SRC_ALPHA,GL_ONE);
or
glBlendFunc(GL_SRC_COLOR,GL_ONE);

GL_SRC_ALPHA means the alpha chanels
GL_SRC_COLOR means the color chanels
GL_ONE means (1,1,1,1) on RGBA

glBlendFunc(GL_SRC_ALPHA,GL_ONE);

blend the src color with the dist color based on src alpha;

so if the src pixel color 212 and the dist 100;

then the final is 212*alpha+100*1;

and if the src pixel color 0 and the dist 100;

then the final is 0*alpha+100*1;
and it will return the dist color..

so the black color usualy transparant. (in this case)

Share this post


Link to post
Share on other sites
typically you would want to set the b/w image as the alpha channel for the first image. then just use some form of blending or alpha testing

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

or

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);

i use alpha blending for most 3d geometry and only really use alpha test for the bitmap fonts. however, if you are just using a b/w mask, alpha test will probably be better. you don't have to do any blending so that means less framebuffer reads = faster performance.

if you haven't already, check out the masking tutorial hosted by Nehe:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=20

Share this post


Link to post
Share on other sites
btw shaders don't replace alpha testing or alpha blending, there is almost no point in masking in the shader itself unless you are manipulating the texture coords per fragment. here's a quick shader example:

[Vertex shader]

void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = vec2(gl_MultiTexCoord0.xy);
}


[Fragment shader]

uniform sampler2D Tex0;
uniform sampler2D Tex1;

void main()
{
gl_FragColor = vec4(texture2D(Tex0, gl_TexCoord[0]).xyz,0.0);
gl_FragColor.w = texture2D(Tex1, gl_TexCoord[0]).x;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!