[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] A simple OpenGL pygame alpha blending example
This demo program demonstrates using [Py]OpenGL
via pygame for '2D' alpha blended texture/'sprites'
specified by a vertex array and the standard SDL
API exposed by pygame for sound and keyboard control.
The 1, 2, and 3 keys control the rotation of the smaller
square while 4, 5, and 6 the bigger one. For some
reason I couldn't get the letter keys or the INS,
DEL, HME, END, PGUP, PGDN keys to work under XFree...
You will need to have all the prerequisites for the
pygame mixer to work (iirc, smpeg is needed under
Linux) as well as those for OpenGL (only PyOpenGL
afai can remember).
I've successfully tested it under Linux (Slackware
8.1) and Windows XP with both a Voodoo3 and a
GeForce 4 Ti4200 card and AWE32/64 and the onboard
AC97 sound chip on the SiSK7S5A. Works great under
all configuration combinations!
Note that the ancient downloadable PyOpenGL 2.0.44
zipfile has a vertex array bug which I had to fix by
getting a specific version of Swig and recompiling
PyOpenGL. Details are in an old message I posted to
the PyOpenGL list and I can repost them here if needed.
NOTE: Without this fix, the program below will give
a cryptic OpenGL error. I've reported this problem
in the PyOpenGL mailing list a few months but they
don't seem to have fixed it yet - I think they want to
fix everything by making PyOpenGL work with the latest
Swig specs but do not have enough manpower to do so
right now.
=======================================================
import sys
import pygame
from random import choice
from pygame.locals import *
from pygame import image
from OpenGL.GL import *
rot1=0
rot2=0
direction1=0
direction2=0
# create color, texture and vertex arrays
square1Colors=[[1,0,0,1],[0,1,0,1],[0,0,1,1],[1,1,1,1]]
square1Vertices=[[-64,-64],[64,-64],[64,64],[-64,64]]
square2TexCoords=[[0,0],[1,0],[1,1],[0,1]]
square2Vertices=[[-128,-128],[128,-128],[128,128],[-128,128]]
def draw ():
global rot1,rot2,direction1,direction2
global square1Colors,square1Vertices,square2TexCoords,square2Vertices
glClearColor(0.0,0.0,0.0,0.0)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
# Draw the smaller square with corresponding rotation value
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glTranslatef(200,200,0)
glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_VERTEX_ARRAY)
glColorPointerf(square1Colors)
glVertexPointerf(square1Vertices)
glRotatef(rot2,0,0,1)
rot2=(rot2+direction2)%360
glBegin(GL_QUADS)
glArrayElement(0)
glArrayElement(1)
glArrayElement(2)
glArrayElement(3)
glEnd()
glDisableClientState(GL_COLOR_ARRAY)
glPopMatrix()
# Draw the bigger alpha textured square with its own rotation value
glPushMatrix()
glTranslatef(200,200,0)
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glEnableClientState(GL_VERTEX_ARRAY)
glTexCoordPointerf(square2TexCoords)
glVertexPointerf(square2Vertices)
glRotatef(rot1,0,0,1)
rot1=(rot1+direction1)%360
glBegin(GL_QUADS)
glArrayElement(0)
glArrayElement(1)
glArrayElement(2)
glArrayElement(3)
glTexCoord2f(0,0)
glEnd()
glDisable(GL_BLEND)
glDisable(GL_TEXTURE_2D)
glFlush()
glPopMatrix()
pygame.display.flip()
pygame.init()
pygame.display.set_mode((768,768),OPENGL|DOUBLEBUF)
# Manual texture creation, this part could very
# well be loaded from a bitmap instead.
# Every fourth value is the alpha transparency
tex=""
for i in xrange(512):
tex+="\xff\xff\xff\x7f\xff\xff\xff\x7f\xff\xff\xff\x7f\xff\xff\xff\x7f"
tex+="\xff\xff\xff\x3f\xff\xff\xff\x3f\xff\xff\xff\x3f\xff\xff\xff\x3f"
# Set texture render options
glTexImage2D(GL_TEXTURE_2D,0,4,64,64,0,GL_RGBA,GL_UNSIGNED_BYTE,tex)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
glDisable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho (0,512,0,512,-1,1)
draw()
soundList=[]
# Substitute your own wav files below
soundList.append(pygame.mixer.Sound("dingdong.wav"))
soundList.append(pygame.mixer.Sound("trumpet1.wav"))
soundList.append(pygame.mixer.Sound("boo.wav"))
while 1:
event=pygame.event.poll ()
if event.type is QUIT:
sys.exit(0)
draw()
if event.type is KEYDOWN:
if event.key is K_ESCAPE:
sys.exit(0)
if event.key is K_1:
choice(soundList).play()
direction2=0.2
if event.key is K_2:
choice(soundList).play()
direction2=-0.2
if event.key is K_3:
choice(soundList).play()
direction2=0
if event.key is K_4:
choice(soundList).play()
direction1=0.2
if event.key is K_5:
choice(soundList).play()
direction1=-0.2
if event.key is K_6:
choice(soundList).play()
direction1=0
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org