Model View Projection Matrices in OpenGL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is China welcome back to my OpenGL series so last time we talked about projection matrices and we basically over the last few episodes have gotten a math library into our application here that we're building in OpenGL so today what I want to talk about is I guess the next step after the projection matrix so we have projection matrices and specifically we implemented like an orthographic projection for our view if we just launch a program from the last time so that we can see what we actually had we managed to draw this channelago kind of in the in a different position not in the center and at the correct aspect ratio by using an orthographic projection which we provided over here in this line of code so that's kind of one face of it the projection matrix but there's also two other matrices are fairly important when we're dealing with the transformation of geometry when we render it in a scene so the first being projection but then we also have something called a view matrix and we have something called a model matrix as well and that all of that gets multiplied together with our with each vertex position to give us kind of the final position of the vertex in our actual screen like on inside our window or in our kind of 2d projection that is on our computer screen so again lots of theory behind this which I probably will cover at some point but what I want to do today is just kind of introduce the concept and actually show you guys how you can access it and what it's used for and then you can kind of play around with that and hopefully learn it that way because that's probably a better way of learning it they're just studying a lot of theory and not doing anything ok so let's get straight into it model view projection MVP or in you know if you reverse it PVM i guess but usually it's set as MVP and you may have heard that term as well MVP or MVP matrix so that stands for model view projection matrix and that is kind of the transformation pipeline that hat that goes through every vertex now I keep saying pipeline but really what I mean is that each one of those letters MVP is a separate four by four matrix and that and all those matrices get multiplied together in that order Model View projection now in OpenGL since we deal with column-major ordering it's actually done in kind of Reverse ish like if we were to write it out in inside our shader code or inside out GLM code in C++ here it would be projection times of U times model whereas like indirect tax in direct3d because they didn't deal with row major order information C's it would actually be MVP Model View projection you'd actually have your vertex positions on the left side of that kind of equation non left side of the equation but that would be kind of the first thing you multiply if you read left to right but as an open jealous reverse so the fact that if MVP doesn't necessarily define the order lots of stuff we could talk about here that we probably don't need to mention but the idea is that we have these three matrices and they kind of define different things for our vertex positions because what happens in the end is we multiplied all the matrices together and we multiplied that matrix that resulting matrix with our vertex position and that vertex position is the vertex position of our geometry which is stored inside our vertex buffer and that kind of all that multiplication transforms that vertex into where it should be on our actual screen on our computer screen that's what this is all for so we have the projection matrix which we talked about last time which was specifically for actually converting this kind of whatever space we choose to work with into normalized device coordinates meaning we're converting from whatever space we're dealing with and whether that be orthographic or perspective into a negative one to one mapping in kind of every axis and x y&z that's what the projection matrix is responsible for definitely check out that video if you happen already the model and the view matrix are something a little bit different the view matrix sometimes called the eye matrix is kind of the view of the camera so it's a way for us to kind of simulate a camera more or less and then be model matrix is a way for us to basically simulate the model matrix so the model being the model that we're actually drawing so the vertex that we're drawing and the matrix specifically is just a transformation of the model so to put this into more simple terms projection aside because that kind of defines like I guess what kind of space we deal with the view matrix is the position the rotation scale whatever of our camera and then the model matrix is the position there's the rotated transform so the the position rotation scale of our actual model the returning of our object so we have the object transform and we have the cameras transform now it's important to understand that at the end of the day these are just mathematical numbers the fact that I've kind of given you this illustration of we have a camera we have this model kind of transformation matrix that's all kind of an illusion in the sense that you don't like you don't have to have a camera you could just I don't know just you could just have your camera in the center at all times and just have the kind of model matrix and that will work as well because really these are just numbers and they all get multiplied together anyway you can have another matrix that's like your secondary camera matrix or whatever there's so many ways you could do this because it doesn't really matter they're just you're multiplying matrices together you have to understand that it's as simple as that so the convention that we kind of deal with is we have a view matrix that is specifically supposed to represent our cameras kind of position and orientation and then we have a model matrix which is supposed to represent our objects transform object being the thing that we're currently drawing because what will happen is we're going to multiply the projection matrix with the camera matrix with the model matrix with the vertex position and we'll take a look at that in a minute but conceptually I just want you to understand that what I'm introducing here are two different matrices the view matrix and the model matrix the view matrix is supposedly supposed to represent our camera position and orientation and the model matrix is supposed to represent our objects transform meaning by transform by transform and I probably will make your videos specifically on this as well teaching OpenGL is hardman okay I'm gonna make a video on that as well but transform specifically means [Music] position rotation scale or translation I should say that's the word I was looking for it's been a long day man translation is translation rotation scale TRS translation meaning the kind of the position I guess of our object the rotation of being the what orientation is kind of rotated in and the scale being kind of how big or small it is doesn't have to be uniform scaling either so my you might be able to change like if it's a cube you could make it a like a rectangular prism or something like that and we'll definitely get into that in the future as well but that is the idea anyway I'm gonna stop talking because I'm feel like I'm just gonna ramble on for another 18 hours let's just get into this and see what we can do with this newfound knowledge so at the moment what we have is kind of our base vertex positions which are over here we've created a vertex a sorry a projection matrix that is an orthographic matrix that is kind of from 0 to 9 16 from 0 to 540 so in other words it's kind of a pixel space projection here because every unit is one pixel here are our vertex positions and those are just inside our vertex buffer and what we're doing here well this result was just kind of me simulating that that multiplication on the CPU side but if you look at what our shader is doing it's also doing that in the sense that is taking our MVP our Model View projection matrix that we're passing in here and multiplying it with position now if we take a look at what MVP actually is here you can see that what we actually passing here is just this project we're just passing in that projection matrix we're not actually won't have any kind of view matrix or model matrix but what we're going to do now is we're going to add a view matrix and what this is going to be is the position of our camera so at this point we need to decide well how do we want to move our camera maybe we want to translate our camera we won't worry about rotation just to give this simple for now let's maybe move our camera out of no hundred units to the left now here's a trick what we're doing here is we're trying to move our camera so let's kind of for a minute for a brief minute because I will have a separate video as for a brave male let's just thinking about what a camera is well okay there's no it's not actually like a camera doesn't extra actually exist is no such thing as a camera so how would we simulate a camera well let's just say we have an object on this phone over here right we have a camera here so if I want to move the camera to the left then the object would move to the right technically speaking so if I can't move the camera to the left then the object kind of moves to the right that's what happens I you probably can't see much and then I probably should just draw a diagram but that's kind of what I'm getting at if you like the cat there's no such thing as a camera in OpenGL all we can really do real really is just move geometry around we can move vertices around so to simulate a camera moving left what we really need to do is move everything to the right so it's kind of the reverse operation here so what I'm going to do here is create a translation matrix except since I want to move everything to the right hundred units what I'm actually going to write here is a vector E where I'm just moving everything negative 100 or such is left because that will kind of be like moving the camera to the right and I'll just fill in zero for the other units here and let's see what else is wrong so I guess what we should really probably do here is actually just create a an identity view matrix which would mean one of these and then translate that matrix by just doing that and I believe that should change it now it takes a Const which is great so we'll actually have to what we could do I guess is probably end up constructing a identity matrix here and then assigning it to view like that all right cool so that's kind of our view matrix so now what we'll do is this MVP thing will actually create a GLM map for MVP matrix which will be that projection times the view now so let's pass that in to MVP here and hit f5 to run our program all right cool and so the result here is that this has just moved a hundred units to the left because what we've done is we've moved our camera to the right and if we move our camera to the right it means that everything else now seemed kind of removed to the left and I've simulated that by just doing the reverse operation here because I'm typing in negative 100 here which essentially ends up moving the object to the left which again is a simulation of moving the camera to the right so that's how that works obviously if you had a camera class you'll kind of this kind of negation you'll automate all that and the API will take care of that for you so you don't have to worry about that but mathematically that's how it works moving the camera to the left is actually just moving all the objects to the right so with this same principle because all we've really done here I mean you can't tell the difference I've called it view matrix but you can't tell the difference between us moving the camera to the left or asked moving the object to the right the same thing so it's again purely a kind of way to visualize how this actually works but mathematically it's just it's all the same it's just numbers so what we'll do now is we'll create this model matrix and I'll again translate the model somewhere so we ended up translating this well the camera we're moving to the right so what I'll do here maybe is move the model up a bit so maybe it will say when we have a 200 to the right and 200 up and so we should it but we should basically end up with our channel logo being closer to this to the to the center of the screen and we'll multiply all these matrices together so projection times view times model hit f5 and you can see now we have the channel logo being moved another you know 200 units up and 200 units to the right okay and that's what we end up with cool so this the resulting matrix here is what our MVP is Model View projection and again the multiplication is kind of in reverse order that's to do with the fact the memory layout of all the numbers that are there in column major you can kind of think of reading matrix multiplication in OpenGL as right-to-left so we multiply model with viewed with projection that's just how it ends up working out because of the way that we've laid out our memory because of the way that OpenGL actually expects it in the in the GPU and also the way the GLM handles it for us because of that kind of specification by OpenGL but anyway that's what you see here three matrices multiplied together which create the transformation for our vert for our actual vertices that is very simple camera multiply it with the transform of our object multiplied with that projection which converts everything into normalized device coordinates that is basically all the matrix multiplication you need to know if you're worried about positioning objects in a 3d or 2d world that's it that's all there is to know obviously you can do more than just translation I kind of just showed you with simple example now what I want to do next time because we've kind of touched on all this kind of model your projection stuff in this episode which is awesome next time I want to actually start introducing a perspective projection and maybe get into some 3d stuff now because it's going to be really important for us to have some kind of way of controlling these parameters live because I can't keep closing my application changing the code recompiling it and updating it that's gonna be really painful and really hard for me to demonstrate anything like that what I'm actually going to do is probably integrate I am GUI which is a kind of a UI framework really easy to use UI kind of layer that we can use for actually kind of having UI controls as part of our our application which we can use to modify variables while our application is running it's kind of like a debug layer really easy kind of UI to use so we'll probably integrate that next time but other than that we're just gonna progress keep going with this kind of math stuff and hopefully get into perspective projections soon and think about how we can render real-world kind of objects on the screen whether that be 3d or 2d we might go back and forth between 3d and 2d you haven't decided yet but that is kind of the gist of this let me know what you guys thought of this whole movie projection thing I am going to probably end up making more detailed videos about each concept but I kind of like to make an overall video discovery just introducing the topic first because this is probably enough information for all of you to actually start doing this yourself and play around with and that's gonna be probably the best way to learn anyway I hope you guys enjoy this video if you did you hit that like button you can go ahead and help support the series by going patreon icon 4/2 channel huge thank you as always to all the wealthy patrons who make this series possible it wouldn't be here without you I will see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 51,363
Rating: 4.9168153 out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, mvp, model view projection, model matrix, projection matrix, model view projection matrix, shader, graphics
Id: x_Ph2cuEWrE
Channel Id: undefined
Length: 15min 53sec (953 seconds)
Published: Wed Apr 04 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.