Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Specular Highlight - power does not affect size?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 haansn08   Members   -  Reputation: 172

Like
0Likes
Like

Posted 15 August 2013 - 05:59 AM

Hello there,

I'm currently trying to implement a simple light shader, but I have a couple of problems with it.

The diffuse lightning works fine, but the specular highlight seems not very correct.

This is my pixel shader:

struct VertexOut
{
	float4 posH : SV_POSITION;
	float3 posW : POSITION;
	float3 normal : NORMAL;
};
cbuffer cbPerObject : register (b0)
{
	float3 eyePos;

	float3 lightDirection;
	float4 lightColor;
	float  lightStrength;
	
	float4 ambient;
	float4 diffuse;
	float4 specular;
	float  specular_power;
};
float4 main (VertexOut input) : SV_TARGET
{
	//Calculate diffuse light
	float3 light0 = normalize(lightDirection);
	float _diff_fac = max(mul(light0, input.normal), 0);
	float4 _diff = _diff_fac * (diffuse * lightColor) * lightStrength;
	//Ambient light
	float4 _ambient = ambient;
	//Specular light
	//Calculate the angle
	float4 _spec = float4(0.0f,0.0f,0.0f,0.0f);
	[flatten]
	if (_diff_fac > 0.0f)
	{
		float3 toEye = normalize(input.posW - eyePos);
		float3 reflection = normalize(reflect(-light0, input.normal));
		float _spec_fac = pow(max(dot(reflection, toEye), 0), specular_power);
		_spec = _spec_fac * specular;
	}
			
	return _diff + _ambient + _spec;
}

And my vertex shader:

cbuffer cbPerObject : register (b0)
{
	float4x4 world;
	float4x4 worldViewProj;
	float4x4 worldInvTrans;
};
struct VertexIn
{
	float3 pos : POSITION;
	float3 normal : NORMAL;
};
struct VertexOut
{
	float4 posH : SV_POSITION;
	float3 posW : POSITION;
	float3 normal : NORMAL;
};
VertexOut main (VertexIn input)
{
	VertexOut output;
	//Transform vertex
	output.posH = mul(float4(input.pos,1.0f), worldViewProj);
	output.posW = mul(input.pos, world);
	//Transform normal
	output.normal = mul (input.normal, (float3x3)worldInvTrans);

	return output;
}

I checked the constant buffer of the pixel shader and everything seems to be right...

Here are some screenshots (I increase the specular_power variable linear with the time)

specular01.pngspecular02.pngspecular03.png

 

As you can see, the specular highlight dismishes over time and is replaced by the diffuse lightning - but actually it should shrink to a point.

 

Please tell me if you need more source code.



Sponsor:

#2 Styves   Members   -  Reputation: 1102

Like
0Likes
Like

Posted 15 August 2013 - 11:23 AM

I can't say without testing but isn't your reflection code backwards?

 

Should be like this (at least, this is how I've always done it):

float3 toEye = normalize(input.posW - eyePos);
float3 reflection = normalize(reflect(-toEye, input.normal));
float _spec_fac = pow(max(dot(reflection, light0), 0), specular_power);
_spec = _spec_fac * specular;

-toEye might be backwards, so if it doesn't work this way try removing the negative.



#3 DwarvesH   Members   -  Reputation: 482

Like
0Likes
Like

Posted 17 August 2013 - 11:33 AM

I can't say exactly what is going on, but I can easily reproduce that visual effect by trying to do normal mapping with specular highlights, but having my tangents be zero.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS