Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

eowdaoc

Member Since 24 May 2011
Offline Last Active Dec 19 2012 11:10 PM
-----

Topics I've Started

Pulling my hair out over D3D11 lighting bug

08 December 2012 - 08:03 PM

I have a simple model loaded and a point light source that I am trying to get to work correctly, but it's just not happening. I have probably spent around 12 hours over the past two days trying to figure out what is wrong, and I'm starting to get really frustrated, so I thought I would ask here as a last resort.

I have checked and double checked my lighting equation many times with at least 3 different sources. At the moment I am using constant lighting coefficient so that I know for a fact my object will be lit no matter what the distance. My light is EXPLICITLY fixed at the -30.0 position on the Z-axis, yet my object appears as if it is lit from a different direction. My camera is located at ( 0.0, 0.0, -20.0 ) so that I can verify the model is not swallowing the light, and it is not. The light is plenty far away from my model. I've changed the light/camera distance many times and have gotten the same result.

The entire front side of my object should be lit, instead it is lit from a different angle.

I use the DirectXMath library and I was VERY, VERY careful to store the const buffer matrices correctly and with the right alignment. I also made sure my HLSL byte alignment was correct by using float4. I use the row-major compiler options for HLSL so I do not need to use matrix transpose, and my code reflects that.

Please give my code a look and tell me where I've gone wrong, because I obviously cannot figure it out! Posted Image

Bugged lighting:
Attached File  bug.jpg   20.02K   29 downloads

Matrix update function:
Attached File  update.jpg   483.45K   32 downloads

Vertex shader:
Attached File  vs.jpg   131.51K   36 downloads

Pixel shader:
Attached File  ps.jpg   292.01K   26 downloads

Parentheses in HLSL cause light attenuation function to not work correctly?

13 September 2012 - 03:10 PM

I have a diffuse+specular equation in my pixel shader, and it works pretty well except for this one issue:

When I change this:
float attenuation = 1.0f / d*d;
To this:
float attenuation = 1.0f / ( d*d );

My model is no longer lit, and is instead the color of my ambient intensity. I find this extremely strange. The reason I want parentheses is so I can use a different attenuation function such as ( 1 + 0.045*d + 0.0075*d*d ).

It also messes up if I try this:
float denominator = d*d;
float attenuation = 1.0f / denominator;


Here is my entire pixel shader. The reason for all of the "tmp_stuff" variables is to make everything "politically correct" since I have no choice but to input them as float4 due to constant buffer alignment properties. I would rather convert everything to temporary float3 rather than risk the chance of something not working right because of float4. Anyways, it shouldn't have anything to do with my current problem. I've doubled, tripled, and quad-checked my lighting equations with four different books, and I'm just baffled with this problem.

void ps( in v2p input, out float4 final_color : SV_TARGET )
{
	float3 ambient_intensity = float3( 0.1f, 0.1f, 0.1f );
	float3 diffuse_color = float3( 0.8f, 0.8f, 0.8f);
	float3 specular_color = float3( 1.0f, 1.0f , 1.0f );

	float3 tmp_light;
	tmp_light.x = light_vector.x;
	tmp_light.y = light_vector.y;
	tmp_light.z = light_vector.z;

	float3 norm_light = normalize( tmp_light );

	float3 tmp_pos;
	tmp_pos.x = input.pos.x;
	tmp_pos.y =  input.pos.y;
	tmp_pos.z = input.pos.z;

	float3 tmp_norm;
	tmp_norm.x = input.norm.x;
	tmp_norm.y = input.norm.y;
	tmp_norm.z = input.norm.z;

	float3 tmp_cam = float3( 0.0f, 0.0f, -20.0f ); // fixed view camera position

	// light intensity
	float d = distance( tmp_pos, tmp_light );
	float attenuation = 1.0f/d*d; // HERE IS THE PROBLEM AREA

	float3 pointlight = attenuation*float3( light_color.x, light_color.y, light_color.z );

	// diffuse lighting
	float diffuse = max( dot( tmp_norm, norm_light) , 0.0f );
	float3 diffuse_final = diffuse_color*ambient_intensity + diffuse_color*pointlight*diffuse;

	// specular lighting
	float3 reflect_vect = 2*dot( tmp_norm, norm_light )*tmp_norm - norm_light;
	float ref_max = max( dot( reflect_vect, normalize(tmp_cam) ), 0.0f );
	float spec_exponent = pow ( ref_max, 50.0f );
	float3 spec_final;

	if( dot( tmp_norm, norm_light ) <= 0 )
	{
	  spec_final = float3( 0.0f, 0.0f, 0.0f );
	}
	if( dot( tmp_norm, norm_light ) > 0 )
	{
	  spec_final = specular_color*pointlight*spec_exponent;
	}
	final_color = float4(  diffuse_final + spec_final, 1.0f );
}

Without parentheses:
Posted Image

With parentheses:
Posted Image

PARTNERS