简体   繁体   中英

Order of vertex for drawing quad

I need to draw n planes in 3D space. The quads are planes created from two points, and with an algorithm I get 4 vertex for drawing the quad. The problem I have is that the order of the vertices affect to the result, obviously. Here it is what I mean: 错误的 But when the plane is horizontal instead of vertical: 正确的

I can imagine two possible solutions. Using triangles and combining them or ordering the vertex properly. I dont know how to do the second idea. I have tried using triangles but I get the same problem.

# self.planos = [('A', (500, 500, 10), (-500, 500, 10), (-500, -500, 10), (500, -500, 10))] for horizontal
# self.planos = [('A', (-500, 10, 500), (500, 10, 500), (-500, 10, -500), (500, 10, -500))] for vertical
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glDepthMask(GL_FALSE)
glBegin(GL_QUADS)
glColor(0.5, 0.5, 0.1, 0.5)
for i in range(len(self.planos)):
    glVertex(self.planos[i][1][0], self.planos[i][1][2], self.planos[i][1][1])
    glVertex(self.planos[i][2][0], self.planos[i][2][2], self.planos[i][2][1])
    glVertex(self.planos[i][3][0], self.planos[i][3][2], self.planos[i][3][1])
    glVertex(self.planos[i][4][0], self.planos[i][4][2], self.planos[i][4][1])
glEnd()
glDepthMask(GL_TRUE)
glDisable(GL_BLEND)

Intersection code for getting four vertex to draw planes:

In init method:
#Vertices of the cube
self.v = (Point3D(500, 500, 500), Point3D(-500, 500, 500), Point3D(-500, -500, 500),
          Point3D(500, -500, 500), Point3D(500, 500, -500), Point3D(-500, 500, -500),
          Point3D(-500, -500, -500), Point3D(500, -500, -500))
# Edges of the cube
self.a = (Segment3D(self.v[0], self.v[1]), Segment3D(self.v[1], self.v[2]),
          Segment3D(self.v[2], self.v[3]), Segment3D(self.v[3], self.v[0]),
          Segment3D(self.v[0], self.v[4]), Segment3D(self.v[1], self.v[5]),
          Segment3D(self.v[2], self.v[6]), Segment3D(self.v[3], self.v[7]),
          Segment3D(self.v[4], self.v[5]), Segment3D(self.v[5], self.v[6]),
          Segment3D(self.v[6], self.v[7]), Segment3D(self.v[7], self.v[4]))
# Algorithm for getting 4 points
def plano_limites(self, point1, point2, point3):
    plano = Plane(Point3D(point1), Point3D(point2), Point3D(point3))
    good = []
    for i in range(12):
        a = intersection(plano, self.a[i]) # Sympy intersection
        if a:
            good.append(a[0])
    return good

First of all, be aware that your intersection can result in more or less than four vertices. But since the region will always be convex, you can simply draw it with a triangle fan.

To sort your vertices, you need the normal n of the plane and the centroid c of all the vertices v_i :

c = 1/(number of vertices) * (v_1 + v_2 + v3 + ...)

Then, we need a coordinate system whose z-axis is the normal. For this, we can simply define an arbitrary other direction vector d and define x = normalize(cross(d, normal)), y = cross(normal, x) . For cases where d conincides with normal , we need an alternative d .

We can then calculate a representative angle of any vertex in this coordinate system:

angle_i = atan2(dot(x, v_i - c), dot(y, v_i - c))

Sort by this angle and you are done.

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