c++ - OpenGL - mask with multiple textures -
i have implemented masking in opengl according following concept:
- the mask composed of black , white colors.
- a foreground texture should visible in white parts of mask.
- a background texture should visible in black parts of mask.
i can make white part or black part work supposed using glblendfunc(), not 2 @ same time, because foreground layer not blends onto mask, onto background layer.
is there knows how accomplish in best way? have been searching net , read fragment shaders. way go?
this should work:
glenable(gl_blend); // use simple blendfunc drawing background glblendfunc(gl_one, gl_zero); // draw entire background without masking drawquad(backgroundtexture); // next, want blendfunc doesn't change color of pixels, // rather replaces framebuffer alpha values values based // on whiteness of mask. in other words, if pixel white in mask, // corresponding framebuffer pixel's alpha set 1. glblendfuncseparate(gl_zero, gl_one, gl_src_color, gl_zero); // "draw" mask (again, doesn't produce visible result, // changes alpha values in framebuffer) drawquad(masktexture); // finally, want blendfunc makes foreground visible in // areas high alpha. glblendfunc(gl_dst_alpha, gl_one_minus_dst_alpha); drawquad(foregroundtexture);
this tricky, tell me if unclear.
don't forget request alpha buffer when creating gl context. otherwise it's possible context without alpha buffer.
edit: here, made illustration.
edit: since writing answer, i've learned there better ways this:
- if you're limited opengl's fixed-function pipeline, use texture environments
- if can use shaders, use fragment shader.
the way described in answer works , not particularly worse in performance these 2 better options, less elegant , less flexible.
Comments
Post a Comment