PythonOpenGLWaterMol.py

A rotating Water Molecule written in Python and OpenGL

Here is a complete and working Example of a Python (2.7) Script with OpenGL (2.1).
(I just found Code Snippets and little pieces, who doesn't work together or where deprecated)

When Python and all Libraries are installed, this Script can run without changes on Linux (Knoppix 6.7) and OSX (10.6.8 Snow Leopard, newer Python and Libraries by MacPorts)




from OpenGL.GL import *
from OpenGL.GLE import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

rotx = 0
roty = 0
rotz = 0

print "Start"

def init():
    #Info one of GL_VERSION, GL_VENDOR, GL_RENDERER, and GL_EXTENSIONS
    print "GL Version:", glGetString(GL_VERSION)
    print "GL Vendor:", glGetString(GL_VENDOR)
    print "GL Renderer:", glGetString(GL_RENDERER)
    print "GL Extensions:", glGetString(GL_EXTENSIONS)
    print "" #Newline

    #glClearColor(0, 0, 0, 0) #Black Background
    glClearColor(0.5, 0.5, 0.5, 0) #Gray Background
    #glClearColor(1, 1, 1, 0) #White Background

    glMatrixMode(GL_PROJECTION)
    #glMatrixMode(GL_MODELVIEW)

    glEnable(GL_NORMALIZE)
    glEnable(GL_CULL_FACE)
    glShadeModel(GL_SMOOTH)

    glEnable(GL_DEPTH_TEST)
    glClearDepth(1.0)
    glEnable(GL_LIGHTING)


    glLoadIdentity()

    #Light 1
    glLightfv(GL_LIGHT1, GL_AMBIENT,  [0.0, 0.0, 0.0, 1.0]) # R G B A
    glLightfv(GL_LIGHT1, GL_DIFFUSE,  [1.0, 1.0, 1.0, 1.0]) # R G B A
    glLightfv(GL_LIGHT1, GL_POSITION, [0.0, 3.0, 3.0, 0.0]) # x y z w
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [0.2, 0.2, 0.2, 1.0])

    glEnable(GL_LIGHT1)

    #Light 0
    lightZeroPosition = [10.,4.,10.,1.]
    lightZeroColor = [1.0,1.0,1.0,1.0]
    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)


    gluPerspective(40.,1.,1.,40.) # "Field of View" Angle (Deg), Aspect Ratio , Near Clip, Far Clip
    glMatrixMode(GL_MODELVIEW)



def mouse(button,state,x,y):
    global beginx,beginy,rotate
    if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
        print "Mouseclick: ",x,"x> ",y,"yv"
        rotate = 1
        beginx = x
        beginy = y
    if button == GLUT_LEFT_BUTTON and state == GLUT_UP:
        rotate = 0
    return


def motion(x,y):
    global rotx,roty,beginx,beginy,rotate
    if rotate:
        rotx = rotx + (y - beginy)
        roty = roty + (x - beginx)
        beginx = x
        beginy = y
        glutPostRedisplay()
    return

def Timer(a):
    global rotx,roty,rotz
    rotx = rotx + 1
    roty = roty + 1
    rotz = rotz + 2
    glutPostRedisplay()
    glutTimerFunc(a, Timer, a) # Milliseconds , Function call, Value for Function
    return


def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity() #Loads Global Matrix

    gluLookAt(0,0,10,   # Camera Position
    0,0,0,      # Point the Camera looks at
    0,1,0)      # the Up-Vector

    glRotatef(roty,0,1,0)
    glRotatef(rotx,1,0,0)
    glRotatef(rotz,0,0,1)

    # R  G  B  A  Red (Oxygen)
    glMaterialfv(GL_FRONT, GL_AMBIENT,  [1.0, 0.0, 0.0, 0.0])
    glMaterialfv(GL_FRONT, GL_DIFFUSE,  [1.0, 0.0, 0.0, 0.0])
    glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 0.0, 0.0, 0.0])
    glMaterialf(GL_FRONT, GL_SHININESS, 80)

    glTranslatef(0.,0.,0.) #Set Position for next Object relative to the last Translation !
    glutSolidSphere(1.0,20,20) #Insert a solid Sphere, Radius 1, Resolution 20x20

    # R  G  B  A White (Hydrogen)
    glMaterialfv(GL_FRONT, GL_AMBIENT,  [1.0, 1.0, 1.0, 0.0])
    glMaterialfv(GL_FRONT, GL_DIFFUSE,  [1.0, 1.0, 1.0, 0.0])
    glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 0.0])
    glMaterialf(GL_FRONT, GL_SHININESS, 80)

    glTranslatef(0.,1.,0.) #Set Position for next Object relative to the last Translation
    glutSolidSphere(0.7,20,20) # Insert a solid Sphere, Radius 0.7, Resolution 20x20

    glTranslatef(0.97,-1.25,0.0) #Set Position for next Object relative to the last Translation
    glutSolidSphere(0.7,20,20) # Insert a solid Sphere, Radius 0.7, Resolution 20x20
    
    glTranslatef(-0.97,0.25,0.0) #Set Position for next Object relative to the last Translation = 0

    #Labels
    glRasterPos2f(1.8,-0.5)
    glutBitmapString(GLUT_BITMAP_HELVETICA_18,"H -")

    glRasterPos2f(0.0,1.8)
    glutBitmapString(GLUT_BITMAP_HELVETICA_18,"H -")

    glRasterPos2f(-0.9,-0.9)
    glutBitmapString(GLUT_BITMAP_HELVETICA_18,"O +")

    glutSwapBuffers()
    glFlush()
    return

print "Hello GLUT"
glutInit('Hello GLUT')
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH)
glutInitWindowSize(800, 800)
glutCreateWindow('Hello GLUT')
#glutSetDisplayFuncCallback(display)  # Deprecated !
glutDisplayFunc(display)
glutMouseFunc(mouse)
glutMotionFunc(motion)

Timer(40) # Milliseconds

print "Init"
init()
print "Loop"
glutMainLoop()

print "Will never be printed#Closing the Window stops the Script


Script as Text (Remove the Ending .txt)


Important Hints: Do NOT use Python Threading, because the "glutPostRedisplay()"-Command can't be called from other Threads, use "glutTimerFunc()" instead !


Vertices with 4 Vectors: x, y, z, w
    x,y,z Koordinate and w Type.
    It's a position when w is 1.
    It's a direction when w is 0.