Sign in to follow this  
genesys

reflection map with texCUBE

Recommended Posts

Hi! I try to program a metal shader with HLSL in rendermonkey. unfortunately all rendermonkey examples which would cover this type of shader are written in binary asm and not HLSL . . . Could someone explain me how i must use texCUBE(s,t) (i couldn't figure out whats the purpose of the third member of the 3D Vector t) and how to calculate Vector t to create a reflective looking surface?? is there also somehow a way to create such a surface with a spherical mapped environment?? THX for the help!

Share this post


Link to post
Share on other sites
You need to output cubic texture coordinates from your vertex shader.

Here is an example

// Global variables

texture g_CubeTexture; // cube map texture

float4x4 g_mWorld; // World matrix for object
float4x4 g_mViewProjection; // View * Projection matrix
float4x4 g_mWorldViewProjection; // World * View * Projection matrix
float3 g_vCameraPosition; // Camera or "eye" postion in world space



// Texture sampler

samplerCUBE CubeSampler = sampler_state
{
Texture = <g_CubeTexture>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};


// Vertex shader output structure

struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 CubeTexcoord : TEXCOORD0;
};



// Vertex shader for model

VS_OUTPUT RenderModelVS(
float3 InPos : POSITION, // Vertex position in model space
float3 InNormal : NORMAL) // Vertex normal in model space
{
VS_OUTPUT Out = (VS_OUTPUT)0;

// transform the position and normal

// position (view space)
float3 Pos = mul(float4(InPos, 1), (float4x3)g_mWorld);

// normal (view space)
float3 Normal = normalize(mul(InNormal, (float3x3)g_mWorld));

// vector from vertex towards eye
float3 EyeToVertex = normalize(Pos - g_vCameraPosition);

// position (projected)
Out.Pos = mul(float4(Pos, 1), g_mViewProjection);

Out.CubeTexcoord = reflect(EyeToVertex, Normal);
return Out;
}


// pixel shader for model
float4 RenderModelPS ( VS_OUTPUT In) : COLOR
{
float4 OutColor = texCUBE(CubeSampler, In.CubeTexcoord);
return OutColor;
}


technique RenderModel
{
pass P0
{
VertexShader = compile vs_1_1 RenderModelVS();
PixelShader = compile ps_1_1 RenderModelPS();
}
}





Ofcourse you will need to pass your camera (or "eye") position into the effect on every frame.
Note: reflect is an intrinsic shader function.

HTH,
Cambo_frog

Share this post


Link to post
Share on other sites
Hi,

the third member of the 3D vector t as you said is the z component of any 3D vector ^^
In fact, imagine cube mapping as a big cube with the texture placed on it. Place yourself on the center. Then, your t vector will give you a direction from the center. Follow this direction, and you'll hit your cube at a given position. texCUBE will just give you the color of the cube map at this position.

That's just a little text to explain the principle of cube map, and the code from Cambo_frog gives you the way of implementing it in HLSL as you asked ^^

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