Тёмный

Math for Game Developers - Frustum Culling 

Jorge Rodriguez
Подписаться 40 тыс.
Просмотров 44 тыс.
50% 1

Use frustum culling to avoid drawing entities that are outside of the player's field of view. It's the one of the simplest and most effective scene management techniques.
Find the source code here: github.com/BSV...
A paper on how to get the planes from the projection matrix can be found here: gamedevs.org/up... It's not terribly educational so I'm omitted it.
Question? Leave a comment below, or ask me on Twitter: / vinobs

Опубликовано:

 

28 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 72   
@GuardianMatrix2005
@GuardianMatrix2005 11 лет назад
You are great at explaining this. I already knew the concept, but I was thoroughly engaged and entertained. You were also right to the point and not stuttering/stumbling over words. The lesson seemed planned and you seemed organized. I will be checking out your other videos. Much better than other "tutorials" that go on about stupid crap that is irrelevant to the topic.
@absorbingdude
@absorbingdude 7 месяцев назад
Still useful! Finally managed to understand how do I implement Light Culling in my vulkan forward+ renderer!
@JorgeVinoRodriguez
@JorgeVinoRodriguez 7 месяцев назад
Math doesn't go out of date! :)
@chris_burrows
@chris_burrows 7 лет назад
So dope man. Killing it. You better plan to do more of these.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
Thank you! I spend a great deal of time thinking about and planning and recording these videos. Sometimes I do multiple takes. I'm glad you liked it, I hope they help you. :)
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
When BSP was developed there were few video cards out there, so the rendering was done in software. BSP provided an efficient way to render the scene in software. These days it's all hardware and scene management isn't about spatial partitioning as much. Octrees were popular for a while, but these days I think it's hardware occlusion methods since tri counts don't matter as much? But honestly I don't really know. :P
@anzatzi
@anzatzi 10 лет назад
the videos are great but its hard to follow the ordering--even looking at the google playlists.
@darksecond
@darksecond 11 лет назад
Thank you a lot, just made it so that i calculate an AABB from all points of a mesh (and save the AABB). seems to work fine :)
@SB-rf2ye
@SB-rf2ye 3 года назад
Really useful video!!! Thanks Jorge!!
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
I'm not sure I'll be covering BSP, if I do it will be much later. BSP is a bit outdated as far as engines go.
@julianaskuratovsky8701
@julianaskuratovsky8701 27 дней назад
Awesome!!
@PeterLakeTV
@PeterLakeTV 11 лет назад
Fantastic, thanks for the video
@TheNo1WarLord
@TheNo1WarLord 11 лет назад
What are "All 6 planes"?
@NeilRoy
@NeilRoy 8 лет назад
I tried implementing a simpler version of this no a program I am working on that is a 3D world, but it is a fairly flat 2Dish world. So I figured perhaps I would get away with just drawing what was in my view by figuring out an angle so many degrees to the left and right of my view, but my trig math really sux! ;) I am about ready to go back to 2D games, they're so much easier. ;)
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 лет назад
+Neil Roy Why don't you try a dot product? Take a vector to the object and dot it with the view vector. If it's bigger than cos of the field of view, you must be out of the field of view. Make sure to normalize them before you dot them.
@NeilRoy
@NeilRoy 8 лет назад
Jorge Rodriguez Because I am still new to this level of math. I know what normalizing is, I know what a vector is etc... I am still getting used to exactly what a "dot product" even is! LOL ;) It must be nice to think in math like that. but I am getting there. But I took a note of what you just suggested and I will see what i can do. I did manage to get it working somewhat so progress is being made... albeit slowly. I don't give up easily. :) In fact, I was laying in bed trying to sleep and my brain was still on all of this. LOL
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 лет назад
+Neil Roy I solve problems by sleeping on them all the time, it's a good method. It helps your brain get used to new things that you're learning.
@jeremyh9841
@jeremyh9841 7 лет назад
lol i saw you on making games with ben
@NeilRoy
@NeilRoy 7 лет назад
Yeah, I loved Ben's tutorials, very well done! I did manage to implement a sort of hack of this Frustum Culling on a 3D game I am playing with. It's not a true 3D frustum as I do not take into account your pitch, but in the game you won't be looking up or down so it doesn't matter. But I did manage to get it working and it really sped up my 3D game big time! I still want to work on implementing true Frustum Culling code, but what I have gets the job done for now. Next on my todo list is Occlusion culling I think it is called (eliminating polygons that are not facing the camera?), which should be easier, just test the polygon's normal and if it faces away from the camera, don't draw it. Should speed things up more. The biggest problem I see with most of these sort of tutorials is they almost never get into organizing your 3D data, which is vital. I have learned a lot of very BAD habits from poorly written tutorials over the years. This is why Ben's were so good, because he avoided the bad habits and though some of what he taught was rather involved, it was the right way to do it (except how he almost never used constructors and destructor's properly, that bugged me and I rewrote his code to use them properly on my system). HOW you store your 3D data is VERY important! You need to be able to examine individual polygons so you can check their position, whether or not to draw them, collision etc. when y ou have tutorials which use things like opengl triangle strips, that seems okay, but in reality is is garbage and useless to use. You need to store polygone individually. Usually you should store your vertices and a separate index list that make up all the polygons which you can then use to optimize for speed, collision etc. Almost none of the online tutorials cover this sort of data organization. I imagine most people give up when they realize their code cannot be used to detect collisions etc and they have to unlearn a lot. Anyhow... rambling again. ;)
@proxict3310
@proxict3310 10 лет назад
Please, what program do you use for drawing? Thank you :)
@JorgeVinoRodriguez
@JorgeVinoRodriguez 10 лет назад
Photoshop.
@proxict3310
@proxict3310 10 лет назад
Jorge Rodriguez Thank you :]
@kevgmei
@kevgmei 9 лет назад
You sound just like a high school teacher.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 9 лет назад
+Kevin Meisenbacher Thank you.
@arunparolikkal4674
@arunparolikkal4674 4 года назад
@@JorgeVinoRodriguez hmm. Was that a compliment?
@angelobalcueba7081
@angelobalcueba7081 3 года назад
@@arunparolikkal4674 after 5 years? Yeah...
@vahida6146
@vahida6146 11 месяцев назад
I think slightly simpler solution would be: 1- Find the vector from the plane location to the acotr location: PA = Actor(Location) - Plane(Center Location) 2- Now you can create a dot product between the normal of the plane and PA and then add the radius to result: DotProduct(Normal, PA) + Radius 3- If result is less than 0 so object is behind the plane and should not be sent to GPU for render
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
There are probably many schemes for calculating the bounding sphere of an object. You want it to cover the entire entity but still be as small as possible. What I did (and you can look at the code in GitHub) was calculate a bounding box for the entity and then take the box's center to be the sphere's center and the longest diagonal of that bounding box to be the "diameter" of the sphere. I'm sure you could do it other ways too. I'd link you to the code, but RU-vid doesn't allow it.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
I think I cover this in one of my other videos, but (A, B, C) can be thought of as a "normal" of the plane, and D can be thought of as the distance from the plane to the origin (not exactly, but close enough.)
@focotaku
@focotaku 5 лет назад
At the end you mention the painters algorithm and how you should render things in the back first, but that's only if you are rendering transparent objects. For opaque objects, if you use the depth buffer, you should render front to back to minimize the overdraw.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 5 лет назад
...unless sorting on the CPU will be more expensive than overdraw. As with any performance issue, one should profile the application first to see what the performance characteristics are and approach the most serious ones first, resolving them in the way that makes the most sense for that particular problem. :)
@cezito6831
@cezito6831 6 месяцев назад
Damn still working til this day!! Thank you so much sir!
@NeilRoy
@NeilRoy 8 лет назад
Thanks, you explained this better than any of the nonsense I seen online!
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
It's not a plane intersection algorithm. If it were then something completely inside the frustum but not touching any planes wouldn't be rendered. It's not testing if it's touching each plane, it's testing which side of the plane the object is on. If it's on the "inside" of all six planes, then it's intersecting the frustum.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
A frustum is like a pyramid. A pyramid has four sides, a top, and a bottom, for a total of six sides. Each side is represented by a plane. Thus, six planes :)
@icaroamorim3123
@icaroamorim3123 2 года назад
how do I calculate d?
@Dbowler92
@Dbowler92 11 лет назад
Subed - This is the channel (3D graphics maths) that I've been hunting for!! Well explained, interesting and overall excellent content - keep up the hard work :)
@TebiByyte
@TebiByyte 8 лет назад
Thank god I found this video, because otherwise I would have way over-complicated the math for detecting stuff inside a volume.
@michaelbacigalupo4573
@michaelbacigalupo4573 11 лет назад
Really enjoyed this video, but there's a concept I'm not getting - how do you get D in the first place? You just sorta defined it as the distance from the origin without explaining how we'd get that in the first place.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 лет назад
In this case d is given as part of the problem, the frustum is created in another part of the code and right now we're only using it to solve the problem. If you want to create the plane yourself then you'll have to use the plane formula, ax + by + cz = d, there's your d. If you have a point (x', y', z') and a plane normal (a, b, c) and you want to find the corresponding plane formula, then you do a(x-x') + b(y-y') + c(z-z') = 0, and simplify to get it in ax + by + cz = d form. I think I may cover this stuff in another video, I don't quite remember.
@darksecond
@darksecond 11 лет назад
So how would you go at calculating the radius for every object? Would you do it at run/load time? or would you generate it at asset compile time, or even manually pick something?
@fenrircreativestudios2129
@fenrircreativestudios2129 6 лет назад
is this possible in a 2D game ?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 6 лет назад
Absolutely, and the math ends up being much the same or even a bit easier.
@ezstudio3d
@ezstudio3d 11 лет назад
I am tune to see when you start a tutorial with BSP since I have found it a little hard to understand
@eloquentlearner3760
@eloquentlearner3760 3 года назад
this is so good thanks so much for posting
@bemdav
@bemdav 7 лет назад
I'm sorry I know I'm commenting really nitpicky things on almost all of your videos but here I am confused again. What I find confusing is how can d be negative if it is only a measure of distance? I understand how it would be negative if it was a vector but further on in the video, as well as in the code, you only use scalars. So d, (p . n) , and c would be scalars. Do I understand it correctly here, that d is negative if it's below the plane and positive if it is above it? That would make a lot more sense to me if d was a vector but then you couldn't add it to the scalars. So how do you reach a distance scalar that is negative? How do you calculate d then? Wouldn't it be easier to use d as a vector? And use p', the vector, rather than using e, the scalar. Again, I really don't mean to seem like a troll. I really appreciate these videos and I'm learning a ton from them! Although often I get stuck on these small things that I don't quite understand 100% and I want to make sure I know exactly what is going on before moving onto the next video.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 7 лет назад
Very good questions. d is not just distance, it's signed distance. That means that its absolute value is the distance, and its sign depends on which side of the line it is. You understand correctly, d is negative below the plane and positive above. You can calculate d by setting up and solving a system of linear equations. I'll try to explain it here, hopefully it makes sense only with text. If not, try drawing a picture. Let's say you have a point x0 and two linearly independent vectors (v0 v1) that define a plane. Then your plane is the set of all points x0 + a0 * v0 + a1 * v1, where a0 and a1 are any real number. The normal of the plane is n = (v0 x v1) / |v0 x v1| (x is the cross product, and I divide by magnitude to make sure that it's unit length.) Then there's some scalar multiple of n that will span from the origin to the plane, ie x0 + a0 * v0 + a1 * v1 = d * n. Since x0 v0 and v1 are vectors with 3 components, this is a series of 3 linear equations and you can solve for the 3 unknowns, a0 a1 and d. Since d is the number of unit-length normal vectors that separate the plane from the origin, d is the signed distance. If you need a picture to go with this, email me: bs.vino@gmail.com You're not a troll at all, in fact you're asking excellent questions and I hope you'll continue to do so! I'm happy to answer questions about any of my videos, so keep them coming :)
@bemdav
@bemdav 7 лет назад
Okay, I tried drawing what you said and it makes a lot more sense now, thanks! If anyone else is also interested, here is the picture: imgur.com/a/JlY2V I also tried to calculate a0, a1, d from what I drew and it worked fine. I have a much better understanding now. Note, I made a mistake when drawing and I have drawn n in the opposite direction; it should actually be pointing towards the origin, not away from it. My bad. But other than that, it should all be correct.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 7 лет назад
Nice work!
@procrastinator7408
@procrastinator7408 7 лет назад
Very interesting!
@rutgerdemaeyer
@rutgerdemaeyer 9 лет назад
wich program language is that?
@aiman_yt
@aiman_yt 8 лет назад
+Rutger De Maeyer C++
@DarkDrakman
@DarkDrakman 10 лет назад
mmm why not calculate the difference vector between a monster and the origin, dot it with the forward direction, and render it only if dot is greater then a certain value?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 10 лет назад
That assumes the camera is at the origin. If you want to transform all objects to camera space you could do it this way. Also it will get you a conical Frustum and not a rectangular Frustum.
@DarkDrakman
@DarkDrakman 10 лет назад
oh ok that seems right, thanks!
@DarkDrakman
@DarkDrakman 10 лет назад
One thing, when you develope games, you usally use a game engine or do everything from scratch?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 10 лет назад
DarkDrakman Depends on what I'm making. I've done both.
@srhalnon
@srhalnon 10 лет назад
Jorge Rodriguez In the end though, the camera is always at the origin. In OpenGL, the caamera is always looking down negative z. Camera models that let you treat the camera as a movable thing are just extra math, but the camera is always at the center.
@SoulGuitarMetal
@SoulGuitarMetal 9 лет назад
But how do you get the planes from the camera projection?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 9 лет назад
SoulGuitarMetal Not covered in this video, sorry. It's on my list to make a video about that.
@TimRex.
@TimRex. 9 лет назад
+Jorge “Vino” Rodriguez +1 for this please :D I think I see what's going on here, but.. well.. it's late and i need sleep
@TimRex.
@TimRex. 9 лет назад
Actually, I just found this paper - which gets into the detail of the procedure. Quite handy for anyone that is looking. gamedevs.org/uploads/fast-extraction-viewing-frustum-planes-from-world-view-projection-matrix.pdf
@JorgeVinoRodriguez
@JorgeVinoRodriguez 9 лет назад
+SoulGuitarMetal I was going to do a video on it but after reviewing it I don't think it's terribly educational to learn how it's done, it's just some simple calculation and manipulation. But Mr. Kane linked a pdf that does it (also in the description) and you can try reading the sample code and to figure out how it's done. It's actually all pretty straightforward.
@SoulGuitarMetal
@SoulGuitarMetal 9 лет назад
+Jorge Rodriguez (Vino) Yeah the code works fine but I have a hard time viewing how it works which sometimes is more important to me than just get it working. Particularly the part where it states that the extracted planes are an axis-aligned box or "untransformed' viewing frustum. "If vertex v' is inside this box, then the untransformed vertex v is inside the 'untransformed' viewing frustum". So we are not working with a frustum at all...? I don't get it.
@ezstudio3d
@ezstudio3d 11 лет назад
really, so what is new, I am very curious?
Далее
Math for Game Developers - Perspective Matrix
10:09
Просмотров 47 тыс.
pumpkins #shorts
00:39
Просмотров 12 млн
Трудности СГОРЕВШЕЙ BMW M4!
49:41
Просмотров 1,5 млн
#1 How Occlusion Culling Works: Introduction
13:38
Просмотров 22 тыс.
Frustum Culling // Terrain Rendering episode #9
21:44
What is Frustum Culling?
6:50
Просмотров 6 тыс.
Optimizing My Minecraft Clone With GREEDY MESHING
6:27