Jump to content
  • Advertisement
Sign in to follow this  
DirectXFreak

Very odd parallax mapping errors....

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I have written a shader that implements bump mapping on a box (with a brick texture), complete with diffuse and specular highlights. It looks great, the problem is, when I go to implement offset mapping to it, it gets screwed up. Here is the important shader code:

//-- Global Variables 						 --
//-------------------------------------------------------

//--	Matrices
shared float4x4 g_matWorldViewProj;
shared float4x4 g_matWorld;

//--	Materials
shared float4   g_diffuseColor;
shared float4   g_specularIntensity;

//--	Camera
shared float4   g_cameraPos;

//--	Point Light Specific
shared float	g_invLightRange;
shared float4   g_lightPos;

struct VS_BUMPPOINT
{
	float4 Pos		: POSITION;
	float2 Tex		: TEXCOORD0;
	float3 Light	: TEXCOORD1;
	float3 View		: TEXCOORD2;
};
//-------------------------------------------------------

//-- Samplers										   --
//-------------------------------------------------------
texture BaseMap;
sampler BaseMapSampler = sampler_state
{
	Texture   = <BaseMap>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU  = Wrap;
	AddressV  = Wrap;
}; 

texture BumpMap;
sampler BumpMapSampler = sampler_state
{
	Texture   = <BumpMap>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU  = Wrap;
	AddressV  = Wrap;
}; 
//-------------------------------------------------------


VS_BUMPPOINT VSBumpPoint( float4 Pos  : POSITION,float2 Tex : TEXCOORD, 
						  float3 Norm : NORMAL, float3 Tang : TANGENT  )
{
	VS_BUMPPOINT Out=(VS_BUMPPOINT)0;

	//-- variables
	float4 light;
	float4 view;
	float4 posWorld;
	float3 Binorm = cross( Norm,Tang );

	//-- transform the position
	Out.Pos = mul(Pos,g_matWorldViewProj);
	
	//-- move the texture coordinates
	Out.Tex = Tex;

	//-- attenuation	--
	//--------------------
	posWorld  = mul(Pos,g_matWorld);		
	view  = g_cameraPos - posWorld; 
	light = g_invLightRange * (g_lightPos - posWorld);
	//--------------------
	
	//-- set the light
	Out.Light.x = dot(light, Tang);
	Out.Light.y = dot(light, Binorm);
	Out.Light.z = dot(light, Norm);
	
	//-- set the view
	Out.View.x = dot(view, Tang);
	Out.View.y = dot(view, Binorm);
	Out.View.z = dot(view, Norm);
		
	return Out;
}

float4 PSBumpPoint( float2 texCoord : TEXCOORD0, float3 texLight : TEXCOORD1, float3 texView : TEXCOORD2 ) : COLOR
{
	//-- attenuation	--
	//--------------------
	float atten = saturate(1 - dot(texLight, texLight));
	//--------------------

	if( atten == 0.0f ) 
	{	
		return 0.0;
	}
	else
	{
		float3 viewVec  = normalize(texView);
		float3 lightVec = normalize(texLight);
		
		float  height = (0.04*tex2D(BumpMapSampler,texCoord).a)-0.02;
		float2 newTexCoord = height*viewVec + texCoord; 
		
		//-- base texture   --
		//--------------------
		float4 base = tex2D(BaseMapSampler, newTexCoord);
		float3 bump = tex2D(BumpMapSampler, newTexCoord) * 2 - 1;
		bump = normalize(bump);		
		//-- diffuse 	    --
		//--------------------	
		float diffuse  = saturate( dot( lightVec,bump ) );
		//--------------------
		//-- specular	    --
		//--------------------
		float specular = pow(saturate(dot(reflect(-viewVec, bump), lightVec)), 32);
		//--------------------			
				
		return g_diffuseColor * ( diffuse * base + g_specularIntensity * specular ) * atten;
	}
}


technique TBumpPoint
{
	pass P0
	{
		//-- Compiler Directives
		VertexShader = compile vs_1_1 VSBumpPoint();
		PixelShader  = compile ps_2_0 PSBumpPoint();
	}
}


/* 
 *== Within the application!
 */

Vec4 camPos(pScene->getCamera()->getPosition().x,
			pScene->getCamera()->getPosition().y,
			pScene->getCamera()->getPosition().z,0.0f);

pEffect->SetVector(SD_LightPosition,&vec);
pEffect->SetVector(SD_CameraPosition,&camPos);
pEffect->SetVector(SD_InverseLightRange,&vAux);

/*
 *==
 */


I stored the heightmap for the normal map in it's alpha channel (it's a .dds file, A8R8G8B8), and the shader accesses it from the bump map. The results are that the texture seems to bend up and down, as I move left/right. It appears that the view vector is the problem, but the odd thing is that the specular highlights are working perfectly. Am I setting the camera vector correctly? If you need more information, I'm here.. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Try
float2 newTexCoord = height * viewVec * float2( 1.0f, -1.0f ) + texCoord
or something like that.

The UVs are top to bottom.

Share this post


Link to post
Share on other sites
well you see, it goes up and down as I go left/right, but when I reverse the x's and y's, it still looks borked..

I don't get it, I've doubled checked the shader code with other samples that work

Share this post


Link to post
Share on other sites
Well, after two days of hell debugging this... I found the issue:

float2 newTexCoord = height * viewVec.yx * float2( 1.0f, -1.0f ) + texCoord;

I inverted the y axis AND flipped the x and y, and it works!!

Thanks Blaster, that piece of code helped me find the problem!
rating++

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!