- from OpenGL.GLUT import *
- from OpenGL.GLU import *
- from OpenGL.GL import *
- from Image import *
- import particle, random
- class ParticleSwarm(object):
- def __init__(self):
- self.MAX_PARTICLES = 100
- self.slowdown = 2
- self.xspeed = 0.0
- self.yspeed = 0.0
- self.zoom = -40.0
- self.loop = 0
- self.col = 0
- self.xpos = 0
- self.ypos = -0.1
- self.zpos = 0
- self.texture = self.loadTexture()
- self.particles = [particle.Particle()] * self.MAX_PARTICLES
- for loop in range(self.MAX_PARTICLES):
- self.particles[loop].active = True
- self.particles[loop].life=1.0; # Give All The Particles Full Life
- self.particles[loop].fade=0.2 # Random Fade Speed
- self.particles[loop].xi=random.random() # Random Speed On X Axis
- self.particles[loop].yi=random.random() # Random Speed On Y Axis
- self.particles[loop].zi=random.random() # Random Speed On Z Axis
- self.particles[loop].xg=0.0 # Set Horizontal Pull To Zero
- self.particles[loop].yg=-10 # Set Vertical Pull Downward
- self.particles[loop].zg=0.0
- def loadTexture(self):
- """Creates the Textureobject for the particles"""
- img = open("particles/Particle.png")
- img = img.tostring("raw", "RGBX", 0, -1)
- texture = glGenTextures(1)
- glBindTexture(GL_TEXTURE_2D, texture)
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 32, 32, 0, GL_RGBA, GL_UNSIGNED_BYTE, img)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
- return texture
- def display(self):
- for loop in range(self.MAX_PARTICLES):
- if self.particles[loop].active:
- x = self.particles[loop].x
- y = self.particles[loop].y
- z = self.particles[loop].z
- size = 0.01
- #load the Textureobject
- glEnable(GL_TEXTURE_2D)
- glBindTexture(GL_TEXTURE_2D, self.texture)
- glColor4f(1.0,1.0,1.0,1.0);
- #glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- glBegin(GL_QUADS); # Build Quad From A Triangle Strip
- glTexCoord2d(1,1); glVertex3f(x+size,y+size,z); # Top Right
- glTexCoord2d(0,1); glVertex3f(x-size,y+size,z); # Top Left
- glTexCoord2d(1,0); glVertex3f(x+size,y-size,z); # Bottom Right
- glTexCoord2d(0,0); glVertex3f(x-size,y-size,z); # Bottom Left
- glEnd(); # Done Building Triangle Strip
- glDisable(GL_TEXTURE_2D)
- self.particles[loop].x+=self.particles[loop].xi/(self.slowdown*1000)# Move On The X Axis By X Speed
- self.particles[loop].y+=self.particles[loop].yi/(self.slowdown*1000)# Move On The Y Axis By Y Speed
- self.particles[loop].z+=self.particles[loop].zi/(self.slowdown*1000)# Move On The Z Axis By Z Speed
- self.particles[loop].xi+=self.particles[loop].xg # Take Pull On X Axis Into Account
- self.particles[loop].yi+=self.particles[loop].yg # Take Pull On Y Axis Into Account
- self.particles[loop].zi+=self.particles[loop].zg # Take Pull On Z Axis Into Account
- self.particles[loop].life-=self.particles[loop].fade # Reduce Particles Life By 'Fade'
- if self.particles[loop].life<0.0: # If Particle Is Burned Out
- self.particles[loop].life=1.0 # Give It New Life
- self.particles[loop].fade=0.15 # Random Fade Value
- self.particles[loop].x=self.xpos # Center On X Axis
- self.particles[loop].y=self.ypos # Center On Y Axis
- self.particles[loop].z=self.zpos # Center On Z Axis
- self.particles[loop].xi=self.xspeed+(10*(random.random())) # X Axis Speed And Direction
- self.particles[loop].yi=self.yspeed+(10*(random.random())) # Y Axis Speed And Direction
- self.particles[loop].zi=((60 * random.random())-30.0) # Z Axis Speed And Direction