i finally took the jump into OpenGL, and its not as bad as i thought (althought it is far too C, if you know what i mean). The pyOlenGL module provides a full wrapper, although i would like a more pythonic layer on top of ogl, just to soften the blow. Anyway, here’s my first OpenGL program (its hoping to grow into a pong-type game)
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *
def resize((width, height)):
if height==0:
height=1
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
#gluPerspective(45, 1.0*width/height, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def init():
global quad
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
quad=gluNewQuadric()
def draw(x,y):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
y=600-y
#square((x,y,0),(x+100,y,0),(x+100,y+100,0),(x,y+100,0),(255,100,0))
#sqaure(x,y,100,100)
cube(100,100,0,100,100,-100)
def square(v1,v2,v3,v4,color=(255,255,255)):
def cx(x):return (1.0/300)*x-1.0
glBegin(GL_QUADS)
glColor3f(*[c*(1.0/255) for c in color])
for point in [v1,v2,v3,v4]:
glVertex3f(*[cx(o) for o in point])
glEnd()
def sqaure(x,y,w,h,color=(255,255,255)):
def cx(x):return (1.0/300)*x-1.0
glBegin(GL_QUADS)
glColor3f(*[c*(1.0/255) for c in color])
glVertex2f(cx(x),cx(y))
glVertex2f(cx(x)-cx(w),cx(y))
glVertex2f(cx(x)-cx(w),cx(y)-cx(h))
glVertex2f(cx(x),cx(y)-cx(h))
glEnd()
xat=0
def cube(x,y,z,w,h,d,color=(255,255,255)):
global xat
xat+=0.1
def cx(ax):return (1.0/300)*ax-1.0
glRotatef(xat,1.0,1.0,0.0)
glBegin(GL_QUADS)
glColor3f(*[c*(1.0/255) for c in color])
glVertex3f(cx(x),cx(y),cx(z))
glVertex3f(cx(x)-cx(w),cx(y),cx(z))
glVertex3f(cx(x)-cx(w),cx(y)-cx(h),cx(z))
glVertex3f(cx(x),cx(y)-cx(h),cx(z))
glEnd()
glColor3f(*[c*(1.0/255)-0.5 for c in color])
glBegin(GL_QUADS)
glVertex3f(cx(x),cx(y),cx(z)+cx(d))
glVertex3f(cx(x)-cx(w),cx(y),cx(z)+cx(d))
glVertex3f(cx(x)-cx(w),cx(y)-cx(h),cx(z)+cx(d))
glVertex3f(cx(x),cx(y)-cx(h),cx(z)+cx(d))
glColor3f(*[c*(1.0/255)-0.1 for c in color])
# glVertex3f(cx(x),cx(y),cx(z)+cx(d))
# glVertex3f(cx(x)-cx(w),cx(y),cx(z)+cx(d))
# glVertex3f(cx(x)-cx(w),cx(y),cx(z))
# glVertex3f(cx(x),cx(y),cx(z))
glEnd()
def main():
x=y=0
video_flags = OPENGL|DOUBLEBUF
pygame.init()
pygame.display.set_mode((600,600), video_flags)
resize((600,600))
init()
frames = 0
ticks = pygame.time.get_ticks()
while 1:
if pygame.event.get(QUIT):
break
for ev in pygame.event.get():
if ev.type==MOUSEMOTION:
x,y=ev.pos
if ev.type==MOUSEBUTTONDOWN:
mp=1
if ev.type==MOUSEBUTTONUP:
mp=0
pygame.event.pump()
draw(x,y)
pygame.display.flip()
frames = frames+1
print “fps: %d” % ((frames*1000)/(pygame.time.get_ticks()-ticks))
pygame.display.quit()
if __name__ == ‘__main__’: main()