-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzglPrimParticle.cpp
executable file
·288 lines (237 loc) · 6.71 KB
/
zglPrimParticle.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (C) 2010 ZiiLABS Pte Ltd
* All Rights Reserved.
*
* This software and its associated documentation may contain some
* proprietary, confidential and trade secret information of
* ZiiLABS Pte Ltd and except as provided by written agreement with
* ZiiLABS Pte Ltd
*
* a) no part may be disclosed, distributed, reproduced, transmitted,
* transcribed, stored in a retrieval system, adapted or translated
* in any form or by any means electronic, mechanical, magnetic,
* optical, chemical, manual or otherwise,
*
* and
*
* b) the recipient is not entitled to discover through reverse
* engineering or reverse compiling or other such techniques or
* processes the trade secrets contained therein or in the
* documentation.
*/
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include "zglEngine.h"
#include "zglCore.h"
#include "zglPrim.h"
#include "zglPrimParticle.h"
zglPrimParticle::zglPrimParticle()
{
m_type = TYPE_SPRITE;
m_vertex_num = 0;
m_aliveCount = 0;
}
zglPrimParticle::~zglPrimParticle()
{
if (m_vertex != NULL)
{
free( m_vertex);
}
}
void zglPrimParticle::init(zglParticleNode* particles, unsigned int particleNum,
zglTexture * texture)
{
if (particleNum != m_vertex_num)
{
if (m_vertex != NULL)
{
free( m_vertex);
}
int buffer_size = particleNum * 4 * sizeof(zglVertex);
m_vertex = (zglVertex*) malloc(buffer_size);
memset(m_vertex, 0, buffer_size);
}
m_particles = particles;
m_vertex_num = particleNum;
m_texture = texture;
m_world = 1.0f;
if (m_aliveCount == 0 || m_aliveCount > particleNum)
{
m_aliveCount = particleNum;
}
//Add to the node list
queue();
}
void zglPrimParticle::setAliveCount(int count)
{
m_aliveCount = count;
}
void zglPrimParticle::setTexCoord(float left, float top, float right,
float bottom)
{
this->u1 = left;
this->v1 = top;
this->u2 = right;
this->v2 = bottom;
}
void zglPrimParticle::render()
{
zglCore::setModelView(zglEngine::getEngine()->m_world_to_view);
zglVector vec_w, vec_h;
zglParticleNode* particle;
float w, h;
float angle;
float cos_a, sin_a;
unsigned vert_index = 0;
for (unsigned int i = 0; i < m_aliveCount; i++)
{
particle = m_particles + i;
if (particle->a == 0)
{
continue;
}
w = particle->width * 0.5f * particle->scale;
h = particle->height * 0.5f * particle->scale;
angle = particle->angle;
if (angle == 0.0f)
{
m_vertex[vert_index + 0].x = particle->x - w;
m_vertex[vert_index + 0].y = particle->y + h;
m_vertex[vert_index + 0].z = particle->z;
m_vertex[vert_index + 1].x = particle->x + w;
m_vertex[vert_index + 1].y = particle->y + h;
m_vertex[vert_index + 1].z = particle->z;
m_vertex[vert_index + 2].x = particle->x + w;
m_vertex[vert_index + 2].y = particle->y - h;
m_vertex[vert_index + 2].z = particle->z;
m_vertex[vert_index + 3].x = particle->x - w;
m_vertex[vert_index + 3].y = particle->y - h;
m_vertex[vert_index + 3].z = particle->z;
}
else
{
cos_a = cos(angle);
sin_a = sin(angle);
m_vertex[vert_index + 0].x = particle->x - w * cos_a - h * sin_a;
m_vertex[vert_index + 0].y = particle->y - w * sin_a + h * cos_a;
m_vertex[vert_index + 0].z = particle->z;
m_vertex[vert_index + 1].x = particle->x + w * cos_a - h * sin_a;
m_vertex[vert_index + 1].y = particle->y + w * sin_a + h * cos_a;
m_vertex[vert_index + 1].z = particle->z;
m_vertex[vert_index + 2].x = particle->x + w * cos_a + h * sin_a;
m_vertex[vert_index + 2].y = particle->y + w * sin_a - h * cos_a;
m_vertex[vert_index + 2].z = particle->z;
m_vertex[vert_index + 3].x = particle->x - w * cos_a + h * sin_a;
m_vertex[vert_index + 3].y = particle->y - w * sin_a - h * cos_a;
m_vertex[vert_index + 3].z = particle->z;
}
//!< texture coordinate
if (m_texture)
{
m_vertex[vert_index + 0].setUV(this->u1, this->v1);
m_vertex[vert_index + 1].setUV(this->u2, this->v1);
m_vertex[vert_index + 3].setUV(this->u1, this->v2);
m_vertex[vert_index + 2].setUV(this->u2, this->v2);
}
//!< Color, Alpha
m_vertex[vert_index + 0].r = particle->r;
m_vertex[vert_index + 0].g = particle->g;
m_vertex[vert_index + 0].b = particle->b;
m_vertex[vert_index + 0].a = particle->a;
m_vertex[vert_index + 1].r = particle->r;
m_vertex[vert_index + 1].g = particle->g;
m_vertex[vert_index + 1].b = particle->b;
m_vertex[vert_index + 1].a = particle->a;
m_vertex[vert_index + 2].r = particle->r;
m_vertex[vert_index + 2].g = particle->g;
m_vertex[vert_index + 2].b = particle->b;
m_vertex[vert_index + 2].a = particle->a;
m_vertex[vert_index + 3].r = particle->r;
m_vertex[vert_index + 3].g = particle->g;
m_vertex[vert_index + 3].b = particle->b;
m_vertex[vert_index + 3].a = particle->a;
vert_index += 4;
}
if (m_texture)
{
zglCore::setTexture(m_texture->getTexID(),
(m_flag & FLAG_TEX_POINT) ? false : true);
zglCore::setTexCoordPointer2((float*) &m_vertex->u, sizeof(zglVertex));
}
else
{
zglCore::setTexture(0, false);
}
zglCore::setBlendType((zglBlendType) m_blend_type);
zglCore::setVertexPointer3((float*) &m_vertex->x, sizeof(zglVertex));
zglCore::setColorPointer4(&m_vertex->r, sizeof(zglVertex));
zglCore::drawArrays(DRAW_QUADS, 0, vert_index);
}
#if 0
//!< The API will not be invoked in current version.
void zglPrimParticle::calcSortDepth()
{
int i;
float rel_z;
float tx = 0.0f;
float ty = 0.0f;
float tz = 0.0f;
for (i = 0; i < m_vertex_num; i++)
{
tx += (m_particles + i)->x;
ty += (m_particles + i)->y;
tz += (m_particles + i)->z;
}
tx = tx / m_vertex_num;
ty = ty / m_vertex_num;
tz = tz / m_vertex_num;
zglVector pos(tx, ty, tz);
int sort_value;
zglEngine * glEngine = zglEngine::getEngine();
rel_z = pos.RelZ(glEngine->m_view);
if (rel_z < glEngine->m_near_dist)
{
sort_value = 0x00;
}
else if (rel_z > glEngine->m_far_dist)
{
sort_value = 0xff;
}
else
{
sort_value = (unsigned short) ((rel_z - glEngine->m_near_dist) * 255.0f
/ (glEngine->m_far_dist - glEngine->m_near_dist));
}
sort_value += m_sort_offset;
if (sort_value > 0xff)
{
sort_value = 0xff;
}
if (sort_value < 0x00)
{
sort_value = 0x00;
}
m_sort_value = sort_value;
}
#endif
//!< --------------------------------------------------------------------------
void zglParticleNode::setPosition(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
}
void zglParticleNode::setColor(unsigned int color)
{
this->r = (unsigned char) ((color >> 24) & 0xff);
this->g = (unsigned char) ((color >> 16) & 0xff);
this->b = (unsigned char) ((color >> 8) & 0xff);
this->a = (unsigned char) ((color) & 0xff);
}
void zglParticleNode::setDimension(float width, float height, float angle)
{
this->width = width;
this->height = height;
this->angle = angle;
}