Archived

This topic is now archived and is closed to further replies.

Keermalec

Transparency Troubles

Recommended Posts

I thought I had smoothly working transparency, until I noticed some objects were being drawn on top of transparent objects which they should have been behind of. When I started playing with glDepthMask I was unable to find a simple solution. Here is an illustration: With glDepthMask being disabled for drawing the transparent objects, then enabled for the opaque. Btw, that's a window behind a transparent shower cabinet - see how the window frame, which is a separate object, disappears completely behind the shower glass. With glDepthMask not being called at all when drawing transparent object (it is on by default). The window frame is now drawn above the shower glass (?) And here is the rendering code:
void RenderScene ( Mscene *s )
{
	unsigned int a, b, c;
	for ( a = 0; a < s->numObj; a++ )
	{
		for ( b = 0; b < s->obj[a].numMesh; b++ )
		{
			glColor4ub( s->obj[a].mesh[b].col[0], s->obj[a].mesh[b].col[1], s->obj[a].mesh[b].col[2], s->obj[a].mesh[b].opac );
			// If mesh is transparent, enable transparency

			if ( s->obj[a].mesh[b].opac < 255 )	
			{
				glEnable(GL_BLEND);
				glDepthMask(GL_FALSE);
				glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
			}	
			if ( s->obj[a].mesh[b].textured )
				glBindTexture ( GL_TEXTURE_2D, s->obj[a].mesh[b].texID );
			glBegin(GL_TRIANGLES);
				for ( MVEuint16 c = 0; c < s->obj[a].mesh[b].numTri; c++ )
				{
					glTexCoord2fv( s->obj[a].tc[s->obj[a].mesh[b].tri[c].ti[0]] );
					glVertex3fv( s->obj[a].vert[s->obj[a].mesh[b].tri[c].vi[0]] );
					glTexCoord2fv( s->obj[a].tc[s->obj[a].mesh[b].tri[c].ti[1]] );
					glVertex3fv( s->obj[a].vert[s->obj[a].mesh[b].tri[c].vi[1]] );
					glTexCoord2fv( s->obj[a].tc[s->obj[a].mesh[b].tri[c].ti[2]] );
					glVertex3fv( s->obj[a].vert[s->obj[a].mesh[b].tri[c].vi[2]] );
				}
			glEnd();
			// If mesh is transparent, disable transparency

			if ( s->obj[a].mesh[b].opac < 255 )
			{
				glDepthMask(GL_TRUE);
				glDisable(GL_BLEND);
			}	
		}
	}
}
Can anyone explain the logic behind drawing multiple transparent and opaque objects, I find the OpenGL doc is a bit mystical on these issues. [edited by - Keermalec on October 8, 2003 5:21:15 PM]

Share this post


Link to post
Share on other sites
The procedure is something like this:
-first draw all opaque objects (you might sort them front to back to get benifit of early z-out)
-then draw all your transparent faces sorted back to front. (there are a few cases where you don''t have to sort but that''s not a big overhead so just do it) There is no need to disable glDepthMask.

You should never let your fears become the boundaries of your dreams.

Share this post


Link to post
Share on other sites
Don't disable your depth buffer.

Just do a glDepthMask(GL_FALSE) before drawing a blended polygon.

Remember to set it back to true after drawing it.

That way you don't have to worry about draw order. Just draw transparent after opaque.

EDIT: I don't thing you have to worry about the drawing order of teh transparents with depthmask false, but darkwings post makes me think i may be worng.

[edited by - skow on October 8, 2003 6:55:31 PM]

Share this post


Link to post
Share on other sites
In my FX-Engine, I render the particles as this :


// Blending Params

glBlendFunc(GL_SRC_ALPHA, GL_ONE);

// We enable the blending with the new params

glEnable(GL_BLEND);
// We disable the writting in the Z-Buffer

glDepthMask(0);

// I draw my particle here



// We enable the writting in the Z-Buffer

glDepthMask(1);

// We disable the blending

glDisable(GL_BLEND);


And with that I've never had some problems.

========================
Leyder Dylan (dylan.leyder@slug-production.be.tf
http://www.slug-production.be.tf/

[edited by - Leyder Dylan on October 8, 2003 6:58:58 PM]

Share this post


Link to post
Share on other sites
Thanks darkwing et al. I simply rendered all transparent objects after all opaque objects and that did the trick.



Strangely enough, whether I put glDepthMask or not does not change anything... Oh well, at least I got it working :-)

//draw opaque objects 

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//draw transparent things

glDisable(GL_BLEND);



[edited by - Keermalec on October 9, 2003 6:35:09 PM]

Share this post


Link to post
Share on other sites