Line data Source code
1 : #include <stdlib.h>
2 :
3 : #include "Object.h"
4 : #include "Snow.h"
5 :
6 : //------------------------------------------------------------------------------
7 0 : Snow::Snow(Object* tOrigin) {
8 0 : origin = tOrigin;
9 :
10 0 : maxParticles = numParticles = MAX_SNOW_PARTICLES;
11 :
12 0 : particles = new Particle[maxParticles];
13 :
14 0 : init();
15 :
16 : // We force an update here so that the snow will already be in the scene
17 : // once it is loaded
18 0 : update(3.0f);
19 0 : }
20 :
21 : //------------------------------------------------------------------------------
22 0 : Snow::~Snow() {
23 0 : delete[] particles;
24 0 : }
25 :
26 : //------------------------------------------------------------------------------
27 0 : void Snow::init() {
28 0 : for (int i = 0; i < numParticles; ++i) {
29 0 : initParticle(i);
30 0 : }
31 0 : }
32 :
33 : //------------------------------------------------------------------------------
34 0 : void Snow::draw() {
35 0 : glColor3f( 1.0f, 1.0f, 1.0f );
36 :
37 0 : for (int i = 0; i < numParticles; ++i) {
38 0 : Vector up, normal;
39 0 : up.setVector( 0.0f, 0.0f, 1.0f );
40 :
41 0 : Vector originPosition = origin->getPosition();
42 :
43 0 : normal.setVector(
44 0 : (originPosition.x - particles[i].position.x),
45 0 : 0.0f,
46 0 : (originPosition.z - particles[i].position.z)
47 : );
48 0 : normal.normalize();
49 :
50 0 : Vector rotationAxis;
51 0 : rotationAxis.crossProduct(up, normal);
52 0 : rotationAxis.normalize();
53 0 : float rotationAngle = up.dotProduct(normal);
54 :
55 0 : glPushMatrix();
56 0 : rotationAngle = acos(rotationAngle);
57 0 : rotationAngle = ((rotationAngle * 180.0f) / 3.14f);
58 :
59 0 : glTranslatef(particles[i].position.x, particles[i].position.y, particles[i].position.z);
60 0 : glRotatef(rotationAngle, rotationAxis.x, rotationAxis.y, rotationAxis.z);
61 :
62 0 : Vector t;
63 0 : t.setVector(
64 0 : (originPosition.x - particles[i].position.x),
65 0 : (originPosition.y - particles[i].position.y),
66 0 : (originPosition.z - particles[i].position.z)
67 : );
68 0 : t.normalize();
69 :
70 0 : rotationAngle = normal.dotProduct(t);
71 :
72 0 : if (t.y < 0)
73 0 : glRotatef(acos(rotationAngle) * 180.0f / 3.14f, 1.0f, 0.0f, 0.0f);
74 : else
75 0 : glRotatef(acos(rotationAngle) *180.0f / 3.14f, -1.0f, 0.0f, 0.0f);
76 :
77 0 : glScalef(3.0f, 3.0f, 3.0f);
78 :
79 0 : glBegin(GL_TRIANGLES);
80 0 : glVertex3f(-0.1f, -0.1f, 0.0f);
81 0 : glVertex3f(0.1f, -0.1f, 0.0f);
82 0 : glVertex3f(0.1f, 0.1f, 0.0f);
83 :
84 0 : glVertex3f(-0.1f, -0.1f, 0.0f);
85 0 : glVertex3f(0.1f, 0.1f, 0.0f);
86 0 : glVertex3f(-0.1f, 0.1f, 0.0f);
87 :
88 0 : glVertex3f(-0.14f, 0.0f, -0.001f);
89 0 : glVertex3f(0.0f, -0.14f, -0.001f);
90 0 : glVertex3f(0.14f, 0.0f, -0.001f);
91 :
92 0 : glVertex3f(-0.14f, 0.0f, -0.001f);
93 0 : glVertex3f(0.14f, 0.0f, -0.001f);
94 0 : glVertex3f(0.0f, 0.14f, -0.001f);
95 0 : glEnd();
96 0 : glPopMatrix();
97 0 : }
98 0 : }
99 :
100 : //------------------------------------------------------------------------------
101 0 : void Snow::initParticle(const int &index) {
102 0 : Vector originPosition = origin->getPosition();
103 :
104 0 : particles[index].position.setVector(
105 0 : originPosition.x + rand() % 60,
106 0 : originPosition.y + 30.0f + rand() % 20,
107 0 : originPosition.z - rand() % 100
108 : );
109 :
110 0 : particles[index].velocity.setVector(
111 0 : rand() % 5,
112 0 : -(5.0f + (rand() % 10)),
113 0 : -rand() % 5
114 : );
115 :
116 : PRINT_DEBUG_LVL(3, "New particle coords=%f,%f,%f\n", particles[index].position.x, particles[index].position.y, particles[index].position.z);
117 : PRINT_DEBUG_LVL(3, "New particle velocity=%f,%f,%f\n", particles[index].velocity.x, particles[index].velocity.y, particles[index].velocity.z);
118 0 : }
119 :
120 : //------------------------------------------------------------------------------
121 0 : void Snow::update(const float &elapsedTime) {
122 0 : for (int i = 0; i < numParticles; ++i) {
123 0 : particles[i].position.x += particles[i].velocity.x * elapsedTime;
124 0 : particles[i].position.y += particles[i].velocity.y * elapsedTime;
125 0 : particles[i].position.z += particles[i].velocity.z * elapsedTime;
126 :
127 0 : if (particles[i].position.y <= 0.0f) {
128 : PRINT_DEBUG_LVL(3, "Create a new particle...\n");
129 0 : initParticle(i);
130 0 : }
131 0 : }
132 0 : }
|