Blending Options

Hey. I’m trying to do some lighting effects and basically I was wondering if there is a way where you can use blending and it will show up blended if the quad is within a certain distance of the destination, but if the source is too far from the dest then it won’t show up at all. I know that sounds kind of vague so I’ll try to explain it better. Like if you have a wall and you draw a quad .1 infront of the wall, with the same normal as the wall, it will blend the source quad and show up, but if you then drew that quad 1.0 away from the wall it would just be completely transparent because it’s too far from the dest. Is that at all possible? If not what’s an alternative to that? Thanks.

That works for most cases, yeah. But the real problem is if half of the source quad is infront of the wall and the other half is in front of a wall further away. In that case I would only want the half in front of the close wall to show up, that’s pretty much what I’m faced with. But thanks for the input there, that would work based on what I said before, I just have this problem to work with as well.

as the object is farther away it is more opaque? Well. Have the translate distance as a variable. depending on where you translate from changes how you handle this. If you are going farther from the wall in your translation, then as the bigger the variable(say 1.0) then make that variable the alpha value

/*draw wall*/
glTranslatef(dist,0.0,0.0);
glEnable(GL_BLEND_FUNC);
glColor4f(0.0,0.0,1.0,dist); /*blue*/
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glRectf(/*points x1,y1,x2,y2*/);

this will work until one distance away, after that the alpha value is clamped to 1.0, no biggie still works the way you want(fully opaque). and the opposite is true. If it is negative it will be clamped to 0. Does this help you?

I just read what you wrote above my code I just wrote. So the wall and the quad are not parralell? If so, is the wall translucent at all? If it is, then I would suggest a clip plane. I don’t know how to make certain areas of primitives certain opacities, only a whole one.

Here’s a picture of what I’m trying to do.

http://filebox.vt.edu/users/rengle/stuff/oglthing.jpg

The wall with the red dot if the front one and see how the red dot cuts off as soon as it’s not toucing the wall anymore, yet there’s still another wall behind it. With your method, unless I’m mistaken, there would still be a somewhat transparent part showing up on the other wall because it extends past the first quad’s edges and turning the quad transparent wouldn’t allow it to show up on the first wall. I hope that helps clearify what I’m trying to do. Thanks.

I am not familiar with fragment programs or shading language stuff, but it sounds like something that could handle this.
basically you would need the “difference” depth value ie depth buffer value of pixel before you draw it and afterwards.

if you use a single quad you would always have the problem of not being able to set the alpha properly or inaccurately, you could tessalate it but not really a pixel accurate solution

another hack would be changing order of how things are rendered and not allow the blended thing write into depth buffer, but that would basically be an “on/off” effect.

hopefully some shading guru will find his way in here

This is indeed relatively easy to implement using a fragment program.
First make sure your graphics card supports the GL_ARB_fragment_program extension.

The fragment program itself would be something like this:

First copy the color and depth buffer to textures 0 and 1 respectively using coptexsubimage.

const char fp[] = 
"!!ARBfp1.0
"
"TEMP	R0,R1;
"
//read previous color
"TEX R0, fragment.texcoord[0], texture[0], 2D;
"
//read previous depth
"TEX R1, fragment.texcoord[0], texture[1], 2D;
"
//subtract depth of the incoming fragment from the previous depth
"SUB R1,R1,fragment.position.z;
"
//multiply by a user specified factor and saturate to the range [0,1]
"MUL_SAT R1,R1,program.local[0];
"
//perform a linear interpolation of the previous color with the color of the incoming fragment based on the valu stored in R1
"LRP result.color,R1,R0,fragment.color;
"
"END";

You can set the user specified value for the fragment program width glLocalParameter4d(…)

You will also have to set the texgen so that the correct pixels are adressed. It’s easiest to use a texture_rectangle, then you can skip the texgen and replace the first 2 lines of the fragment program to

"TEX R0, fragment.position, texture[0], RECT;
"
"TEX R1, fragment.position, texture[1], RECT;
"

The specification of this extension can be found at http://oss.sgi.com/projects/ogl-sample/registry/ARB/fragment_program.txt

It’s possible that I made some mistakes in the code above, but I’m rather tired at the moment :slight_smile:
Just thought I might put you on the right track with this.

Nico