简体   繁体   中英

Compute mesh vertices from a Plane

I would like to draw my Plane with OpenGL to debug my program but I don't know how I can do that (I'm not very good in math).

I've got a Plane with 2 attributs:

  • A constant
  • A normal

Here is what i've got:

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

I would like to draw it to see if everything is ok. How I can do that? (I need to compute vertices and indices to draw it)

When you want to draw, you need to find two vectors that are perpendecular to normal and a point on the plane. That's not so hard. First, let's get a vector that is not normal . Call it some_vect . For example:

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

Then, calculating vect1 = cross(normal, some_vect) would give you a vector perpendecular to normal . Calculating vect2 = cross(normal, vect1) would give you another vector that is perpendecular to normal .

Having two perpendecular vectors vect1 and vect2 and one point on the plane, drawing the plane becomes trivial. For example the sqaure with the following four points (remember to normalize the vectors):

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

where point is a point on the plane. If your constant is distance from the origin, then one point would be constant * normal .

The difficulty with drawing a plane is that it's an infinite surface; ie by definition it has no edges or vertices. If you want to show the plane in a typical polygonal fashion then you'll have to crop it to a particular area, such as a square.

A fairly easy approach is this:

  1. Pick an arbitrary unit vector which is perpendicular to the normal. Store it as v1.
  2. Use the cross product of v1 and the plane normal to get v2.
  3. Negate v1 to get v3.
  4. Negate v2 to get v4.

The points v1-4 now express the four corners of a square which has the same orientation as your plane. All you need to do is multiply them up to whatever size you want, and draw it relative to any point on your plane.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM