#+TITLE: Python CAD Tutorial 02 - Draw points in 3d Space
#+DATE: 2013-10-02 12:00:00 UTC
#+DESCRIPTION: How to draw points in 3D space
#+FILETAGS: GTK-3:python:cad:opengl
#+CATEGORY: python
#+SLUG: 02-cad-draw-points-in-3d-space
#+THUMBNAIL: ../../../images/cad/tut02-interface.png
#+BEGIN_COMMENT
.. title:  Python CAD Tutorial 02 - Draw points in 3d Space
.. slug: 02-cad-draw-points-in-3d-space
.. date: 2013-10-02 12:00:00 UTC
.. tags: GTK-3, python, cad, opengl
.. category: python
.. description: How to draw points in 3D space  
.. type: text
#+END_COMMENT

[[https://code.launchpad.net/~oly/fabricad/tut02][View/Download Code]]

#+CAPTION: Point in 3D space 
[[../../../images/cad/tut02-interface.png]]

 In this tutorial we will create a 3D point class and position and draw it to the screen.

This will be a base class for storing positions in space, we will also implement code to display the points in this class. For now we will hard code a single point in the camera view. We do not have a working camera class yet, so this will be fixed with a position so we know its working.We will store the x, y and z positions and colour for the point along with the display size. The important methods here are the '__init__' and the 'draw' method, the others are helper methods which we will use later on.The helper methods include:

 + The __eq__ method will be usefull for testing if two points share the same location in space.
 + __str__ will format the point into a string, which is usefull for debugging.
 + get_position will return a numeric version of the point as a tuple.


#+BEGIN_SRC python
class createpoint:
    x = y = z = 0.0
    display_color = (0, 0, 1)

    def __init__(self, p, c=(0, 1, 0)):
        """ Position in 3d space as a tuple or list, and colour in tuple or list format"""
        self.point_size = 5
        self.color = c
        self.display_color = c
        self.x, self.y, self.z = p

    def get_position(self):
        """ Return the cordinates as a tuple"""
        return self.x, self.y, self.z

    def glvertex(self):
        """ Opengl vertex useful so we can dont have to glbegin and glend for each point"""
        glVertex3f(self.x, self.y, self.z)

    def __getitem__(self, index):
        """ Get a cordinate handy for use in for loops where we want to calculate things"""
        return (self.x, self.y, self.z)[index]

    def __str__(self):
        """ Print point cordinates useful for debugging"""
        return '(%s, %s, %s)' % (str(self.x), str(self.y), str(self.z))

    def __eq__(self, point):
        """ Equality test so we can test if points occupy same space"""
        return self.x == point.x and self.y == point.y and self.z == point.z

    def draw(self, c=(0, 1, 0)):
        """ Set the size of the point and render"""
        glPointSize(self.point_size)
        glBegin(GL_POINTS)
        glColor3f(self.color[0], self.color[1], self.color[2])
        glVertex3f(self.x, self.y, self.z)
        glEnd()
#+END_SRC

Update the draw method to test out new code works.

#+BEGIN_SRC python
def on_draw(self, *args):
    """ Test code to make sure we can draw a pixel successfully can play with the parameters here"""
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT)
    self.glwrap.draw_start()
    test_point = point.createpoint((0,0,0.5))
    test_point.draw()

    self.glwrap.draw_finish()
#+END_SRC
