Drawing masked sprites

Hi, i would like to know how to draw a sprite (rendered using a textured quad) on the screen in such a way that some parts of it are not rendered at all (masked).

For example, suppose i have a wall with a window at its center. The window is open and i want to render a sprite inside the window. Suppose that the sprite is animated: the character is walking from left to right. I want to appear behind the window gradually.

The easy way is to use glScissor. Even if the window is not perfectly rectangular, i can use, for glScissor, a slightly bigger rectangle.

But what i i want to use a non rectangular mask (and even a non polygonal but complex mask)?

Draw the sprite, then draw the window on top of it.

Use paintshop pro or photoshop and add an Alpha channel to the texture containing the mask image.
Render the sprite using alpha testing and/or blending
glEnable (GL_ALPHA_TEST);
glAlphaFunc (GL_GREATER, 0.01);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Hi Alfonse, if i draw the sprite first and then the window,
part of the sprite will appear on the screen if i move it from left to right in order to show it appearing inside the window gradually.

BionicBytes: you propose to create a texture with an alpha channel.
When i should render it? I mean, i have the background (say a wall). I have the character sprite. I have a window sprite (the window is openm, so the central part is completely transparent.

How should i create the mask? When should i use it (in what order)?
Consider that i want to render other sprites above the window.

Ps.
I need a solution good for OpenGL ES, since the app will run on mobile phones, with OpenGL ES 1.1 ( 2.0 for the most recent phones).

Hi Alfonse, if i draw the sprite first and then the window,
part of the sprite will appear on the screen if i move it from left to right in order to show it appearing inside the window gradually.

I mean on the same rendering frame. You need to have an order for the sprite rendering. Draw things that are behind the window/wall/etc, then draw the window/wall/etc.

It’s 2D; ordering by z-distance is a solved problem.