Skip to content
Browse files

Implemented billboarding, so Particles wil always face the camera, ch…

…anged particles from polys to quads, big increase in performance
  • Loading branch information...
1 parent 42d0abf commit bfce8266de6e5e1331505f3c566a642a23998a5f @bettsmatt committed Oct 7, 2012
Showing with 64 additions and 9 deletions.
  1. BIN BetaFX
  2. +29 −2 Particle.cpp
  3. +5 −3 ParticleEmitter.cpp
  4. +30 −4 main.cpp
View
BIN BetaFX
Binary file not shown.
View
31 Particle.cpp
@@ -104,13 +104,40 @@ void Particle::renderParticle() {
color[2]
);
+ // Translate to where the particle is
glTranslatef(
position[0],
position[1],
position[2]
);
- glutSolidSphere(0.1,3,3);
+ /*
+ * Since we always want the particles to face the camera we need to billboard
+ * 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);
+
+ // Undo all rotations, scaling lost too
+ for( int i=0; i<3; i++ )
+ for( int j=0; j<3; j++ ) {
+ if ( i==j )
+ modelview[i*4+j] = 1.0;
+ else
+ modelview[i*4+j] = 0.0;
+ }
+
+ // Set the modelview with no rotations and scaling
+ glLoadMatrixf(modelview);
+
+ // 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);
+ glEnd();
glPopMatrix();
@@ -141,7 +168,7 @@ void Particle::applyAttractiveForce(Particle* p1, Particle* p2, float strength,
vec[0] * vec[0] +
vec[1] * vec[1] +
vec[2] * vec[2]
- );
+ );
// Close enough
if(dist < minDist){
View
8 ParticleEmitter.cpp
@@ -65,8 +65,8 @@ bool ParticleEmitter::isGravityOn(){
void ParticleEmitter::emit(){
- float HI = 0.02f;
- float LO = -0.02f;
+ float HI = 0.1f;
+ float LO = -0.1f;
float* v = new float[3];
v[0] = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
@@ -119,7 +119,7 @@ void ParticleEmitter::tick(){
particles[i]->applyFriction(0.01f);
if(gravityOn)
- particles[i]->applyAttractiveForce(particles[i],gravity,-0.002f,100);
+ particles[i]->applyAttractiveForce(particles[i],gravity,-0.0002f,100);
}
@@ -211,13 +211,15 @@ void ParticleEmitter::renderParticles() {
glPopMatrix();
glPushMatrix();
+
// 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
+
glPopMatrix();
View
34 main.cpp
@@ -46,6 +46,8 @@ void menu (int);
void tick();
+float camAngle;
+
/*
* Particle Emitter
*/
@@ -84,6 +86,8 @@ int main(int argc, char** argv) {
v[2] = 0;
particeEmitter->setVector(v);
+ camAngle = 0;
+
G308_init();
glutIdleFunc(tick);
glutMouseFunc(mouse);
@@ -125,9 +129,29 @@ void tick (){
particeEmitter->tick();
/*
- * Rotate the world for effect
+ * Rotate the camera for effect
*/
- glRotatef(0.2f, 0.5f, 1, 0.2f);
+
+ glMatrixMode(GL_PROJECTION);
+ 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;
+
+ gluLookAt(
+ x,-0,z,
+ 0,0,0,
+ 0,1,0
+ );
+
+ printf("x:%f, y:%f\n",x,z );
+
+
+ camAngle+= 0.005f;
+
G308_display();
}
@@ -168,12 +192,14 @@ void G308_display() {
/*
* Draw the particle emmiter and it's particles
*/
- particeEmitter->renderParticles();
+ particeEmitter->renderParticles();
+
glPopMatrix();
+
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT2);
@@ -275,7 +301,7 @@ void G308_SetCamera() {
glLoadIdentity();
gluLookAt(
- 0, 0, 40,
+ 30, 30, 30,
0, 0, 0,
0, 1, 0
);

0 comments on commit bfce826

Please sign in to comment.
Something went wrong with that request. Please try again.