ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Open a GLUT window and draw a sphere using Python/OpenGL
Submitter: Rick Muller (other recipes)
Last Updated: 2004/10/27
Version no: 1.0
Category: Image

 

Not Rated yet


Description:

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.

Source: Text Source

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()

Discussion:

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.



Add comment

Number of comments: 1

ImportError: No module named GL__init__, Not specified Not specified, 2006/03/20
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
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Treat the Win32 Registry ...

5. a friendly mkdir()

6. Wrapping template engine ...

7. Assignment in expression

8. Changing return value ...

9. Implementation of sets ...

10. bag collection class




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.