Skip to main content
gamedev.net
🔒 Locked

HLSL BumpMap Problem...Please Help

Started by SgtArchAngel Jan 11, 2007 at 8:33 PM 6 replies 1k views
Original Post
SgtArchAngel
SgtArchAngel
Can someone please help me. I cant seem to get my specular reflections on the bumpmap to come out correctly they keep moving whenever i move my view around( I would liek them to only move when i move the camera)

void NormalMap_Diffuse_VS2(float4 Pos : POSITION,float3 Normal : NORMAL,float2 Tex : TEXCOORD0,float3 Tan : TANGENT,
				 out float4 oPos : POSITION,
				 out float2 oTex : TEXCOORD0,
				 out float3 oLight1 : TEXCOORD1,
				 out float3 oView : TEXCOORD2,
				 out float4 ovPos : TEXCOORD3)
{
	oPos   = mul(Pos, matWorldViewProj);
	ovPos = mul(Pos, matWorld);
	float3x3 TanMatrix;
	TanMatrix[0] = mul(Tan,matWorldView);
	TanMatrix[1] = mul(cross(Normal,Tan),matWorldView);
	TanMatrix[2] = mul(Normal,matWorldView);

	float3 LightDir = mul(matWorldViewInv,Light1Pos-Pos);
	oLight1 = mul(TanMatrix,LightDir);

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
	// I THINK THIS IS WHAT IS CAUSEING IT TO MOVE WHEN I DONT WANT IT TOO
	// BUT IM NOT SURE
	float3 EyeVec = normalize(vecEye );
	oView  = mul(matWorldViewInv,-Light1Pos) + EyeVec;
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
	// THIS LINE KEEPS THE LIGHT STILL BUT WONT REFLECT THE SPECULAR CORRECTLY
	//oView  = mul(vecEye.xyz-Pos, matWorld);

	

	oTex = Tex;
}

float4 NormalMap_Diffuse_PS2(float2 Tex: TEXCOORD0,
							 float3 Light1: TEXCOORD1,
							 float3 View : TEXCOORD2,
							 float3 vPos : TEXCOORD3): COLOR
{

	float4 Ambient = tex2D( g_samScene, Tex ) * 0.3f;
   
	float3 vLight = normalize( float3( vPos - Light1Pos ) );
    if( dot( vLight, -normalize(Light1Pos) ) > 0.78539816339744830961566084581988 ) // Light must face the pixel (within Theta)
    {
		float3 Normal = (tex2D(g_samBump,Tex));
		Normal = (Normal) * 2 - 1;

		float3 ViewVec = normalize(View);
		
		Light1 = normalize(Light1);

		float diff = saturate(dot(Normal, Light1));
		float3 R = normalize(2 * diff * Normal - (Light1));
	
		float spec = max(dot(R,ViewVec),0);
		float SelfShadow = saturate(4*diff);
		

		float4 specular = SelfShadow*pow(spec,Shinyness) * (Scolor*Sintensity) ;

		float4 Diffuse = SelfShadow*diff*Light1Color;

		//return Ambient + ( tex2D( g_samScene, Tex ) * (Diffuse+specular));
		//return Diffuse+specular;
		//return Diffuse;
		return (tex2D( g_samScene, Tex )*0.3) + specular;
	}
	else
		return Ambient;
}
b3rs3rk
b3rs3rk
assuming that vecEye contains the camera position, this should do the trick:
//in the vertex shader:oView = mul(TanMatrix, vecEye - ovPos);//in the fragment shader:float3 ViewVec = normalize(View);float specVal = saturate(dot(Normal, ViewVec )); specVal = pow(specular, Shinyness);float4 specular = specVal * Scolor ;


hope this helps ^_^
SgtArchAngel
SgtArchAngel
unfortunately that didn't work either

here's some images of what's happening and a zip of my program to show you

L will put you at the position of the light
arrow keys move you forward and backwards
home and end are up and down
mouse will look around
(frame rate will seem low for this type of scene (60fps) cause of the PresentationInterval i am using)




http://www.kingsofwar.net/Users/ArchAngel/angle1.jpg

then the scene from the same position but looking slightly to the left
http://www.kingsofwar.net/Users/ArchAngel/angle2.jpg

[Edited by - SgtArchAngel on January 13, 2007 6:47:47 AM]
Sc4Freak
Sc4Freak
Program doesn't work. I just get 2 "couldn't create texture" errors, then it crashes.

Core 2 Duo E6600
Sapphire X1950XTX 512mb
b3rs3rk
b3rs3rk
how are you computing your normal and tangent? i've noticed that you compute the binormal in the vertex shader. If you're using meshes try to use D3DXComputeTangentFrame or D3DXComputeTangentFrameEx..
SgtArchAngel
SgtArchAngel
ok fixed the texture problem (sorry had the paths hard coded in the models)

Should work now
http://www.kingsofwar.net/Users/ArchAngel/Shader_Test.zip

useing the D3DXComputeTangent function for the tangents
if(FAILED(D3DXComputeTangent(ModeledObject.Mesh, 0, 0, 0, true, NULL))) return false;

[Edited by - SgtArchAngel on January 12, 2007 4:11:26 PM]
SgtArchAngel
SgtArchAngel
no ideas?

does the shader code look correct if you have checked it out?
any changes that could be made?
any other information needed?

or at least a good HLSL or CG tutorial on Bump/normal mapping with specular
SgtArchAngel
SgtArchAngel
ok nevermind i figured it out...
it was right in front of my face

b3rs3rk thanks for the help.. i had tried what you said to do before and it didnt work(spec was still moving with the view)
but i new it needed to be done just didnt know what was wrong
took another look and realized that my tanmatrix wasnt correct

this also alowed me to get rid of some garbage code in the shader

        float3x3 TanMatrix;	TanMatrix[0] = mul(Tan,matWorld);	TanMatrix[1] = mul(cross(Normal,Tan),matWorld);	TanMatrix[2] = mul(Normal,matWorld);        oLight1 = mul(TanMatrix,Light1Pos-Pos);	oView  = mul(TanMatrix,vecEye.xyz-Pos);


changing the matWorldView to matWorld was all that was needed
works perfectly now
thanks for trying guys

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.