virabhadra's picture

Transparency problem

Hallo together!

I expect the problem is known and solution is known too.
May be someone can give me a link or quick solution of the problem.

I'm coding in the enveronment using OpenTK.Graphics.OpenGL4; just to not use deprecated functions.

My OpenGL initialization looks standard:

GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

Rendering with GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

Fragment shader (diffuse/specular lighting + transparency for some parts) looks this way:

#version 400
	vec3 N, L, V;
	vec2 tex;
} fs_in;
// Material properties
uniform vec4 Ka, Kd, Ks;
uniform float Ns;
// Lighting properties
uniform vec4 La, Ld, Ls;
uniform vec3 Lpos;
uniform sampler2D texture1;
uniform int texture_id;          // if texture_id is -1, object is not textured
out vec4 frag_color;
void main()
	vec3 N = normalize(fs_in.N);
	vec3 L = normalize(fs_in.L);
	vec3 V = normalize(fs_in.V);
	vec4 Ia = La * Ka;
	vec4 Id = Ld * Kd * max(dot(N, L), 0);
	vec4 Is = Ls * Ks * pow(max(dot(reflect(-L, N), V), 0.0), Ns);
	vec4 texcolor = texture2D(texture1, fs_in.tex);
	if (texture_id == -1)
		frag_color = vec4((Ia + Id + Is).xyz, Kd.a);
		frag_color = Ia + Id * vec4(texcolor.rgb, 0) + Is * texcolor.a;

The result on a test model looks terrible (see a sample image in the attachment).
I have investigated only that transparency is depended on the order rendering.
A sphere and a cube on the picture are semi transparent, but another sphere inside is absolutely invisible and rendering of visible objects is not correct.

I tried to apply to apply some solutions from the Internet, but they always play with BlendFunc and without Depth Test.
I want to keep transparency and depth at the same time. I saw it work in software like "Deep Exploration".
May be I haven't found anything useful.

Thank you!

Inline Images


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

As you found out, transparency depends on the order of rendering. See here for some suggestions: