Welcome, guest | Sign In | My Account | Store | Cart

This code snippet uses Python/OpenGL (http://pyopengl.sourceforge.net) to open a window using GLUT, and draw a sphere into it. I've used this many times as the starting point for more complicated applications.

Python, 47 lines
 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
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys

name = 'ball_glut'

def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
    glutInitWindowSize(400,400)
    glutCreateWindow(name)

    glClearColor(0.,0.,0.,1.)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_CULL_FACE)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_LIGHTING)
    lightZeroPosition = [10.,4.,10.,1.]
    lightZeroColor = [0.8,1.0,0.8,1.0] #green tinged
    glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
    glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
    glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
    glEnable(GL_LIGHT0)
    glutDisplayFunc(display)
    glMatrixMode(GL_PROJECTION)
    gluPerspective(40.,1.,1.,40.)
    glMatrixMode(GL_MODELVIEW)
    gluLookAt(0,0,10,
              0,0,0,
              0,1,0)
    glPushMatrix()
    glutMainLoop()
    return

def display():
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glPushMatrix()
    color = [1.0,0.,0.,1.]
    glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
    glutSolidSphere(2,20,20)
    glPopMatrix()
    glutSwapBuffers()
    return

if __name__ == '__main__': main()

The Python/OpenGL bindings make OpenGL programming fairly easy in Python. I use the following code snippet to test whether Python/OpenGL is installed properly, and as a starting point to build more complex OpenGL applications.

3 comments

jon 18 years, 1 month ago  # | flag

ImportError: No module named GL__init__. hi on Fedora Core 3 this is what happens to me with the above code:

$ python blah.py Traceback (most recent call last): File "blah.py", line 1, in ? from OpenGL.GLUT import * File "/usr/lib/python2.3/site-packages/PIL/__init__.py", line 26, in ?

File "/usr/lib/python2.3/site-packages/PIL/__init__.py", line 2, in ? # The Python Imaging Library. ImportError: No module named GL__init__

that's with python-opengl and freeglut installed. i'm hoping that somebody has seen this error before and has a clue what it might be. i have python-imaging and python-numeric installed also, which i read are important here.

best jon

Jon, You need to run this command in the terminal:

sudo easy_install PyOpenGL

All the best, Alex

Jonny Galloway 10 years, 5 months ago  # | flag

I get this error (and I've had similar with other test code) I am not sure what I have done wrong?

TypeError: 'NoneType' object is not callable

line 9, in main glutInit(sys.argv)

File "E:\Python26\Lib\site-packages\PyOpenGL-3.0.2-py2.6.egg\OpenGL\GLUT\special.py", line 324, in glutInit _base_glutInit( ctypes.byref(count), holder )