Blending in OpenGL

  • 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);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);

glColor4f(0.0f,1.0f,0.0f,1.0f); //Green Quad
glBlendFunc(GL_ONE,GL_ONE);
glBegin(GL_QUADS);
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();

glColor4f(1.0f,0.0f,0.0f,1.0f); //Red Quad

glBegin(GL_QUADS);
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

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 
discarded. In the other order, the back quad is drawn and the front quad 
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
Reply Andy 10/17/2004 2:38:07 AM


1 Replies
295 Views

(page loaded in 0.161 seconds)

Similiar Articles:













7/26/2012 3:56:44 PM


Reply: