Masking

Member
Posts: 509
Joined: 2002.05
Post: #1
I have everything working on my GTA style mini map except the mask. What I am trying to do is render a black circle (this part is 100% working), and then render a quad on top of the circle but only rendering the pixels with the black mask underneath it. Here is my process below. I have tried just about every glBlendFunc I could think of.

Code:
glColor4f(1.0,1.0,1.0,1.0);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_LIGHTING);
        glEnable( GL_BLEND );
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        
        glBindTexture(GL_TEXTURE_2D, texture[tmask]);
        glBegin( GL_QUADS );
        glTexCoord2f(0.0,0.0);
        glVertex3f(-rsize, -rsize, 0.0f);
        glTexCoord2f(1.0,0.0);
        glVertex3f(rsize, -rsize, 0.0f);
        glTexCoord2f(1.0,1.0);
        glVertex3f(rsize,rsize, 0.0f);
        glTexCoord2f(0.0,1.0);
        glVertex3f(-rsize, rsize, 0.0f) ;
        glEnd();
        
        glBlendFunc(GL_DST_COLOR,GL_ONE);
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
        //glBlendFunc(GL_ONE, GL_ONE);
        
        glBindTexture(GL_TEXTURE_2D, texture[tminimap]);
        glBegin( GL_QUADS );
        glTexCoord2f(tx-tsize,ty-tsize);
        glVertex3f(-rsize+.5, -rsize, 0.0f);
        glTexCoord2f(tx+tsize,ty-tsize);
        glVertex3f(rsize, -rsize, 0.0f);
        glTexCoord2f(tx+tsize,ty+tsize);
        glVertex3f(rsize,rsize, 0.0f);
        glTexCoord2f(tx-tsize,ty+tsize);
        glVertex3f(-rsize, rsize, 0.0f) ;
        glEnd();
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Have you thought of using the stencil buffer? That's exactly what it's for...
When you render the black circle, make it set the stencil bits as well. Then set your stencil function so that it only draws into the stenciled bits.

Or...

You could render to a texture, and then draw that. It might be slower (probably by very little), but you could do some cooler things this way. That's how I'd do it.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #3
You could also resort to multitexturing, or creating a polygon disk. Setting up tex coords for a disk is not much harder than for a quad, and you don't need other fancy things. Is it possible that you are trying to draw a textured disk in a very backwards way?
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #4
Thanks for the replies. Is there a way to do it with blending or even better, multitexturing? I don't want to make a stencil buffer unless I have to because I know I am already hurting on VRAM use without half of the assets done. I could use a disk but with gluDisk but I want to eventually have a nice boarder around it so thats why I was trying the mask.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #5
With multitexturing, you'd have to draw your map texture onto a quad with a second round alpha texture to mask it. If you want to draw anything on top of that you'd have to be careful not no draw them outside the disk. (unless you use render to texture)
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #6
Skorche Wrote:With multitexturing, you'd have to draw your map texture onto a quad with a second round alpha texture to mask it. If you want to draw anything on top of that you'd have to be careful not no draw them outside the disk. (unless you use render to texture)

Thats what I thought, thats why I just wanted to do a 2 pass approach like I was trying. it seems like i'm missing something simple.
Quote this message in a reply
Moderator
Posts: 522
Joined: 2002.04
Post: #7
Couldn't you just compare the squared distance of the blips of your radar to the center point against some constant? (Then don't draw them if they are too far away from center)

-Jon
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #8
aarku Wrote:Couldn't you just compare the squared distance of the blips of your radar to the center point against some constant? (Then don't draw them if they are too far away from center)

-Jon

Its just a 1024x1024 texture map, not too much detail but enough to see roads and buildings easily
Quote this message in a reply
Moderator
Posts: 522
Joined: 2002.04
Post: #9
Oh, I didn't realize it wasn't generated dynamically. Sounds like a lot of work in level design.
Quote this message in a reply
AngelDaniel
Unregistered
 
Post: #10
Hi.

I don't post here much but I thought of a possible suggestion.

Maybe you can first draw the circle into the ALPHA channel of the backbuffer only, or draw the circle with a blend mode that will store the circle's alpha channel in the backbuffers alpha channel. Then set the blend mode so that it uses GL_DST_ALPHA for the source and GL_ONE or GL_ONE_MINUS_DST_ALPHA (or whatever) for the dest. Then render the texture covering the whole screen or at least the circle area.

It should, I would think, multiply the texture pixel values by the value in the backbuffer's alpha channel before producing the final output. This should end up showing the pixels inside the circle as textures and the pixels outside as transparent, ie the existing background shows through. You could even have it antialiased since you can use the alpha channel as a regular alpha value which can have multiple levels of transparency.

If you need the black circle to be visible you can draw it to the backbuffer as normal, but you'd need to draw one into the alpha channel as well - or as part of drawing the circle.

I think this should work. Let me know how it goes.
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #11
aarku Wrote:Oh, I didn't realize it wasn't generated dynamically. Sounds like a lot of work in level design.

Well it is sort of, I have a button in my road editor and object editor that draws onto the base light map of the terrain. I then go to photoshop and do a little work with blending and stroking to make it look better.

AngelDaniel, Thanks for the post. I'm going to try that right now, someone else suggested that a day ago but I wanted to do it without the alpha channel. After more thinking It is a good idea to use the destination alpha, I'm sure I will find other uses for it too. I may try a stencil buffer too, but they are both basically the same thing (I virtual screen)
Quote this message in a reply
Member
Posts: 509
Joined: 2002.05
Post: #12
Actually I just used the stencil buffer because I know I am going to need it for shadows later on. It seems to be working just fine now. Thanks for the help everyone
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #13
The use of DST_ALPHA is working really well for me -- I've now got the map for my game in a nice antialiased circle. Thanks for the tip, AngelDaniel!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES Texture Masking airfire 6 14,743 Mar 17, 2014 07:07 PM
Last Post: baioses
  OpenGL ES Texture Masking dalasjoe sin 0 3,842 Apr 13, 2012 12:17 AM
Last Post: dalasjoe sin
  Masking without a stencil buffer? Bachus 12 13,371 Sep 2, 2010 02:42 PM
Last Post: Skorche
  Alpha Masking Problem bonanza 5 3,771 Jun 29, 2008 11:58 AM
Last Post: maximile
  masking without blending? ghettotek 2 2,558 Mar 5, 2003 04:41 PM
Last Post: ghettotek