xref: /haiku/src/add-ons/screen_savers/gravity/Particle.cpp (revision 93f124253a04af96e300ffaf224885df729463d7)
1*93f12425SAlexander von Gluck IV /*
2*93f12425SAlexander von Gluck IV  * Copyright 2012, Haiku, Inc.
3*93f12425SAlexander von Gluck IV  * Distributed under the terms of the MIT License.
4*93f12425SAlexander von Gluck IV  *
5*93f12425SAlexander von Gluck IV  * Authors:
6*93f12425SAlexander von Gluck IV  * 		Tri-Edge AI <triedgeai@gmail.com>
7*93f12425SAlexander von Gluck IV  */
8*93f12425SAlexander von Gluck IV 
9*93f12425SAlexander von Gluck IV 
10*93f12425SAlexander von Gluck IV #include "Particle.hpp"
11*93f12425SAlexander von Gluck IV 
12*93f12425SAlexander von Gluck IV 
13*93f12425SAlexander von Gluck IV #define frand() ((float)rand() / (float)RAND_MAX)
14*93f12425SAlexander von Gluck IV 
15*93f12425SAlexander von Gluck IV vector<Particle*> Particle::list;
16*93f12425SAlexander von Gluck IV 
17*93f12425SAlexander von Gluck IV 
18*93f12425SAlexander von Gluck IV void
19*93f12425SAlexander von Gluck IV Particle::Initialize(int32 size, int32 shade)
20*93f12425SAlexander von Gluck IV {
21*93f12425SAlexander von Gluck IV 	for (int32 i = 0; i < size; i++) {
22*93f12425SAlexander von Gluck IV 		Particle* p = new Particle(frand() * 30.0f - 15.0f,
23*93f12425SAlexander von Gluck IV 			frand() * 30.0f - 15.0f, frand() * 5.0f, frand() * 360.0f);
24*93f12425SAlexander von Gluck IV 
25*93f12425SAlexander von Gluck IV 		p->vx = frand() - 0.5f;
26*93f12425SAlexander von Gluck IV 		p->vy = frand() - 0.5f;
27*93f12425SAlexander von Gluck IV 		p->vz = frand() - 0.5f;
28*93f12425SAlexander von Gluck IV 		p->vr = (frand() - 0.5f) * 180.0f;
29*93f12425SAlexander von Gluck IV 
30*93f12425SAlexander von Gluck IV 		if (shade == 0) { // Red
31*93f12425SAlexander von Gluck IV 			p->red = 0.1f + frand() * 0.2f;
32*93f12425SAlexander von Gluck IV 			p->green = 0.0f;
33*93f12425SAlexander von Gluck IV 			p->blue = frand() * 0.05f;
34*93f12425SAlexander von Gluck IV 		} else if (shade == 1) { // Green
35*93f12425SAlexander von Gluck IV 			p->red = 0;
36*93f12425SAlexander von Gluck IV 			p->green =  0.1f + frand() * 0.2f;
37*93f12425SAlexander von Gluck IV 			p->blue = frand() * 0.05f;
38*93f12425SAlexander von Gluck IV 		} else if (shade == 2) { // Blue
39*93f12425SAlexander von Gluck IV 			p->red = 0;
40*93f12425SAlexander von Gluck IV 			p->green = frand() * 0.05f;
41*93f12425SAlexander von Gluck IV 			p->blue = 0.1f + frand() * 0.2f;
42*93f12425SAlexander von Gluck IV 		} else if (shade == 3) { // Orange
43*93f12425SAlexander von Gluck IV 			p->red = 0.1f + frand() * 0.1f;
44*93f12425SAlexander von Gluck IV 			p->green =  0.05f + frand() * 0.1f;
45*93f12425SAlexander von Gluck IV 			p->blue = 0.0f;
46*93f12425SAlexander von Gluck IV 		} else if (shade == 4) { // Purple
47*93f12425SAlexander von Gluck IV 			p->red = 0.1f + frand() * 0.2f;
48*93f12425SAlexander von Gluck IV 			p->green = 0.0f;
49*93f12425SAlexander von Gluck IV 			p->blue = 0.1f + frand() * 0.2f;
50*93f12425SAlexander von Gluck IV 		} else if (shade == 5) { // White
51*93f12425SAlexander von Gluck IV 			p->red = p->green = p->blue = 0.1f + frand() * 0.2f;
52*93f12425SAlexander von Gluck IV 		} else if (shade == 6) { // Rainbow
53*93f12425SAlexander von Gluck IV 			p->red = 0.1f + frand() * 0.2f;
54*93f12425SAlexander von Gluck IV 			p->green = 0.1f + frand() * 0.2f;
55*93f12425SAlexander von Gluck IV 			p->blue = 0.1f + frand() * 0.2f;
56*93f12425SAlexander von Gluck IV 		} else {
57*93f12425SAlexander von Gluck IV 			// Man, this shouldn't even happen..	Blue.
58*93f12425SAlexander von Gluck IV 			p->red = 0;
59*93f12425SAlexander von Gluck IV 			p->green = frand() * 0.05f;
60*93f12425SAlexander von Gluck IV 			p->blue = 0.1f + frand() * 0.2f;
61*93f12425SAlexander von Gluck IV 		}
62*93f12425SAlexander von Gluck IV 
63*93f12425SAlexander von Gluck IV 		list.push_back(p);
64*93f12425SAlexander von Gluck IV 	}
65*93f12425SAlexander von Gluck IV }
66*93f12425SAlexander von Gluck IV 
67*93f12425SAlexander von Gluck IV 
68*93f12425SAlexander von Gluck IV void
69*93f12425SAlexander von Gluck IV Particle::Terminate()
70*93f12425SAlexander von Gluck IV {
71*93f12425SAlexander von Gluck IV 	for (uint32 i = 0; i < list.size(); i++)
72*93f12425SAlexander von Gluck IV 		delete list[i];
73*93f12425SAlexander von Gluck IV 
74*93f12425SAlexander von Gluck IV 	list.clear();
75*93f12425SAlexander von Gluck IV }
76*93f12425SAlexander von Gluck IV 
77*93f12425SAlexander von Gluck IV 
78*93f12425SAlexander von Gluck IV void
79*93f12425SAlexander von Gluck IV Particle::RunAll()
80*93f12425SAlexander von Gluck IV {
81*93f12425SAlexander von Gluck IV 	for (uint32 i = 0; i < list.size(); i++)
82*93f12425SAlexander von Gluck IV 		list[i]->Run();
83*93f12425SAlexander von Gluck IV }
84*93f12425SAlexander von Gluck IV 
85*93f12425SAlexander von Gluck IV 
86*93f12425SAlexander von Gluck IV void
87*93f12425SAlexander von Gluck IV Particle::DrawAll()
88*93f12425SAlexander von Gluck IV {
89*93f12425SAlexander von Gluck IV 	for (uint32 i = 0; i < list.size(); i++)
90*93f12425SAlexander von Gluck IV 		list[i]->Draw();
91*93f12425SAlexander von Gluck IV }
92*93f12425SAlexander von Gluck IV 
93*93f12425SAlexander von Gluck IV 
94*93f12425SAlexander von Gluck IV Particle::Particle(float x, float y, float z, float r)
95*93f12425SAlexander von Gluck IV {
96*93f12425SAlexander von Gluck IV 	this->x = x;
97*93f12425SAlexander von Gluck IV 	this->y = y;
98*93f12425SAlexander von Gluck IV 	this->z = z;
99*93f12425SAlexander von Gluck IV 	this->r = r;
100*93f12425SAlexander von Gluck IV }
101*93f12425SAlexander von Gluck IV 
102*93f12425SAlexander von Gluck IV 
103*93f12425SAlexander von Gluck IV void
104*93f12425SAlexander von Gluck IV Particle::Run()
105*93f12425SAlexander von Gluck IV {
106*93f12425SAlexander von Gluck IV 	x += vx;
107*93f12425SAlexander von Gluck IV 	y += vy;
108*93f12425SAlexander von Gluck IV 	z += vz;
109*93f12425SAlexander von Gluck IV 	r += vr;
110*93f12425SAlexander von Gluck IV 
111*93f12425SAlexander von Gluck IV 	vx *= 0.98f;
112*93f12425SAlexander von Gluck IV 	vy *= 0.98f;
113*93f12425SAlexander von Gluck IV 	vz *= 0.98f;
114*93f12425SAlexander von Gluck IV 	vr *= 0.98f;
115*93f12425SAlexander von Gluck IV }
116*93f12425SAlexander von Gluck IV 
117*93f12425SAlexander von Gluck IV 
118*93f12425SAlexander von Gluck IV void
119*93f12425SAlexander von Gluck IV Particle::Draw() const
120*93f12425SAlexander von Gluck IV {
121*93f12425SAlexander von Gluck IV 	glPushMatrix();
122*93f12425SAlexander von Gluck IV 	glTranslatef(x, y, z);
123*93f12425SAlexander von Gluck IV 	glRotatef(r, 0.0f, 0.0f, 1.0f);
124*93f12425SAlexander von Gluck IV 	glBegin(GL_QUADS);
125*93f12425SAlexander von Gluck IV 	glColor3f(red, green, blue);
126*93f12425SAlexander von Gluck IV 	glVertex3f(-0.5f,  0.5f,  0.0f);
127*93f12425SAlexander von Gluck IV 	glVertex3f(-0.5f, -0.5f,  0.0f);
128*93f12425SAlexander von Gluck IV 	glVertex3f( 0.5f, -0.5f,  0.0f);
129*93f12425SAlexander von Gluck IV 	glVertex3f( 0.5f,  0.5f,  0.0f);
130*93f12425SAlexander von Gluck IV 	glEnd();
131*93f12425SAlexander von Gluck IV 	glPopMatrix();
132*93f12425SAlexander von Gluck IV }
133