transparencia principiante / opaco en openGL

I am going through the NeHe tutorials for OpenGL... I am at lesson 8 (drawing a cube with blending).

I wanted to experiment and change half of the faces to be opaque so that there is always a semi-transparent face opposite to an opaque one and be able to rotate the cube...

I changed the code a little bit, the entire source is there : I changed a few things :

  • enable blending by default and set the blending function to glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  • changed the order of drawing the faces and colors for each face.
    • I set the depth testing
    • I draw all the opaque faces
    • I disable depth test
    • I draw all the transparent faces

Now, it's hard to tell exactly what is going wrong, but it definitely does not look right, I cannot recognize what face is opaque compared to the transparent ones, sometimes some faces do not seem to get drawn when they should...etc...

Seems like calculating what face is in front compared to back would not be trivial (although I am sure possible), I hope there is a way of doing that would not need to do that.

Looking either for what is wrong in my code or whether this is not the right way of doing it in the first place.

preguntado el 09 de enero de 11 a las 05:01

please show a picture. Also, describe what you find looking wrong. -

Just by the way - you're learning a very outdated and ineffective version of OpenGL right now. Browse SO for some resources for newer OpenGL versions when you feel like it. Newer versions are more convenient too! -

1 Respuestas

If you disable depth testing before drawing the transparent faces, then they will be drawn with no regard for their correct z-ordering. It probably looks like the transparent faces are being drawn atop all the other faces. Leave depth testing on.

Respondido el 09 de enero de 11 a las 09:01

You should indeed leave depth test on, to allow opaque geometry to occlude transparent geometry. What is commonly done is to turn off ''depth write'' using glDepthMask(GL_FALSE), so that transparent geometry does not occlude itself. In you case, as a cube is convex, it will not change anything though. - rotoglup

Also you need to sort transparent faces back to front, since with blending enabled you inevitably need to implment a Painters Algorithm's_algorithm in case of convex shapes, like a cube it's easy: glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); draw_cube_transparent_faces(); glCullFace(GL_BACK); draw_cube_transparent_faces(); – in case you implement proper depth sorting, depth writes won't even hurt you, too. - datenwolf

Thanks ! I had to add what rotoglup added to be able to see the "stacked" transparent faces and a couple of more things I was doing wrong and it now works fine... - Mateo

Otro enlace: transparency sorting, and not exactly beginner: order independent transparency - jozxyqk

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.