PDA

View Full Version : transparant



11-24-2005, 05:47 PM
Hello, can someone tell me how to make everything transparant.
I made a cube and i added this line in the code
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
Wich displays the wireframe of the cube. But now i want to make it slightly transparant. How can i do this? What function do i need for this?

11-24-2005, 06:42 PM
Forgot to say that i dont want it fully transparant, but just 50% transparant.

memfr0b
11-25-2005, 01:01 AM
The magic lines are


glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);Combined with a color alpha value of 0.5. The result is draw-order dependent though. You have to disable depth writing (glDepthMask(GL_FALSE)) and order your objects/primitives back-to-front for correct results.

11-25-2005, 06:15 AM
Hey, i tried that but it left pieces out of the objects, like i could see where to floor is through the wall and that piece of the wall was gone. I also placed crates on the field and those where completely invisible exept for the shadow on the ground...
I placed them in this function.

void APIENTRY tempglTexCoordPointer (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer)
{
if (transparant)
{
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
}

Any idea what the problem could be?

memfr0b
11-25-2005, 07:45 AM
As I said, the result is order dependent. If you don't disable depth write and the nearer object is drawn first, it will fill the depth buffer and the depth test will prevent the farther object from being drawn.

You should at least disable depth writes for transparent objects. That way transparent objects don't occlude other transparent objects, but the resulting color will still be off if the objects are drawn in the wrong (front-to-back) order.

11-25-2005, 09:16 AM
arrgg, im doing it all wrong! Its getting worse now!
Can you pls pls pls show me how this is done, im really screwing it up.

memfr0b
11-25-2005, 10:06 AM
You have to draw your whole scene in two passes. First the opaque objects are drawn the usual way (no blending, depth write and depth test enabled). After that, in a second pass, the translucent objects are added to the scene, this time with disabled depth writes. This means that opaque objects will still occlude translucent objects (because depth testing is still enabled) but translucent objects don't occlude other translucent objects (because their depth values aren't written to the depth buffer).

In pseudo code it could look like this (assuming a glut-style display callback):


void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
draw_opaque_geometry();

glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_translucent_geometry();

glutSwapBuffers();
}For optimal results, make sure draw_translucent_geometry draws the objects in back-to-front order (farthest objects first, nearest last).

11-25-2005, 12:08 PM
Thanks its working now :)