Intro to Graphics 16 - Shading Transformations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you for coming to another lecture for introduction to computer graphics i hope you are awake if you're not this is the time to um make yourself awake you know do whatever you need to do and get ready because we're going to start a little slow but towards the end this is going to get a little mathy you're going to need to your pay attention this is not going to be difficult per se but you won't really need to pay attention because we're going to talk about shading related math stuff today we're going to dive deep on the sort of fundamental concepts behind shading related math um so here's your warning try to be awake all right this is going to be it's going to require you to pay some close attention but we're going to start a little we're going to start slow right so we're going to start with there's this concept of smooth shading what do i mean by illustrating well here's here's here's what i mean now if i'm trying to shade this sphere i and i have an implicit sear implicit sphere is just a function that defines a sphere it can we can perfectly define a sphere right we talked about this when we're talking about surfaces uh if you have an implicit sphere we don't have any problems so i can compute the surface normal at any point on this implicit sphere just like computing anything on the on the server i'm not gonna tell you the math but math is actually very very easy and actually in if this is a unit sphere in object space that is uh the center of the sphere being the the origin the position of a point on the sphere is also that the same vector is also its surface normal so it's fairly easy uh so using that surface normal i can change any point on this surface surface not a problem not at all right but this is yeah we use we do use implicit spheres for rendering sometimes but oftentimes what we deal with are not implicit surfaces oftentimes what we deal with are polygons right polygon meshes now this one looks like a quad mesh it's actually a triangular mesh you just can't see you know two triangles form a quad you just can't see the difference nonetheless doesn't look very smooth does it it looks sort of jaggedy because it's a polygonal sphere so let's let's see what's going on here so we're going to talk about how to handle objects like this and get simultaneous so the simplest thing we can do is what we call flat shading right so flat shading is what you see here and concept is very very very very simple for each triangle here i can compute the surface normal of that triangle right so if this is made up of a triangular mesh for every triangle i can compute the surface normal of that triangle how do i compute the surface normal of the triangle there are many ways of doing that but the simplest thing is take two of the edges and take a cross product the cross product would give you a vector that's going to be perpendicular to two of the edges right now of course you need to normalize it so you get a unit vector and there it is there's your surface normal right there's a normal of a triangle you can take any any two edges take a cross product of any two edges actually that goes for any two vectors on the same plane as this this triangle that are not in the same direction as long as you pick two vectors that are not in the same direction the cross product of these two vectors would form a vector in the surface normal direction of course it could be positive or negative so you kind of need to if you if you want to make sure that your surface in the right direction not in the opposite direction you kind of need to be more careful about which to add which order you multiply the edge vectors but other than that it's fairly simple right and then you know this is a triangle and i have a surface normal for a triangle the entire triangle uses the same surface normal and what happens is that the entire triangle becomes flat so we get flat right that's that's what you would expect and that's what you get and your point on mesh looks like this not so great definitely not smooth so um back in the day gora came out with this alternative idea after this he said you know what we can get smooth shading like so if i have a triangular mesh and if i have a different surface normal defined on each one of my vertices as i in this case i have three vertices i have three vertex normals and zero and one and two then i can do shading on each one of these vertices separately like each one of these vertices they're their own individual surface normals so i can do shading separately on each one of these vertices and if i do that i'm going to get a different a color value so for this vertex i'm getting c0 for the next word vertex i'm getting c1 and the next one c2 all right so i have these three uh shaded colors computed at each vertex position then go around shading says i can just use those as vertex colors and i can interpolate them so if i interpolate the the computed colors over here i can compute the color at any point on this triangle right so and any picks for any pixel over here what i'm going to do is i'm going to shade the vertices first and then i'm going to compute the the color that corresponds to a pixel by interpolating those shaded colors this is called go out shading so how do you get a vertex normal that's an excellent question you should think about this as something that's given to you so whoever is preparing this pluto mesh will need to provide somehow these vertex normals as well so those vertex normals those normals that correspond to those vertices will be the normals of the surface that where the smooth surface that we're trying to approximate using a polygonal mesh so in this case if if i'm trying to form a sphere the normal should be the sphere normals at these vertices that's what they should be but you know it's it's not so sphere is an implicit surface right so we can we can actually compute the the surface normal of a sphere easily what if i have an arbitrary mesh like a i don't know like a dinosaur or something whatever like how do you compute the surface normals there then there are various ways of doing this but the more standard way of doing that would be you first compute the face normals normals for each triangle and then for each vertex you look at the triangle normals around it so look at all the triangles that use the same vertex and then you take some weighted average of those normals and that would give you your vertex normal so that's a very typical way of computing a vertex normal but at the end of the day when you're doing shading you don't compute the normals normals are given to you okay so i think about this as a part of the modeling process oftentimes they are computed automatically because oftentimes when we have polygonal mesh what we are trying to build is an approximation of a smooth surface so modeling software will often times automatically compute these normals and they would compute them exactly as i'm describing you they first compute the phase normals and then average the phase normals to compute the vertex normals and once we have vertex normals we can do this we can do go around shading actually the pole gpu rendering pipeline is designed to do just this opengl back in the day when we did not have programmable shaders was doing go round trading so we could get sort of smooth looking surfaces now here's a revelation if you haven't thought about it why do we call every stage every programmable stage in our on our gpus on our gpu pipeline a shader the the answer to that is hidden in go around shading because see this is doing shading at the vertices so the standard thing to do when we had our first programmable shaders was to do shading in the vertex shader because this is where you do vertex processing right so you kind of need to compute the the color the shading operation that we talked about in the previous lecture you do you do it here at the vertices and then in your fragment shader you just interpolate the color values well actually your gpu will interpolate the color values automatically for you but with the programmable shaders we can now do a little more and we'll talk about that in a little bit so if you're doing shading at the vertices that would be go around shading and that would be relatively fast because typically look at this image for example i have a lot fewer vertices than i have pixels right so instead of computing the color of each pixel individually it would be a lot more efficient to compute the color only at the vertices and then interpolate the color right so this is what gorilla shading does and that's the reason why this was the opengl standard back in the day when were our gpus were not very powerful and so we needed to limit the number of shading operations once you compute the the color values at the vertices the interpolation is fairly simple we talked about this you can do very sensitive interpolation right so do a very centric interpolation to get the color c at any point on this triangle gpu you can can do that you will do that automatically for us so you could just compute the vertex colors at these three vertices and then send it to your fragment shader gpu will send you the interpolated value you see here and you just sand it out and you're done in your fragment cheater that's very good very very efficient because oftentimes our triangles the triangles we will use for especially for real-time rendering they will be much larger than one pixel so they will span a whole bunch of pixels like in this example so you get a lot fewer vertices than pixels so this would be the efficient way of handling shading go around shading nonetheless i'm pretty sure you all noticed this is not looking all that great i mean look at that specular highlight over there on the sphere it's a little um shall i say triangulated but you can kind of see the triangular edges on that specular highlight right there over here specular highlights see that it's not so great all right so probably we could do better so falling to the rescue functioning now don't confuse this with the foam material model foam material model is something else on its own phone shaving is a concept of doing smooth shading phone did a lot of things and you know we we give his name for multiple things so phong shading is this so here's how it works i have a triangle again and again i have vertex normals defined on the vertices of my triangle now i want to know the color at some point on this triangle so let's say that i'm doing rendering on the gpu these vertices pass through my vertex shader and i got this this point in the fragment shader and i want to compute the color at this point on that triangle that's where i'm going to do shading now what do i need i need the surface normal off at that point right that's what i need and if i'm not doing flood shading i can't just compute this normal based on the triangle normal what i'm going to do with functioning concept with a punctuated concept is instead of interpolating the colors computed at the vertices i am just going to interpolate the surface normals defined at the vertices so again this is just very centered interpolation actually this is fairly easy to do on the gpu if you just pass your normals from your vertex shader to your fragment shader your fragment shader will get the interpolated normal just just you're gonna get you're gonna get this in the fragment shader so we're we can just use this right we're done are we done there's something missing here can you see what's missing here huh maybe maybe not remember surface normal is supposed to be a unit vector and this most likely isn't i mean these individual vectors were unit vectors but unless they were perfectly aligned uh percentage interpolation is not going to give you a unit vector anymore so this is not going to be a unit vector so what we need to do is that we need to normalize it yeah normalization is written in this form but yeah you don't you don't need the original original vectors to do normalization you can just get the unnormalized vector in your fragment shader and normalize it without having to know the n zero and one and two right but it's important that we must normalize it without normalization this is not going to work of course very very important the rest of your computations are going to be you know that you're going to get wrong wrong results again let me remind you we're going to do shading in the next project and we're going to implement something very much like this in our in our vertex shaders we're going to send the vertex normals and we're going to get the interpolated vertex normal at our fragment shaders and that vertex normal we're going to get is not going to be normalized we are going to have to do the normalization inside our fragment shaders okay that's that's very important do not forget that if you forget that your shading will look weird all right good okay and when you do that you have the surface normal so we're done now we can do we can do shading so that's the the main problem was getting the surface normal and foam shaped sort of solves that so if you if you if you look at this if you look at the surface the shading here especially this specular highlight here you see that that looks actually pretty smooth and nice right it actually looks very good i mean look look around the edges this is not a very this is not a very high quality sphere at all it's actually very low poly on sphere with very large triangles still phong shading produces very nice looking approximation of a a surface of a smooth surface here so let's let's get an intuition of what is happening here and why exactly this is the case i'm going to take a step cross section from this surface i'm just look at this just one line one curve around the surface so let me put it aside so this is the this is the curve as we see it but that's not the curve that i'm rendering right and what i'm reading is a polygonal mesh because it's a polygonal mesh each for my triangles they're flat so that cross section actually looks like this the actually this this red stuff is what i'm rendering not this this orange nice curve that i would like to render but that's not what i have right i have a polygonal so the nice thing that's happening here is that since i have this these nice vertex normals defined and presumably these nice vertex normals are aligned with this smooth surface so it's the the normal that i would get on this smooth surface and in the end i'm trying to do shading at some point on this triangle right its position is over here not on the smooth surface out there is actually on that triangle that that's where the position is this is the point that i'm shading but the surface normal that i'm going to be using is going to be the interpolation of the nearby surface normal so it's going to be an approximation of this surface normal on the smooth surface right so even though the position is not quite right the surface normal normal direction is pretty good in fact for a a sphere this this very sensitive interpolation of the surface normal gives you a very very good approximation i can't remember how accurate it is but it's actually very very good for a sphere for for other types of objects who knows we don't have a precise definition of what that object is anyway so maybe you don't care nonetheless you get a smooth looking surface anyway and that's what's important and over here if you look at this this image yeah i'm actually rendering a polygonal mesh and it's made out of these these flat pieces but i cannot see it except for the edges except for the silver edge here i cannot see it because this nice interpolation of the surface normal sort of hides that artifact for me completely i cannot i cannot notice that at all it appears like a smooth surface even though it isn't all right so again let's compare these three modes flat shading everything's flat go around shading it's just interpolating the colors and foam shading is interpolating surface normals in which case you are doing a separate shading computation for each pixel so for rendering with the g on the gpu using the gpu pipeline that means that i'm going to be doing shading in the fragment shader not in the vertex shader right if i'm using phong shading i am doing shading in the fragment shader if i'm using go around shading i am doing shading in the vertex shader this this word shading is overloaded here i understand that but that's that's the proper word so i kind of need to keep using this that the same word for talking about two very different things shader and shading them different things right so if i'm using goat shading if i'm using gold shading i'm going to be doing shading in the vertex reader if i'm using foam shading i'm going to be doing shading in the fragment shader and this does not mean that i'm going to be using the foam material i can do i can use the blend material model i can use any kind of material model still going to be functioning because i'm interpolating the surface normals okay as you can see it looks very nice it actually solves all our problems with the fact that we are using polygonal measures and our triangles are flat they don't look flat except for the celiac of course so we're happy everything's good right and this is how we get smooth looking surfaces in computer graphics even though we almost never use human surfaces i i shouldn't say almost never except for special cases we don't use smooth surfaces we just use polygonal meshes all right so let's move on to the mathy part of things are you ready all right if you're not ready this is the time to wake up all right shading transformations now to be able to do this shading we kind of need to figure out the transformations to bring everything into the same space so that we can we can work with these vectors now we talked about shading operations about the blend material and the foam material models in in either case we are dealing with vectors right we're doing the dot products of vectors and those vectors have to be in the same space because if they're not in the same space the dot product of two vectors in two different spaces what does that mean there's no you can't really make any sense of that everything has to be in the same space so that this this whole concept of the dot product gives you the cosine of the angle between the two vectors it only it only works if the vectors are in the same space right so we're gonna have to deal with shading transformations so let's talk about that just a little bit now i've shown you similar slides before when we were talking about viewing transformations right so we said that we have this model or object space where our models are defined and then we are going to build our scene which will bring all our objects to the world space and for that we're going to use a particular transformation that we call the model transformation so model transformation will bring our vertices from model space to world space right we talked about this and then what we do is that we transform everything to the view space view or camera space and to do that we get the what we have is the view transformation so model transformation goes from model space to world space ps transformation goes from world space to the view space okay [Music] so this is this is the whole procedure i'm just going to call it this one model this one do you what what's next after the view space we transform everything to the canonical v volume right over here and that was our projections projection operation so we start with model transformation view transformation and then projection uh of course we don't have to do these transformations one after the other we can combine these transformations into a single transformation for example instead of doing model transformation and then view transformation i can just have a combined transformation just multiply two matrices together and i can have the model view transformation right and i can i can even combine the projection operation and i can have this model view projection transformation right so this is the stuff that we talked about this is actually exactly what i'm hoping that you all have implemented in our previous projects uh when we're rendering we computed this model viewer projection transformation and we use the model v projection matrix to directly move from the model space all the way to the canonical view volume right so that's what we did for rendering with with webgl all right so let's talk about shading transformations so this was all about rendering now when we're doing shading we kind of need to uh the first thing we need to do is to decide where we're going to do shading i mean we have four spaces here we kind of need to pick one to do our shading right all our vectors have to be in the same space for us to do shading so actually we can do shading in in any one of these spaces that would be fine except that this canonical view volume becomes a little tricky the reason why it gets tricky is that it's because of this projection if this projection involves perspective transformation things are going to be skewed a little bit right because of this perspective transformation things are going to be in this in this space everything is going to be deformed a little bit so the angle between vertices in this space is not going to be the same angle in these other spaces because everything is sort of deformed in this with this perspective the formation here if i'm using perspective projection so you know what let's let's get rid of this all right so that's that's we don't want to mess with the canonical view only when we're doing shading but these three spaces they're fine i can do shading on in any one of these spaces all right so that brings me but i can you know this is my model i have everything in my model my surface normals are defined in model space and i can bring them all the way to view space easily or i can bring them to the mobile space if i want to [Music] um what else do i need i need the light direction right and where is that defined hmm well oftentimes my light directions will be defined in the world space or sometimes they are defined in the model space of the light source if i have a light source object that could be the model space of the light source but i can transform them to the world space easily if i want to so you can assume that it's in this in the world space that's a very reasonable assumption so it kind of makes sense to do shading in the world space because yeah it's a unified space for everything you might see right um another space where it would make sense would be the view space because everything will eventually come to the view space to be processed so we're already going through the view space so we might as well use that but when you think about it there isn't too much of a difference in terms of which space to use view space or the world space even model space is fine i just need to have i just need to know the inverse transformation so i can take this light direction and have inverse transformation from world space to model space to transform it to the model space and and i can do that and all is fine but oftentimes i'm going to prefer the view space for doing shading this is not the this is not written in stone you can definitely do shading in any one of these spaces it will be okay all that's important is that all of the vectors that you're using for shading and they all have to be in the same space and it doesn't matter what space that is you can have them in whatever space you like as long as they're in the same space uh the equations that we we looked at last time all of those equations will be valid as long as all of my vectors are in the same space so we just need to pick one and go with it and i'm telling you all of these three are viable spaces i could come up with a fourth one imaginary i could call it my training space and and transform everything to that sharing space and do that i mean there's no reason why i would do that but it's definitely a possibility so a typical thing to do is to do all of this in the view space because that would often times well not often times sometimes that will give you slightly better numerical precision and this is not a very strong argument in most cases but in some cases that actually makes a difference so view space is um more or less a standard i would say for doing shading operations and this is the space that i would recommend that you do shading but again you don't have to mathematically all of these spaces and anything else would be just just fine right all you need to make sure is that you everything is defined in the same space now if my right directions are defined in the world space let's say i have a directional light i know it's direction it is this it's given to me already i can take that direction in the world space and using the view transformation and i can bring it to my view space right or alternatively my direction might be defined in the view space to begin with my light direction might be defined in the view space to begin with what does that mean if my light direction is defined in view space that means my light source or my light direction in this case is sort of attached to my camera right because it's defined in the view space that means as i move my camera around it's going to move around with my camera i'm telling you this specifically because this is exactly what's going to happen in our next project next project the light direction that will be given to you will be given in the view space right so we won't have to worry about this transformation the light direction will already be in the view space all good all right so you know what we're going to do everything in the view space let's assume that we're doing shading in the view space even though these other spaces will be valid as well so what do we need to do then for any position that's defined in model space i need to transform it to the view space as well that means i need the model view matrix or model and view matrices separately or just the model view matrix so i can transform those positions to positions in view space okay very very important we will use the model view space now in our previous project we did not need the model use phase model mu transformation we needed model view projection transformation we needed to combine transformation that brings everything all the way to the canonical view volume now we will need this model view transformation separately because this is where we're going to stop to do our shading for transforming our positions all the way from model space to the canonical view volume i can do two hops like go from model space to view space and then add projection to go to the canonical v volume or i can directly go from model space all the way to canonical view volume and go from wall speed all the way to the view space separately and that's exactly what i would recommend that that you you do for the next project uh so you always start with a model space vector you directly transform it to wherever you would like that vector to go to and that vector will have to go to two places the rasterizer will require you to specify the position in the canonical v volume so we're going to need the model view projection matrix for that before doing shading our fragment shader that we're writing will need the positions in the view space so we'll need to do the model view transformation as well okay so for that we will need a model u matrix all right stuff that we already talked about this may seem obvious to you but here's where things get a little tricky things get a little tricky when we start thinking about the surface normal that's defined in our model space and you might think i can just use the model view matrix to transform this this vector over here and that would be fine and that could be fine under certain cases but not always let's see that let's say that m is our model view matrix m is not the model matrix m is the model view matrix all right and it is the model view transformation brings a something from a vector from model space all the way to b space do we need to normalize the vector again uh yes after you do transformation depending on the types of transformation your vector may or may not be a normal vector still it really depends on the transformation if this transformation involves any kind of scale then then your vector lengths are going to be different also translation but they're there ways to handle translation we'll we'll talk about that in just a little bit so our problem stating transformation problem we take mole space vectors and we move them to view space so for positions that's fairly easy to do i take position multiply by my transformation matrix m and get my camera space position in this case m is the model u transformation matrix all right so but what do i do with my normals can i do the same thing can i not do the same thing what happens if i do the same thing let's talk about that so now it's time we're going this way from model space to view space so for positions it's fine if i have a sphere that's defined in model space i can just transform in view space and i can apply any kind of transformation even transformation that involves non-uniform scale and that's fine this transformation is going to bring everything there but when i have vertex normals defined now here's the tricky part here's the part i need you to pay attention i have my vertex normals defined in my model space i can transform them to my view space and if this this is just including rotation and stuff it's fine rotations are okay but but if this transformation involves any kind of non-uniform scale any kind of non-uniform scale here's what happens are you paying attention now look at these look at these normals and how they will differ you see they are deforming like this like you would expect i've squished everything down right so these normals are squished down too and this one is fine uh that one is fine this one oh this is not normal to the surface anymore you see that this vector is no longer perpendicular to the surface uh this is no longer the surface normal and just because i applied non-uniform scale now i'm just gonna show you the original original uh vertex normals before this transformation on the surface so if i did not transform if i just ignore the scale non-uniform scale this is where this is what my surface normals looked like but after the transformation this is what they look like so and it kind of looks like they deformed in the wrong direction right my surface is getting flatter so i would expect these surface normals to go this way right but instead they're going in the opposite way so from here they went this way but actually what i expected them to do is from this way i i want them to rotate in the opposite way and turns out okay yeah maybe you're freaking out about the length of these vectors don't worry about it we're going to normalize them right so their directions are what's important their magnitudes are not important at all this is what i want so it looks like what i need is the inverse of my non-uniform scale so i need to apply the inverse of my non-uniform scale to my to my surface normals rotations are fine but somehow i need to apply the inverse of my surface normals or my transformation to my surface normals so i'm i'm hoping that you all are satisfied with this explanation and you're happy that to apply the inverse of the surface normal but for those skeptics around you among you i'm hoping that there are skeptics among you we need you guys don't lose your skepticism don't believe me because i told you that inverse of the scale transformation on me from scale will just give us what we need we know that regular scale is not what we need but we don't we're not sure that the inverse of that is exactly what we need we know that we kind of need to do something like the opposite of that but you know what i'm going to show you that we actually need the inverse that inverse is going to be the right solution right so let's let's do this all right so this is just to explain to you that the inverse is actually going to give you exactly what we need now i have a surface here and i have a surface normal that normally is defined by this n so i have n x and y and z components now i'm going to pick a vector on this surface at that point so it's a it's a vector so it's a tangent vector at that point on the surface so because this is a tangent vector and this is a normal vector they have to be perpendicular to each other right right so if i take the dot product of two perpendicular vectors i get what i get zero right that's sort of like the definition of two vectors being perpendicular so the dot product of these two vectors that we just multiply and add each components that would give me zero right just the definition of vectors being perpendicular now let's say that i apply some transformation to my surface vector here that transformation included we know that rotations are fine we're just applying non-uniform scale and that non-uniform scale is scaling these components by different factors i call an abc it doesn't matter they're just possibly different values okay so this is my transform vector t prime and i'm telling you that if i apply the inverse of this transformation inverse of this scale to my surface normal i'm going to get the right result so if i apply the inverse of this this is the vector that i'm going to be getting right i'm just dividing by the same scale and factors instead of multiplying by them so if this is the right answer then these two vectors have to be perpendicular to each other as well right so let's see if that works if i take the dot product of these two vectors this is what i get right and as you can see here these a's will cancel over here the b's will cancel over here and c's will cancel out over here right so i'm going to get this which is the same as the equation about so it's zero that means yes these two vectors are perpendicular now this does not say anything about this n prime vector being a unit vector still it it isn't it probably isn't because i apply some non-uniform scale so if i need a unit vector i'm going to have to normalize it but when i apply the inverse of my scale inverse of my magniform scale i am getting a vector that is guaranteed to be perpendicular that means guaranteed to be along the surface normal direction okay so that's the important bit so going back to what we were trying to do for positions i'm applying our regular transformation matrix for normals i can't just blindly do that right i kind of need to i need to figure out what to do with them so we're transforming positions using something and we're uh transforming normals differently but we're transforming all of them so remember we've been using four by four matrices for transforming everything using homogeneous coordinates since now normals are direction vectors positions are position vectors so with position vectors remember we're using one down here for directions we talked about this where we were going to use zero and what this does is this eliminates the translation component of our transformation matrix right so because these are direction vectors we don't want them to translate and just putting 0 here allowed us to achieve that that was very very easy it was just a uniform trans unified transformation so if this matrix m here does not include any non-uniform scale it can involve rotation translation and uniform scale but not non-uniform scale then everything is fine then i can just use m over here and i'll be i'll be okay right that will be just fine i won't have any problems in fact because this fourth coordinate here is zero and the translation components of this matrix m is going to disappear i might as well just use a three by three part of this four by four matrix i can just take the i could just get rid of the last column and last row and use the first three columns the first three three rows of this matrix i'm going to call this m three by three okay i can just use the m three by three and in that case i don't need the fourth chord material obviously i can just use it as a three 3d vector right so i can transform my surface normals like this if and if my transformation here does not include any non-uniform scale if there's any non-uniform scale you know these normals are going to be deforming in the wrong direction in which case i'm going to have to use some other matrix and this matrix is going to be a 3x3 matrix because i'm not going to have translation okay i don't i don't need translation this is a direction vector so 3 by 3 is good enough no need for homogeneous coordinates here and if there's no non-uniform scale then of course this 3 by three component of the original transformation was good enough now let's see if we can get what we want out of this now remember remember when we talked about matrices i told you something that i found interesting i'm not sure if you found it interesting back then but i'm hoping that today you will find this mathematical relationship interesting that is i can take any three by three matrix and i can represent this i can decompose it into these three matrices one rotation followed by a scale followed by another rotation all right so i can take any three by three matrix and i can convert it into these three matrices this is actually called a singular value decomposition so i i can do that i can actually find a rotation a scale and another rotation that would correspond to this transformation whatever that is now i'm not saying that this is how this three by three matrix was generated it can be generated in whatever way i do not care i can decompose it into a multiplication of these three matrices the nice thing here is that rotation is good scale is potentially giving us trouble the other rotation is good too so what i want for my normals my normal transformation what i want is this guy i want the same rotation followed by the inverse of my scale because my scale may may be non-uniform followed by the same rotation so this is what i want this is what i got all right that's the problem that we're going to solve are you ready this is going to be this is going to be fun all right so how do i start with this matrix and i get this one very easy if i do single value decomposition and i get these three three matrices it's trivial to invert a scale matrix you just have you know it's a diagonal matrix right only have diagonal values you just invert the diagonal values individually everything is one over whatever it was and i'm done i have this inverse of this and i'm done the problem there is the singular value decomposition is not a cheap operation also it's um yeah it's numerically error-prone and whatnot i don't really want to do that and i also don't care about having these individual matrices i just want this guy all right i want this i don't care about these individual components so can we do something else here's the trick ready for the trick here's the trick now what if i took my this m3 by three and i inverted why if i take the inverse of this matrix what would i get the inverse of this matrix would be equivalent to applying these transformations the inverse of these transformations in the reverse order right so it will be inverse of this one this rotation first and the inverse of scale and then inverse of of this rotation so it would be it would be this matrix right so you first apply the inverse of the second rotation inverse of the scale and inverse of the first rotation right that would be its inverse now here's here's where things get fun ready if i take the transpose of this transpose right you take a matrix and you you flip it around the ground is diagonal that's the transpose operation if i take the transpose of it now pay attention let's see let's see what happens now we talked about this i'm not sure if you all remember i told you that the transpose of a rotation matrix is its inverse so inverse of rotation matrix is its transpose and in 2d that's sort of easy to understand you rotate in one direction if you rotate in the other direction you get the transpose matrix because theta minus theta if you think about it you get the transpose matrix right so instead of writing these r r two inverse and r one inverse i'm just gonna write them as r two transpose and r one transpose because it's the same thing right so transpose of a transpose is the original original matrix right so if i apply transpose twice on a matrix i get the original matrix so cancel out so if i apply transpose to this uh i'm gonna do it in pieces so then of course they're gonna swap order because of the transpose operation so this comes r2 comes out of transpose by having is transpose removed so auto transpose transpose will be r2 and over here they're gonna swap places as well and r1 transpose transpose will be r1 itself so what i'm left with is is this guy uh what is the transpose of a scale matrix the scale matrix is a diagonal matrix all of its off diagonal values are zeros right so the transpose of a scale matrix is itself because we only have values along the diagonal and transpose operation does not change that so the transpose of this inverse scale is going to be inverse scale hmm so what happened here what did i get you see you're gonna see i wanted this i got this right wow magic and all of a sudden i have what i want so apparently what i wanted was this i wanted the inverse transpose of my the three by three component of my transformation my part of my transformation that did not include translation that's all i needed so i apply these this to my normals and i'm done so i could just tell you there's magic you just do this and it works but instead i decided to actually explain why this works and all this is doing is that it's um all of the all this is doing is inverse transpose is keeping the rotations exactly as they are and it's only inverting the the uh the scale component of the transformation right so that's that's what where the magic is with this inverse transpose operation so this is this is what we're going to do for transforming our normals and when you apply inverse transpose of your transformation matrix that the three by three component of your transformation matrix it's safe to transform your normals even when you have non-uniform scale in your transformation you're going to get the right transformations you're going to get the right normals in your view space or whichever space you're transforming them into and everything will work out just fine and then you can do shading so from this m matrix animation is a four by four matrix right so i'm just going to use the first three columns in first three rows i'm gonna eliminate the last column that involves the translation component and i'm gonna get rid of the last row which does nothing it's just made out of zeros anyway for regular transformations right so um i'm gonna get rid of the last last row and the last column that will give me the three by three components of uh my transformation and i'm gonna do the inverse transpose here right so this is exactly what we're going to do for transforming our normals and once we transform our normals we have everything we need to do shading or do we oh dude [Music] what do i need for shade i need uh surface normal okay there it is i need the light direction let's say that it's given to me in camera space or view space so i have that as well what else do i need i need a view direction what is the view direction all right i need a new direction to compute the the specular component right in camera space my position is the inverse of my view direction that's the nice thing about camera space uh if you were to you can compute the view direction in other spaces as well you just need to transform your camera position to that space but in camera space the camera is at the origin so any vector from the origin to a position that would be the inverse of your view direction from that point towards the camera so you just take the p prime and normalize it that will give you sorry inverse of p prime minus negative p prime normalized that will give you the view direction all right so this is what i plan to talk about for today brain hurts uh yeah so today was a little more matty than usual but um this was not more this was not very complicated math it was just matrices and and vectors uh next time we're going to talk about integrals uh next time it's going to be more fun there's going to be more math but we're going to talk about things at a higher level instead of talking about them at a lower level like this so probably it's going to be a little bit uh less painful even though we're going to be using we're going to be using a more advanced math next time all right we're just out of time now so we can end it here thank you all for coming all out see you all next time thank you bye
Info
Channel: Cem Yuksel
Views: 1,429
Rating: undefined out of 5
Keywords:
Id: Q_TYQvZS6WE
Channel Id: undefined
Length: 55min 57sec (3357 seconds)
Published: Thu Jun 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.