COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Blending in OpenGL

• Email
• Follow

```Hello everyone,

I am trying to use blending in OpenGL.  I tried out a simple program,
but it doesn't work.  I render a quad at z = -4.0f(Green) and another
quad at z = -2.0f(Red) and the two quads do not intersect.  I have
enabled blending using glEnable(GL_BLEND) and the depth test using
glEnable(GL_DEPTH_TEST). The blending function I use is
glBlendFunc(GL_SRC_ALPHA,GL_ONE).  I use 1.0f for the alpha values.  If
my understanding is right, the quad at -2.0f should hide the quad at
-4.0f. If I draw the quad at z=-2.0f first and then the quad at
z=-4.0f, the hidden surfaces are removed. But if I draw them in the
reverse order (z = -4.0f first and then z = -2.0f) there is a blending
of colors.  Can someone explain why this happens?  Have I understood
the blend function wrong?

Here's what I expect the program to do. The two quads should be drawn
according to their depths, ie. the hidden surfaces should not be drawn
and this should be done in the same way irrespective of the order in
which they are drawn.  When they intersect, blending should occur in
the region of intersection.  If I have gone wrong in coding this, what
should I do to achieve the above result.

Here is a section of the code.

draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);

glBlendFunc(GL_ONE,GL_ONE);
glVertex3f(-5.0f,-5.0f,-4.0f);
glVertex3f(-5.0f,5.0f,-4.0f);
glVertex3f(5.0f,5.0f,-4.0f);
glVertex3f(5.0f,-5.0f,-4.0f);
glEnd();

glVertex3f(-2.5f,-2.5f,-2.0f);
glVertex3f(-2.5f,2.5f,-2.0f);
glVertex3f(7.5f,2.5f,-2.0f);
glVertex3f(7.5f,-2.5f,-2.0f);
glEnd();
glflush();
}

Thank you,

Sriram.

P.S: I have tried using accumulation buffer to achieve the same result,
but in vain. Is there a way, I can get through the accumulation buffer
method?

```
 0
Reply absriram324 (11) 10/17/2004 12:19:07 AM

See related articles to this posting

```Sriram wrote:
> Hello everyone,
>
> I am trying to use blending in OpenGL.  I tried out a simple program,
> but it doesn't work.  I render a quad at z = -4.0f(Green) and another
> quad at z = -2.0f(Red) and the two quads do not intersect.  I have
> enabled blending using glEnable(GL_BLEND) and the depth test using
> glEnable(GL_DEPTH_TEST). The blending function I use is
> glBlendFunc(GL_SRC_ALPHA,GL_ONE).  I use 1.0f for the alpha values.  If
> my understanding is right, the quad at -2.0f should hide the quad at
> -4.0f.

If your camera is in the right position, yes.

> If I draw the quad at z=-2.0f first and then the quad at
> z=-4.0f, the hidden surfaces are removed.

The second quad's fragments will fail the z test and be discarded.

> But if I draw them in the
> reverse order (z = -4.0f first and then z = -2.0f) there is a blending
> of colors.  Can someone explain why this happens?  Have I understood
> the blend function wrong?

In this case the second quad's fragments will pass the z test and be
blended with the pixels in the frame buffer.
>
> Here's what I expect the program to do. The two quads should be drawn
> according to their depths, ie. the hidden surfaces should not be drawn
> and this should be done in the same way irrespective of the order in
> which they are drawn.

Yes and no. In one order, the front quad is drawn and the back quad is
is drawn on top of it. Without blending, the result is the same.

> When they intersect, blending should occur in
> the region of intersection.  If I have gone wrong in coding this, what
> should I do to achieve the above result.

If you want the blending to take place in both orders, you need to
disable the depth test.

--
Andy V

```
 0