Original Post
Hello I am working on my first particle engine ever. I think I understand the concept however it seems to be opperating pretty sluggish,50fps or so for 30 particles + game. My game before my particle engine was pumping along at about 90 fps+ and it's not complex at all. So correct me if I am wrong here, A particle is a quad floating infront of your screen(HUD of sorts?) textured using alpha-blending(to remove transparent colors). The way that these particles move are based then on other commands described. For my code I am trying to imitate snow, not perfectly by any means but I was wondering if anyone could introduce me to some GL Commands that might speed up what I am doing, or present a better method in doing this. I am not asking you for your particle engine or your code, just like he use this "blank" method in the GL object to texture it it renders quicker or something. YES I did read the NeHe ported tutorial however I couldn't figure out how to get his example running in/with my game without it either not being rendered or it consuming all of my other renderings(possibly having to do with the fact that I do not have the .getPixles() command in my JOGL library)... So i just wrote my own from scratch that is obviously insufficient...
import javax.media.opengl.GL;
import com.sun.opengl.util.texture.*;
public class ParticleHandler {
private static final int MAX_PARTICLES = 30;
private ImageHandler images = new ImageHandler();
private Texture particleTexture;
private int ScreenWidth, ScreenHeight;
private static class Particle {
boolean alive = true;
float fade;
float speed;
float[] xyz = {0, 0, 0};
float[] size = {4.0f, 4.0f};
double angle = 0;
}
private Particle[] particles = new Particle[MAX_PARTICLES];
public void prepareParticles(GL gl, int ScreenW, int ScreenH) {
particleTexture = images.loadTexture("Particle.png");
ScreenWidth = ScreenW;
ScreenHeight = ScreenH;
for (int loop = 0; loop < MAX_PARTICLES; loop++) {
particles[loop] = new Particle();
particles[loop].fade = (float) (2.0 * Math.random());
particles[loop].speed = (float) 1;
particles[loop].xyz[0] = (float) (ScreenW * Math.random());
particles[loop].xyz[1] = (float) (ScreenH * Math.random());
particles[loop].angle = Math.toRadians(90);
}
}
public void renderParticles(GL gl) {
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL.GL_ALPHA_TEST);
gl.glAlphaFunc(GL.GL_GREATER, 0);
gl.glEnable(GL.GL_TEXTURE_2D);
particleTexture.bind();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
for (int loop = 0; loop < MAX_PARTICLES; loop++) {
if (particles[loop].alive == true) {
float x = particles[loop].xyz[0], y = particles[loop].xyz[1], z = particles[loop].xyz[2];
x += (float) (Math.cos(particles[loop].angle) * (particles[loop].speed));
particles[loop].xyz[0] = x;
y += (float) (Math.sin(particles[loop].angle) * (particles[loop].speed));
particles[loop].xyz[1] = y;
if(x > ScreenWidth || y > ScreenHeight || x < 0 || y < 0){particles[loop].alive = false;}
float xSize = particles[loop].size[0] * particles[loop].fade;
float ySize = particles[loop].size[1] * particles[loop].fade;
if(particles[loop].alive == true){
gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glTexCoord2d(1, 1);
gl.glVertex3f(x + xSize, y + ySize, z);
gl.glTexCoord2d(0, 1);
gl.glVertex3f(x - xSize, y + ySize, z);
gl.glTexCoord2d(1, 0);
gl.glVertex3f(x + xSize, y - ySize, z);
gl.glTexCoord2d(0, 0);
gl.glVertex3f(x - xSize, y - ySize, z);
gl.glEnd();
}
if(particles[loop].alive == false){
particles[loop].xyz[0] = (float) (ScreenWidth * Math.random());
particles[loop].xyz[1] = (float) ((ScreenHeight/10) * Math.random());
particles[loop].alive = true;
particles[loop].fade = (float) (2.0 * Math.random());
}
}
}
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);
gl.glDisable(GL.GL_ALPHA);gl.glDisable(GL.GL_BLEND);
}
}