Luckily Python has set of OpenGL bindings --PyOpenGL-- which magically (No seriously, I don't know how it works.
When I say "ready-to-roll" what I really mean is an intimidating mess of undocumented arrgghh. There is no "PyOpenGL for Dummies" to guide you through the initial steps, which makes it very unappealing to those without any expirience with OpenGL. Thankfully I came across a PyGame-PyOpenGL sample which I was able to use to get the basics done (initalizing OpenGL and creating displays).
The Python binding's are --as far as I'm aware-- identical to the native OpenGL ones so I was able to use a great introductionary tutorial on GameDev.net to produce my first ever 3D... thingy!
The code doesn't reflect the tutorial as closely as my 2D game engine does, but it should still be easy to follow.
- Code: Select all
#!/usr/bin/env python
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
def pyramid():
glBegin(GL_TRIANGLES)
glColor3f(1.0, 0.0, 0.0) # // Red
glVertex3f(0.0, 1.0, 0.0) # // Top Of Triangle (Front)
glColor3f(0.0, 1.0, 0.0) # // Green
glVertex3f(-1.0, -1.0, 1.0) # // Left Of Triangle (Front)
glColor3f(0.0, 0.0, 1.0) # // Blue
glVertex3f(1.0, -1.0, 1.0) # // Right Of Triangle (Front)
glColor3f(1.0, 0.0, 0.0) # // Red
glVertex3f(0.0, 1.0, 0.0) # // Top Of Triangle (Right)
glColor3f(0.0,0.0, 1.0) # // Blue
glVertex3f(1.0, -1.0, 1.0) # // Left Of Triangle (Right)
glColor3f(0.0, 1.0, 0.0) # // Green
glVertex3f(1.0, -1.0, -1.0) # // Right Of Triangle (Right)
glColor3f(1.0, 0.0, 0.0) # // Red
glVertex3f(0.0, 1.0, 0.0) # // Top Of Triangle (Back)
glColor3f(0.0,1.0, 0.0) # // Green
glVertex3f(1.0, -1.0, -1.0) # // Left Of Triangle (Back)
glColor3f(0.0, 0.0, 1.0) # // Blue
glVertex3f(-1.0, -1.0, -1.0) # // Right Of Triangle (Back)
glColor3f(1.0, 0.0, 0.0) # // Red
glVertex3f(0.0, 1.0, 0.0) # // Top Of Triangle (Left)
glColor3f(0.0, 0.0, 1.0) # // Blue
glVertex3f(-1.0, -1.0, -1.0) # // Left Of Triangle (Left)
glColor3f(0.0, 1.0, 0.0) # // Green
glVertex3f(-1.0, -1.0, 1.0) # // Right Of Triangle (Left)
glEnd()
glBegin(GL_QUADS)
glColor3f(0.0, 0.0, 1.0)
glVertex3f(-1.0, -1.0, -1.0)
glColor3f(0.0, 1.0, 0.0)
glVertex3f(1.0, -1.0, -1.0)
glColor3f(0.0, 0.0, 1.0)
glVertex3f(1.0, -1.0, 1.0)
glColor3f(0.0, 1.0, 0.0)
glVertex3f(-1.0, -1.0, 1.0)
glEnd()
def cube():
glBegin(GL_QUADS)
glColor3f(0.0, 1.0, 0.0) # // Set The Color To Green
glVertex3f(1.0, 1.0, -1.0) # // Top Right Of The Quad (Top)
glVertex3f(-1.0, 1.0, -1.0) # // Top Left Of The Quad (Top)
glVertex3f(-1.0, 1.0, 1.0) # // Bottom Left Of The Quad (Top)
glVertex3f(1.0, 1.0, 1.0) # // Bottom Right Of The Quad (Top)
glColor3f(1.0, 0.5, 0.0) # // Set The Color To Orange
glVertex3f(1.0, -1.0, 1.0) # // Top Right Of The Quad (Bottom)
glVertex3f(-1.0, -1.0, 1.0) # // Top Left Of The Quad (Bottom)
glVertex3f(-1.0, -1.0, -1.0) # // Bottom Left Of The Quad (Bottom)
glVertex3f(1.0, -1.0, -1.0) # // Bottom Right Of The Quad (Bottom)
glColor3f(1.0, 0.0, 0.0) # // Set The Color To Red
glVertex3f(1.0, 1.0, 1.0) # // Top Right Of The Quad (Front)
glVertex3f(-1.0, 1.0, 1.0) # // Top Left Of The Quad (Front)
glVertex3f(-1.0, -1.0, 1.0) # // Bottom Left Of The Quad (Front)
glVertex3f(1.0, -1.0, 1.0) # // Bottom Right Of The Quad (Front)
glColor3f(1.0, 1.0, 0.0) # // Set The Color To Red
glVertex3f(1.0, -1.0, -1.0) # // Top Right Of The Quad (Back)
glVertex3f(-1.0, -1.0, -1.0) # // Top Left Of The Quad (Back)
glVertex3f(-1.0, 1.0, -1.0) # // Bottom Left Of The Quad (Back)
glVertex3f(1.0, 1.0, -1.0) # // Bottom Right Of The Quad (Back)
glColor3f(0.0, 0.0, 1.0) # // Set The Color To Blue
glVertex3f(-1.0, 1.0, 1.0) # // Top Right Of The Quad (Left)
glVertex3f(-1.0, 1.0, -1.0) # // Top Left Of The Quad (Left)
glVertex3f(-1.0, -1.0, -1.0) # // Bottom Left Of The Quad (Left)
glVertex3f(-1.0, -1.0, 1.0) # // Bottom Right Of The Quad (Left)
glColor3f(1.0, 0.0, 1.0) # // Set The Color To Violet
glVertex3f( 1.0, 1.0, -1.0) # // Top Right Of The Quad (Right)
glVertex3f( 1.0, 1.0, 1.0) # // Top Left Of The Quad (Right)
glVertex3f( 1.0, -1.0, 1.0) # // Bottom Left Of The Quad (Right)
glVertex3f( 1.0, -1.0, -1.0) # // Bottom Right Of The Quad (Right)
glEnd()
if __name__ == "__main__":
from sys import exit
print "Initalizing..."
resolution = (400, 300)
pygame.init()
screen = pygame.display.set_mode(resolution, pygame.OPENGL|pygame.DOUBLEBUF)
print "Doing GL stuff..."
glViewport(0, 0, resolution[0], resolution[1])
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(resolution[0]) / resolution[1], 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
print "More GL stuff..."
glEnable(GL_DEPTH_TEST)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
print "Minor details..."
clock = pygame.time.Clock()
rot_tri = 0
rot_quad = 0
import random
print "Mainloop..."
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity()
glTranslatef(-1.5, 0.0, -6.0)
glRotatef(rot_tri, 0.0, 1.0, 0.0)
pyramid()
glLoadIdentity()
glTranslate(1.5, 0.0, -6.0)
glRotatef(rot_quad, 1.0, 0.0, 0.0)
cube()
rot_tri += 2.0
rot_quad -= 1.5
pygame.display.set_caption("hello_opengl.py FPS: %i" % clock.get_fps())
pygame.display.flip()
clock.tick()
For the above to work you'll need PyGame and PyOpenGL. If it works properly you should see two rotating polygons. A sqaure-based pyramid and cube. Warning: The acelleration package that is an optional extra with PyOpenGL may cause problems with certain configurations. To work around it, simply uninstall (or rename) the package in Lib/site-packages.
