Sign in to follow this  
alimae

environment mapping with hlsl

Recommended Posts

ello. I try to have a environment mapping effect with hlsl shader and a cubemap. But it doesn't work reflection is not coherent. I don't see what's wrong. I think the texCUBE() function needs a float3 in world space, no? (world_matrix, viewproj_matrix and camera_position are given by Ogre) Your help is welcome. :) the vertex shader :
struct VS_OUTPUT
{
   float4 Pos					: POSITION;
   float3 Reflect				: TEXCOORD0;
};

VS_OUTPUT main
(    
   float4 Pos			: POSITION, 
   float3 Normal		: NORMAL,
   
   uniform float4x4  world_matrix,
   uniform float4x4  viewproj_matrix,
   uniform float4 	camera_position			//camera position in world space
   
)
{
	//init : 
	VS_OUTPUT Out = (VS_OUTPUT) 0; 
	
	float4 posWS  	= 	mul( world_matrix, Pos );
	Out.Pos 		= 	mul( viewproj_matrix, posWS );
	
	//calculate normal in world space
	float3 normWS 	= normalize(mul( world_matrix, Normal ));
		
	// Calculate the view vector in world space.
	float3 viewVectorWS = (posW - camera_position);	
	
	//Calculate reflection vector
	float3 reflection_vector = reflect(viewVectorWS, normWS);
	Out.Reflect = normalize( reflection_vector );

	return Out;
}
and the fragment :
samplerCUBE envMap :register(s0); 
float4 main
( 
	float4 Pos						: POSITION,
	float3 Reflect					: TEXCOORD0
	
) : COLOR
{ 
	// Calculate the resulting reflection color.
   	float4 finalColor = texCUBE( envMap, normalize(Reflect) );
	return finalColor;

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this