import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * from random import * gravity = Vector([0, -2, 0]) tex = Texture2D('fireparticle.png') class particle: def __init__(self, pos=Vector([0,0,0])): self.pos = pos[:] self.pos[0] += gauss(0,0.25) self.pos[1] += uniform(0,0.1) self.vel = Vector([uniform(-1,1), 6, uniform(-1,1)]) self.removeTime = time.time() + abs(gauss(0.0,0.25)) def update(self, dt): self.pos += self.vel * dt self.vel += gravity * dt class particleSystem: def __init__(self, num=1): self.particles = [] self.addParticles(num) def addParticles(self, num): for i in range(0,num): p = particle() self.particles.append(p) def draw(self): glColor3f(1,1,1) tex.apply() glEnable(GL_BLEND) glBlendFunc(GL_ONE, GL_ONE) glDepthMask(GL_FALSE) glBegin(GL_QUADS) for p in self.particles: glTexCoord2f(0,0) glVertex3f(p.pos[0]-0.2, p.pos[1]-0.2, p.pos[2]) glTexCoord2f(1,0) glVertex3f(p.pos[0]+0.2, p.pos[1]-0.2, p.pos[2]) glTexCoord2f(1,1) glVertex3f(p.pos[0]+0.2, p.pos[1]+0.2, p.pos[2]) glTexCoord2f(0,1) glVertex3f(p.pos[0]-0.2, p.pos[1]+0.2, p.pos[2]) glEnd() glDepthMask(GL_TRUE) glDisable(GL_BLEND) tex.disable() def update(self, dt): for p in self.particles: p.update(dt) t = time.time() for i in range(len(self.particles)-1, -1, -1): if (self.particles[i].removeTime <= t) or (self.particles[i].pos[1] < 0): del self.particles[i] self.addParticles(1) systems = [particleSystem(100)] camera = PerspCamera() camera.moveBackward(5) camera.moveUp(1) def draw(): Color.Black.clear() camera.apply() for s in systems: s.draw() glutSwapBuffers() def keyboard(key, x, y): global viewDistance if key == chr(27): sys.exit(0) elif key == '.': camera.moveForward(1) elif key == ',': camera.moveBackward(1) def specialkey(key,x,y): global cameraVelX, cameraVelY if key == GLUT_KEY_LEFT: cameraVelY += 90 elif key == GLUT_KEY_RIGHT: cameraVelY -= 90 elif key == GLUT_KEY_UP: cameraVelX += 90 elif key == GLUT_KEY_DOWN: cameraVelX -= 90 def specialkeyUp(key,x,y): global cameraVelX, cameraVelY if key == GLUT_KEY_LEFT: cameraVelY -= 90 elif key == GLUT_KEY_RIGHT: cameraVelY += 90 elif key == GLUT_KEY_UP: cameraVelX -= 90 elif key == GLUT_KEY_DOWN: cameraVelX += 90 cameraVelX = 0.0 cameraVelY = 0.0 prevTime = time.time() def update(dummy): glutTimerFunc(16,update,0) global viewRotX, viewRotY, cameraVelX, cameraVelY global prevTime t = time.time() dt = t - prevTime prevTime = t camera.pitch(cameraVelX * dt) camera.turn(cameraVelY * dt) for s in systems: s.update(dt) for i in range(len(systems)-1, -1, -1): if len(systems[i].particles) == 0: del systems[i] glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutTimerFunc(0,update,0) glEnable(GL_DEPTH_TEST) glutMainLoop()