static-sites/bases/do-blog/resources/documents/python/cad/06-cad-drawing-planes.org

3.1 KiB
Executable File

Python CAD Tutorial 06 - Drawing planes in 3D space

.. title: Python CAD Tutorial 06 - Drawing planes in 3D space .. slug: 06-cad-drawing-planes-in-3d-space.org .. date: 2013-10-06 12:00:00 UTC .. tags: GTK-3, python, cad, opengl .. category: python .. description: Drawing planes in 3d space .. type: text

View/Download Code

/oly/static-sites/src/commit/696dfdf65c71b1882ab90018cd493e56bb1aedca/bases/do-blog/resources/images/cad/tut06-interface.png
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:

  • The first class will be used for the grid in the next tutorial. A plane does not normally have a limit of size but in this we are limiting it for display purposes.'createplanesimple' takes a point in space and a size. It will then calculate the plane by appending the size to the x and y coridinates, z will remain fixed.
  • The second class is a more accurate way of representing a plane. Usually we only need to store a point in space and a direction, so by adding the coordinates together we can get a central point and direction this will be added at a later date.
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))