Python CAD Tutorial 06 - Drawing planes in 3D space
rss_feed

Python CAD Tutorial 06 - Drawing planes in 3D space

homeHome
pagesGTK-3
pagespython
pagescad
pagesopengl

View/Download Code

Planes

We will now add a plane class, this is another fundamental building block. It will also be used for various collision detections at a later stage.We have created two classes below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from point import createpoint

class createplanesimple:
    def __init__(self, p1, plane_size=10):
        size = plane_size / 2
        self.p1 = createpoint((p1[0] + size, p1[1]+size, p1[2]))
        self.p2 = createpoint((p1[0] - size, p1[1]+size, p1[2]))
        self.p3 = createpoint((p1[0] + size, p1[1]-size, p1[2]))
        self.p4 = createpoint((p1[0] - size, p1[1]-size, p1[2]))
        self.normal = createpoint((0, 1, 0))

    def draw(self):
        glBegin(GL_TRIANGLE_STRIP)
        glColor3f(0.9, 0.9, 0.9)
        glNormal3f(self.normal.x, self.normal.y, self.normal.z)
        glVertex3f(self.p1.x, self.p1.y, self.p1.z)
        glVertex3f(self.p2.x, self.p2.y, self.p2.z)
        glVertex3f(self.p3.x, self.p3.y, self.p3.z)

        glVertex3f(self.p2.x, self.p2.y, self.p2.z)
        glVertex3f(self.p3.x, self.p3.y, self.p3.z)
        glVertex3f(self.p4.x, self.p4.y, self.p4.z)
        glEnd()
        self.p1.draw((0, 0, 1))
        self.p2.draw((0, 0, 1))
        self.p3.draw((0, 0, 1))
        self.p4.draw((0, 0, 1))


class createplane:
    def __init__(self, p1, p2, p3):
        self.p1 = createpoint(p1)
        self.p2 = createpoint(p2)
        self.p3 = createpoint(p3)
        self.normal = createpoint((0, 1, 0))


    def draw(self):
        glBegin(GL_TRIANGLE_STRIP)
        glColor3f(0.8, 0.8, 0.8)
        glNormal3f(self.normal.x, self.normal.y, self.normal.z)
        glVertex3f(self.p1.x, self.p1.y, self.p1.z)
        glVertex3f(self.p2.x, self.p2.y, self.p2.z)
        glVertex3f(self.p3.x, self.p3.y, self.p3.z)
        glEnd()
        self.p1.draw((1, 0, 0))
        self.p2.draw((0, 1, 0))
        self.p3.draw((0, 0, 1))