How to do it...

In order to use PyOpenGL, we have to first install it. The following URL is the official Python package installer website: 

https://pypi.python.org/pypi/PyOpenGL/3.0.2.

This seems to be the correct installation but, as it turns out, it doesn't work with Windows 10 64-bit OS with Python 3.6 64-bit.

A better place to look for Python installation packages was mentioned in a recipe in the previous chapter. You are probably already familiar with it. The URL is http://www.lfd.uci.edu/~gohlke/pythonlibs/.

We have to download the package that matches both our OS and our Python version. It comes with the new .whl format. If you are using an older version of Python, you have to install the Python wheel package first:

How to install the Python wheel package is described in a recipe, Installing Matplotlib using pip with whl extension, in Chapter 5, Matplotlib Charts.

Installing PyOpenGL via the PyOpenGL-3.1.1-cp36-cp36m-win_amd64.whl file using the pip command is both successful and installs all the 64-bit modules we require.

Replace <your full path> with the full path you downloaded the .whl installer to:

    pip install <your full path> PyOpenGL-3.1.1-cp36-cp36m-win_amd64.whl

When we now try to import some PyOpenGL modules, it works, as can be seen in this code example:

import wx                   
from wx import glcanvas
from OpenGL.GL import *
from OpenGL.GLUT import *

All this code is doing is importing several OpenGL Python modules, in addition to importing wxPython. It does not do anything else but, when we run our Python module, we do not get any errors.

This proves that we have successfully installed the OpenGL bindings to Python.

Now that our development environment has been successfully set up, we can try it out using wxPython.

Many online examples are restricted to using Python 2 as well as using the classic version of wxPython. We are using Python 3.6 and Phoenix.

Using the code based on the wxPython demo examples creates a working 3D cube. In contrast, running the cone example did not work, but this example got us started on the right track.

Here is the URL: http://wiki.wxpython.org/GLCanvas%20update

Here are some modifications to the code:

import wx 
from wx import glcanvas
from OpenGL.GL import *
from OpenGL.GLUT import *

class MyCanvasBase(glcanvas.GLCanvas):
def __init__(self, parent):
glcanvas.GLCanvas.__init__(self, parent, -1)
# This context was missing from the original code
self.context = glcanvas.GLContext(self)

def OnPaint(self, event):
dc = wx.PaintDC(self)
# self.SetCurrent() # commented out because:
self.SetCurrent(self.context) # We have to pass in a context

We now can create the following GUI:

import_OpenGL_cube_and_cone.py

In the classic version of wxPython, SetCurrent() did not require a context. Here is some code we might find when searching online:

    def OnPaint(self, event): 
dc = wx.PaintDC(self)
self.SetCurrent()
if not self.init:
self.InitGL()
self.init = True
self.OnDraw()

The preceding code does not work when using wxPython Phoenix. We can look up the correct syntax for Phoenix online:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.49.247