# Reflection/environment mapping

I'm implementing reflection/environment mapping using a cubemap. I have it working for the most part but when I move my camera around, I see the same area reflected on the object. I am calculating the eye vector and normal vector in view space similar to OpenGL Shading Language 3rd Edition example (p.311, Section 10.4).

Why does the reflection stay as if it is from the same view?

Which space should I be doing my calculations?

How render looks: Snippets of HLSL code

Vertex shader:

normal = mul( (float3x3)mvMatrix, input.normal );
normal = normalize(normal);
viewPos = mul( mvMatrix, float4(input.pos, 1) );


Pixel shader:

viewPos.w = 0;
V = normalize(viewPos);
normal.w = 0;
normal = normalize(normal);

float4 reflectVec = normalize(reflect(V, normal));
float4 reflectColor = tex.Sample(samLinear, reflectVec.xyz );

• What exactly is the question here ? – Raxvan Jan 28 '15 at 10:04
• Shouldn't viewPos be position3D-cameraPosition? And I hope mvMatrix is the world matrix? – János Turánszki Jan 28 '15 at 11:03
• @Raxvan, why isn't the reflection direction changing as I move around the object? – Plato Jan 28 '15 at 16:23
• @JánosTuránszki its in eye space, so its pos-eye, but eye = 0. mvMatrix includes world matrix (it is identity) – Plato Jan 28 '15 at 16:25

## 1 Answer

Right now you transform the normal by both the model and view matrices. Instead, just transform by the model matrix. If the view matrix is identity and you're just manipulating the model matrix, then the image results are expected, since rotating a teapot doesn't rotate the world around it. In that case, you should switch and leave the model matrix fixed, and manipulate the view matrix, so that you're manipulating the camera instead of the teapot.

• My model matrix is identity matrix and I'm moving around the camera (view matrix) – Plato Jan 28 '15 at 20:57
• @yxk In that case the first fix should correct the issue - multiply the normal by the model matrix only. – MooseBoys Jan 28 '15 at 22:22
• now the object looks like it has been textured with the cubemap and not looking like its 'reflecting'. Also, why can't I do the calculations in view space? – Plato Jan 29 '15 at 5:43
• @yxk That probably means your "viewPos" value isn't actually changing, and is probably just unit-Z. Ultimately you just need to be consistent about what coordinate space you're in. You can do calculations in the view space, but then you need to apply a transform (inverse of the view matrix) to get it back into env-map space; otherwise you get the effect of the cubemap basically moving with the camera, as in your original pictures. Try drawing it in 2D on a piece of paper - that will help you visualize what the different vectors actually mean. – MooseBoys Jan 29 '15 at 8:49