Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-19-2011, 06:46 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
daLavaTroll is on a distinguished road
Default opengles blendfunc simple mask

Been working and searching on this for about a week. I have two image objects: a mask (I have tried three of these, half back/half transparent, half white/half transparent and half white/half black), and an object (a heart in this case) that has transparent components (png). I also have a background that is made up of lots of Sprites (quads moving through 3d space). I'm handling all the depth management on my own.

I don't really understand how these blending algorithms work, so I created an app where you can cycle through all the combinations. Here is the code I'm using:

Code:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//render all background objects
    
[self setBlend:before1 dstNum:before2 doTrace:NO tf:nil];
//eg glBlendFunc(GL_ZERO,GL_ZERO);
    
//render mask image

[self setBlend:after1 dstNum:after2 doTrace:NO tf:nil];
//render masked image
    
//render the rest of the scene
There are 11 parameters that I know of:

GL_ZERO (0, 0, 0, 0)

GL_ONE (1, 1, 1, 1)

GL_SRC_COLOR (Rs/kR, Gs/kG, Bs/kB, As/kA)

GL_ONE_MINUS_SRC_COLOR (1, 1, 1, 1) - (Rs/kR, Gs/kG, Bs/kB, As/kA )

GL_DST_COLOR (Rd/kR, Gd/kG, Bd/kB, Ad/kA)

GL_ONE_MINUS_DST_COLOR (1, 1, 1, 1) - (Rd/kR, Gd/kG, Bd/kB, Ad/kA )

GL_SRC_ALPHA (As/kA, As/kA, As/kA, As/kA)

GL_ONE_MINUS_SRC_ALPHA (1, 1, 1, 1) - (As/kA, As/kA, As/kA, As/kA )

GL_DST_ALPHA (Ad/kA, Ad/kA, Ad/kA, Ad/kA)

GL_ONE_MINUS_DST_ALPHA (1, 1, 1, 1) - (Ad/kA, Ad/kA, Ad/kA, Ad/kA )

GL_SRC_ALPHA_SATURATE (i, i, i, 1)


The first test I did, I had the first 10 on this list and went through all 10,000 combinations manually (with the black/transparent mask). I did not find what I needed there. So then I found the GL_SRC_ALPHA_SATURATE. With that, there are 14,641 combinations. I have not gone through all of those yet. In this second version, I put in all three mask types. It is a nice application, but I'm still not finding what I need. Here are the closest ones:


glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);


glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_DST_ALPHA);

I obviously need the shape of the first one, but the color of the second one. You know, just a plain old mask. I get really turned around thinking about all these combinations. And of course there are lots of duplicates. It would take the better part of a day to go through all the combinations with all 11 parameters. And I'm starting to lose hope that I'll find one that works. I've researched other methods, but haven't found anything else that comes this close.

Any ideas?
daLavaTroll is offline   Reply With Quote
Old 12-20-2011, 12:26 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 2
onimitch is on a distinguished road
Default

Unfortunately using a mask in OpenGL isn't as simple as you'd think. If your using OpenGL ES 1 you have to jump through alot of hoops to do it and on OpenGL ES 2 it can be done alot easier with shaders.

I was looking todo the same recently too and I found this page which was helpful:

How To Mask a Sprite with Cocos2D 1.0 | Ray Wenderlich
onimitch is offline   Reply With Quote
Old 12-20-2011, 01:03 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
daLavaTroll is on a distinguished road
Default

Yes, when I first looked into opengl masking, it looked damn near impossible. Then I got into these blend modes and got really hopeful. I just ran through the 14,641 combinations and did not find one that worked. I see all the pieces of what I need being used, but I can't get them in the right combination. If I didn't need to preserve the transparent part of my image, it would work.

The only other thing I can see with blend modes is glBlendFuncSeparateOES(). That takes four parameters though. That would be 110 * 14,641 combinations. I've played around with that a little bit. I'm going to try putting that into the tester and see if it does anything different.
daLavaTroll is offline   Reply With Quote
Old 12-20-2011, 01:35 PM   #4 (permalink)
Registered Member
 
headkaze's Avatar
 
Join Date: Feb 2010
Posts: 359
headkaze is on a distinguished road
Default

I needed an alpha mask once and I used Texture Combiners with the two textures in level 0 and 1. Not sure if you'll find it useful for your needs. You can read up more about Texture Combiners here

Code:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_PREVIOUS);

glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
__________________
Headsoft | Jungool
headkaze is offline   Reply With Quote
Old 12-20-2011, 04:26 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
daLavaTroll is on a distinguished road
Default

Thanks headkraze. I have seen this before. And glColorMask(). I couldn't get anything good with glColorMask. This texture combiner has a lot of lines of code. I plugged this into my program in various ways. I'm not really getting anything useful from it yet. All my objects through the end of this code are not showing up on the screen. I guess I'll have to play with this some more.

Thanks for the link to documentation on this. It looks a little more thorough than others I've seen.
daLavaTroll is offline   Reply With Quote
Reply

Bookmarks

Tags
glblendfunc, mask, opengl, opengles

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 376
10 members and 366 guests
apatsufas, comicool, Creativ, Dalia, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0