Getting Plane Vertices

Started by
2 comments, last by HappyCoder 9 years, 10 months ago

I'm trying to draw a plane, I have the plane normal vector and the distance as the following:


struct Plane
{
         D3DXVECTOR3 Normal;
         float Distance;
};

How do I get the plane vertices position?

Advertisement

A plane is by definition infinitely large, so it has no "vertices". I guess you could analytically project the plane to clip space, and then draw the resulting vertices without any transformation matrix (since they are already transformed) but that's probably not the best way to solve whatever your problem is. So, how large do you need the quad to be?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

@Bacterius: I'm trying to render the camera bounding frustum and I have 6 planes with the same structure as above.

Two non-parallel planes intersect at at a line, three non parallel planes intersect at a point. Each plane is in the form

Ax + By + Cz + D = 0

Where (A, B, C) is the normal and D is the distance. (x, y, z) is any point on the plane. If you are given three planes you can find where the intersect by setting up a matrix equation.


The normals of the three planes arranged in a matrix
    |A1 B1 C1|
M = |A2 B2 C2|
    |A3 B3 C3|

The intersection point of the three planes
    |x|
v = |y|
    |z|

The distances of the three planes
    |-D1|   
N = |-D2|
    |-D3|

M * v = N

So solving for v gives us

v = inverse(M) * N

You would have to solve this equation 8 times for each set of three adjacent planes

However, if you want to find the corners of a frustum I would recommend using the projection and view matrix instead. You simply find the inverse of the view matrix multiplied by the projection matrix and then use that matrix to transform a set of constant points.

It would look something like this, I am just writing this out in pseudo code


// find the matrix that transforms from normalized space to world space
frustrumMatrix = inverse(viewMatrix * projectionMatrix);
// the corners of the frustrum in normalized space
cornerPoints = {
    Vector3(-1, -1, 1), Vector3(-1, 1, 1), 
    Vector3(1, 1, 1), Vector3(1, -1, 1), 
    Vector3(-1, -1, 0), Vector3(-1, 1, 0), 
    Vector3(1, 1, 0), Vector3(1, -1, 0)}

result = new Array();

foreach (Vector3 sourcePoint in cornerPoints)
{
    // extend the 3d sourcePoint to a 4d vector by making the 4th component 1
    // then transform the 4d point from normalized space, to world space
    Vector4 transPt = Vector4(sourcePoint, 1) * frustrumMatrix;

    // divide the 4d point by its 'w' value and turn it back into a 3d point
    // this point is a corner of the frustrum in world space
    result.Add(Vector3(transPt.x / transPt.w, transPt.y / transPt.w, transPt.z / transPt.w));
}

Let me explain why this works.

Some definitions first

world space - In world space, locations are measured relative to the world origin

camara space - Points measured relative to the camera

clipping space - Used by directx to cut out polygons or parts of polygons that aren't seen

normalized space - Points from -1 to 1 in the x and y and from 0 to 1 in the z are mapped to the screen.

Normally, the view matrix transforms points from the world space to the camera space. The projection matrix transforms from view space to clipping space. Any point that is transformed by a matrix has to be a 4D vector. So a 3D points is extended by adding a 4th component. For points, this extra value, called w, should be 1.

Multiplying the 4d vector by the view and projection matrix puts the point in clipping space. To get it to normalized coordinates, we need to divide the point by its w component. After that any point that was inside the frustum will be inside a cube ranging for -1 to 1 both the x and y and from 0 to 1 in the z. All points inside this range are mapped and drawn to the screen.

So that is the process that DirectX uses to transform geometry to the screen. The code above that I gave does the process in reverse starting in the normalized coordinates space.

This is an attempt to explain a fairly complicated subject very quickly, so it is understandable if you don't grasp it right away.

My current game project Platform RPG

This topic is closed to new replies.

Advertisement