Jump to content
  • Advertisement
Sign in to follow this  
RealMarkP

Using a texture as a mask

This topic is 3882 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

Greetings, I have a texture with an alpha channel. When applied normally, it has proper transparency and what not. What I am trying to do now is multi texture a second texture on top of this one. Is it possible for the second texture to blend properly (because it has its own alpha channel) with the bottom texture, and also is it possible (for the places where the bottom texture has alpha) that the bottom texture to work as a mask, and only render in places where there is not transparency? How would I get this effect?

Share this post


Link to post
Share on other sites
Advertisement
You could get this effect easily with shader programming. Anyway you can do blending without GL_ARB_multitexture, but just with GL_BLEND and glEnable/glDisable + another quad it should be possible (Never tried that, I always did this kind of things with shaders). The second effect just use stencil buffer to match, where alpha is visible (F.e. draw white quad into stencil buffer and increase number in it, when it passes the stencil test) and then render the second quad with texture on it just where stencil test passed (that means on F.e. number 1, if you had cleared your stencil to zero before mentioned "stencil test")

Share this post


Link to post
Share on other sites
ah I see. currently I am doing the quad way, but without using stencil buffers, so I get textures in places where there shouldnt be any.

could you post on how one would use shaders for this?

Share this post


Link to post
Share on other sites
Sure, so you have to first load shader programms into your app, and they'll look like this in GLSL:

Vertex Program

// We're sending texCoord from vertex to fragment shader
varying vec2 texCoord;

// Main loop
void main()
{
// texcoords are from first (zero) texturing unit of opengl (the
// standart if using glMultiTexCoord at GL_TEXTURE0 or standart
// glTexCoord)
texCoord = gl_MultiTexCoord0.xy;

// Rasterize vertices by multiplying them with modelview and projection
// matrices
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


Fragment Program

// Getting texCoord from vertex shader
varying vec2 texCoord;

// First texture that the shader got from app
uniform sampler2D primaryTexture;
// Second texture from the app
uniform sampler2D secondaryTexture;
// Type of mixing - 0 = blending them together, 1 = alpha testing away of pixels
// variable from app
uniform int mode;
// value to do alpha test from app
uniform float alphaTestValue;

// main loop
void main()
{
// Getting our texture into vec4 variable type (4 * float)
vec4 primaryBuffer = texture2D(primaryTexture, texCoord);
// Getting our second texture into another vec4
vec4 secondaryBuffer = texture2D(secondaryTexture, texCoord);

// And final color as variable
vec4 finalColor;

// GLSL languague doesn't support switch()+case yet (maybe OpenGL 3.0
// and GLSL's update?), so we have to use if/else/else if to do this

// if mode is mixing...
if(mode == 0)
{
// Then final color is mix of these two textures (A + B) / 2
finalColor = 0.5 * (primaryBuffer + secondaryBuffer);
}
// else if mode is testing away...
else if(mode == 1)
{
// if "alpha texture's alpha channel" is smaller then value
if(primaryBuffer.w < alphaTestValue)
{
// then discard pixel (cull pixel away - same as
// GL_ALPHA_TEST with glEnable/glDisable token)
discard;
}

// If pixel hasn't been discarded, then set final color as
// second texture
finalColor = secondaryBuffer;
}

// Output pixel color is final color
gl_FragColor = vec4(finalColor);
}


In those shaders are samplers as RGBA textures, which have transparency in alpha channel!
Well, I hope I've written this correctly (It's around 11:30 PM here and I hadn't slept much in last several days - so I hope there isn't any issue in code and it's understandable - if not, please let me know ;-)), so good night for now.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!