   Sphere Environment Maps in CgFx
  HiIm trying to get the texture coordinates into a sphere map in my shader but the refelctions look wrong. heres my code:/************* UN-TWEAKABLES **************/const float4x4 WorldITXf : WorldInverseTranspose< string UIWidget="None"; >;const float4x4 WorldViewProjection   : WorldViewProjection< string UIWidget="None"; >;const float4x4 WorldXf   : World< string UIWidget="None"; >;const float4x4 View   : VIEW< string UIWidget="None"; >;const float4x4 Projection   : PROJECTION< string UIWidget="None"; >;const float4x4 ViewIXf   : ViewInverse< string UIWidget="None"; >;float time : TIME;/*********** Textures & Samplers *************/texture diffuse_texture <string ResourceName = "rockwall.jpg";string UIName = "Diffuse Texture";string ResourceType = "2d";>;sampler2D sampler0 : TEXTURE_SLOT_0 = sampler_state {Texture = <diffuse_texture>;MagFilter = Linear;MinFilter = Linear;MipFilter = Anisotropic;AddressU = Wrap;AddressV = Wrap;};texture sphere_texture <string ResourceName = "sphericalmap.tga";string UIName = "sphere Texture";string ResourceType = "2d";>;sampler2D sampler1 : TEXTURE_SLOT_1 = sampler_state {Texture = <sphere_texture>;MagFilter = Linear;MinFilter = Linear;MipFilter = Anisotropic;AddressU = Wrap;AddressV = Wrap;};/*********** Tweakables **********************/float3 LightPos<string Object = "PointLight0";string Space = "World";> = {10.0f, 10.0f, -10.0f};float3 LightPos1 <string Object = "PointLight0";string Space = "World";> = {-10.0f, 10.0f, -10.0f};float3 LightPos2<string Object = "PointLight0";string Space = "World";> = {10.0f, 10.0f, -10.0f};/////float3 AmbiColor : Ambient <string UIName =  "Ambient Lighting";string UIWidget = "Color";> = {0.1f, 0.1f, 0.1f};/////float3 SurfColor : DIFFUSE <string UIName =  "Surface Color";string UIWidget = "Color";> = {1.0f, 1.0f, 1.0f};float SpecularIntensity <string UIWidget = "slider";float UIMin = 0.0;float UIMax = 1.0;float UIStep = 0.01;string UIName =  "Specular Intensity";> = 1.0;float SpecExpon : SpecularPower <string UIWidget = "slider";float UIMin = 1.0;float UIMax = 128.0;float UIStep = 1.0;string UIName =  "Specular Power";> = 119.0;float reflectionIndex <string UIWidget = "slider";float UIMin = 0.0;float UIMax = 1.0;float UIStep = 0.01;string UIName =  "Reflection Index";> = 0.25;/************* DATA STRUCTS **************//* data from application vertex buffer */struct appdata {float3 Position: POSITION;float4 UV: TEXCOORD0;float4 Normal: NORMAL0;};/* data passed from vertex shader to pixel shader */struct vertexOutput {float4 HPosition: POSITION;float2 UV: TEXCOORD0;float3 LightVec: TEXCOORD1;float3 WorldNormal: TEXCOORD2;float3 WorldEyeVec: TEXCOORD3;float3 pos: TEXCOORD4;};/*********** vertex program ******/vertexOutput simpleVP(appdata IN) {vertexOutput OUT = (vertexOutput)0;OUT.WorldNormal = mul(WorldITXf, IN.Normal).xyz;float4 Po = float4(IN.Position.xyz,1);float3 Pw = mul(WorldXf, Po).xyz;OUT.LightVec = (LightPos - Pw);OUT.WorldEyeVec = (float3(ViewIXf[0].w, ViewIXf[1].w, ViewIXf[2].w) - Pw);OUT.HPosition = mul(WorldViewProjection, Po);OUT.UV = IN.UV.xy;OUT.pos = OUT.HPosition.xyz;return OUT;}float2 SphereMap(const in float3 U, const in float3 N){float3 R;R = reflect(U,N);R.z += 1.0;R = normalize(R);return R.xy*0.5f+0.5f;}/********* fragment program ********/float4 simpleFP(vertexOutput IN) : COLOR {float3 Ln = normalize(IN.LightVec);float3 Vn = normalize(IN.WorldEyeVec);float3 Nn = normalize(IN.WorldNormal);float3 Hn = normalize(Vn + Ln);float4 lightVector = (lit(dot(Ln,Nn), dot(Hn,Nn), SpecExpon));float3 DiffResult = SurfColor * (lightVector.yyy + AmbiColor);float3 SpecResult = SpecularIntensity * lightVector.zzz;float3 reflectionNormal = float3(Nn.x,Nn.y,Nn.z);float2 reflectionTextureCoords = SphereMap((IN.pos),reflectionNormal);float4 texture2Pixel = tex2D(sampler1, reflectionTextureCoords);//return texture2Pixel;float4 diffusion = float4(DiffResult, 1.0f) * 1.5;float4 texture1Pixel = tex2D(sampler0,IN.UV);float  texture1Level = (texture1Pixel.x + texture1Pixel.y + texture1Pixel.z) / 3;float4 specular = float4(SpecResult, 1.0f);float4 reflection = texture2Pixel * reflectionIndex;reflection *= texture1Level > 0.3f;float4 colour = (diffusion * texture1Pixel)  + (specular * texture1Level) + (reflection * texture1Level) ;colour.a = 1.0f;return colour;}/*************/technique main{pass p0{VertexProgram = compile arbvp1 simpleVP();FragmentProgram = compile arbfp1 simpleFP();DepthTestEnable = true;//DepthMask = true;}}/***************************** eof ***/the function SphereMap(const in float3 U, const in float3 N) is what seems to be going wrong. can anyone help me out?   
  It's hard to say what the problem is without seeing the incorrect image.  If you could post a screen shot it would be helpful in figuring out what is going on.Some general information about sphere mapping can be found in the environment mapping section of our book (see my signature below for the link to the D3D10 book).  It covers the math and the exact calculation that you are questioning here for cube, paraboloid, and sphere mapping.  Take a look at it and let me know if you still think the calculation is incorrect.By the way, where did you get the shader?  Is it a known good implementation?   