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: Example of environment mapping gone awry

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

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.

  • \$\begingroup\$ My model matrix is identity matrix and I'm moving around the camera (view matrix) \$\endgroup\$ – Plato Jan 28 '15 at 20:57
  • \$\begingroup\$ @yxk In that case the first fix should correct the issue - multiply the normal by the model matrix only. \$\endgroup\$ – MooseBoys Jan 28 '15 at 22:22
  • \$\begingroup\$ 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? \$\endgroup\$ – Plato Jan 29 '15 at 5:43
  • \$\begingroup\$ @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. \$\endgroup\$ – MooseBoys Jan 29 '15 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.