Perspective Projection - Part 1 // OpenGL Tutorial #11

Поділитися
Вставка
  • Опубліковано 16 лис 2024

КОМЕНТАРІ • 118

  • @Jdkdbxbd6272
    @Jdkdbxbd6272 2 роки тому +16

    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! :)

    • @OGLDEV
      @OGLDEV  2 роки тому

      Thanks a lot!

  • @hodayfa000h
    @hodayfa000h 21 день тому

    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.

    • @OGLDEV
      @OGLDEV  20 днів тому +1

      Thanks!

  • @Bunny99s
    @Bunny99s 8 місяців тому +2

    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.

    • @OGLDEV
      @OGLDEV  8 місяців тому

      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.

    • @shailmurtaza9082
      @shailmurtaza9082 2 місяці тому

      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.

  • @kerbalcadet2783
    @kerbalcadet2783 3 роки тому +9

    Amazing that this has so few views - I couldn't find a good explanation of projection matrices until I stumbled on this. Great work!

    • @OGLDEV
      @OGLDEV  3 роки тому

      Thanks a lot for your feedback!

  • @flocela
    @flocela 15 днів тому

    Thank you for the clarity and precise word choices.

    • @OGLDEV
      @OGLDEV  15 днів тому

      You're welcome :-)

  • @BackUp-cz6zn
    @BackUp-cz6zn 2 роки тому +2

    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

    • @OGLDEV
      @OGLDEV  2 роки тому +1

      Thanks! I'll add the row/column major topic to the TODO list. Regarding matrix multiplication order - please check out my video ua-cam.com/video/gdlu6EEWm2I/v-deo.html. I think it should address your question.

  • @coltsmith6467
    @coltsmith6467 8 місяців тому

    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 :)

    • @OGLDEV
      @OGLDEV  8 місяців тому

      Glad it was helpful!

  • @ReversedFootage
    @ReversedFootage Рік тому +1

    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
      @OGLDEV  Рік тому

      You're very welcome!

  • @赖宏涛
    @赖宏涛 3 роки тому +1

    Best opengl tutorial I've ever seen!

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      Thank you!

  • @Forka137
    @Forka137 2 роки тому +1

    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!

    • @OGLDEV
      @OGLDEV  2 роки тому

      Thanks and good luck!

  • @weifang2218
    @weifang2218 2 роки тому +1

    Best OpenGl tutorials that I have seen. Thanks !!!

    • @OGLDEV
      @OGLDEV  2 роки тому

      Thank you!

  • @sripradpotukuchi9415
    @sripradpotukuchi9415 2 роки тому +3

    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!

    • @OGLDEV
      @OGLDEV  2 роки тому +1

      Glad it was helpful!

  • @EgorChebotarev
    @EgorChebotarev Рік тому

    good explanation with 2d and 3d definition

  • @jamesbeilby
    @jamesbeilby 3 роки тому +1

    Thanks! Great explanation from first principles

    • @OGLDEV
      @OGLDEV  3 роки тому

      You're welcome :-)

  • @Mochu_s_Junkyard
    @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
      @OGLDEV  Рік тому +1

      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...

  • @ryarsh1
    @ryarsh1 3 роки тому +1

    You are a _fantastic_ teacher, thank you so much for sharing your knowledge!
    all the best,

    • @OGLDEV
      @OGLDEV  3 роки тому

      Thank you Jack!

  • @Sgoose105
    @Sgoose105 2 роки тому

    very very good lecture
    better than my prof

    • @OGLDEV
      @OGLDEV  2 роки тому

      lol! Thanks :-)

  • @thormind7338
    @thormind7338 2 роки тому +1

    Wow, thank you for this video. Everything is explained so clearly!

    • @OGLDEV
      @OGLDEV  2 роки тому

      Your'e welcome :-)

  • @kratostheinevitable3932
    @kratostheinevitable3932 Рік тому +1

    amazing, explanation.

  • @Glyn-Leine
    @Glyn-Leine 3 роки тому +1

    actually really interesting and helpful! am looking forward to the next video!

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      Thank you! It will be ready in a few days.

  • @Sneha_Negi
    @Sneha_Negi 3 роки тому +4

    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...

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      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_Negi
      @Sneha_Negi 3 роки тому

      @@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...

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      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_Negi
      @Sneha_Negi 3 роки тому

      @@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

    • @OGLDEV
      @OGLDEV  3 роки тому

      You can choose the type of the coordinate system in glm using glm::frustumLH and glm::frustumRH

  • @alexmassy
    @alexmassy Рік тому

    So cool. Love the breakdown. It clarify a lot for me. Thank you !

    • @OGLDEV
      @OGLDEV  Рік тому

      Glad it was helpful!

  • @chad4094
    @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
      @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.

  • @antoinedevldn
    @antoinedevldn 3 роки тому +1

    Awesome series!

    • @OGLDEV
      @OGLDEV  3 роки тому

      Thanks Antoine!

  • @laurentbedief2199
    @laurentbedief2199 3 роки тому +1

    Super tuto !!!!!! j'ai hâte de voir le prochain . Merci à toi !!! J'oubliais , tu parles bien le français ?

    • @OGLDEV
      @OGLDEV  3 роки тому

      Merci! Je parle français uniquement avec google translate ;-)

    • @laurentbedief2199
      @laurentbedief2199 3 роки тому

      @@OGLDEV ok ;-) thanks

    • @laurentbedief2199
      @laurentbedief2199 3 роки тому

      @MO 9845 don't worry, i can speak to @oglDev in english, i really thought @oglDev speaks french ;-)

  • @saranh1687
    @saranh1687 3 роки тому +1

    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++.

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      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.

    • @saranh1687
      @saranh1687 3 роки тому

      @@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.

    • @OGLDEV
      @OGLDEV  3 роки тому

      The Cherno has a good series on game engine development in C++ - ua-cam.com/play/PLlrATfBNZ98dC-V-N3m0Go4deliWHPFwT.html

    • @saranh1687
      @saranh1687 3 роки тому

      @@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

    • @OGLDEV
      @OGLDEV  3 роки тому

      @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...

  • @foebelboo
    @foebelboo Рік тому

    thanx ogldev!!

    • @OGLDEV
      @OGLDEV  Рік тому

      You're welcome!

  • @Sneha_Negi
    @Sneha_Negi 3 роки тому

    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.

    • @OGLDEV
      @OGLDEV  3 роки тому

      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.

  • @lilit-v2i
    @lilit-v2i 8 місяців тому

    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.

    • @OGLDEV
      @OGLDEV  8 місяців тому

      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.

  • @MargarethaSaputri-o3b
    @MargarethaSaputri-o3b Рік тому

    Very easy to understand ^o^

    • @OGLDEV
      @OGLDEV  Рік тому

      Glad it helped!

  • @leoreleases
    @leoreleases 2 роки тому

    This tutorial is lovely amazing :D

    • @OGLDEV
      @OGLDEV  2 роки тому +1

      Thank you!

  • @aleksandrstukalov
    @aleksandrstukalov 2 роки тому

    Hey, how exactly you've exported vertex coordinates and indices from blender?

  • @kryptoid2568
    @kryptoid2568 2 роки тому +4

    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.

    • @OGLDEV
      @OGLDEV  2 роки тому +2

      And we have almost the same number of subscribers ;-)

  • @omlachake2551
    @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
      @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
      @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
      @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.

  • @anthonynolanjannotta4688
    @anthonynolanjannotta4688 2 роки тому

    @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!

    • @OGLDEV
      @OGLDEV  2 роки тому

      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.

    • @anthonynolanjannotta4688
      @anthonynolanjannotta4688 2 роки тому

      @@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!! 🙏

    • @OGLDEV
      @OGLDEV  2 роки тому

      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.

  • @abdullahazam-yf3qw
    @abdullahazam-yf3qw Місяць тому

    hi Mr OGL, hv u implement abstract algebra in opengl?

    • @OGLDEV
      @OGLDEV  Місяць тому +1

      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-yf3qw
      @abdullahazam-yf3qw 21 день тому

      @@OGLDEV Nice, Thanks!

    • @OGLDEV
      @OGLDEV  21 день тому

      @@abdullahazam-yf3qw You're welcome :-)

  • @Sneha_Negi
    @Sneha_Negi 3 роки тому

    or can you just tell me...what should we take care of while changing the angle and the depth in translation matrix

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      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_Negi
      @Sneha_Negi 3 роки тому

      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?

    • @OGLDEV
      @OGLDEV  3 роки тому +1

      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_Negi
      @Sneha_Negi 3 роки тому

      @@OGLDEV your code for this tutorial which i found on github..

    • @OGLDEV
      @OGLDEV  3 роки тому

      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.

  • @ChoudharyRamesh
    @ChoudharyRamesh 3 роки тому

    21:42 , when z will be divided by w it willl became 1 , so how this depth imformation is preserved ?

    • @OGLDEV
      @OGLDEV  3 роки тому

      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.

    • @ChoudharyRamesh
      @ChoudharyRamesh 3 роки тому

      @@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 ?

    • @OGLDEV
      @OGLDEV  3 роки тому

      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.

  • @somebody_2837
    @somebody_2837 11 днів тому

    I keep getting perspective distortion, how do i fix this?

    • @OGLDEV
      @OGLDEV  10 днів тому

      What's the nature of the distortion? What's the contents of the projection matrix?

    • @somebody_2837
      @somebody_2837 10 днів тому

      @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.

    • @OGLDEV
      @OGLDEV  9 днів тому

      OK, and how does the projection matrix looks like? Try using a square window do disable interference of the aspect ratio.

  • @thelifelearner5618
    @thelifelearner5618 6 місяців тому

    can i get this code completely in pdf form

    • @OGLDEV
      @OGLDEV  6 місяців тому

      Have you tried using one of the online tools to convert code to pdf?

  • @matthewmorrison2071
    @matthewmorrison2071 3 роки тому

    could you get away with just dividing by Z

    • @OGLDEV
      @OGLDEV  3 роки тому

      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.

  • @NewThingsAk
    @NewThingsAk 2 роки тому

    how to install ogldev_math_3d.h

    • @OGLDEV
      @OGLDEV  2 роки тому

      It's in the git repo github.com/emeiri/ogldev
      In the Include directory.