【问题标题】:Compute mesh vertices from a Plane从平面计算网格顶点
【发布时间】:2013-11-04 14:16:24
【问题描述】:

我想用 OpenGL 绘制我的平面来调试我的程序,但我不知道该怎么做(我数学不太好)。

我有一个有 2 个属性的飞机:

  • 一个常数
  • 正常

这是我得到的:

////////////////////////////////////////////////////////////
Plane::Plane( const glm::vec3& a, const glm::vec3& b, const glm::vec3& c )
{
    glm::vec3 edge1 = b - a;
    glm::vec3 edge2 = c - a;

    this->normal    = glm::cross(edge1, edge2);
    this->constant  = -glm::dot( this->normal, a );
    this->normalize();
}

////////////////////////////////////////////////////////////
Plane::Plane( const glm::vec4& values )
{
    this->normal    = glm::vec3( values.x, values.y, values.z );
    this->constant  = values.w;
}

////////////////////////////////////////////////////////////
Plane::Plane( const glm::vec3& normal, const float constant ) :
constant    (constant),
normal      (normal)
{

}

////////////////////////////////////////////////////////////
Plane::Plane( const glm::vec3& normal, const glm::vec3& point )
{
    this->normal    = normal;
    this->constant  = -glm::dot(normal, point);
    this->normalize();
}

我想把它画出来看看是否一切正常。我怎么能这样做? (我需要计算顶点和索引来绘制它)

【问题讨论】:

    标签: c++ opengl math


    【解决方案1】:

    当你想画的时候,你需要找到两个垂直于法线的向量和平面上的一个点。那不是那么难。首先,让我们得到一个不是normal 的向量。叫它some_vect。例如:

    if normal == [0, 0, 1]
        some_vect = [0, 1, 0]
    else
        some_vect = [0, 0, 1]
    

    然后,计算vect1 = cross(normal, some_vect) 会得到一个垂直于normal 的向量。计算 vect2 = cross(normal, vect1) 会给你另一个垂直于 normal 的向量。

    有两个垂直向量vect1vect2 以及平面上的一个点,绘制平面变得微不足道。例如具有以下四个点的方格(记得对向量进行归一化):

    point + vect1 * SIZE
    point + vect2 * SIZE
    point - vect1 * SIZE
    point - vect2 * SIZE
    

    其中point 是平面上的一个点。如果您的constant 与原点有距离,那么point 将是constant * normal

    【讨论】:

      【解决方案2】:

      绘制平面的难点在于它是一个无限的表面;即根据定义,它没有边或顶点。如果您想以典型的多边形方式显示平面,则必须将其裁剪为特定区域,例如正方形。

      一个相当简单的方法是:

      1. 选择一个垂直于法线的任意单位向量。将其存储为 v1。
      2. 使用 v1 和平面法线的叉积得到 v2。
      3. 否定 v1 得到 v3。
      4. 否定 v2 以获得 v4。

      点 v1-4 现在表示与平面方向相同的正方形的四个角。您需要做的就是将它们乘以您想要的任何大小,然后相对于平面上的任何点绘制它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 2017-05-12
        相关资源
        最近更新 更多