full transparent object in openGL

475 Views Asked by At

I need to create a completely transparent surface passing through the origin of the axes and always parallel to the screen. I'm trying to use this code (in c++) but the result is something like 50% blend (not completely):

glPushMatrix();     

    glLoadIdentity();                       

    glBlendFunc(1, 1);

    glBegin(GL_QUADS);
    glVertex3f(-100, -100, -0.003814);
    glVertex3f(100, -100, -0.003814);
    glVertex3f(100, 100, -0.003814);
    glVertex3f(-100, 100, -0.003814);
    glEnd();

glPopMatrix();

Additional informations: I need this transparent surface to get a point on it with the function gluUnProject(winX, winY, winZ, model, proj, view, &ox, &oy, &oz);

3

There are 3 best solutions below

0
On

The blend function you are using is known as additive blending:

   Final Color = (SourceColor * 1.0) + (DestinationColor * 1.0).

This is anything but fully transparent, unless the framebuffer is already fully white at the location you are blending (DestinationColor == (1.0, 1.0, 1.0)). And even then this behavior only works if you are using a fixed-point render target, because values are clamped to [0.0,1.0] after blending by default.


Instead, consider glBlendFunc (GL_ZERO, GL_ONE):

   Final Color = (SourceColor * 0.0) + (DestinationColor * 1.0).

         [...]

   Final Color = Original Color


That said, you will probably get better performance if you simply use a color mask to disable color writes as genpfault suggested. Using a blending function to discard the color of your surface is needlessly complicated.

3
On

Additional informations: I need this transparent surface to get a point on it with the function gluUnProject(winX, winY, winZ, model, proj, view, &ox, &oy, &oz);

No you don't. OpenGL is not a scene graph and there's no obligation to use values with gluUnProject obtained from OpenGL. You can simply pass in whatever you want for winZ. Use 0 if you're interested in the near clipping plane and 1 for the far clipping plane. Also you can perfectly fine just calculate the on-screen position for every point. OpenGL is not magic, the transformations it does are well documented and easy to perform yourself.

1
On

If you just want to fill the depth buffer you can disable color writes via glColorMask():

glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
drawQuad();
glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
doUnproject();