Sign in to follow this  

Simple Question: glColor4f() with glDrawArrays()

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I am drawing flat sprite objects using the following:
glBindTexture( GL_TEXTURE_2D, * );
glVertexPointer( 3, GL_FLOAT, 0, * );
glTexCoordPointer( 2, GL_FLOAT, 0, * );
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
I am using glColor4f( 1.0f, 1.0f, 1.0f, 0.5f ); before this to change the alpha of the rendered sprite, but obviously it is not working. I assume I have to set some texture state to some value, but what? I tried glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE ); but it changes nothing. Thank you, L. Spiro

Share this post


Link to post
Share on other sites
IIRC GL_MODULATE in the texEnv affects how the different texture units are combined, but not how the final fragment is composited into the framebuffer.

To blend with the framebuffer, you need to look at glBlendFunc and glEnable(GL_BLEND).

Share this post


Link to post
Share on other sites
glEnable( GL_BLEND ) has been called and I messed with many combinations of glBlendFunc() values but none gave the proper result.

I am open to suggestions as to what to pass to glBlendFunc(), because I am just getting every result but the one I want, which is to simply combine a 4-value color with the sprite I am drawing, so that I may fade it in and out.

Changing the color of the sprite may be nice in the future, but right now I simply want to change the alpha.


Regards,
L. Spiro

Share this post


Link to post
Share on other sites
Have you tried setting glBlendFunc to "standard" alpha blending?

IE:


//Will make whatever you draw transparent by a factor of 50%
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(1.f, 1.f, 1.f, 0.5f);
//Draw stuff..
glDisable(GL_BLEND);


The texture shouldn't matter much in this case, if all you're trying to do is to fade the sprite out.

If that doesn't work, you should do what mrbastard said, and post a screenshot of what you get. ;)

Share this post


Link to post
Share on other sites
I can not post the actual images in my game as they are confidential, but here are example images and the code I am using to get the results (note that glEnable( GL_BLEND ); has been called and blending is always enabled):




First the normal code without my attempts at special effects.

	// Logo.
glPushMatrix();
{
glTranslatef( logoX, 480.0f / 2.0f, 0 );
glRotatef( 90.0f, 0.0f, 0.0f, 1.0f );
[baseSprites[MM_LOGO] drawAtPoint:CGPointMake( 0.0f, 0.0f )];
}
glPopMatrix();


Which gives me the following result:
http://img522.imageshack.us/img522/512/defaultsx7.gif





#1: Regular blending attempt:
		GLint srcBled;
glGetIntegerv( GL_BLEND_SRC, &srcBled );

GLint dstBled;
glGetIntegerv( GL_BLEND_DST, &dstBled );

glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glColor4f( 1.0f, 1.0f, 1.0f, 0.125f );
//glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE );
[baseSprites[MM_LOGO] drawAtPoint:CGPointMake( 0.0f, 0.0f )];

glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glBlendFunc( srcBled, dstBled );

Which gives me the following result:
http://img522.imageshack.us/img522/512/defaultsx7.gif

The result is exactly the same as when glColor4f() was not used.





#2: Color blending attempt (without glColor4f()):
		GLint srcBled;
glGetIntegerv( GL_BLEND_SRC, &srcBled );

GLint dstBled;
glGetIntegerv( GL_BLEND_DST, &dstBled );

glBlendFunc( GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR );
// glColor4f( 1.0f, 1.0f, 1.0f, 0.125f );
//glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE );
[baseSprites[MM_LOGO] drawAtPoint:CGPointMake( 0.0f, 0.0f )];

glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glBlendFunc( srcBled, dstBled );

Which gives me something very similar to the following result:
http://img149.imageshack.us/img149/2049/coloram6.gif

Notice that glColor4f() is not used here, but this serves as proof that blending is enabled.





#3: Color blending attempt (WITH glColor4f()):
		GLint srcBled;
glGetIntegerv( GL_BLEND_SRC, &srcBled );

GLint dstBled;
glGetIntegerv( GL_BLEND_DST, &dstBled );

glBlendFunc( GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR );
glColor4f( 1.0f, 1.0f, 1.0f, 0.125f );
//glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE );
[baseSprites[MM_LOGO] drawAtPoint:CGPointMake( 0.0f, 0.0f )];

glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
glBlendFunc( srcBled, dstBled );

Which gives me something very similar to the following result:
http://img149.imageshack.us/img149/2049/coloram6.gif

The result is exactly the same as when glColor4f() was not used.




The desired result:
http://img149.imageshack.us/img149/4734/desiredbb0.gif


Basically the bottom line is that glColor4f() is always ignored.
Blending is enabled and I can get different results by changing the blend function, but I can not overlay a color/alpha mask with glColor4f().


It should not make a difference, but this is for an iPhone game (hence the Objective-C syntax).

On the [baseSprites[MM_LOGO] drawAtPoint:CGPointMake( 0.0f, 0.0f )]; line I have checked the drawing function carefully and it does not change any states.
It does rotation, scaling, and the 4 functions in my first post. Nothing more.


Whatever works to get the desired result is all I need. I just want the image to be transparent to some specifiable degree when I draw it.



Thank you,
L. Spiro

Share this post


Link to post
Share on other sites
Sorry, I misunderstood - I thought the problem was that it wasn't blending with the background correctly.

try:

glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

Share this post


Link to post
Share on other sites
Blending a texture with geometry color will work only if the texture has alpha channel in it. If the texture's alpha is 1 always, I don't think that BLENDing is going to work out for you.

However you can try blending the texture color with a constant color by setting the texture environment mode to GL_BLEND.


glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color);

Share this post


Link to post
Share on other sites
Quote:
Original post by KumGame07
Blending a texture with geometry color will work only if the texture has alpha channel in it. If the texture's alpha is 1 always, I don't think that BLENDing is going to work out for you.

In OpenGL's behavior description, a texture always have an alpha channel. If not explicitely defined by the texture data, it is implicitely defined to be 1. Blending works by blending the final fragment color with the frame buffer, and so it is irrrelevant whether the texture's alpha channel is explicitely defined or not.

Share this post


Link to post
Share on other sites
Quote:
Original post by KumGame07
Blending a texture with geometry color will work only if the texture has alpha channel in it. If the texture's alpha is 1 always, I don't think that BLENDing is going to work out for you.


This is both true and false. ;) It's possible to blend a texture with the environment without an alpha channel (IE; in a fading manner), but it's not possible to get transparent areas within the texture without an alpha channel or an alpha mask by using just GL_BLEND.

EDIT: BrotherBob already explained this.. Note to self; Don't write the reply and wait a few hours before pressing "Submit". ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by KumGame07
Blending a texture with geometry color will work only if the texture has alpha channel in it. If the texture's alpha is 1 always, I don't think that BLENDing is going to work out for you.

However you can try blending the texture color with a constant color by setting the texture environment mode to GL_BLEND.


glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND );
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color);


Now I am getting closer results but still off.


If I pass the following array:
GLfloat fCoolColor[] = { 1.0f, 1.0f, 1.0f, 0.0f };
glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, fCoolColor );

My logo becomes pure white (alpha is ignored).


If I pass:
GLfloat fCoolColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, fCoolColor );

My logo becomes inverted (alpha still ignored).


My image has an alpha channel; it has transparent parts and partially transparent parts.



I will remember these effects for later as they are nice, but for now I just want this image to be partially transparent.


Thank you,
L. Spiro

Share this post


Link to post
Share on other sites

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this