Being accurate: Ray Tracing is often favored New shader types available in Vulkan and DX12 for that What I'm about to teach will probably be a historical relic in a few years Sorry! But, it's the way things are done now, and will be until everyone has an RTX or equivalent How long? Brief unscientific poll... We like to say technology moves along and everyone has to adapt This time we can see it coming https://paroj.github.io/gltut/Illumination/Tutorial%2009.html Ok, pretend it's still 2018: Simple model of light Brief philisophical note: gravity We can land a space probe on Mars Think about real light hitting a surface Partial and colored reflections Photorealistic Rendering: Believe it or not, that's the name of the type we'll use! vs. cartoons and such Lights and temperature and spectrum: "full spectrum" grow lights daylight, incandescent, sodium vapor, etc. Reflections have color RGB: Trick of human visual processing! It's ok, we don't really care about non-humans anyway... Light absorbtion: Pigments absorb the colors they're not Black absorbs everything, pure white nothing mirror vs. white: white scatters, mirror does not Laser armor, and nothing is perfect Light and angles: Why is the Winter sun weak? More than one reason, but the angle is one of them Angle between the light and the surface is critical That's why normal vectors come into play! A big triangle and apparent curves Diffuse Lighting: Light hits at some angle (could be straight on) and scatters Even distribution in all directions No surface is actually like this, but it's a decent approximation Diffuse Equation: R = D * I * cos(Theta) R is the diffuse lighting color D is the absorbtion (vec3) I is the light intensity (vec3) Theta is the angle between the light and the surface normal vector cos(0) is 1.0, cos(90) or cos(-90) is 0 abs(Theta) > 90 shouldn't be lit, or it'd be negative Where to calculate it: Vertex shader: Since the triangle is flat, it's mostly consistent over the whole thing! Angle to light source can vary Historical method, kind of Good if light is far away Facets, and approximation of curves Geometry Shader: Less redundancy, once per triangle (nobody does this) Fragment Shader: More invocations unless triangles are really small Allows re-calculation at each point Slower, especially for low-res models We've got the power, and it normally looks better How to do the cosine: Could just calculate it the normal way, but we don't need to dot product: definition, actual calculation with just multiplication Magnitude when both vectors were unit vectors glsl dot function Gouraud Shading: Diffuse, done in the vertex shader Interpolated over the whole triangle Very popular for a decade or so Normals, and calculation in difference spaces The model is not actually in model space if we've scaled, etc. The normal vectors are in model space MVP and what happens to these Short answer: Compute the inverse-transpose matrix, and multiply by that instead If you're really interested in 3D, take linear algebra. It counts as an upper-division elective Do it once on the CPU instead of a whole bunch of times on the card This is only a problem if the object has had a non-uniform scale applied Can avoid overhead if you just make the models so they don't need a non-uniform scale A very practical note about normal vectors and vertices: With the cube, we're storing each vertex once Each vertex has THREE normal vectors! obj file internals Can manage this with a variety of buffers Don't sweat it too much. In a curved mesh, each vertex just has one normal Ambiant Light Doesn't exist Basically, we just add a little "bonus" light Can use diffuse colors Can set an ambiant color, for the extra light Diffuse gives us a term, R L = R + A Can't exceed 1.0 here - OpenGL will clamp the color at 1.0 This'll cause the color to be washed out if it exceeds 1.0 More terms will keep being added! Dynamic range Point lighting: A directional light for diffuse lighting comes from some direction A point light comes from a point We can figure out a direction to a point, not a problem? More or less defeats gouraud shading Fragment shader: Must preserve surface location and normal Attenuation: Decrease light power with distance! Real: I / (1.0 + k*r^2) k constant: Can use distance where half the light is lost Less attenuation: I / (1.0 + k*r) Just take out the r, and lights work when further away Calculations happen in world-space distances Can compute from camera space, or preserve, or whatnot Specular Reflection: Reflection in a specific direction! Comes with a different color Microfacets, roughness Angle to the camera matters! Phong: dot product of V and R raised to the s power V is the viewing direction R is the reflection direction s is a term that represents roughness The Phong term is multiplied by the light intensity and the specular absorption Specular absorption might not be the same color as diffuse absorption Only works for angle less than 90 Blinn term: Use half angle vector Gaussian: Probability distribution of microfaucets Dynamic and HDR lights Practice: Add some kind of model into a lighting demo