193f12425SAlexander von Gluck IV /* 2*8cd10c71STri-Edge AI * Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com> 3*8cd10c71STri-Edge AI * All rights reserved. Distributed under the terms of the MIT license. 493f12425SAlexander von Gluck IV */ 593f12425SAlexander von Gluck IV 693f12425SAlexander von Gluck IV 7*8cd10c71STri-Edge AI #include "Particle.h" 8*8cd10c71STri-Edge AI 9*8cd10c71STri-Edge AI #include <List.h> 10*8cd10c71STri-Edge AI 11*8cd10c71STri-Edge AI #include <stdlib.h> 1293f12425SAlexander von Gluck IV 1393f12425SAlexander von Gluck IV 1493f12425SAlexander von Gluck IV #define frand() ((float)rand() / (float)RAND_MAX) 1593f12425SAlexander von Gluck IV 16*8cd10c71STri-Edge AI 17*8cd10c71STri-Edge AI BList* Particle::list; 1893f12425SAlexander von Gluck IV 1993f12425SAlexander von Gluck IV 2093f12425SAlexander von Gluck IV void 2193f12425SAlexander von Gluck IV Particle::Initialize(int32 size, int32 shade) 2293f12425SAlexander von Gluck IV { 23*8cd10c71STri-Edge AI list = new BList(); 24*8cd10c71STri-Edge AI 2593f12425SAlexander von Gluck IV for (int32 i = 0; i < size; i++) { 26*8cd10c71STri-Edge AI Particle* p = new Particle(); 27*8cd10c71STri-Edge AI 28*8cd10c71STri-Edge AI p->x = frand() * 30.0f - 15.0f; 29*8cd10c71STri-Edge AI p->y = frand() * 30.0f - 15.0f; 30*8cd10c71STri-Edge AI p->z = frand() * 5.0f; 31*8cd10c71STri-Edge AI p->r = frand() * 360.0f; 3293f12425SAlexander von Gluck IV 3393f12425SAlexander von Gluck IV p->vx = frand() - 0.5f; 3493f12425SAlexander von Gluck IV p->vy = frand() - 0.5f; 3593f12425SAlexander von Gluck IV p->vz = frand() - 0.5f; 3693f12425SAlexander von Gluck IV p->vr = (frand() - 0.5f) * 180.0f; 3793f12425SAlexander von Gluck IV 38*8cd10c71STri-Edge AI if (shade == 0) { 39*8cd10c71STri-Edge AI // Red 4093f12425SAlexander von Gluck IV p->red = 0.1f + frand() * 0.2f; 4193f12425SAlexander von Gluck IV p->green = 0.0f; 4293f12425SAlexander von Gluck IV p->blue = frand() * 0.05f; 43*8cd10c71STri-Edge AI } else if (shade == 1) { 44*8cd10c71STri-Edge AI // Green 4593f12425SAlexander von Gluck IV p->red = 0; 4693f12425SAlexander von Gluck IV p->green = 0.1f + frand() * 0.2f; 4793f12425SAlexander von Gluck IV p->blue = frand() * 0.05f; 48*8cd10c71STri-Edge AI } else if (shade == 2) { 49*8cd10c71STri-Edge AI // Blue 5093f12425SAlexander von Gluck IV p->red = 0; 5193f12425SAlexander von Gluck IV p->green = frand() * 0.05f; 5293f12425SAlexander von Gluck IV p->blue = 0.1f + frand() * 0.2f; 53*8cd10c71STri-Edge AI } else if (shade == 3) { 54*8cd10c71STri-Edge AI // Orange 5593f12425SAlexander von Gluck IV p->red = 0.1f + frand() * 0.1f; 5693f12425SAlexander von Gluck IV p->green = 0.05f + frand() * 0.1f; 5793f12425SAlexander von Gluck IV p->blue = 0.0f; 58*8cd10c71STri-Edge AI } else if (shade == 4) { 59*8cd10c71STri-Edge AI // Purple 6093f12425SAlexander von Gluck IV p->red = 0.1f + frand() * 0.2f; 6193f12425SAlexander von Gluck IV p->green = 0.0f; 6293f12425SAlexander von Gluck IV p->blue = 0.1f + frand() * 0.2f; 63*8cd10c71STri-Edge AI } else if (shade == 5) { 64*8cd10c71STri-Edge AI // White 6593f12425SAlexander von Gluck IV p->red = p->green = p->blue = 0.1f + frand() * 0.2f; 66*8cd10c71STri-Edge AI } else if (shade == 6) { 67*8cd10c71STri-Edge AI // Rainbow 6893f12425SAlexander von Gluck IV p->red = 0.1f + frand() * 0.2f; 6993f12425SAlexander von Gluck IV p->green = 0.1f + frand() * 0.2f; 7093f12425SAlexander von Gluck IV p->blue = 0.1f + frand() * 0.2f; 7193f12425SAlexander von Gluck IV } else { 7293f12425SAlexander von Gluck IV // Man, this shouldn't even happen.. Blue. 7393f12425SAlexander von Gluck IV p->red = 0; 7493f12425SAlexander von Gluck IV p->green = frand() * 0.05f; 7593f12425SAlexander von Gluck IV p->blue = 0.1f + frand() * 0.2f; 7693f12425SAlexander von Gluck IV } 7793f12425SAlexander von Gluck IV 78*8cd10c71STri-Edge AI list->AddItem(p); 7993f12425SAlexander von Gluck IV } 8093f12425SAlexander von Gluck IV } 8193f12425SAlexander von Gluck IV 8293f12425SAlexander von Gluck IV 8393f12425SAlexander von Gluck IV void 8493f12425SAlexander von Gluck IV Particle::Terminate() 8593f12425SAlexander von Gluck IV { 86*8cd10c71STri-Edge AI for (int32 i = 0; i < list->CountItems(); i++) 87*8cd10c71STri-Edge AI delete (Particle*)list->ItemAt(i); 8893f12425SAlexander von Gluck IV 89*8cd10c71STri-Edge AI list->MakeEmpty(); 90*8cd10c71STri-Edge AI delete list; 9193f12425SAlexander von Gluck IV } 9293f12425SAlexander von Gluck IV 9393f12425SAlexander von Gluck IV 9493f12425SAlexander von Gluck IV void 95*8cd10c71STri-Edge AI Particle::Tick() 9693f12425SAlexander von Gluck IV { 97*8cd10c71STri-Edge AI for (int32 i = 0; i < list->CountItems(); i++) { 98*8cd10c71STri-Edge AI Particle* p = (Particle*)list->ItemAt(i); 99*8cd10c71STri-Edge AI p->_Logic(); 100*8cd10c71STri-Edge AI p->_Render(); 101*8cd10c71STri-Edge AI } 10293f12425SAlexander von Gluck IV } 10393f12425SAlexander von Gluck IV 10493f12425SAlexander von Gluck IV 10593f12425SAlexander von Gluck IV void 106*8cd10c71STri-Edge AI Particle::_Logic() 10793f12425SAlexander von Gluck IV { 108*8cd10c71STri-Edge AI // Motion 10993f12425SAlexander von Gluck IV x += vx; 11093f12425SAlexander von Gluck IV y += vy; 11193f12425SAlexander von Gluck IV z += vz; 11293f12425SAlexander von Gluck IV r += vr; 11393f12425SAlexander von Gluck IV 114*8cd10c71STri-Edge AI // Friction 11593f12425SAlexander von Gluck IV vx *= 0.98f; 11693f12425SAlexander von Gluck IV vy *= 0.98f; 11793f12425SAlexander von Gluck IV vz *= 0.98f; 11893f12425SAlexander von Gluck IV vr *= 0.98f; 11993f12425SAlexander von Gluck IV } 12093f12425SAlexander von Gluck IV 12193f12425SAlexander von Gluck IV 12293f12425SAlexander von Gluck IV void 123*8cd10c71STri-Edge AI Particle::_Render() const 12493f12425SAlexander von Gluck IV { 12593f12425SAlexander von Gluck IV glPushMatrix(); 12693f12425SAlexander von Gluck IV glTranslatef(x, y, z); 12793f12425SAlexander von Gluck IV glRotatef(r, 0.0f, 0.0f, 1.0f); 12893f12425SAlexander von Gluck IV glColor3f(red, green, blue); 129*8cd10c71STri-Edge AI glBegin(GL_QUADS); 130*8cd10c71STri-Edge AI glVertex3f(-0.25f, 0.25f, 0.0f); 131*8cd10c71STri-Edge AI glVertex3f(-0.25f, -0.25f, 0.0f); 132*8cd10c71STri-Edge AI glVertex3f( 0.25f, -0.25f, 0.0f); 133*8cd10c71STri-Edge AI glVertex3f( 0.25f, 0.25f, 0.0f); 13493f12425SAlexander von Gluck IV glEnd(); 13593f12425SAlexander von Gluck IV glPopMatrix(); 13693f12425SAlexander von Gluck IV } 137