Moving Calculations to the Vertex Shader

We’ve been doing all these calculations per fragment. For example, Position is interpolated and then the lightDistance is computed per fragment. This gives pretty high-quality lighting, at the cost of doing an expensive square-root computation (hidden in the length() built-in function) per fragment. Sometimes, we can swap these steps: perform the light distance calculation per vertex in the vertex shader and interpolate the result. That is, rather than interpolating all the terms in the calculation and calculating per fragment, calculate per vertex and interpolate the result. The fragment shader then gets the result as an input and directly uses it.

Interpolating vectors between two normalized vectors ...

Get OpenGL Programming Guide: The Official Guide to Learning OpenGL, Version 4.3, Eighth 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.