Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / View Frustum Culling

Author
Message
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 3rd Nov 2015 22:00 Edited at: 3rd Nov 2015 22:03
I'm curious as to how to calculate if a 3D point would be culled or not inside the viewing frustum. I've been looking up tons of things, but I recall one time coming across something where they converted the world coordinates to frustum coordinates. So say the frustum went from being a cone to a perfect cube and the boxes went from being perfect cubes into the cones. You could then use this cubed frustum to do a simple check if any points were inside. I'll try and find some images and edit this post if I come across any.

Edit: Similar to this. Where he morphs the view frustum into a perfect cube and then alters any Vertex coordinates within the frustum
chafari
Valued Member
17
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 4th Nov 2015 13:19
Hi pictionaryjr . Not sure if this is what you need . I use this piece of code in my 3D editor .



Cheers.
I\'m not a grumpy grandpa
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 4th Nov 2015 15:04
That's actually very useful although it's not quite what I need, but I'm definitely going to pull the data from the perspective matrix and see how it's set up. Either way. I'm going to have to figure out how to normalize it. After I normalize the matrix I can then check to see if any vertices are within the box.
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 4th Nov 2015 17:01 Edited at: 4th Nov 2015 17:05
So finally after some time of looking at the perspective matrix you gave me and searching online. I finally found the formulas they used in their perspective matrix and got mine to match up! So your code turned out to be very useful, b/c I now knew the numbers I had to match up with which made my searches more definitive. I didn't have to question if it was correct b/c if the numbers didn't match then it clearly wasn't so thank you so much! I ended up creating a function for everyone to use.

The function checks if the 3D point is within the view frustum ( NOTE: This will not work if the camera is not centered at 0,0,0 and facing down the Z axis ) If you want the camera off the Z axis for obvious reasons. You'll have to subtract the camera coordinates from the 3D point coordinates and then use a 3D rotation matrix rotated to the opposite direction of the camera. I'll have an updated function doing that in a moment:


Here's an example with a sphere you can move around. The sphere may be inside the camera and say it's out, but you are not testing to see if the edges of the sphere are in the camera. You are testing the sphere central point.
pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 4th Nov 2015 17:38 Edited at: 4th Nov 2015 19:48
New function where camera can be placed anywhere:


More example code with the new function:


EDIT:
There is a problem with when you get to around 90 or 270 degrees and looks at the sphere. It appears to only be while looking up or down. I'm positive it has to do with the rotation matrices. I'll update the code on this post when I do. I ported the rotation matrices code over and formed it to fit this which is why I assume that's what the problem is.
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 4th Nov 2015 21:08 Edited at: 4th Nov 2015 21:11
Typically frustum culling is handled by extracting 6 clip planes from a camera's viewprojection matrix. Points can then be tested against each plane using the plane<->point equivalent to a vec4 dotproduct (where the point vector's fourth component is assumed to be 1.0 ) which will return the signed distance from the plane; The sign indicates weather the point was in front of or behind the plane, 0.0 for points which lie along the plane. If the result of all 6 tests >= 0.0 then the point is within the frustum.

Edit: Very nice, WickedX was actually posting an example of this at the same time
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 4th Nov 2015 21:08
my 3DMath is a little rusty. How about this.

pictionaryjr
15
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 4th Nov 2015 22:21 Edited at: 4th Nov 2015 22:26
Wow Wicked. That is far far far prettier looking than mine lol. I guess I need to fool around with vectors and matrices more.

@Revenant I realized why I was getting confused down the line b/c there are actually 3 separate ways to check if something is within the view frustum. There's a radar approach, clip space approach which is the one both you and Wicked referred to where you grab the planes. There is also the geometric approach that I took. This also explains why I was having so much trouble getting the correct formula though b/c there were sooo many different websites with different formulas. So I would be doing part clip space, part geometric, and part radar which obviously wouldn't work. After chafari clued me in with the perspective matrix I got it, but I'll definitely be taking a look at Wicked's post here to get a grip on how dbp handles matrix and vector interactions.

Edit:
After taking a look at Wicked's. It seems his struggled with the camera being moved from the origin or rotating as well, but the matrix thing like I said is still something I'm going to look at so thank you everyone!
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 5th Nov 2015 03:43 Edited at: 5th Nov 2015 03:44
Im off to work at the moment but when I get back I intend to take a look at WickedX's code. At first glance it seems the problem may be the transform coords vector3 10, 10, 9 line, transforming the coordinates should not be needed.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 5th Nov 2015 06:59
I just got home from work. This is how I originally wrote the function, should have worked. By transforming the point and comparing the planes distance with 1.75, it seemed to be close. Evidently not, Sorry!

I will check the planes to see if I made an error.

WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 5th Nov 2015 17:01 Edited at: 5th Nov 2015 18:35
Ok, found the problem. I should have been exiting the function and returning 0 on any negative plane collision. Doh.

WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 9th Nov 2015 17:10 Edited at: 9th Nov 2015 17:16
Just for the fun of it, I added FPS - WASD controls and moved the camera back a little. Now the point is initially in the frustum.

Login to post a reply

Server time is: 2024-03-28 23:37:51
Your offset time is: 2024-03-28 23:37:51