Jump to content
  • Advertisement
Sign in to follow this  

reflection map with texCUBE

This topic is 4775 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

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

// 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.


Share this post

Link to post
Share on other sites

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
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!