How it works...

Both shaders are quite similar to the shaders in the previous recipe.

The vertex shader computes the position, normal, and view direction in world coordinates (worldPos, worldNorm, and worldView). They are then used to compute the reflected direction using the reflect function, and the result is stored in the output variable ReflectDir. The transmitted direction is computed using the built-in function refract (which requires the ratio of the indices of refraction Material.Eta). This function makes use of Snell's law to compute the direction of the transmitted vector, which is then stored in the output variable RefractDir.

In the fragment shader, we use the two vectors ReflectDir and RefractDir to access the cube map texture. ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.