Results 1 to 5 of 5

Thread: Drawing masked sprites

  1. #1
    Member Newbie
    Join Date
    Jun 2008
    Posts
    34

    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)?

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,671

    Re: Drawing masked sprites

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

  3. #3
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Drawing masked sprites

    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);

  4. #4
    Member Newbie
    Join Date
    Jun 2008
    Posts
    34

    Re: Drawing masked sprites

    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).

  5. #5
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,671

    Re: Drawing masked sprites

    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.

Similar Threads

  1. Replies: 0
    Last Post: 02-07-2012, 09:39 AM
  2. Drawing "masked" character also with transparency- WHY?
    By AE in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 03-30-2005, 11:01 AM
  3. how to draw "Masked" 2D image with fade-in/out
    By mdive in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 08-01-2003, 05:22 PM
  4. How to draw fade-in/out Masked 2D image
    By mdive in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 06-19-2003, 04:58 AM
  5. masked blending and fog
    By Polymorph in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-06-2002, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean