PDA

View Full Version : alpha blending



nkt
05-10-2011, 01:26 PM
I want to draw a box with GL_LINES, connect 4 lines to form a box

Then draw a line that passes through the box created above.

When the line is around the area of the box, line should not be visible for the area of box

How can I achieve this, I am not understanding alpha blending correctly.

The code is below


//I can draw a black occlusion zone around the lined box, but it is not preferred so commented here
/*
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);

glColor3f(0.20, 0.20, 0.60);

glBegin(GL_POLYGON);
glVertex2f(-62, 62);
glVertex2f( 62, 62);
glVertex2f( 62, -2);
glVertex2f(-62, -2);
glEnd();

glBlendFunc(GL_ONE, GL_ZERO);
glDisable(GL_BLEND);

glPopMatrix();
*/


// Here I am drawing the lined Box before drawing the lines that passes through it
glPushMatrix();
glLineWidth(2);
glColor3f(1.0, 1.0, 0.95);
glBegin(GL_LINES);
glVertex2f(-60, 0);
glVertex2f(60, 0);
glVertex2f(-60, 60);
glVertex2f(60, 60);
glVertex2f(60, 60);
glVertex2f(60, 0);
glVertex2f(-60, 0);
glVertex2f(-60, 60);
glEnd();
glPopMatrix();




// Here I am drawing a white line with width 2, this should not be visible when it goes over or under the
// lined box area. I can draw the occlusion zone and change the order to do it. But I would like to know
// how we can change this using alpha blending. The order has to be same, the background color I did is orange.

glColor3f(1.0, 1.0, 1.0);

glPushMatrix();


glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);

glLineWidth(2);
glBegin(GL_LINES);
glVertex2f(-120, 30);
glVertex2f(120.0, 30.0);
glEnd();


glDisable(GL_BLEND);

glPopMatrix();


How can I do this using blend function. Lines should be visible all other area except the lined box area. If I draw
the box occlusion zone, this will not match with the background image (which is a video, color is not the same always).
Please help me.
ntpkd

BionicBytes
05-10-2011, 01:58 PM
Perhaps you could use scissor test or stencil to mask off the box area

przemo_li
05-10-2011, 10:58 PM
There can be many ways to achieve this effect.

You can draw your line first, then turn off depth test and then draw your box. (So box pixels will override any line pixels even though line pixels are closer ones).

Alpha blending is usually used for transparency. Like glass windows, fog or other sfx (when implemented as textures), etc. Not to hide parts of your object (unless you want to hide them permamently, but then why to bother gpu with sending this data to it?).


Oh and you can always draw your box and line with the same color, and you will get your effect without any additional fuss. ;P