Permalink
Browse files

Tweaks to particle emitter and partciles, Added transparent sprites

  • Loading branch information...
bettsmatt committed Oct 7, 2012
1 parent bfce826 commit 2cafa7c79c76f41e4ae41ecbffc33e9b83d8c42b
Showing with 114 additions and 49 deletions.
  1. BIN BetaFX
  2. +25 −16 Particle.cpp
  3. +2 −0 Particle.h
  4. +37 −9 ParticleEmitter.cpp
  5. +2 −2 define.h
  6. +48 −22 main.cpp
View
BIN +1.76 KB (100%) BetaFX
Binary file not shown.
View
@@ -40,18 +40,12 @@ Particle::Particle(float* initialVelocity) {
color = new float [3];
float HI = 1;
float LO = 0;
color[0] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
color[1] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
color[2] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
/*
* Lifespan
*/
HI = 2000;
LO = 500;
lifeSpanLeft = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
float HI = 20000;
float LO = 18000;
lifeSpanLeft = lifeSpan = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
}
@@ -98,10 +92,11 @@ void Particle::renderParticle() {
glPushMatrix();
// Fade color from red to blue over time
glColor3f(
color[0],
color[1],
color[2]
(float)lifeSpanLeft / (float)lifeSpan,
0,
1 - ((float)lifeSpanLeft / (float)lifeSpan)
);
// Translate to where the particle is
@@ -116,6 +111,7 @@ void Particle::renderParticle() {
* This is the cheapest implementation, and will serve for now,
* We just remove the rotation information from the modelview matrix
*/
float modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);
@@ -131,14 +127,27 @@ void Particle::renderParticle() {
// Set the modelview with no rotations and scaling
glLoadMatrixf(modelview);
float size = 0.1f;
// Draw Quad
glBegin(GL_QUADS);
glVertex3f(-0.1f, 0.1f, 0.0f);
glVertex3f( 0.1f, 0.1f, 0.0f);
glVertex3f( 0.1f,-0.1f, 0.0f);
glVertex3f(-0.1f,-0.1f, 0.0f);
glTexCoord2f(0, 1);
glVertex3f(-1 * size, 1 * size, 0.0f);
glTexCoord2f(1, 1);
glVertex3f( 1 * size, 1 * size, 0.0f);
glTexCoord2f(1, 0);
glVertex3f( 1 * size,-1 * size, 0.0f);
glTexCoord2f(0,0);
glVertex3f(-1 * size,-1 * size, 0.0f);
glEnd();
glPopMatrix();
}
View
@@ -28,6 +28,8 @@ class Particle
float* velocity; // x, y, z
float* acceletation; // x, y, z
float mass;
int lifeSpan;
int lifeSpanLeft;
float* color;
View
@@ -39,7 +39,7 @@ ParticleEmitter::ParticleEmitter(void) {
float* p = new float[3];
p[0] = 5;
p[1] = -3;
p[1] = 0;
setGravity(p);
gravityOn=true;
@@ -65,13 +65,20 @@ bool ParticleEmitter::isGravityOn(){
void ParticleEmitter::emit(){
float HI = 0.1f;
float LO = -0.1f;
float XHI = 0.1f;
float XLO = -0.1f;
float YHI = 0.1f;
float YLO = -0.1f;
float ZHI = 0.3f;
float ZLO = 0.2f;
float* v = new float[3];
v[0] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
v[1] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
v[2] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
v[0] = XLO + (float)rand()/((float)RAND_MAX/(XHI-XLO));
v[1] = YLO + (float)rand()/((float)RAND_MAX/(YHI-YLO));
v[2] = ZLO + (float)rand()/((float)RAND_MAX/(ZHI-ZLO));
int start = index;
@@ -116,10 +123,10 @@ void ParticleEmitter::tick(){
if(!particles[i]->isDead()){
//particles[i]->applyForce(force);
particles[i]->tick(); // Simulate
particles[i]->applyFriction(0.01f);
particles[i]->applyFriction(0.0001f);
if(gravityOn)
particles[i]->applyAttractiveForce(particles[i],gravity,-0.0002f,100);
particles[i]->applyAttractiveForce(particles[i],gravity,-0.002f,100);
}
@@ -212,18 +219,39 @@ void ParticleEmitter::renderParticles() {
glPopMatrix();
glPushMatrix();
/*
* All particles have the same texture, so we do this here.
*/
glEnable(GL_TEXTURE_2D);
glDepthMask(0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SPRITE_ARB);
glBindTexture(GL_TEXTURE_2D, 1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Loop through all active particles drawing them
for(int i = 0; i < MAX_PARTICLES && i < created ; i ++)
// Alive!
if(!particles[i]->isDead())
particles[i]->renderParticle(); // Render
glDisable(GL_BLEND);
glDisable(GL_POINT_SPRITE_ARB);
glDisable(GL_TEXTURE_2D);
gravity->renderParticle();
glPopMatrix();
gravity->renderParticle();
View
@@ -18,8 +18,8 @@
#pragma once
// Default Window
#define G308_WIN_WIDTH 640
#define G308_WIN_HEIGHT 480
#define G308_WIN_WIDTH 1600
#define G308_WIN_HEIGHT 900
// Projection parameters
#define G308_FOVY 20.0
View
@@ -47,6 +47,7 @@ void menu (int);
void tick();
float camAngle;
float camHeight;
/*
* Particle Emitter
@@ -86,7 +87,10 @@ int main(int argc, char** argv) {
v[2] = 0;
particeEmitter->setVector(v);
loadTexture("sprite2.png", 1);
camAngle = 0;
camHeight = 0;
G308_init();
glutIdleFunc(tick);
@@ -104,7 +108,7 @@ void loadTexture (char* filename, GLuint id){
texInfo* t = (texInfo*) malloc(sizeof(texInfo));
loadTextureFromJPEG(filename, t);
loadTextureFromPNG(filename, t);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &id);
@@ -114,7 +118,7 @@ void loadTexture (char* filename, GLuint id){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, t->width, t->height, 0, GL_RGB,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, t->width, t->height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, t->textureData);
}
@@ -132,26 +136,23 @@ void tick (){
* Rotate the camera for effect
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(G308_FOVY, (double) g_nWinWidth / (double) g_nWinHeight, G308_ZNEAR_3D, G308_ZFAR_3D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLoadIdentity();
gluPerspective(G308_FOVY, (double) g_nWinWidth / (double) g_nWinHeight, G308_ZNEAR_3D, G308_ZFAR_3D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float x = sin(camAngle) * 30.0f;
float z = cos(camAngle) * 30.0f;
float x = sin(camAngle) * 50.0f;
float z = cos(camAngle) * 50.0f;
gluLookAt(
x,-0,z,
x,camHeight,z,
0,0,0,
0,1,0
);
printf("x:%f, y:%f\n",x,z );
camAngle+= 0.005f;
//camAngle+= 0.005f;
G308_display();
}
@@ -169,8 +170,6 @@ void G308_display() {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
@@ -192,18 +191,13 @@ void G308_display() {
/*
* Draw the particle emmiter and it's particles
*/
particeEmitter->renderParticles();
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT2);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT0);
glDisable(GL_COLOR_MATERIAL);
@@ -264,22 +258,37 @@ void G308_keyboardListener(unsigned char key, int x, int y) {
}
if(key == 'g')
if(key == 'g'){
if(particeEmitter->isGravityOn())
particeEmitter->turnGravityOff();
else
particeEmitter->turnGravityOn();
}
if(key == 'e')
for(int i = 0 ; i < 50 ; i ++)
particeEmitter->emit();
if(key == '4')
camAngle += 0.05f;
if(key == '6')
camAngle -= 0.05f;
if(key == '8')
camHeight += 1;
if(key == '2')
camHeight -= 1;
}
/*
* Reshape function
*/
void G308_Reshape(int w, int h) {
if (h == 0)
h = 1;
@@ -311,6 +320,23 @@ void G308_SetCamera() {
// Set View Position
void G308_SetLight() {
/*
* Ambient
*/
GLfloat ambientG[] = { 1, 1, 1, 1 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientG);
/*
* Point Light
*/
GLfloat positionPoint[] = { 0, 0, 0, 1 };
float diffusePoint[] = { 1, 1, 1, 1 };
float specularPoint[] = { 1, 1, 1, 1 };
glLightfv(GL_LIGHT0, GL_POSITION, positionPoint);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffusePoint);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularPoint);
}

0 comments on commit 2cafa7c

Please sign in to comment.