Line data Source code
1 : #include <stdlib.h>
2 :
3 : #include "Object.h"
4 : #include "StarField.h"
5 :
6 : //------------------------------------------------------------------------------
7 0 : StarField::StarField(Object* tOrigin) {
8 0 : origin = tOrigin;
9 :
10 0 : maxParticles = MAX_STAR_PARTICLES;
11 0 : numParticles = MAX_STAR_PARTICLES;
12 :
13 0 : particles = new Particle[maxParticles];
14 :
15 0 : init();
16 0 : }
17 :
18 : //------------------------------------------------------------------------------
19 0 : StarField::~StarField() {
20 0 : delete[] particles;
21 0 : }
22 :
23 : //------------------------------------------------------------------------------
24 0 : void StarField::init() {
25 0 : for (int i = 0; i < numParticles; ++i) {
26 0 : initParticle(i);
27 0 : }
28 0 : }
29 :
30 : //------------------------------------------------------------------------------
31 0 : void StarField::draw() {
32 0 : Vector originPosition = origin->getPosition();
33 :
34 0 : for (int i = 0; i < numParticles; ++i) {
35 0 : glColor4f( 1.0f, 1.0f, 1.0f, particles[i].velocity.x );
36 :
37 0 : glPushMatrix();
38 0 : glTranslatef(
39 0 : originPosition.x + particles[i].position.x,
40 0 : originPosition.y + particles[i].position.y,
41 0 : originPosition.z - particles[i].position.z
42 : );
43 :
44 0 : const float size = particles[i].intensity;
45 0 : glScalef(size, size, size);
46 :
47 0 : glBegin( GL_TRIANGLES );
48 0 : glVertex3f(-0.1, -0.1, 0.0);
49 0 : glVertex3f(0.1, -0.1, 0.0);
50 0 : glVertex3f(0.1, 0.1, 0.0);
51 :
52 0 : glVertex3f(-0.1, -0.1, 0.0);
53 0 : glVertex3f(0.1, 0.1, 0.0);
54 0 : glVertex3f(-0.1, 0.1, 0.0);
55 :
56 0 : glVertex3f(-0.14, 0.0, -0.001);
57 0 : glVertex3f(0.0, -0.14, -0.001);
58 0 : glVertex3f(0.14, 0.0, -0.001);
59 :
60 0 : glVertex3f(-0.14, 0.0, -0.001);
61 0 : glVertex3f(0.14, 0.0, -0.001);
62 0 : glVertex3f(0.0, 0.14, -0.001);
63 0 : glEnd();
64 0 : glPopMatrix();
65 0 : }
66 0 : }
67 :
68 : //------------------------------------------------------------------------------
69 0 : void StarField::initParticle(const int &index) {
70 0 : particles[index].position.setVector(
71 0 : -200.0f + rand() % 400,
72 0 : -150.0f + rand() % 300,
73 0 : 250.0f
74 : );
75 :
76 : // TODO: For now we'll use the values already in the Particle struct to
77 : // control size and flicker, but this isn't ideal
78 0 : particles[index].intensity = 3.0f + rand() % 5;
79 :
80 0 : bool flickerEnable = (rand() % 1000) < 10;
81 0 : float flickerRate = flickerEnable ?
82 0 : (-(float)(rand() % 500)) / 500.0f : 0.0f;
83 :
84 0 : particles[index].velocity.setVector(
85 0 : 1.0f, // current alpha value
86 : flickerRate, // rate of change
87 0 : 0.0f // not presently used
88 : );
89 0 : }
90 :
91 : //------------------------------------------------------------------------------
92 0 : void StarField::update(const float &elapsedTime) {
93 0 : for (int i = 0; i < numParticles; ++i) {
94 0 : Vector attributes = particles[i].velocity;
95 :
96 0 : attributes.x += (attributes.y * elapsedTime);
97 :
98 0 : if (attributes.y > 0) {
99 0 : if (attributes.x >= 1.0f) {
100 0 : attributes.x = 1.0f;
101 0 : attributes.y = 0.0f;
102 0 : }
103 0 : }
104 0 : else if (attributes.y < 0){
105 0 : if (attributes.x <= 0.0f) {
106 0 : attributes.x = 0.0f;
107 0 : attributes.y *= -1.0f;
108 0 : }
109 0 : }
110 : else {
111 0 : bool flickerEnable = (rand() % 1000) < 10;
112 0 : attributes.y = flickerEnable ?
113 0 : (-(float)(rand() % 500)) / 500.0f : 0.0f;
114 : }
115 :
116 0 : particles[i].velocity = attributes;
117 0 : }
118 0 : }
|