193f12425SAlexander von Gluck IV /* 28cd10c71STri-Edge AI * Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com> 3*8e727810SJohn Scipione * Copyright 2014 Haiku, Inc. All rights reserved. 4*8e727810SJohn Scipione * 5*8e727810SJohn Scipione * Distributed under the terms of the MIT license. 6*8e727810SJohn Scipione * 7*8e727810SJohn Scipione * Authors: 8*8e727810SJohn Scipione * Tri-Edge AI 9*8e727810SJohn Scipione * John Scipione, jscipione@gmail.com 1093f12425SAlexander von Gluck IV */ 1193f12425SAlexander von Gluck IV 1293f12425SAlexander von Gluck IV 138cd10c71STri-Edge AI #include "Particle.h" 148cd10c71STri-Edge AI 158cd10c71STri-Edge AI #include <stdlib.h> 1693f12425SAlexander von Gluck IV 1793f12425SAlexander von Gluck IV 1893f12425SAlexander von Gluck IV #define frand() ((float)rand() / (float)RAND_MAX) 1993f12425SAlexander von Gluck IV 208cd10c71STri-Edge AI 21*8e727810SJohn Scipione BObjectList<Particle>* Particle::list; 2293f12425SAlexander von Gluck IV 2393f12425SAlexander von Gluck IV 24*8e727810SJohn Scipione /*static*/ void 2593f12425SAlexander von Gluck IV Particle::Initialize(int32 size, int32 shade) 2693f12425SAlexander von Gluck IV { 27*8e727810SJohn Scipione list = new BObjectList<Particle>(2048); 288cd10c71STri-Edge AI 2993f12425SAlexander von Gluck IV for (int32 i = 0; i < size; i++) { 308cd10c71STri-Edge AI Particle* p = new Particle(); 31*8e727810SJohn Scipione Particle::_FillParticle(p, size, shade); 328cd10c71STri-Edge AI list->AddItem(p); 3393f12425SAlexander von Gluck IV } 3493f12425SAlexander von Gluck IV } 3593f12425SAlexander von Gluck IV 3693f12425SAlexander von Gluck IV 37*8e727810SJohn Scipione /*static*/ void 38*8e727810SJohn Scipione Particle::AddParticles(int32 size, int32 shade) 39*8e727810SJohn Scipione { 40*8e727810SJohn Scipione for (int32 i = list->CountItems(); i < size; i++) { 41*8e727810SJohn Scipione Particle* p = new Particle(); 42*8e727810SJohn Scipione Particle::_FillParticle(p, size, shade); 43*8e727810SJohn Scipione list->AddItem(p); 44*8e727810SJohn Scipione } 45*8e727810SJohn Scipione } 46*8e727810SJohn Scipione 47*8e727810SJohn Scipione 48*8e727810SJohn Scipione /*static*/ void 49*8e727810SJohn Scipione Particle::RemoveParticles(int32 size, int32 shade) 50*8e727810SJohn Scipione { 51*8e727810SJohn Scipione while (list->CountItems() > size) 52*8e727810SJohn Scipione delete list->RemoveItemAt(list->CountItems() - 1); 53*8e727810SJohn Scipione } 54*8e727810SJohn Scipione 55*8e727810SJohn Scipione 56*8e727810SJohn Scipione /*static*/ void 57*8e727810SJohn Scipione Particle::ColorParticles(int32 size, int32 shade) 58*8e727810SJohn Scipione { 59*8e727810SJohn Scipione for (int32 i = 0; i < size; i++) { 60*8e727810SJohn Scipione Particle* p = list->ItemAt(i); 61*8e727810SJohn Scipione Particle::_ColorParticle(p, size, shade); 62*8e727810SJohn Scipione } 63*8e727810SJohn Scipione } 64*8e727810SJohn Scipione 65*8e727810SJohn Scipione 66*8e727810SJohn Scipione /*static*/ void 6793f12425SAlexander von Gluck IV Particle::Terminate() 6893f12425SAlexander von Gluck IV { 698cd10c71STri-Edge AI list->MakeEmpty(); 708cd10c71STri-Edge AI delete list; 7193f12425SAlexander von Gluck IV } 7293f12425SAlexander von Gluck IV 7393f12425SAlexander von Gluck IV 74*8e727810SJohn Scipione /*static*/ void 758cd10c71STri-Edge AI Particle::Tick() 7693f12425SAlexander von Gluck IV { 778cd10c71STri-Edge AI for (int32 i = 0; i < list->CountItems(); i++) { 78*8e727810SJohn Scipione Particle* p = list->ItemAt(i); 798cd10c71STri-Edge AI p->_Logic(); 808cd10c71STri-Edge AI p->_Render(); 818cd10c71STri-Edge AI } 8293f12425SAlexander von Gluck IV } 8393f12425SAlexander von Gluck IV 8493f12425SAlexander von Gluck IV 8593f12425SAlexander von Gluck IV void 868cd10c71STri-Edge AI Particle::_Logic() 8793f12425SAlexander von Gluck IV { 888cd10c71STri-Edge AI // Motion 8993f12425SAlexander von Gluck IV x += vx; 9093f12425SAlexander von Gluck IV y += vy; 9193f12425SAlexander von Gluck IV z += vz; 9293f12425SAlexander von Gluck IV r += vr; 9393f12425SAlexander von Gluck IV 948cd10c71STri-Edge AI // Friction 9593f12425SAlexander von Gluck IV vx *= 0.98f; 9693f12425SAlexander von Gluck IV vy *= 0.98f; 9793f12425SAlexander von Gluck IV vz *= 0.98f; 9893f12425SAlexander von Gluck IV vr *= 0.98f; 9993f12425SAlexander von Gluck IV } 10093f12425SAlexander von Gluck IV 10193f12425SAlexander von Gluck IV 10293f12425SAlexander von Gluck IV void 1038cd10c71STri-Edge AI Particle::_Render() const 10493f12425SAlexander von Gluck IV { 10593f12425SAlexander von Gluck IV glPushMatrix(); 10693f12425SAlexander von Gluck IV glTranslatef(x, y, z); 10793f12425SAlexander von Gluck IV glRotatef(r, 0.0f, 0.0f, 1.0f); 10893f12425SAlexander von Gluck IV glColor3f(red, green, blue); 1098cd10c71STri-Edge AI glBegin(GL_QUADS); 1108cd10c71STri-Edge AI glVertex3f(-0.25f, 0.25f, 0.0f); 1118cd10c71STri-Edge AI glVertex3f(-0.25f, -0.25f, 0.0f); 1128cd10c71STri-Edge AI glVertex3f( 0.25f, -0.25f, 0.0f); 1138cd10c71STri-Edge AI glVertex3f( 0.25f, 0.25f, 0.0f); 11493f12425SAlexander von Gluck IV glEnd(); 11593f12425SAlexander von Gluck IV glPopMatrix(); 11693f12425SAlexander von Gluck IV } 117*8e727810SJohn Scipione 118*8e727810SJohn Scipione 119*8e727810SJohn Scipione /*static*/ void 120*8e727810SJohn Scipione Particle::_FillParticle(Particle* p, int32 size, int32 shade) 121*8e727810SJohn Scipione { 122*8e727810SJohn Scipione p->x = frand() * 30.0f - 15.0f; 123*8e727810SJohn Scipione p->y = frand() * 30.0f - 15.0f; 124*8e727810SJohn Scipione p->z = frand() * 5.0f; 125*8e727810SJohn Scipione p->r = frand() * 360.0f; 126*8e727810SJohn Scipione 127*8e727810SJohn Scipione p->vx = frand() - 0.5f; 128*8e727810SJohn Scipione p->vy = frand() - 0.5f; 129*8e727810SJohn Scipione p->vz = frand() - 0.5f; 130*8e727810SJohn Scipione p->vr = (frand() - 0.5f) * 180.0f; 131*8e727810SJohn Scipione 132*8e727810SJohn Scipione Particle::_ColorParticle(p, size, shade); 133*8e727810SJohn Scipione } 134*8e727810SJohn Scipione 135*8e727810SJohn Scipione 136*8e727810SJohn Scipione /*static*/ void 137*8e727810SJohn Scipione Particle::_ColorParticle(Particle* p, int32 size, int32 shade) 138*8e727810SJohn Scipione { 139*8e727810SJohn Scipione switch(shade) { 140*8e727810SJohn Scipione case 0: 141*8e727810SJohn Scipione // Red 142*8e727810SJohn Scipione p->red = 0.1f + frand() * 0.2f; 143*8e727810SJohn Scipione p->green = 0.0f; 144*8e727810SJohn Scipione p->blue = frand() * 0.05f; 145*8e727810SJohn Scipione break; 146*8e727810SJohn Scipione 147*8e727810SJohn Scipione case 1: 148*8e727810SJohn Scipione // Green 149*8e727810SJohn Scipione p->red = 0; 150*8e727810SJohn Scipione p->green = 0.1f + frand() * 0.2f; 151*8e727810SJohn Scipione p->blue = frand() * 0.05f; 152*8e727810SJohn Scipione break; 153*8e727810SJohn Scipione 154*8e727810SJohn Scipione case 2: 155*8e727810SJohn Scipione default: 156*8e727810SJohn Scipione // Blue 157*8e727810SJohn Scipione p->red = 0; 158*8e727810SJohn Scipione p->green = frand() * 0.05f; 159*8e727810SJohn Scipione p->blue = 0.1f + frand() * 0.2f; 160*8e727810SJohn Scipione break; 161*8e727810SJohn Scipione 162*8e727810SJohn Scipione case 3: 163*8e727810SJohn Scipione // Orange 164*8e727810SJohn Scipione p->red = 0.1f + frand() * 0.1f; 165*8e727810SJohn Scipione p->green = 0.05f + frand() * 0.1f; 166*8e727810SJohn Scipione p->blue = 0.0f; 167*8e727810SJohn Scipione break; 168*8e727810SJohn Scipione 169*8e727810SJohn Scipione case 4: 170*8e727810SJohn Scipione // Purple 171*8e727810SJohn Scipione p->red = 0.1f + frand() * 0.2f; 172*8e727810SJohn Scipione p->green = 0.0f; 173*8e727810SJohn Scipione p->blue = 0.1f + frand() * 0.2f; 174*8e727810SJohn Scipione break; 175*8e727810SJohn Scipione 176*8e727810SJohn Scipione case 5: 177*8e727810SJohn Scipione // White 178*8e727810SJohn Scipione p->red = p->green = p->blue = 0.1f + frand() * 0.2f; 179*8e727810SJohn Scipione 180*8e727810SJohn Scipione case 6: 181*8e727810SJohn Scipione // Rainbow 182*8e727810SJohn Scipione p->red = 0.1f + frand() * 0.2f; 183*8e727810SJohn Scipione p->green = 0.1f + frand() * 0.2f; 184*8e727810SJohn Scipione p->blue = 0.1f + frand() * 0.2f; 185*8e727810SJohn Scipione break; 186*8e727810SJohn Scipione } 187*8e727810SJohn Scipione } 188