Before we dive back into lighting: Project Test coming next week AMD limit on workgroup size Linear Algebra, Math 340 A lot of stuff we talk about is covered in more detail in Math 340 If you want to get into 3D graphics, take this course Mike Love likes programmers. He's cool. https://paroj.github.io/gltut/Illumination/Tutorial%2009.html This is pretty old, but still relevant It does have a cmake build thing now The cmake build thing doesn't work on cmake <3.12 Like we have in the lab... Before we start: Can't we figure this all out ahead of time? "Baked in" lighting and the chalet Let's get sidetracked a moment since we might use it later: A note on adding a gui We *might* fit this in sometime If not, just look for a tutorial, there are a bunch 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 Let's review the dot product: Also called scaler product How to calculate it with multiplication and addition How to calculate it with cosine We'll need the magnitude How to calculate cosine with it We'll need to be able to normalize Then we can have a cosine function! Will it be fast? Square root isn't particularly There IS a GLSL function for this Interesting chart somebody did 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 What would that look like? 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 One of the fastest lighting models to apply because interpolation is fast Best if none of the triangles is too huge Assumes diffuse light, not as good with close light sources Demo for this point: Let's hack this into round_cube Normals: We can output normals easily, from the center What about MVP though? Could put the light in model space, but it'll be spinning around with the model Let's do that for starters Do we need to do the lighting in the TES? Strange Gouraud, but ok... Can we make the light move around or Next issue: What if we wanted the light to be in world coordinates? Or camera? Lab today: Add a second light source somewhere else in a different color Limitations: The light source is directional from a long distance As in, it has the same angle over the whole scene Like the sun Not like a flashlight Normals, and calculation in different 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 (we'll use it anyway) 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