Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw arbitrary plane from plane equation, OpenGL

Tags:

c++

math

opengl

I have a plane defined by the standard plane equation a*x + b*y + c*z + d = 0, which I would like to be able to draw using OpenGL. How can I derive the four points needed to draw it as a quadrilateral in 3D space?

My plane type is defined as:

struct Plane {
    float x,y,z; // plane normal
    float d;
};

void DrawPlane(const Plane & p)
{
    ???
}

EDIT:

So, rethinking the question, what I actually wanted was to draw a discreet representation of a plane in 3D space, not an infinite plane. Base on the answer provided by @a.lasram, I have produced this implementation, which doest just that:

void DrawPlane(const Vector3 & center, const Vector3 & planeNormal, float planeScale, float normalVecScale, const fColorRGBA & planeColor, const fColorRGBA & normalVecColor)
{
    Vector3 tangent, bitangent;
    OrthogonalBasis(planeNormal, tangent, bitangent);

    const Vector3 v1(center - (tangent * planeScale) - (bitangent * planeScale));
    const Vector3 v2(center + (tangent * planeScale) - (bitangent * planeScale));
    const Vector3 v3(center + (tangent * planeScale) + (bitangent * planeScale));
    const Vector3 v4(center - (tangent * planeScale) + (bitangent * planeScale));

    // Draw wireframe plane quadrilateral:
    DrawLine(v1, v2, planeColor);
    DrawLine(v2, v3, planeColor);
    DrawLine(v3, v4, planeColor);
    DrawLine(v4, v1, planeColor);

    // And a line depicting the plane normal:
    const Vector3 pvn(
       (center[0] + planeNormal[0] * normalVecScale),
       (center[1] + planeNormal[1] * normalVecScale),
       (center[2] + planeNormal[2] * normalVecScale)
    );
    DrawLine(center, pvn, normalVecColor);
}

Where OrthogonalBasis() computes the tangent and bi-tangent from the plane normal.

like image 708
glampert Avatar asked Dec 18 '13 19:12

glampert


1 Answers

To see the plane as if it's infinite you can find 4 quad vertices so that the clipped quad and the clipped infinite plane form the same polygon. Example:

Sample 2 random points P1 and P2 on the plane such as P1 != P2.

Deduce a tangent t and bi-tangent b as

t = normalize(P2-P1); // get a normalized tangent
b = cross(t, n); // the bi-tangent is the cross product of the tangent and the normal

Compute the bounding sphere of the view frustum. The sphere would have a diameter D (if this step seems difficult, just set D to a large enough value such as the corresponding sphere encompasses the frustum).

Get the 4 quad vertices v1 , v2 , v3 and v4 (CCW or CW depending on the choice of P1 and P2):

v1 = P1 - t*D - b*D;
v2 = P1 + t*D - b*D;
v3 = P1 + t*D + b*D;
v4 = P1 - t*D + b*D;
like image 161
a.lasram Avatar answered Sep 28 '22 01:09

a.lasram