Transparency Issue

Started by
5 comments, last by kauna 9 years, 9 months ago

Right so i have a transparency issue that i hope someone could explain? i should see the texture through the red square.

transparencyissue_zps5f58ccb5.png?t=1404

I created a bounding box around my obj (yes that textured cube is an obj tongue.png) however i noticed a transparency issue, so i removed all sides of the box bar the top. the top is a solid red with an alpha of 0.2.

originally i rendered another transparent box behind the red one and i could see that fine, just not the textured obj, which leads me to believe it is a conbination of my shader and glblend thats the issue.

so here is my code for my frag shader, if it doesnt have a texture (coord -98) then just do colour, i dont currently have a world light set so it never goes into the first part for textures


if (pass_Texture[0] <= -98.0 && pass_Texture[1] <= -98.0)
    {
    	out_Color = pass_Color;

    } else {

    	if(WorldLight.isWorldSource)
    	{
    		out_Color = texture(myTexture, pass_Texture) * pass_Color * (AmbientColor + DiffuseColor);
    	} else {
   			out_Color = texture(myTexture, pass_Texture) * pass_Color;	
   		}
    }

my bounding box (in this case just the red square) as you can see ive tried a variety of options


//glDisable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glUniformMatrix4fv(shader->modelMatrixLocation, 1, GL_FALSE, &transform[0][0]);
glBindVertexArray(Cube->VAOID); // Bind our Vertex Array Object

glDrawArrays(GL_TRIANGLES, 0, Cube->points.size()); // Draw our square


glBindVertexArray(0); // Unbind our Vertex Array Object
glDisable(GL_BLEND);
//glDisable(GL_CULL_FACE);
//glEnable(GL_DEPTH_TEST);

and finally my obj which again ive tried a variety of blend options and depthtest options, most of it is my own code but didnt want to cut incase it was something else entirely that i missed


glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE);
//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glUniformMatrix4fv(shader->modelMatrixLocation, 1, GL_FALSE, &ModelMatrix[0][0]);

glBindVertexArray(VAIO); // Bind our Vertex Array Object	

std::map<std::string, Group>::const_iterator itr;

for (itr = Groups.begin(); itr != Groups.end(); itr++)
{
    Group gp = itr->second;
    cMtl* mtl = &Materials[gp.Mtl];

    if (mtl->MainTexture.loaded && !mtl->MainTexture.GeneratedBuffer)
    {
        if (mtl->MainTexture.TextType == SARAGAN_PNG)
	{
	    mtl->MainTexture.GenerateAlphaBuffer();
	} else {
	    mtl->MainTexture.GenerateBuffer();
	}
    }

    if (mtl->MainTexture.loaded && mtl->MainTexture.GeneratedBuffer)
    {
	if (CurrentTextureID != mtl->MainTexture.TextureID)
	{
	    CurrentTextureID = mtl->MainTexture.TextureID;
	}
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, mtl->MainTexture.TextureID); 

	glDrawArrays(GL_TRIANGLES, gp.index, gp.Endindex); // Draw our square
    }
}
glBindVertexArray(0); // Unbind our Vertex Array Object
glDisable(GL_BLEND);

for the love of me i cant seem to get the right combination, which to me suggests i dont really understand whats really going on here

if anyone could shed some light onto the situation then i would be very greatful

Kind Regards
Rob

Advertisement

You probably have depth writing enabled (glDepthMask), so when you draw the transparent quad first, it will write the depth for those transparent pixels. And now when you try to draw the other object behind it, it will skip the drawing the pixels because of depth testing (something is in front of the pixel). Simplest way to solve this is to render opaque objects first, then transparent objects from back to front. It still won't work for all kinds of shapes though.

Try changing the order you render, first the textured box and then the transparent thing.

Derp

Thanks for the reply I changed the render order and indeed that did seem to fix the issue which is great.

You said it still wont work for all kinds of shapes though? how do you mean?

The problem is basically that the triangles are not still sorted. For example, if you want one object which has two transparent boxes in it, how do you know which box of those will be drawn first? It will look weird depending on the viewing direction.

Derp

Right ok, thanks for your input, as im doing basics i think ill just try and avoid that situation for now until i have a firmer grasp

out of interest would culling the faces be a solution to that? not sure how that would work with transparency

Culling would not help much, it just hides some of the faces. I haven't done much this kind of stuff, but "order independent transparency" is something you could google for more information. Youtube also has some nice videos about how much better it looks like.

Derp

Culling won't change the matter that with certain kind of a scene from certain point of view the transparency will fail.

Generally, transparent surfaces should be drawn from back to front. Or more precisely, transparent pixels should be drawn from back to front. Now, for a typical rendering setup, it is already very difficult (at least in performance wise) to guarantee that the surfaces are in correct order ... and it is impossible to guarantee that the pixels are in correct order (without some scene preprocessing such as BSP ... which you don't want to implement these days) in the general triangle soup case.

Consider that transparent triangles intersecting other transparent triangles already pose a problem since you can't draw them in a correct order, and then, if different objects intersect, you'll have the same problem.

There is a technique called OIT (order independent transparency) and there is a lot of research done in that field. The general solution is quite somewhat simple, but maybe not very efficient. In the simplest version, you'll gather all the transparent pixels in a per-pixel linked list, then perform sorting based on the distance. Other technique is called stochastic transparency which works differently than the linked list version, but it produces noisy results at low qualities.

Cheers!

This topic is closed to new replies.

Advertisement