开发者

OpenGL ES 2.0. GLSL - cannot use two sampled colors at the same time in fragment shader

开发者 https://www.devze.com 2023-04-01 07:37 出处:网络
I have a fragment shader that does parallax mapping and lighting computations. I\'ve managed to trace the problem to the sampling of textures and/or lighting computation so I\'ll only post code for th

I have a fragment shader that does parallax mapping and lighting computations. I've managed to trace the problem to the sampling of textures and/or lighting computation so I'll only post code for that to keep it short:

// sample maps
vec3 albedo = texture2D(u_albedo_texture_1, p_texc).rgb;
vec3 ao = texture2D(u_ambientocclusion_texture, v_texcoord).rgb - 0.5; // ambient occlusion
vec3 normalT = texture2D(u_normalmap_texture, p_texc).rgb * 2.0 - 1.0; // tangent space normal

vec4 normalW = vec4(normalize(TBN * normalT).xyz, 1.0); // world space normal

vec3 color = vec3(0.0);
vec3 ambient = textureCube(u_cubemap_texture, -normalW.xyz).rgb; // get ambient lighting from cubemap
c开发者_如何学Color += ambient * ao;

float d = length(light_vector);
float atten = 1.0 / dot(light_attenuation, vec3(1.0, d, d * d)); // compute attenuation of point light

// diffuse term
float ndotl = max(dot(normalW.xyz, light_vector), 0.0);
color += atten * ndotl * light_diffuse.rgb * albedo;

// specular term
float hdotn = max(0.0, dot(half_vector, normalW.xyz));
color += atten * ((shininess + 8.0) / 8.0 * 3.1416) * light_specular.rgb * p_ks * pow(hdotn, shininess);

gl_FragColor = vec4(color.rgb, 1.0);

I have this code working in other shaders, but here in the parallax mapping shader it just discards the pixels and I can't understand why. So instead of showing the object it simply discards the pixels and the object doesn't show up at all. No wrong or messy colors, nothing.

I managed to trace the problem to the albedo and ambient sampled colors. They're both sampled correctly. If I print out

gl_FragColor = vec4(ambient.rgb, 1.0);

or

gl_FragColor = vec4(albedo.rgb, 1.0);

they both get displayed correctly but if I try to do something with both colors, the pixels get discarded. None of the following work:

gl_FragColor = vec4(ambient + albedo, 1.0);
gl_FragColor = vec4(ambient * albedo, 1.0);
gl_FragColor = vec4(ambient.r, albedo.g, ambient.b, 1.0);

ambient is sampled from a cube map and albedo from a 2D texture. I have other samplers for ambient occlusion and normal mapping and those work and they also work in conjunction with ambient OR albedo. So ambient + ao works and albedo + ao works.

So, this is my problem and I can't figure out what causes it. Like I said, I have the same code in another shader and that one works.

Worth mentioning, this runs on a PowerVR emulator for OpenGL ES 2.0. and the GPU is an nVidia GT220. Also feel free to point out other mistakes in my code like inefficient stuff. And if necessary, I'll post the full fragment shader code. Sorry for the long post :P

SOLVED

Wow, can't believe the cause was so stupid, but I completely missed it. Basically, I forgot to load the cubemap for that particular model and it wasn't sent to the shader. Still, how the hell did it sample something in the first place? I mean, it was working separately, so what did the texture unit sample? A cubemap left in the cache from previous processing?


Still, how the hell did it sample something in the first place?

Usually when my shaders doesn't produce the desired result but are looking annoyingly 'normal' it is because the shader compilation discarded it (I made an error somewhere) and decided to use the 'fixed pipeline' shader instead...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号