Frustum culling Rastertek Tutorial - Need urgent help!

Started by
15 comments, last by mister345 6 years, 5 months ago

Hi, there's a great tutorial on frustum culling, but impossible to compile because it uses old DirectX 11 types (Direct3DXPlane instead of XMVECTOR, etc).  Can someone please help me update this one class - frustumClass - to the new DirectX11 types (XMMATRIX, XMVECTOR, etc)?

http://www.rastertek.com/dx11tut16.html

Furthermore, can anyone please explain how he gets the minimum Z distance from the projection matrix by dividing one element by another? He leaves no explanation for this math and it's extremely frustrating.

// Calculate the minimum Z distance in the frustum.
zMinimum = -projectionMatrix._43 / projectionMatrix._33;
r = screenDepth / (screenDepth - zMinimum);
projectionMatrix._33 = r;
projectionMatrix._43 = -r * zMinimum;

Also not sure how to use an XMVECTOR instead of the old Plane class that he uses. Confused as to where all the m12, m13, etc correspond to the elements of an XMVECTOR. I thought you're not supposed to even access the elements of an XMVECTOR directly! So incredibly frustrating.

Please help, thanks.

Advertisement

Getting the near/far values from the projection matrix is basically done by reversing the projection matrix equation.  In particular, you take the perspective projection equation, then solve for near/far values.  There's a plethora of information online about the perspective projection formula, and still a lot of information already on how to solve for the near/far values (including caveats), so I'll leave that to you to look up.

As for XMVECTOR, the plane equation is just a 4D set of values (a, b, c, d), so anything that holds the 4D values can be used to store a plane.  You can store this inside an XMVECTOR if you like, then operate on it while it's loaded into the vector.  DirectXMath has a wide range of functions to set/get values inside an XMVECTOR

Does this help: https://matt77hias.github.io/blog/culling/2017/08/24/creating-a-view-frustum.html ;) ?

🧙

@xycsoscyx

Thanks for all your advice and I appreciate your desire to make me seek the answers myself. However, I am still inexperienced with the right ways of using these new XM types, is this the right way to access the element of an XMMATRIX say at row 4, column 3?

float element43 = projectionMatrix.r[3].m128_f32[2];

Also, you said "as for XMVECTOR, the plane equation is just a 4D set of values," but a plane would need 12 values - 3 floats to express each of the 4 points, right? (or in the case of XMVECTOR, it has 4 floats). So how on earth do you store 4 3D points inside of 1 XMVECTOR? Even msdn specifically says use XMVECTOR instead of plane, but it makes no sense!

And could you please tell me the best way to access the element of an XMVECTOR? They make it really user unfriendly, you can't just index into an XMVECTOR, like you have to load it into an XMFLOAT4 or something, which seems like a huge waste of resources.

5 minutes ago, mister345 said:

but a plane would need 12 values - 3 floats to express each of the 4 points, right? (or in the case of XMVECTOR, it has 4 floats)

plane normal: 3 floats

signed distance from origin: 1 float

= 4 floats

🧙

I'm sorry, could you please elaborate? A plane would normally need at least 3 points to express in 3D space. How exactly do you only express it using a scalar value for distance from origin and normal? That tells you nothing about the location of the plane relative to the origin, or its width and height, only the direction of its surface. What am I missing here?

If I remember right, then my frustum class is directly ported from the rastertek tutorials for the new DirectXMath, you can check it out:

https://github.com/turanszkij/WickedEngine/blob/master/WickedEngine/wiFrustum.h

https://github.com/turanszkij/WickedEngine/blob/master/WickedEngine/wiFrustum.cpp

6 minutes ago, mister345 said:

I'm sorry, could you please elaborate? A plane would normally need at least 3 points to express in 3D space. How exactly do you only express it using a scalar value for distance from origin and normal? That tells you nothing about the location of the plane relative to the origin, or its width and height, only the direction of its surface. What am I missing here?

The normal specifies the orientation/rotation of the plane.

The distance specifies the position/translation of the plane.

Scaling is not needed, because a plane has an infinte area.

See: https://en.wikipedia.org/wiki/Plane_(geometry)

3 minutes ago, turanszkij said:

If I remember right, then my frustum class is directly ported from the rastertek tutorials for the new DirectXMath, you can check it out:

https://github.com/turanszkij/WickedEngine/blob/master/WickedEngine/wiFrustum.h

https://github.com/turanszkij/WickedEngine/blob/master/WickedEngine/wiFrustum.cpp

You can use some more SIMD ;)

https://github.com/matt77hias/MAGE/blob/master/MAGE/MAGE/src/math/view_frustum.cpp

🧙

2 minutes ago, matt77hias said:

For sure, this is like one of my oldest codes so it is a bit low quality :D

1 minute ago, matt77hias said:

The normal specifies the orientation of the plane.

The distance specifies the translation of the plane.

Scaling is not needed, because a plane has an infinte area.

Yes, but translation in which direction? You can't just translate a 3D object with a single scalar value. If you wanted to translate some object by that value, you would have to multiply a normalized direction vector by your desired distance, then add it to the 3D position of the object in question. So which point on the plane exactly are you "translating" with this "distance" scalar, and in what direction exactly? Thanks.

This topic is closed to new replies.

Advertisement