-
Notifications
You must be signed in to change notification settings - Fork 1
/
particles.cpp
executable file
·204 lines (172 loc) · 4.41 KB
/
particles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "particles.h"
#include "handlers.h"
#ifdef WIN32
#include <windows.h>
#endif
#include <gl/gl.h>
#include <iostream>
using namespace std;
/*
========
Render @ particle_t
========
*/
void particle_t::Render()
{
if( !life ) return;
glPointSize( 4.0 );
// calc new pos
float dt = ( float(TIMER_TICK)/1000 );
v0 = (a0 * dt) + v0;
v1 = (a1 * dt) + v1;
world_translate = (v0+v1) * dt + world_translate;
--life;
// draw the point
glColor3f( 1.0, 0.0, 0.0 );
glBegin( GL_POINTS );
glVertex3fv( &world_translate[0] );
glEnd();
// draw axis
if( 0 )
{
glPushMatrix();
mat4_t m4;
TRS( world_translate, world_rotate, world_scale, m4 );
m4.Transpose();
glMultMatrixf( &m4[0][0] );
glBegin( GL_LINES );
// x-axis
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( 1.0, 0.0, 0.0 );
// y-axis
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
// z-axis
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( 0.0, 0.0, 1.0 );
glEnd();
glPopMatrix();
}
// debug 1
if( 0 )
{
// draw the v0
glColor3f( 0.0, 0.0, 1.0 );
glBegin( GL_LINES );
glVertex3fv( &world_translate[0] );
glVertex3fv( &(v0+world_translate)[0] );
glEnd();
// draw the v1
glColor3f( 0.0, 1.0, 0.0 );
glBegin( GL_LINES );
glVertex3fv( &world_translate[0] );
glVertex3fv( &(v1+world_translate)[0] );
glEnd();
// draw the Sv
glColor3f( 1.0, 1.0, 1.0 );
glBegin( GL_LINES );
glVertex3fv( &world_translate[0] );
glVertex3fv( &(v0+v1+world_translate)[0] );
glEnd();
}
};
/*
========
Init @ particle_emitter_t
========
*/
void particle_emitter_t::Init()
{
particles.Malloc(200);
for( int i=0; i<particles.size; i++ )
particles[i].parent = this;
particle_life[0] = 200;
particle_life[1] = 400;
particles_per_frame = 1;
v0_angs[0] = euler_t( 0.0, -30.0, 10.0 );
v0_angs[1] = euler_t( 0.0, 30.0, 45.0 );
v0_magnitude = 5.0;
a0_magnitude = 0.0;
v1_angs[0] = euler_t( 0.0, 0.0, 270.0 );
v1_angs[1] = euler_t( 0.0, 0.0, 270.0 );
v1_magnitude = 0.0;
a1_magnitude = 1.0;
particles_local_translate[0] = vec3_t( 0.0, 0.0, 0.0 );
particles_local_translate[1] = vec3_t( 0.0, 0.0, 0.0 );
}
/*
========
ReInitParticle @ particle_emitter_t
========
*/
void particle_emitter_t::ReInitParticle( particle_t& particle )
{
particle.life = RandRange( particle_life[0], particle_life[1] );
particle.local_translate = vec3_t(
RandRange( particles_local_translate[0].x(), particles_local_translate[1].x() ),
RandRange( particles_local_translate[0].y(), particles_local_translate[1].y() ),
RandRange( particles_local_translate[0].z(), particles_local_translate[1].z() )
);
particle.UpdateWorldTransform();
euler_t tmp_angs = euler_t(
RandRange( v0_angs[0].x(), v0_angs[1].x() ),
RandRange( v0_angs[0].y(), v0_angs[1].y() ),
RandRange( v0_angs[0].z(), v0_angs[1].z() )
);
mat3_t m3;
m3.LoadEuler( tmp_angs );
particle.v0 = particle.a0 = m3 * vec3_t( 1.0, 0.0, 0.0 );
particle.v0 *= v0_magnitude;
particle.a0 *= a0_magnitude;
tmp_angs = euler_t(
RandRange( v1_angs[0].x(), v1_angs[1].x() ),
RandRange( v1_angs[0].y(), v1_angs[1].y() ),
RandRange( v1_angs[0].z(), v1_angs[1].z() )
);
m3.LoadEuler( tmp_angs );
particle.v1 = particle.a1 = m3 * vec3_t( 1.0, 0.0, 0.0 );
particle.v1 *= v1_magnitude;
particle.a1 *= a1_magnitude;
}
/*
========
Render @ particle_emitter_t
========
*/
void particle_emitter_t::Render()
{
UpdateWorldTransform();
// emitt new particles
int remain = particles_per_frame;
for( int i=0; i<particles.size; i++ )
{
if( particles[i].life != 0 ) continue;
ReInitParticle( particles[i] );
--remain;
if( remain == 0 ) break;
}
// render all particles
for( int i=0; i<particles.size; i++ )
particles[i].Render();
// render the debug cube
if( 1 )
{
glEnable( GL_DEPTH_TEST );
glDisable( GL_TEXTURE_2D );
glDisable( GL_BLEND );
glLineWidth( 2.0 );
glPolygonMode( GL_FRONT, GL_LINE );
glPushMatrix();
mat4_t transform;
UpdateWorldTransform();
TRS( world_translate, world_rotate, world_scale, transform );
transform.Transpose();
glMultMatrixf( &transform[0][0] );
glColor3f( 0.0, 1.0, 0.0 );
RenderCube();
glPopMatrix();
}
}