as a maths teacher from germany i have to admit i wish i could explain like u do. i dont remember understanding a new topic that quick. thank you, great work! :)
@OGLDEV3 жыл бұрын
Thanks a lot!
@kerbalcadet27833 жыл бұрын
Amazing that this has so few views - I couldn't find a good explanation of projection matrices until I stumbled on this. Great work!
@OGLDEV3 жыл бұрын
Thanks a lot for your feedback!
@hodayfa000h2 ай бұрын
i took a break for a while, came back somehow understanding ALL of what i learned, you are a great teacher! thank you for the tutorial, this made it quite clear for me, by far one of the coolest videos in the series! like you answered the EXACT questions i had.
@OGLDEV2 ай бұрын
Thanks!
@赖宏涛3 жыл бұрын
Best opengl tutorial I've ever seen!
@OGLDEV3 жыл бұрын
Thank you!
@ReversedFootage Жыл бұрын
Wow this is the most thorough video about the subject I've seen so far, it's really good, you explain things very well. Thank you
@OGLDEV Жыл бұрын
You're very welcome!
@Bunny99s10 ай бұрын
I know you want to keep it simple and on point. Though the "perspective divide" is generally called the homogeneous divide. We use homogeneous coordinates because "normal" cartesian coordinates and matrix multiplication can't represent translation or projection. The idea of homogeneous coordinates is that we simply go one dimension higher and offset our desired coordinate system by one unit in that new dimension. That's why points in space have "w" set to 1. So the whole 3d coordinate space simply lives at a 3d "sliver" of 4d space, at a distance of 1 from the origin. Matrix multiplications still can't do translations, however we can do a shear operation in 4d space. Since our 3d space that we are interested in is at distance 1, when applying a shear, our "flat" 3d space within the 4d space would appear to have shifted. So we actually want to keep our coordinate space at a "w" distance of 1 at all times. So if w contains a value different from 1, we "normalize" our coordinates by dividing the whole vector by w. Yes, this division is hardcoded into the rendering pipeline. This simple convention allows us all sorts of things to represent with just matrices and since you can combine matrices this is super efficient for more complex setups. If you have trouble understanding 4d coordinates, homogeneous coordinates also work for 2d coordinates in the same way. When you have 2d coordinates in a 2d plane, we simply offset them by 1 in the z direction. By applying a shear, we can move the 2d coordinate system around. This concept also applies to higher dimensions. So when you want to represent actual 4d space, we can use 5x5 matrices to do the translation and projection down into 3d space.
@OGLDEV10 ай бұрын
Thanks for the comment. I actually think that 'perspective division' is a bit more common and this is how it's called in the OpneGL spec, but both terms are fine.
@shailmurtaza90824 ай бұрын
Now a lot of things make sense. Please tell me from where I can learn more about these things. I am working on 3D renderer and it is very difficult for me.
@coltsmith64679 ай бұрын
I'm a bit late to the party, but this video finally made this stuff click for me - thank you! I've been reading textbooks and trying to understand for a couple weeks now, and your explanation made it finally made sense. Thank you for making these learning resources :)
@OGLDEV9 ай бұрын
Glad it was helpful!
@weifang22183 жыл бұрын
Best OpenGl tutorials that I have seen. Thanks !!!
@OGLDEV3 жыл бұрын
Thank you!
@sripradpotukuchi94153 жыл бұрын
My mind is _blown!_ Your way of explanation is truly amazing. It was very intuitive and everything just clicked into place for me. Thank you so much for putting out this content. Instantly subscribed!
@OGLDEV3 жыл бұрын
Glad it was helpful!
@Forka1372 жыл бұрын
I'm learning OpenGL using Joey de Vries book and this video was a great complement for me to really understand what was going on. I will probably watch the whole series, thank you!
@OGLDEV2 жыл бұрын
Thanks and good luck!
@flocela2 ай бұрын
Thank you for the clarity and precise word choices.
@OGLDEV2 ай бұрын
You're welcome :-)
@Mochu_s_Junkyard Жыл бұрын
My man thank you very for the tutorial you are the real MVP and not those matrices I'm learning opengl on my own with no computer science background and this small series in cameras has been the real deal for me, keep it up, try to be a little more dynamic and I am sure that the channel will go very far :)
@OGLDEV Жыл бұрын
Thanks! I wish I was able to publish more frequently but it's very difficult with a full time job and a few kids. Maybe in the future I'll be able to work on this full time and publish every week. Kind of a chicken and an egg problem...
@BackUp-cz6zn2 жыл бұрын
great video saved me from depression i was llooking like every where for two days and a 20 minute video made it look easy it would be great if you did a video about the whole row-major vs column-major difference and how do i know what matrix to multiply first. like i have no idea what happens when i multiply the translation first but it just doesnt work unless i re order the projection matrix to look like a column-major one
@OGLDEV2 жыл бұрын
Thanks! I'll add the row/column major topic to the TODO list. Regarding matrix multiplication order - please check out my video kzbin.info/www/bejne/nZXPpml7erygaKs. I think it should address your question.
@Sneha_Negi3 жыл бұрын
your videos are a great help.. just one query... i have applied your same code your code works fine... but then i applied your code for a mesh data given. without projection matrix it works fine like it should work..but then i apply the projection matrix the screen goes black..i applied different angles wide and small..for angle 180 i can see something appearing in my screen...but seems like instead of getting a solid mesh i see lines coming out towards screens..seems like from a far depth for all other angles i barely see something on screen..i also tried changing the depth using the translation matrix nothing worked... any suggestion would be great help...
@OGLDEV3 жыл бұрын
What is the range of the vertices of the problematic range on all the axis? I don't think you should work with an angle of 180 because it seems the frustum will be weird in this case (not a pyramid). Stay with 45 degrees and check the z range of the vertices. Perhaps they are being clipped on the near and far z.
@Sneha_Negi3 жыл бұрын
@@OGLDEV thank you very much for the reply.. my range of the given mesh data for x(-18,20) for y(-20,25) for z(-32,32) these are approximations.. what i did is i scaled my points to 0.01 so that they become inside -1,1 and then tried applying your projection matrix... also i watched many lecture they say camera is at origin then how are we able to see positive z values since camera facing towards neg z axis... so much confusion can you provide the links from where i can have better understanding of how to modify the protection matrix according to ones need...
@OGLDEV3 жыл бұрын
You are not supposed to scale the positions by 0.01 and then apply projection because you may end up with degenerate triangles. Perspective projection should bring the visible vertices into the [-1,1] range. I take a somewhat different path from other sources and I teach perspective projection before the camera transform so when you are on tutorial 11/12 you need to ignore the camera which is explained in tutorials 13-15. The camera must indeed be at the origin when you apply pers proj but since we have no notion of a camera at this point you can assume that it is there by default. The other lecture probably assume that you are using a right handed coordinate system with the camera looking down along the -z. This is indeed confusing. I'm using a left handed coordinate system. From the top of my head I think you just need to replace 1 in location [4,3] with -1 to switch from left hand to right hand. A couple of resources that may help you here are www.songho.ca/opengl/gl_projectionmatrix.html and www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix
@Sneha_Negi3 жыл бұрын
@@OGLDEV ohh thank you very much sir... that clears my doubt...because the libraries that already are there like glm library i think they use right hand rule... and reading from different different resources had confused my understanding..thank you that cleared...my doubt
@OGLDEV3 жыл бұрын
You can choose the type of the coordinate system in glm using glm::frustumLH and glm::frustumRH
@ryarsh13 жыл бұрын
You are a _fantastic_ teacher, thank you so much for sharing your knowledge! all the best,
@OGLDEV3 жыл бұрын
Thank you Jack!
@thormind73382 жыл бұрын
Wow, thank you for this video. Everything is explained so clearly!
@OGLDEV2 жыл бұрын
Your'e welcome :-)
@Glyn-Leine3 жыл бұрын
actually really interesting and helpful! am looking forward to the next video!
@OGLDEV3 жыл бұрын
Thank you! It will be ready in a few days.
@jamesbeilby3 жыл бұрын
Thanks! Great explanation from first principles
@OGLDEV3 жыл бұрын
You're welcome :-)
@Amine-gz7gq2 күн бұрын
Very nice video ! Greetings from Tunisia.
@OGLDEV2 күн бұрын
Thank you so much!
@EgorChebotarev Жыл бұрын
good explanation with 2d and 3d definition
@OGLDEV Жыл бұрын
Thanks :-)
@lilit-v2i10 ай бұрын
Hello Etay, thanks for the series. Could you please explain, why in projected point we set z_p to be equal to 1 instead of d? At time 9:52 you pointed out that z_p = d, but then from the final matrix 12:35 it is easy to see that z_p will be equal to 1 after division by z.
@OGLDEV10 ай бұрын
The projection matrix is composed of two completely independent transformations - projection of X&Y for the illusion of 3D and mapping of Z to -1 to 1 between the near and far Z. In this video we handle the first one and in the next one we handle the second. So right now the final matrix looks like that because we are still not mapping the Z properly.
@Sneha_Negi3 жыл бұрын
just one more question that is eating up my head.. the near and far plane are always on negative z axis according to right hand convention...and so my screen will clip only what's inside the frustum..my question is ...that what happens to positive z coordinates... as they are not inside our frustum they will not be displayed right? so for taking care of that do we have to translate all our object coordinates in negative z direction so that now our object is in the near and far plane range.
@OGLDEV3 жыл бұрын
The camera is at the origin and it can only point at either the positive or negative half of the coordinate system. So in the right handed the negative Z is in front of you and the negative Z is behind you and must be clipped.
@alexmassy Жыл бұрын
So cool. Love the breakdown. It clarify a lot for me. Thank you !
@OGLDEV Жыл бұрын
Glad it was helpful!
@anthonynolanjannotta46882 жыл бұрын
@6:00 What if the camera is located at the same y value as the objects that are being projected? the lines from the top of each object would just be parallel and they wouldn't be scaled down in the perspective view, right? Or am I more confused than I thought lol. Thanks!
@OGLDEV2 жыл бұрын
The fact that the camera and the objects have the same Y value just means they are at the same "height". Whether or not the objects are visible depends on their relation to the frustum and for that we need to know the target vector.
@anthonynolanjannotta46882 жыл бұрын
@@OGLDEV gotcha, So In my example let’s say the objects are people who are the same size, if I wanted a 3rd person view from the perspective of one of the people, I’d have to set the viewing plane to be above and behind the head of the person, right? If I wanted a 1st person perspective, the viewing plane would be in front of the person, and at the same height(y value) right? So in that case, the farther objects wouldn’t appear smaller, is that right? Thanks!! 🙏
@OGLDEV2 жыл бұрын
With perspective projection the further the object the smaller it looks, same as with regular human vision. Whether this is 1st person or 3rd person view is just a matter of whether the camera is "inside" the character or hovering a bit above.
@ChoudharyRamesh3 жыл бұрын
21:42 , when z will be divided by w it willl became 1 , so how this depth imformation is preserved ?
@OGLDEV3 жыл бұрын
This is explained in part 2 of the perspective projection tutorial (next one on the playlist). In this video I only talk about making X and Y dependent on the distance from the object and the aspect ratio and the normalization of the Z is handled in the next one.
@ChoudharyRamesh3 жыл бұрын
@@OGLDEVThanks for rply. I am doing same but my output is black screen only even after model translated to 3 ,as you did. What I am doing wrong ?
@OGLDEV3 жыл бұрын
Difficult to say, what do you have as the original Z value in the vertex? Does it work without the perspective matrix? Please try to compare your code to my code from the repo. If you can't find the problem you can send it to me and I'll try to find some time to take a look.
@kratostheinevitable39322 жыл бұрын
amazing, explanation.
@OGLDEV2 жыл бұрын
Thank you!
@Sgoose1052 жыл бұрын
very very good lecture better than my prof
@OGLDEV2 жыл бұрын
lol! Thanks :-)
@antoinedevldn3 жыл бұрын
Awesome series!
@OGLDEV3 жыл бұрын
Thanks Antoine!
@chad4094 Жыл бұрын
I wasn't able to get it to work until I used the glm library and used glm::perspective. The matrix glm::perspective creates is very different from your Projection matrix. How did you get yours to work without specifying a far or near clip plane? Why is the Z value on the 4th row 1 instead of -1? Thanks again for your videos, despite my shortcuts I still wouldn't have been able to get as far as I have without them.
@OGLDEV Жыл бұрын
The near/far clip planes are handled in the second part of this video. Once completed, you should find my matrix and the one by GLM almost the same (at least that's what I remember from the last time I checked...). The difference in the sign of Z is due to the right handedness of GLM vs my left handedness.
@aleksandrsstukalovs2 жыл бұрын
Hey, how exactly you've exported vertex coordinates and indices from blender?
@OGLDEV2 жыл бұрын
I usually export to obj from Blender.
@aleksandrsstukalovs2 жыл бұрын
Thanks
@saranh16873 жыл бұрын
this is very nice explanation. you describe everything in detail.is that possible that you go further, and provide tutorials also in gaming, like realtime graphics, game development? or maybe like what thinmatrix did, but he did it in java, not c++.
@OGLDEV3 жыл бұрын
Thanks! The plan for the next few months is to focus on various 3D techniques. From there branching to game development is definitely something that I'm considering.
@saranh16873 жыл бұрын
@@OGLDEV thank you very much,.that would be amazing.cause any game development tutorial i found was with java and opengl.no one was with c++ and opengl.
@OGLDEV3 жыл бұрын
The Cherno has a good series on game engine development in C++ - kzbin.info/aero/PLlrATfBNZ98dC-V-N3m0Go4deliWHPFwT
@saranh16873 жыл бұрын
@@OGLDEV thank you very much for the adivce,i will follow it later,but i could not see any visual results in his tutorials,thats not im not following it yet.like in thimatrix,there will be a game,an environment,something visual,for him it was some cubes...but i will go for it.thanx
@OGLDEV3 жыл бұрын
@Sara nh I watched the first few episodes of his game engine series and they are mostly about setting up all the infra structure of the engine. I think that is expected because there is a lot of infra structure in such sw. I haven't followed the entire series but I noticed lately that he started doing effects like bloom, lighting, etc so I guess there is something to see. I hope I'll be able to find time to watch it...
@Sneha_Negi3 жыл бұрын
or can you just tell me...what should we take care of while changing the angle and the depth in translation matrix
@OGLDEV3 жыл бұрын
Changing the angle causes a zoom in/out effect, so you may want to compensate that in the Z of the translation matrix so that the object will not be too close or too far away. I have a feeling the problem is with the Z. Since you can see the mesh without projection the X and Y are probably already in the -1 to 1 range.
@Sneha_Negi3 жыл бұрын
also one more qsn you haven't use depth buffer...and enable depth tests.. when i tried doing it..your code showed black screen what could be the reason for that?
@OGLDEV3 жыл бұрын
I recently realized that I may have delayed the depth test too long so I've added it to the next tutorial which will be out in a few days. It shouldn't be a problem as long as you use simple models like the cube because back face culling will take care of the depth test. Which tutorial showed a black screen with the depth test?
@Sneha_Negi3 жыл бұрын
@@OGLDEV your code for this tutorial which i found on github..
@OGLDEV3 жыл бұрын
OK, so it probably has something to do with the fact that tutorial 11 is missing the Z transformation which is explained in tutorial 12. I guess when the depth test is enabled most of the vertices end up being clipped. Try to enable the depth test in tutorial 12.
@abdullahazam-yf3qw3 ай бұрын
hi Mr OGL, hv u implement abstract algebra in opengl?
@OGLDEV3 ай бұрын
What do you mean by 'abstract algebra'? sets, vector spaces, fields, etc? The purpose of OpenGL is visualization. If you can visualize something you can do it with OpenGL.
@abdullahazam-yf3qw2 ай бұрын
@@OGLDEV Nice, Thanks!
@OGLDEV2 ай бұрын
@@abdullahazam-yf3qw You're welcome :-)
@laurentbedief21993 жыл бұрын
Super tuto !!!!!! j'ai hâte de voir le prochain . Merci à toi !!! J'oubliais , tu parles bien le français ?
@OGLDEV3 жыл бұрын
Merci! Je parle français uniquement avec google translate ;-)
@laurentbedief21993 жыл бұрын
@@OGLDEV ok ;-) thanks
@laurentbedief21993 жыл бұрын
@MO 9845 don't worry, i can speak to @oglDev in english, i really thought @oglDev speaks french ;-)
@foebelboo Жыл бұрын
thanx ogldev!!
@OGLDEV Жыл бұрын
You're welcome!
@leoreleases2 жыл бұрын
This tutorial is lovely amazing :D
@OGLDEV2 жыл бұрын
Thank you!
@MargarethaSaputri-o3b Жыл бұрын
Very easy to understand ^o^
@OGLDEV Жыл бұрын
Glad it helped!
@omlachake2551 Жыл бұрын
Hello Etay, I have been following the series sequentially and you have been clearing the whole concepts for me, Thanks! Until this video, I faced no issues at all, however, I can now see nothing on my screen, complete Black window, no errors, no issues at all. I changed many things in the Transformation matrices, but still got no results. I am using Visual Studio 2022 on Windows 10. Please would you help me out? I am crying my brains out why this doesn't work. Here is a more detailed Problem : When all three matrices are just identity matrices, I can see the the square / cube. I add Rotation, along the z axis, it starts rotating, but looks weird. I add Translation to Z, it starts moving left and right. I change Projection[0][0] and Projection[1][0] to 'f', it still acts like the identity Projection Matrix. But, when I set the Projection[3][3] to zero. It all goes blank.
@OGLDEV Жыл бұрын
Are rotation and translation working as expected without projection? Note that the order of transformations matters. I have a video about that. Regarding projection - try to compare your matrix to mine. What is the FOV that you are using? Note that Projection[1][0] should be zero while [1][1] should be f.
@omlachake2551 Жыл бұрын
@@OGLDEV Rotation Works, the Translation, instead of moving the camera far away, the complete Cube moves sideways. But atleast I can see something on the screen. With Projection, its black. The order is also correct. Referred to your previous videos. I have now copied the exact code you have, except, I am using glm and not your library. All the matrices are exact as same as you. The FOV is 90 degrees, before converting it to radians. I do use glm::radians to convert.
@OGLDEV Жыл бұрын
I think it's critical to get the translation along Z working correctly because the projection works by dividing X and Y by Z. If the object moves sideways when Z is changed then it may break everything. Start with a basic sample that renders a single triangle (bottom left corner of the screen, top center and bottom right). The camera should be at the origin and facing the Z axis. Apply the projection and start moving the triangle along Z. The simple projection matrix in this video should make it smaller as it moves away. There is no near/far Z at this point which is explained in the next video. Note that without near/far Z mapping the valid range for Z is -1.0 to 1.0 so make sure not to go outside of this range. You can see that in my sample the Z is +0.5 or -0.5 for the vertices of the cube. If you change this to +1.5 and -1.5 you will also get a blank screen every now and then.
@thelifelearner56188 ай бұрын
can i get this code completely in pdf form
@OGLDEV8 ай бұрын
Have you tried using one of the online tools to convert code to pdf?
@matthewmorrison20713 жыл бұрын
could you get away with just dividing by Z
@OGLDEV3 жыл бұрын
Division by Z is indeed the core of projection. The other parts of the matrix are for field of view, aspect ratio and Z transformation.
@somebody_28372 ай бұрын
I keep getting perspective distortion, how do i fix this?
@OGLDEV2 ай бұрын
What's the nature of the distortion? What's the contents of the projection matrix?
@somebody_28372 ай бұрын
@OGLDEV basically, when the object goes to the side of the viewport, it gets heavily distorted, i tried reducing the fov but that doesn't seem to work.
@OGLDEV2 ай бұрын
OK, and how does the projection matrix looks like? Try using a square window do disable interference of the aspect ratio.
@somebody_2837Ай бұрын
@@OGLDEV sorry for being late to reply, but the projection matrix looks like this: 1/ (aspect * tan(halfFOV)), 0, 0, 0 0, 1 / (tan(halfFOV)), 0, 0, 0, -((far_z + near_z) / (far_z + near_z)), -((2*far_z*near_z)/(far_z - near_z)), 0 0, 0, -1, 0 The fov is 45 degrees, while the near_z is 1.0f and the far_z is 1000.0f. Sorry for the congested typing.
@somebody_2837Ай бұрын
@@OGLDEV sorry to bother you again, but have you a solution yet
@kryptoid25683 жыл бұрын
You look like Electroboom if he was more interested in 3D graphics GPUboom lol Other than that, I find this video very helpful, as perspective projection is the hardest thing for me to setup.
@OGLDEV3 жыл бұрын
And we have almost the same number of subscribers ;-)
@NewThingsAk2 жыл бұрын
how to install ogldev_math_3d.h
@OGLDEV2 жыл бұрын
It's in the git repo github.com/emeiri/ogldev In the Include directory.