Blending useing glColor4f(....)

  • Follow


Hi,

I am trying to blend one texture on another using glColor4f(...)... It
refuses to work...  it just does not blend...

Please have a look at the below code...Are any flags i need to enable/
disable?

glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
<gen,bind texture>
....
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
1024,512,0,GL_BGRA_EXT,              GL_UNSIGNED_INT_8_8_8_8_REV,
NULL);

glTexSubImage2D(GL_TEXTURE_2D,
0,0,0,1024,512,GL_BGRA_EXT,GL_UNSIGNED_INT_8_8_8_8_REV,pixels);

glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glBindTexture(..)

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);//please alpha blend!!

glBegin(GL_QUADS)
.....
glEnd();

Drawing of the two textures work fine.. Also blending based on each
pixel's alpha value works fine.... but blending by glColor4f doesnt...

Please note that I am using SDL+Opengl.... Appreciate your help...
0
Reply Akhil.Malhotra (6) 4/29/2009 12:21:55 PM

"pro-grammer" <Akhil.Malhotra@gmail.com> wrote in message 
news:1818627d-f001-4a2d-b0cf-e6b98a8a5065@j9g2000prh.googlegroups.com...
> Hi,
>
> I am trying to blend one texture on another using glColor4f(...)... It
> refuses to work...  it just does not blend...
>
> Please have a look at the below code...Are any flags i need to enable/
> disable?
>
> glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
> <gen,bind texture>
> ...
> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
> glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
>
> glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
> 1024,512,0,GL_BGRA_EXT,              GL_UNSIGNED_INT_8_8_8_8_REV,
> NULL);
>
> glTexSubImage2D(GL_TEXTURE_2D,
> 0,0,0,1024,512,GL_BGRA_EXT,GL_UNSIGNED_INT_8_8_8_8_REV,pixels);
>
> glMatrixMode(GL_MODELVIEW);
> glEnable(GL_TEXTURE_2D);
> glBindTexture(..)
>
> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
> glEnable(GL_BLEND);
> glColor4f(1.0f, 1.0f, 1.0f, 0.5f);//please alpha blend!!
>
> glBegin(GL_QUADS)
> ....
> glEnd();
>
> Drawing of the two textures work fine.. Also blending based on each
> pixel's alpha value works fine.... but blending by glColor4f doesnt...
>
> Please note that I am using SDL+Opengl.... Appreciate your help...

you are missing glTexenv (..., GL_MODULATE).

jbw 


0
Reply jbwest 4/30/2009 12:57:45 AM


pro-grammer wrote:

> glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,
> 1024,512,0,GL_BGRA_EXT,              GL_UNSIGNED_INT_8_8_8_8_REV,
> NULL);
> 
> glTexSubImage2D(GL_TEXTURE_2D,
> 0,0,0,1024,512,GL_BGRA_EXT,GL_UNSIGNED_INT_8_8_8_8_REV,pixels);

It's not, that this is wrong. But for a initial texture creation it's more
efficient, to supply the pixels directly to glTexImage2D. Using
glTexSubImage2D is more efficient only if you want to _replace_ (parts of)
the image of a texture, keeping all it's parameters.

> glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
> glEnable(GL_BLEND);
> glColor4f(1.0f, 1.0f, 1.0f, 0.5f);//please alpha blend!!
> 
> glBegin(GL_QUADS)
> ....
> glEnd();
> 
> Drawing of the two textures work fine.. Also blending based on each
> pixel's alpha value works fine.... but blending by glColor4f doesnt...

Two things:

* You omit glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

* If you're doing a multipass, i.e. render two distinct quads, then the
first quad's fragments (i.e. visible pixels + depth values) will
depth-block the second quads fragments.
  The standard method for drawing such is to render the first quad with
normal depth test operation stencil mask generation enabled. Then the depth
test is disabled and the stencil test enabled.

Instead of multiple quads you also could just use multitexturing blending
the two textures within rendering a single quad.

This is done by

GLuint TexID[2];
glGenTextures(2, TexID);

glBindTexture(GL_TEXTURE_2D, TexID[0]);
glTexImage2D(GL_TEXTURE_2D, ..., texture[0]);

glBindTexture(GL_TEXTURE_2D, TexID[1]);
glTexImage2D(GL_TEXTURE_2D, ..., texture[2]);

glActiveTexture(0);
glBindTexture(GL_TEXTURE_2D, TexID[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glActiveTexture(1);
glBindTexture(GL_TEXTURE_2D, TexID[1]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

glBegin(GL_QUADS);
for(f in model.faces) {
        for(vi in f.vert_indices) {
                glMultiTexCoord2fv(0, &model.verts[vi].texcoord[0]);
                glMultiTexCoord2fv(1, &model.verts[vi].texcoord[1]);
                glVertex3fv(&model.verts[vi].pos);
        }
}
glEnd();


Note: I'm using immediate mode calls here just for clarity, if you aim for
performance use vertex arrays.
0
Reply Wolfgang 4/30/2009 9:35:13 AM

Hi! Thanks for the response Wolfgang... I still am not entirely clear
though... a couple of doubts:

I understand that i must
                 enable depth test
                 draw quad 1
                 disable depth test
                 enable blend
                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE);
                 glColorf4(1,1,1,alpha)
                 draw quad 2
But what are the stencil related operations i need to perform? I am a
newbie to using the stencil buffer, so need a little spoon feeding
here... Really appreciate your help...
0
Reply pro 5/7/2009 7:41:39 AM

On May 7, 3:41=A0pm, pro-grammer <Akhil.Malho...@gmail.com> wrote:
> Hi! Thanks for the response Wolfgang... I still am not entirely clear
> though... a couple of doubts:
>
> I understand that i must
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0enable depth test
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0draw quad 1
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0disable depth test
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0enable blend
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_E=
NV_MODE,
> GL_MODULATE);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0glColorf4(1,1,1,alpha)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0draw quad 2
> But what are the stencil related operations i need to perform? I am a
> newbie to using the stencil buffer, so need a little spoon feeding
> here... Really appreciate your help...

I'm not sure why you need to use the stencil. The stencil
works like a mask and is used when you want to block out
certain parts of the buffer.

Have you also tried disabling depth testing to make sure
both your quads are actually rendered and composited?

..rex
0
Reply rexguo 5/9/2009 4:38:02 AM

rexguo wrote:

> On May 7, 3:41=C2=A0pm, pro-grammer <Akhil.Malho...@gmail.com> wrote:=

>> Hi! Thanks for the response Wolfgang... I still am not entirely clea=
r
>> though... a couple of doubts:
>>
>> I understand that i must
>> enable depth test
enable stencil write, disable stencil test:
glStencilMask(GL_TRUE);
glStencilOp(GL_ZERO, GL_ZERO, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 1);
glDisable(GL_STENCIL_TEST);
>> draw quad 1
>> disable depth test
enable stencil test, disable stencil write:
glStencilMask(GL_FALSE);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilFunc(GL_EQUAL, 1, 1);
glEnable(GL_STENCIL_TEST);
>> enable blend

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

just for reference.

So far you described a multiple pass approach with single texutre appli=
ed to
each quad rendered.

>> glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
>> GL_MODULATE);
>> glColorf4(1,1,1,alpha)
>> draw quad 2

>> But what are the stencil related operations i need to perform? I am =
a
>> newbie to using the stencil buffer, so need a little spoon feeding
>> here... Really appreciate your help...
>=20
> I'm not sure why you need to use the stencil. The stencil
> works like a mask and is used when you want to block out
> certain parts of the buffer.

Yes, he must use the stencil buffer. He's disabling the depth test, so =
the
stencil mask is required to mask obstructed parts of the rendered quad.=


--=20
OpenGL tip #42:
How to exactly map texture texels to screen pixels:
<http://preview.tinyurl.com/cgndc8>
0
Reply Wolfgang 5/9/2009 12:45:18 PM

Thank you for your replies... unfortunately, it still doesnt work (i
tried the stencil logic too)..

Some info that I should mention:
I am using opengl with SDL on Linux(using x). I have enabled
doublebuffer.
The only thing i am doing is im drawing 2 RGBA textures. Both textures
cover the full screen, have identical properties(format, etc), and
both are at the same z..

Texture1 is first drawn with depth testing enabled, blending disabled.
Texture2 is drawn with
(1)glDisable(GL_DEPTH_TEST)
(2)glEnable(BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
(3)glTexi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
(4) glColor4f(1.0f, 1.0f, 1.0f, 0.5f)
Finally I call SDL_GL_SwapBuffers();

Here is what I have noticed:
If I draw Texture1 as above, then do (1),(2),(3),(4),and then I

(a)Draw Texture2,-->> I dont see Texture2 at all on the screen, only
Texture1.

(b)Disable GL_TEXTURE_2D, and draw a quad, -->> I see a transparent
grayish quad.

In case (a), it seems to be ignoring the alpha value of the glColor4f
command, and uses the alpha of the RGBA texture intead(which is 0
throughout).

I want to make it ignore the alpha values of the RGBA texture and pick
up the alpha value I specify using glColor4f. My goal is to be able to
make the RGBA texture transparent without having to modify the apha
values of each pixel.

If i change the alpha values of each pixel of Texture2,
glSubTexImage2D it, and then enable blend and draw, it works fine. But
this is unacceptably slow.

Please let me know if you can suggest something else.... Appreciate
it...
0
Reply pro 5/11/2009 12:37:47 PM

"pro-grammer" <Akhil.Malhotra@gmail.com> wrote in message 
news:5f012189-4ff1-41fd-8ab0-2bf4db815eda@p4g2000vba.googlegroups.com...
> Thank you for your replies... unfortunately, it still doesnt work (i
> tried the stencil logic too)..
>
> Some info that I should mention:
> I am using opengl with SDL on Linux(using x). I have enabled
> doublebuffer.
> The only thing i am doing is im drawing 2 RGBA textures. Both textures
> cover the full screen, have identical properties(format, etc), and
> both are at the same z..
>
> Texture1 is first drawn with depth testing enabled, blending disabled.
> Texture2 is drawn with
> (1)glDisable(GL_DEPTH_TEST)
> (2)glEnable(BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
> (3)glTexi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
> (4) glColor4f(1.0f, 1.0f, 1.0f, 0.5f)
> Finally I call SDL_GL_SwapBuffers();
>
> Here is what I have noticed:
> If I draw Texture1 as above, then do (1),(2),(3),(4),and then I
>
> (a)Draw Texture2,-->> I dont see Texture2 at all on the screen, only
> Texture1.
>
> (b)Disable GL_TEXTURE_2D, and draw a quad, -->> I see a transparent
> grayish quad.
>
> In case (a), it seems to be ignoring the alpha value of the glColor4f
> command, and uses the alpha of the RGBA texture intead(which is 0
> throughout).
>
> I want to make it ignore the alpha values of the RGBA texture and pick
> up the alpha value I specify using glColor4f. My goal is to be able to
> make the RGBA texture transparent without having to modify the apha
> values of each pixel.
>
> If i change the alpha values of each pixel of Texture2,
> glSubTexImage2D it, and then enable blend and draw, it works fine. But
> this is unacceptably slow.
>
> Please let me know if you can suggest something else.... Appreciate
> it...

I think the incoming texture alpha needs to be non-zero -- I think modulate 
is texture alpha * quad alpha.

jbw 


0
Reply jbwest 5/12/2009 12:41:11 AM

Yup.. that was the problem... i set all the alpha values to 1 and it
worked...

Thanks guys!
0
Reply pro 5/12/2009 9:29:59 AM

8 Replies
316 Views

(page loaded in 0.115 seconds)

Similiar Articles:













7/22/2012 12:51:34 AM


Reply: