-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.cpp
435 lines (354 loc) · 13.4 KB
/
particle.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//=============================================================================
//
// パーティクル処理 [particle.cpp]
// Author :
//
//=============================================================================
#include "main.h"
#include "renderer.h"
#include "input.h"
#include "camera.h"
#include "model.h"
#include "shadow.h"
#include "particle.h"
#include "player.h"
//*****************************************************************************
// マクロ定義
//*****************************************************************************
#define TEXTURE_MAX (1) // テクスチャの数
#define PARTICLE_SIZE_X (4.0f) // 頂点サイズ
#define PARTICLE_SIZE_Y (4.0f) // 頂点サイズ
#define VALUE_MOVE_PARTICLE (5.0f) // 移動速度
#define MAX_PARTICLE (512) // パーティクル最大数
#define DISP_SHADOW // 影の表示
//#undef DISP_SHADOW
//*****************************************************************************
// 構造体定義
//*****************************************************************************
typedef struct
{
XMFLOAT3 pos; // 位置
XMFLOAT3 rot; // 回転
XMFLOAT3 scale; // スケール
XMFLOAT3 move; // 移動量
MATERIAL material; // マテリアル
float fSizeX; // 幅
float fSizeY; // 高さ
int nIdxShadow; // 影ID
int nLife; // 寿命
BOOL bUse; // 使用しているかどうか
} PARTICLE;
//*****************************************************************************
// プロトタイプ宣言
//*****************************************************************************
HRESULT MakeVertexParticle(void);
//*****************************************************************************
// グローバル変数
//*****************************************************************************
static ID3D11Buffer *g_VertexBuffer = NULL; // 頂点バッファ
static ID3D11ShaderResourceView *g_Texture[TEXTURE_MAX] = { NULL }; // テクスチャ情報
static int g_TexNo; // テクスチャ番号
static PARTICLE g_aParticle[MAX_PARTICLE]; // パーティクルワーク
static XMFLOAT3 playerPos; // ビルボード発生位置
static float g_fWidthBase = 5.0f; // 基準の幅
static float g_fHeightBase = 10.0f; // 基準の高さ
static float g_roty = 0.0f; // 移動方向
static float g_spd = 0.0f; // 移動スピード
static char *g_TextureName[TEXTURE_MAX] =
{
"data/TEXTURE/effect000.jpg",
};
static BOOL g_Load = FALSE;
//=============================================================================
// 初期化処理
//=============================================================================
HRESULT InitParticle(void)
{
// 頂点情報の作成
MakeVertexParticle();
// テクスチャ生成
for (int i = 0; i < TEXTURE_MAX; i++)
{
g_Texture[i] = NULL;
D3DX11CreateShaderResourceViewFromFile(GetDevice(),
g_TextureName[i],
NULL,
NULL,
&g_Texture[i],
NULL);
}
g_TexNo = 0;
// パーティクルワークの初期化
for(int nCntParticle = 0; nCntParticle < MAX_PARTICLE; nCntParticle++)
{
g_aParticle[nCntParticle].pos = XMFLOAT3(0.0f, 0.0f, 0.0f);
g_aParticle[nCntParticle].rot = XMFLOAT3(0.0f, 0.0f, 0.0f);
g_aParticle[nCntParticle].scale = XMFLOAT3(1.0f, 1.0f, 1.0f);
g_aParticle[nCntParticle].move = XMFLOAT3(1.0f, 1.0f, 1.0f);
ZeroMemory(&g_aParticle[nCntParticle].material, sizeof(g_aParticle[nCntParticle].material));
g_aParticle[nCntParticle].material.Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
g_aParticle[nCntParticle].fSizeX = PARTICLE_SIZE_X;
g_aParticle[nCntParticle].fSizeY = PARTICLE_SIZE_Y;
g_aParticle[nCntParticle].nIdxShadow = -1;
g_aParticle[nCntParticle].nLife = 0;
g_aParticle[nCntParticle].bUse = FALSE;
}
g_roty = 0.0f;
g_spd = 0.0f;
g_Load = TRUE;
return S_OK;
}
//=============================================================================
// 終了処理
//=============================================================================
void UninitParticle(void)
{
if (g_Load == FALSE) return;
//テクスチャの解放
for (int nCntTex = 0; nCntTex < TEXTURE_MAX; nCntTex++)
{
if (g_Texture[nCntTex] != NULL)
{
g_Texture[nCntTex]->Release();
g_Texture[nCntTex] = NULL;
}
}
// 頂点バッファの解放
if (g_VertexBuffer != NULL)
{
g_VertexBuffer->Release();
g_VertexBuffer = NULL;
}
g_Load = FALSE;
}
//=============================================================================
// 更新処理
//=============================================================================
void UpdateParticle(void)
{
//PLAYER *pPlayer = GetPlayer();
//playerPos = pPlayer->pos;
{
for(int nCntParticle = 0; nCntParticle < MAX_PARTICLE; nCntParticle++)
{
if(g_aParticle[nCntParticle].bUse)
{// 使用中
g_aParticle[nCntParticle].pos.x += g_aParticle[nCntParticle].move.x;
g_aParticle[nCntParticle].pos.z += g_aParticle[nCntParticle].move.z;
g_aParticle[nCntParticle].pos.y += g_aParticle[nCntParticle].move.y;
if(g_aParticle[nCntParticle].pos.y <= g_aParticle[nCntParticle].fSizeY / 2)
{// 着地した
g_aParticle[nCntParticle].pos.y = g_aParticle[nCntParticle].fSizeY / 2;
g_aParticle[nCntParticle].move.y = -g_aParticle[nCntParticle].move.y * 0.75f;
}
g_aParticle[nCntParticle].move.x += (0.0f - g_aParticle[nCntParticle].move.x) * 0.015f;
g_aParticle[nCntParticle].move.y -= 0.25f;
g_aParticle[nCntParticle].move.z += (0.0f - g_aParticle[nCntParticle].move.z) * 0.015f;
#ifdef DISP_SHADOW
if(g_aParticle[nCntParticle].nIdxShadow != -1)
{// 影使用中
float colA;
// 影の位置設定
SetPositionShadow(g_aParticle[nCntParticle].nIdxShadow, XMFLOAT3(g_aParticle[nCntParticle].pos.x, 0.1f, g_aParticle[nCntParticle].pos.z));
// 影の色の設定
colA = g_aParticle[nCntParticle].material.Diffuse.w;
SetColorShadow(g_aParticle[nCntParticle].nIdxShadow, XMFLOAT4(0.5f, 0.5f, 0.5f, colA));
}
#endif
g_aParticle[nCntParticle].nLife--;
if(g_aParticle[nCntParticle].nLife <= 0)
{
g_aParticle[nCntParticle].bUse = FALSE;
ReleaseShadow(g_aParticle[nCntParticle].nIdxShadow);
g_aParticle[nCntParticle].nIdxShadow = -1;
}
else
{
if(g_aParticle[nCntParticle].nLife <= 80)
{
g_aParticle[nCntParticle].material.Diffuse.x = 0.8f - (float)(80 - g_aParticle[nCntParticle].nLife) / 80 * 0.8f;
g_aParticle[nCntParticle].material.Diffuse.y = 0.7f - (float)(80 - g_aParticle[nCntParticle].nLife) / 80 * 0.7f;
g_aParticle[nCntParticle].material.Diffuse.z = 0.2f - (float)(80 - g_aParticle[nCntParticle].nLife) / 80 * 0.2f;
}
if(g_aParticle[nCntParticle].nLife <= 20)
{
// α値設定
g_aParticle[nCntParticle].material.Diffuse.w -= 0.05f;
if(g_aParticle[nCntParticle].material.Diffuse.w < 0.0f)
{
g_aParticle[nCntParticle].material.Diffuse.w = 0.0f;
}
}
}
}
}
// パーティクル発生
{
XMFLOAT3 pos;
XMFLOAT3 move;
float fAngle, fLength;
int nLife;
float fSize;
pos = { playerPos.x,playerPos.y + 10.0f,playerPos.z };
fAngle = (float)(rand() % 628 - 314) / 100.0f;
fLength = rand() % (int)(g_fWidthBase * 200 ) / 100.0f - g_fWidthBase;
move.x = sinf(fAngle) * fLength;
move.y = rand() % 300 / 100.0f + g_fHeightBase;
move.z = cosf(fAngle) * fLength;
nLife = rand() % 100 + 150;
fSize = (float)(rand() % 30 + 20);
//pos.y = fSize / 2;
// ビルボードの設定
SetParticle(pos, move, XMFLOAT4(0.8f, 0.7f, 0.2f, 0.85f), fSize, fSize, nLife);
}
}
}
//=============================================================================
// 描画処理
//=============================================================================
void DrawParticle(void)
{
XMMATRIX mtxScl, mtxTranslate, mtxWorld, mtxView;
CAMERA *cam = GetCamera();
// ライティングを無効に
SetLightEnable(FALSE);
// 加算合成に設定
SetBlendState(BLEND_MODE_ADD);
// Z比較無し
SetDepthEnable(FALSE);
// フォグ無効
SetFogEnable(FALSE);
// 頂点バッファ設定
UINT stride = sizeof(VERTEX_3D);
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers(0, 1, &g_VertexBuffer, &stride, &offset);
// プリミティブトポロジ設定
GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
// テクスチャ設定
GetDeviceContext()->PSSetShaderResources(0, 1, &g_Texture[g_TexNo]);
for(int nCntParticle = 0; nCntParticle < MAX_PARTICLE; nCntParticle++)
{
if(g_aParticle[nCntParticle].bUse)
{
// ワールドマトリックスの初期化
mtxWorld = XMMatrixIdentity();
// ビューマトリックスを取得
mtxView = XMLoadFloat4x4(&cam->mtxView);
//mtxWorld = XMMatrixInverse(nullptr, mtxView);
//mtxWorld.r[3].m128_f32[0] = 0.0f;
//mtxWorld.r[3].m128_f32[1] = 0.0f;
//mtxWorld.r[3].m128_f32[2] = 0.0f;
// 処理が速いしお勧め
mtxWorld.r[0].m128_f32[0] = mtxView.r[0].m128_f32[0];
mtxWorld.r[0].m128_f32[1] = mtxView.r[1].m128_f32[0];
mtxWorld.r[0].m128_f32[2] = mtxView.r[2].m128_f32[0];
mtxWorld.r[1].m128_f32[0] = mtxView.r[0].m128_f32[1];
mtxWorld.r[1].m128_f32[1] = mtxView.r[1].m128_f32[1];
mtxWorld.r[1].m128_f32[2] = mtxView.r[2].m128_f32[1];
mtxWorld.r[2].m128_f32[0] = mtxView.r[0].m128_f32[2];
mtxWorld.r[2].m128_f32[1] = mtxView.r[1].m128_f32[2];
mtxWorld.r[2].m128_f32[2] = mtxView.r[2].m128_f32[2];
// スケールを反映
mtxScl = XMMatrixScaling(g_aParticle[nCntParticle].scale.x, g_aParticle[nCntParticle].scale.y, g_aParticle[nCntParticle].scale.z);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxScl);
// 移動を反映
mtxTranslate = XMMatrixTranslation(g_aParticle[nCntParticle].pos.x, g_aParticle[nCntParticle].pos.y, g_aParticle[nCntParticle].pos.z);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxTranslate);
// ワールドマトリックスの設定
SetWorldMatrix(&mtxWorld);
// マテリアル設定
SetMaterial(g_aParticle[nCntParticle].material);
// ポリゴンの描画
GetDeviceContext()->Draw(4, 0);
}
}
// ライティングを有効に
SetLightEnable(TRUE);
// 通常ブレンドに戻す
SetBlendState(BLEND_MODE_ALPHABLEND);
// Z比較有効
SetDepthEnable(TRUE);
// フォグ有効
SetFogEnable(TRUE);
}
//=============================================================================
// 頂点情報の作成
//=============================================================================
HRESULT MakeVertexParticle(void)
{
// 頂点バッファ生成
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX_3D) * 4;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
GetDevice()->CreateBuffer(&bd, NULL, &g_VertexBuffer);
{//頂点バッファの中身を埋める
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_VertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D* vertex = (VERTEX_3D*)msr.pData;
// 頂点座標の設定
vertex[0].Position = XMFLOAT3(-PARTICLE_SIZE_X / 2, PARTICLE_SIZE_Y / 2, 0.0f);
vertex[1].Position = XMFLOAT3(PARTICLE_SIZE_X / 2, PARTICLE_SIZE_Y / 2, 0.0f);
vertex[2].Position = XMFLOAT3(-PARTICLE_SIZE_X / 2, -PARTICLE_SIZE_Y / 2, 0.0f);
vertex[3].Position = XMFLOAT3(PARTICLE_SIZE_X / 2, -PARTICLE_SIZE_Y / 2, 0.0f);
// 法線の設定
vertex[0].Normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
vertex[1].Normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
vertex[2].Normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
vertex[3].Normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
// 反射光の設定
vertex[0].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[1].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[2].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
vertex[3].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
// テクスチャ座標の設定
vertex[0].TexCoord = { 0.0f, 0.0f };
vertex[1].TexCoord = { 1.0f, 0.0f };
vertex[2].TexCoord = { 0.0f, 1.0f };
vertex[3].TexCoord = { 1.0f, 1.0f };
GetDeviceContext()->Unmap(g_VertexBuffer, 0);
}
return S_OK;
}
//=============================================================================
// マテリアルカラーの設定
//=============================================================================
void SetColorParticle(int nIdxParticle, XMFLOAT4 col)
{
g_aParticle[nIdxParticle].material.Diffuse = col;
}
//=============================================================================
// パーティクルの発生処理
//=============================================================================
int SetParticle(XMFLOAT3 pos, XMFLOAT3 move, XMFLOAT4 col, float fSizeX, float fSizeY, int nLife)
{
int nIdxParticle = -1;
for(int nCntParticle = 0; nCntParticle < MAX_PARTICLE; nCntParticle++)
{
if(!g_aParticle[nCntParticle].bUse)
{
g_aParticle[nCntParticle].pos = pos;
g_aParticle[nCntParticle].rot = { 0.0f, 0.0f, 0.0f };
g_aParticle[nCntParticle].scale = { 1.0f, 1.0f, 1.0f };
g_aParticle[nCntParticle].move = move;
g_aParticle[nCntParticle].material.Diffuse = col;
g_aParticle[nCntParticle].fSizeX = fSizeX;
g_aParticle[nCntParticle].fSizeY = fSizeY;
g_aParticle[nCntParticle].nLife = nLife;
g_aParticle[nCntParticle].bUse = TRUE;
nIdxParticle = nCntParticle;
#ifdef DISP_SHADOW
// 影の設定
g_aParticle[nCntParticle].nIdxShadow = CreateShadow(XMFLOAT3(pos.x, 0.1f, pos.z), 0.8f, 0.8f); // 影の設定
if(g_aParticle[nCntParticle].nIdxShadow != -1)
{
SetColorShadow(g_aParticle[nCntParticle].nIdxShadow, XMFLOAT4(1.0f, 1.0f, 1.0f, 0.5f));
}
#endif
break;
}
}
return nIdxParticle;
}