从平面方程OpenGL中绘制任意平面

时间:2013-12-18 19:26:03

标签: c++ opengl math

我有一个由标准平面方程a * x + b * y + c * z + d = 0定义的平面,我希望能够使用OpenGL绘制。如何得出在3D空间中将其绘制为四边形所需的四个点?

我的飞机类型定义为:

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

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

编辑:

因此,重新思考这个问题,我真正想要的是在3D空间中绘制一个谨慎的平面表示,而不是无限平面。 根据@ a.lasram提供的答案,我已经制作了这个实现,它就是这样做的:

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);
}

其中OrthogonalBasis()计算平面法线的切线和双切线。

2 个答案:

答案 0 :(得分:2)

要将平面视为无限,您可以找到4个四边形顶点,以便剪切的四边形和剪切的无限平面形成相同的多边形。例如:

在平面上示例2个随机点P1P2,例如P1 != P2

将切线t和双切线b推断为

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

计算视锥体的边界球。球体的直径为D(如果此步骤看起来很困难,只需将D设置为足够大的值,例如相应的球体包含截头体。)

获取4个四边形顶点v1v2v3v4(CCW或CW取决于P1和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;

答案 1 :(得分:1)

一种可能性(可能不是最干净的)是使正交矢量与平面对齐,然后从那里选择点。

  1. P1 =< x,y,z>
  2. t1 =具有P1的随机非零,非共线矢量。
  3. P2 = norm(P1 cross t1)
  4. P3 = norm(P1 cross P2)
  5. 现在,所需平面中的所有点都被定义为起点加上P2和P3的线性组合。这样,您可以根据需要获得所需的几何点。

    注意:起点只是你的飞机正常< x,y,z>乘以距离原点的距离:abs(d)。

    同样有趣的是,通过巧妙选择t1,您还可以将P2与某些视图对齐。假设您正在从某个z点观察x,y平面。您可能想要选择t1 =< 0,1,0> (只要它与P1不共线)。这使得y组件的P2为0,而x组件的P3为0。