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: Blending in OpenGL - comp.graphics.api.openglHello 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 a... Alpha blending with depth buffer - comp.graphics.api.opengl ...Well, the OpenGL RedBook chapter 6 "Blending, Antialiasing, Fog, and Polygon Offset" says in sub-chapter "Three-Dimensional Blending with the Depth Bu... Image blending - comp.graphics.api.openglHi, I'd like to blend 2 images together such that I can control how much of each image is output. However, I need to vary the proportion of each i... Blending useing glColor4f(....) - comp.graphics.api.opengl ...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 b... alpha blending of lines on polygons with depth - comp.graphics.api ...I am a bit confused about how to get anti-aliased lines on filled polygons with depth test enabled. Without depth testing, everything works and the... Rendering Shafts of Light - comp.graphics.api.openglStep through your two sorted lists in increasing order: If a fragmet comes next, render it into the framebuffer with OpenGL-blending so that it attenuates the colors ... what is depth buffer? - comp.graphics.api.openglAlpha blending with depth buffer - comp.graphics.api.opengl ... Well, the OpenGL RedBook chapter 6 "Blending, Antialiasing, Fog, and Polygon Offset" says in sub-chapter ... True type anti aliased fonts in OpenGL - comp.graphics.api.opengl ...Blending in OpenGL - comp.graphics.api.opengl True type anti aliased fonts in OpenGL - comp.graphics.api.opengl ... Blending in OpenGL - comp.graphics.api.opengl Blending ... Enable/Disable Material - comp.graphics.api.openglBlending in OpenGL - comp.graphics.api.opengl Enable/Disable Material - comp.graphics.api.opengl Blending in OpenGL - comp.graphics.api.opengl Enable/Disable Material ... TGA /BMP to RGBA - Newbie - comp.graphics.api.openglAlpha blending with depth buffer - comp.graphics.api.opengl ... TGA /BMP to RGBA - Newbie - comp.graphics.api.opengl What you probably want, is enable alpha blending with ... OpenGL FAQ / 15 Transparency, Translucency, and Using BlendingOpenGL Developer Web Site. NOTE: This page contains old archived material and may not be relevant anymore. Blending - OpenGL.orgBlending is the stage of OpenGL rendering pipeline that takes the fragment color outputs and combines them with the destination colors that they are adding to. 7/26/2012 3:56:44 PM
|