'how to do cube mapping of a static environment onto a complex model by directx11 and HLSL?

I am very new to Shaders and programming in direct 11(c++) and HLSL for shaders. However, I have been given a task to: Implement cube mapping of a static environment onto a complex model (not a cube). Cube mapping allows an object to reflect the scene around it. There aren't many resources online can anyone please tell me the steps to follow to achieve a correct cube mapping. I'm more concerned about the calculations to do on the HLSL side.



Solution 1:[1]

For a very basic environment mapping, all you need to do is:

  • Compute the position and surface normal of the current pixel (in the pixel shader) in world space
  • Compute the (normalised) view direction (world space pixel position - world space camera position)
  • Compute the reflection vector from view direction and surface normal (there is a builtin HLSL function to do that, if you don't want to do the math yourself)
  • Sample the cube map with that reflection vector, and return that color.

This then works like a mirror: The reflection vector is the direction in which your line of sight would get reflected if the surface of your mesh would be a perfect mirror, and then you ask the cube map what color is in that direction (aka, whats the reflection you're seeing). How simple or complex your mesh shape is, doesn't matter during this, because you're always only looking at one pixel of that (rasterized) mesh at a time, using that pixels sufrace normal as a guide.

More advanced environment mapping techinques will then blur the reflection based on the surface roughness (usually by sampling different mip map levels of your cube map), merge the color with other light/color computations of that pixel, add indirect environment mapping coloring (which requires sampling a different cube map, which was pre-computed in a special way, with the direction of the surface normal directly), etc. That's then where all the papers and stuff come into play, but the very basic concept of environment mapping are just a few lines of code and is very straight forward.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bizzarrus