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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-31-2008, 12:31 PM   #1 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 2
Alfons is on a distinguished road
Question How to extend Texture2d with opacity?

Hi all!

I am trying to extend the opengl Texture2d so it becomes more useful. What I want to achieve is to pass in custom Position, Rotation, Scale and Color in the drawAtPoint function. The position, rotation and scale part was easy, but I can't make the color multiply or blend with the texture. This is how the code looks like now.


Code:
- (void) drawAtPoint:(CGPoint)position rotation:(float)rotation scale:(CGPoint)scale
{
	CGPoint point = CGPointZero;
	GLfloat		coordinates[] = { 0,	_maxT,
		_maxS,	_maxT,
		0,		0,
	_maxS,	0 };
	GLfloat		width = (GLfloat)_width * _maxS,
	height = (GLfloat)_height * _maxT;
	GLfloat		vertices[] = {	-width / 2 + point.x,	-height / 2 + point.y,	0.0,
		width / 2 + point.x,	-height / 2 + point.y,	0.0,
		-width / 2 + point.x,	height / 2 + point.y,	0.0,
	width / 2 + point.x,	height / 2 + point.y,	0.0 };
	
	
	glPushMatrix();
	glTranslatef(position.x, position.y, 0);
	glRotatef(-rotation, 0, 0, 1);
	glScalef(scale.x, scale.y, 1.0f);
	
	glBindTexture(GL_TEXTURE_2D, _name);
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	
	glPopMatrix();
}
After doing some research I found that I should enable the GL_BLEND and then use a function glColor4f. Like this, before I draw the texure;

Code:
        glEnable(GL_BLEND);
	//Blend the texture with 25% alpha
	glColor4f(1.0f, 1.0f, 1.0f, 0.25f);
But it doesn't work. I initialize my code with the same functions as the CrashLanding sample.

I am glad for any help I can get.
Cheers
Alfons
Alfons is offline   Reply With Quote
Old 08-01-2008, 11:40 AM   #2 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 38
Xavier is an unknown quantity at this point
Default

Hi Alfons,

try this before glDrawArrays:

Code:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f, 0.25f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
it should work.
Xavier is offline   Reply With Quote
Old 08-02-2008, 07:03 AM   #3 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 2
Alfons is on a distinguished road
Default

Thanks Xavier,

It works, but I get strange artifacts where the png is half transperant.

I got another answer on another forum telling me to use this code

Code:
        glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE );
	glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULATE);
	glTexEnvf(GL_TEXTURE_ENV,GL_SRC0_RGB,GL_PRIMARY_COLOR);
	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
	glTexEnvf(GL_TEXTURE_ENV,GL_SRC1_RGB,GL_TEXTURE);
	glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);
No artifacts, but I doesn't multiply the colors. It's a bit left but now I know where I should look.

Thanks again!
Alfons
Alfons is offline   Reply With Quote
Old 01-24-2009, 07:04 PM   #4 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

You ever perfect this functionality? I am interested in this as well; thanks!
scotopia is offline   Reply With Quote
Old 01-25-2009, 07:33 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 166
gizmotoy is on a distinguished road
Default

I may be wrong, but I did some testing and found that switching these modes was much too costly to do on a texture by texture basis when you're making more than a couple things transparent. To get around this I draw transparent textures in groups like shown below:

Code:
Excerpt from renderScene():

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.0,1.0,1.0,0.5) // This line sets the alpha for the group to 0.5

** Draw the textures ([texture drawAtPoint/drawInRect], etc.)

glColor4f(1.0,1.0,1.0,1.0)
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
This will get you nice transparent textures with no artifacts
gizmotoy is offline   Reply With Quote
Old 01-25-2009, 07:48 AM   #6 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

Thanks. This works (as does the previous code) but I find that areas that are partially transparent don't show up. For instance consider I have a png of a moon with some glow around it; if I use this method the glow around the moon doesnt show up, even when the alpha is set at 1.0.
scotopia is offline   Reply With Quote
Old 01-25-2009, 08:01 AM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 166
gizmotoy is on a distinguished road
Default

Hmm, that's unusual. My drop shadows work fine.'

So you have a png with some built-in transparancy, correct? And that transparency doesn't work even if you have the alpha set to 1? Then I'd think the problem is with your PNG, and not your draw method. At 1, the PNG should be drawn as opaque as possible. Have you recently added the glow? Try running a Clean and rebuilding.
gizmotoy is offline   Reply With Quote
Old 01-25-2009, 09:45 AM   #8 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

As far as I can tell the PNG is just dandy; basically it is a 256 x 256 square; in the center of it is about a 200x200 moon (opaque) surrounded by some glowness (partial alpha) which fades out to completely transparent as it goes further out. If I use this same graphic in an imageView or whatnot it works fine; confused.
scotopia is offline   Reply With Quote
Old 01-25-2009, 11:33 AM   #9 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 166
gizmotoy is on a distinguished road
Default

I'm confused as to what you're trying to do, then. This thread was about making a texture that is opaque transparent.

Are you saying:
1) That if you just try to display your stock PNG that the glowing part that's affected by the alpha channel is missing? or
2) When you try to apply a constant alpha to your PNG, the glowing part disappears?

It probably has to do with your GL parameters. I have both textures that I'm applying a constant alpha to as well as textures with alpha shadows that I'm applying additional alpha to, and both work correctly with the stock Texture2D (when used with the code I posted).
gizmotoy is offline   Reply With Quote
Old 01-25-2009, 12:07 PM   #10 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

Its really not that complicated. I have the above graphic that I was talking about right? Now, i want to use that graphic in a situation where the whole screen is fading in; to do the fade in I was using the openGL blending modes; but when it fades in; the glow on the moon is gone: problem.
scotopia is offline   Reply With Quote
Old 01-26-2009, 02:09 PM   #11 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

Anyone have any ideas on this?
scotopia is offline   Reply With Quote
Old 01-27-2009, 11:03 AM   #12 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

Maybe I can just get around this by having a black texture over the image in question and fading that out (as opposed to fading the image in).... still curious about this though; any thoughts?
scotopia is offline   Reply With Quote
Old 01-27-2009, 11:27 AM   #13 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 166
gizmotoy is on a distinguished road
Default

I still think it has to do with your OpenGL setup parameters. At one point I was doing exactly this with the standard Texture2D and it worked fine.

I'm still fairly new to OpenGL so I don't have any specific recommendations, but I'd start there.
gizmotoy is offline   Reply With Quote
Old 01-27-2009, 11:33 AM   #14 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

I think everything is set up in the proper way; and on the device the "glow" does show up... a little bit; its just not nearly as vibrant as it should be.
scotopia is offline   Reply With Quote
Old 01-27-2009, 12:14 PM   #15 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 166
gizmotoy is on a distinguished road
Default

Hmm... I don't recall if my shadow seemed darker than usual, I just checked to make sure it was there. May it's doing some kind of multiplication of the alphas that's unexpected.

Have you tried changing your clear color? I recall that having some effect on how transparency is generated. That's really the only thing I can think of.

I do think your workaround will be fine. I use a 1x1 black texture I stretch and apply an alpha to to darken the background at some points, though there is probably a better way to do it.
gizmotoy is offline   Reply With Quote
Old 01-27-2009, 12:16 PM   #16 (permalink)
Registered Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 2,070
scotopia is on a distinguished road
Default

Yeah, thats a good idea to save some memory;and yeah I remember someone else saying something about the clear color; I'll mess around with it. It's not critical but I'd like to figure it out if possible. Thanks for the suggestions.
scotopia is offline   Reply With Quote
Old 01-28-2009, 05:47 PM   #17 (permalink)
New Member
 
Join Date: Oct 2008
Location: West Hills, CA
Posts: 67
rilian is on a distinguished road
Default

Ok, I did some fiddling today to add alpha to Texture2D. Here's my findings.

First off, I had to change:
Code:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
to
Code:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
(*not* GL_COMBINE)

I had to modify my texture atlas rendering code to do the following for rendering.

Code:
    GLfloat color[] = {
        alpha, alpha, alpha, alpha, // RGBA
        alpha, alpha, alpha, alpha,
        alpha, alpha, alpha, alpha,
        alpha, alpha, alpha, alpha,
    };

*snip*

    glEnableClientState(GL_COLOR_ARRAY);
    glBindTexture(GL_TEXTURE_2D, [self name]);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
    glColorPointer(4, GL_FLOAT, 0, color);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glDisableClientState(GL_COLOR_ARRAY);
(assume the other arrays (vertex, texcoord) are set up as normal)

I *believe* that I could equally do this without applying per-vertex alpha by just doing glColor4f(alpha, alpha, alpha, alpha) once, per other notes in this list. But I got it working this way, so I've stopped here for now.

Here's the effect I was after. I have a background texture. I have an overlay texture with an alpha channel in it. When drawing the overlay texture, and alpha was passed in as 0.0f, I want the overlay texture to "be ignored". When it's at 1.0f, I wanted the full value of the overlay color to be rendered on top of the background.

Tip: I originally passed in color values of 1.0, 1.0, 1.0, alpha (RGBA), and I was getting what I'd call "overbrightening" when alpha was zero. Turns out this was because GL_MODULATE was still taking into account the vertex color (full bright white) in the calculation. If I make the RGB value be greyscale, at the same intensity as the alpha, it works as I expect. When alpha is zero, GL_MODULATE sees a vertex color of black, and I don't see overbrightening.

Hope this explanation was helpful for any of your situations.
rilian is offline   Reply With Quote
Old 09-10-2009, 01:52 AM   #18 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: India
Posts: 239
milanjansari is an unknown quantity at this point
Default

Quote:
Originally Posted by rilian View Post
Ok, I did some fiddling today to add alpha to Texture2D. Here's my findings.

First off, I had to change:
Code:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
to
Code:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
(*not* GL_COMBINE)

I had to modify my texture atlas rendering code to do the following for rendering.

Code:
    GLfloat color[] = {
        alpha, alpha, alpha, alpha, // RGBA
        alpha, alpha, alpha, alpha,
        alpha, alpha, alpha, alpha,
        alpha, alpha, alpha, alpha,
    };

*snip*

    glEnableClientState(GL_COLOR_ARRAY);
    glBindTexture(GL_TEXTURE_2D, [self name]);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
    glColorPointer(4, GL_FLOAT, 0, color);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glDisableClientState(GL_COLOR_ARRAY);
(assume the other arrays (vertex, texcoord) are set up as normal)

I *believe* that I could equally do this without applying per-vertex alpha by just doing glColor4f(alpha, alpha, alpha, alpha) once, per other notes in this list. But I got it working this way, so I've stopped here for now.

Here's the effect I was after. I have a background texture. I have an overlay texture with an alpha channel in it. When drawing the overlay texture, and alpha was passed in as 0.0f, I want the overlay texture to "be ignored". When it's at 1.0f, I wanted the full value of the overlay color to be rendered on top of the background.

Tip: I originally passed in color values of 1.0, 1.0, 1.0, alpha (RGBA), and I was getting what I'd call "overbrightening" when alpha was zero. Turns out this was because GL_MODULATE was still taking into account the vertex color (full bright white) in the calculation. If I make the RGB value be greyscale, at the same intensity as the alpha, it works as I expect. When alpha is zero, GL_MODULATE sees a vertex color of black, and I don't see overbrightening.

Hope this explanation was helpful for any of your situations.

hello,

i have one problem,can you help me?

i have change GLPaint Application.

i have set two overlapping image, when i draw line using finger on top of the image that part should be erase (and underling image should be appear).

i am very frustrate about this issue, please help me

Thank you,
milanjansari is offline   Reply With Quote
Reply

Bookmarks

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: 336
9 members and 327 guests
chiataytuday, givensur, ipodphone, jbro, mer10, mtl_tech_guy, Punkjumper, vilisei, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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