-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshfield.cpp
More file actions
409 lines (315 loc) · 11.4 KB
/
meshfield.cpp
File metadata and controls
409 lines (315 loc) · 11.4 KB
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
//=============================================================================
//
// メッシュ地面の処理 [meshfield.cpp]
// Author :
//
//=============================================================================
#include "main.h"
#include "input.h"
#include "meshfield.h"
#include "renderer.h"
#include "collision.h"
//*****************************************************************************
// マクロ定義
//*****************************************************************************
#define TEXTURE_MAX (1) // テクスチャの数
//*****************************************************************************
// グローバル変数
//*****************************************************************************
static ID3D11Buffer *g_VertexBuffer = NULL; // 頂点バッファ
static ID3D11Buffer *g_IndexBuffer = NULL; // インデックスバッファ
static ID3D11ShaderResourceView *g_Texture[TEXTURE_MAX] = { NULL }; // テクスチャ情報
static int g_TexNo; // テクスチャ番号
static XMFLOAT3 g_posField; // ポリゴン表示位置の中心座標
static XMFLOAT3 g_rotField; // ポリゴンの回転角
static int g_nNumBlockXField, g_nNumBlockZField; // ブロック数
static int g_nNumVertexField; // 総頂点数
static int g_nNumVertexIndexField; // 総インデックス数
static int g_nNumPolygonField; // 総ポリゴン数
static float g_fBlockSizeXField, g_fBlockSizeZField; // ブロックサイズ
static char* g_TextureName[TEXTURE_MAX] = {
"data/TEXTURE/field004.jpg",
};
// 波の処理
static VERTEX_3D *g_Vertex = NULL;
// 波の高さ = sin( -経過時間 * 周波数 + 距離 * 距離補正 ) * 振幅
static XMFLOAT3 g_Center; // 波の発生場所
static float g_Time = 0.0f; // 波の経過時間
static float g_wave_frequency = 2.0f; // 波の周波数
static float g_wave_correction = 0.02f; // 波の距離補正
static float g_wave_amplitude = 20.0f; // 波の振幅
static BOOL g_Load = FALSE;
//=============================================================================
// 初期化処理
//=============================================================================
HRESULT InitMeshField(XMFLOAT3 pos, XMFLOAT3 rot,
int nNumBlockX, int nNumBlockZ, float fBlockSizeX, float fBlockSizeZ)
{
// ポリゴン表示位置の中心座標を設定
g_posField = pos;
g_rotField = rot;
// テクスチャ生成
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;
// ブロック数の設定
g_nNumBlockXField = nNumBlockX;
g_nNumBlockZField = nNumBlockZ;
// 頂点数の設定
g_nNumVertexField = (nNumBlockX + 1) * (nNumBlockZ + 1);
// インデックス数の設定
g_nNumVertexIndexField = (nNumBlockX + 1) * 2 * nNumBlockZ + (nNumBlockZ - 1) * 2;
// ポリゴン数の設定
g_nNumPolygonField = nNumBlockX * nNumBlockZ * 2 + (nNumBlockZ - 1) * 4;
// ブロックサイズの設定
g_fBlockSizeXField = fBlockSizeX;
g_fBlockSizeZField = fBlockSizeZ;
// 頂点情報をメモリに作っておく(波の為)
// 波の処理
// 波の高さ = sin( -経過時間 * 周波数 + 距離 * 距離補正 ) * 振幅
g_Vertex = new VERTEX_3D[g_nNumVertexField];
g_Center = XMFLOAT3(0.0f, 0.0f, 0.0f); // 波の発生場所
g_Time = 0.0f; // 波の経過時間(+とーとで内側外側になる)
g_wave_frequency = 1.0f; // 波の周波数(上下運動の速さ)
g_wave_correction = 0.02f; // 波の距離補正(変えなくても良いと思う)
g_wave_amplitude = 30.0f; // 波の振幅(波の高さ)
for (int z = 0; z < (g_nNumBlockZField + 1); z++)
{
for (int x = 0; x < (g_nNumBlockXField + 1); x++)
{
g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.x = -(g_nNumBlockXField / 2.0f) * g_fBlockSizeXField + x * g_fBlockSizeXField;
g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.y = 0.0f;
g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.z = (g_nNumBlockZField / 2.0f) * g_fBlockSizeZField - z * g_fBlockSizeZField;
float dx = g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.x - g_Center.x;
float dz = g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.z - g_Center.z;
// 波紋の中心点からの距離を得る
float len = (float)sqrt(dx * dx + dz * dz);
// 波の高さを、sin関数で得る
// 波の高さ = sin( -経過時間 * 周波数 + 距離 * 距離補正 ) * 振幅
g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.y = sinf(-g_Time * g_wave_frequency + len * g_wave_correction) * g_wave_amplitude;
// 法線の設定
g_Vertex[z * (g_nNumBlockXField + 1) + x].Normal = XMFLOAT3(0.0f, 1.0, 0.0f);
// 反射光の設定
g_Vertex[z * (g_nNumBlockXField + 1) + x].Diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
// テクスチャ座標の設定
float texSizeX = 1.0f;
float texSizeZ = 1.0f;
g_Vertex[z * (g_nNumBlockXField + 1) + x].TexCoord.x = texSizeX * x;
g_Vertex[z * (g_nNumBlockXField + 1) + x].TexCoord.y = texSizeZ * z;
}
}
// 頂点バッファ生成
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX_3D) * g_nNumVertexField;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
GetDevice()->CreateBuffer(&bd, NULL, &g_VertexBuffer);
// インデックスバッファ生成
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(unsigned short) * g_nNumVertexIndexField;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
GetDevice()->CreateBuffer(&bd, NULL, &g_IndexBuffer);
{//頂点バッファの中身を埋める
// 頂点バッファへのポインタを取得
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_VertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D* pVtx = (VERTEX_3D*)msr.pData;
memcpy(pVtx, g_Vertex, sizeof(VERTEX_3D)*g_nNumVertexField);
GetDeviceContext()->Unmap(g_VertexBuffer, 0);
}
{//インデックスバッファの中身を埋める
// インデックスバッファのポインタを取得
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_IndexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
unsigned short *pIdx = (unsigned short*)msr.pData;
int nCntIdx = 0;
for(int nCntVtxZ = 0; nCntVtxZ < g_nNumBlockZField; nCntVtxZ++)
{
if(nCntVtxZ > 0)
{// 縮退ポリゴンのためのダブりの設定
pIdx[nCntIdx] = (nCntVtxZ + 1) * (g_nNumBlockXField + 1);
nCntIdx++;
}
for(int nCntVtxX = 0; nCntVtxX < (g_nNumBlockXField + 1); nCntVtxX++)
{
pIdx[nCntIdx] = (nCntVtxZ + 1) * (g_nNumBlockXField + 1) + nCntVtxX;
nCntIdx++;
pIdx[nCntIdx] = nCntVtxZ * (g_nNumBlockXField + 1) + nCntVtxX;
nCntIdx++;
}
if(nCntVtxZ < (g_nNumBlockZField - 1))
{// 縮退ポリゴンのためのダブりの設定
pIdx[nCntIdx] = nCntVtxZ * (g_nNumBlockXField + 1) + g_nNumBlockXField;
nCntIdx++;
}
}
GetDeviceContext()->Unmap(g_IndexBuffer, 0);
}
g_Load = TRUE;
return S_OK;
}
//=============================================================================
// 終了処理
//=============================================================================
void UninitMeshField(void)
{
if (g_Load == FALSE) return;
// インデックスバッファの解放
if (g_IndexBuffer) {
g_IndexBuffer->Release();
g_IndexBuffer = NULL;
}
// 頂点バッファの解放
if (g_VertexBuffer) {
g_VertexBuffer->Release();
g_VertexBuffer = NULL;
}
// テクスチャの解放
for (int i = 0; i < TEXTURE_MAX; i++)
{
if (g_Texture[i])
{
g_Texture[i]->Release();
g_Texture[i] = NULL;
}
}
if (g_Vertex)
{
delete[] g_Vertex;
g_Vertex = NULL;
}
g_Load = FALSE;
}
//=============================================================================
// 更新処理
//=============================================================================
void UpdateMeshField(void)
{
// 波の処理
float dt = 0.03f;
for (int z = 0; z < g_nNumBlockZField; z++)
{
for (int x = 0; x < g_nNumBlockXField; x++)
{
float dx = g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.x - g_Center.x;
float dz = g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.z - g_Center.z;
// 波紋の中心点からの距離を得る
float len = (float)sqrt(dx * dx + dz * dz);
// 波の高さを、sin関数で得る
// 波の高さ = sin( -経過時間 * 周波数 + 距離 * 距離補正 ) * 振幅
g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.y = sinf(-g_Time * g_wave_frequency + len * g_wave_correction) * g_wave_amplitude;
g_Vertex[z * (g_nNumBlockXField + 1) + x].Position.y = 0.0f;
}
}
g_Time += dt;
// 頂点バッファに値をセットする
D3D11_MAPPED_SUBRESOURCE msr;
GetDeviceContext()->Map(g_VertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &msr);
VERTEX_3D* pVtx = (VERTEX_3D*)msr.pData;
// 全頂点情報を毎回上書きしているのはDX11ではこの方が早いからです
memcpy(pVtx, g_Vertex, sizeof(VERTEX_3D)*g_nNumVertexField);
GetDeviceContext()->Unmap(g_VertexBuffer, 0);
}
//=============================================================================
// 描画処理
//=============================================================================
void DrawMeshField(void)
{
// 頂点バッファ設定
UINT stride = sizeof(VERTEX_3D);
UINT offset = 0;
GetDeviceContext()->IASetVertexBuffers(0, 1, &g_VertexBuffer, &stride, &offset);
// インデックスバッファ設定
GetDeviceContext()->IASetIndexBuffer(g_IndexBuffer, DXGI_FORMAT_R16_UINT, 0);
// プリミティブトポロジ設定
GetDeviceContext()->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
// マテリアル設定
MATERIAL material;
ZeroMemory(&material, sizeof(material));
material.Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
SetMaterial(material);
// テクスチャ設定
GetDeviceContext()->PSSetShaderResources(0, 1, &g_Texture[g_TexNo]);
XMMATRIX mtxRot, mtxTranslate, mtxWorld;
// ワールドマトリックスの初期化
mtxWorld = XMMatrixIdentity();
// 回転を反映
mtxRot = XMMatrixRotationRollPitchYaw(g_rotField.x, g_rotField.y, g_rotField.z);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxRot);
// 移動を反映
mtxTranslate = XMMatrixTranslation(g_posField.x, g_posField.y, g_posField.z);
mtxWorld = XMMatrixMultiply(mtxWorld, mtxTranslate);
// ワールドマトリックスの設定
SetWorldMatrix(&mtxWorld);
// ポリゴンの描画
GetDeviceContext()->DrawIndexed(g_nNumVertexIndexField, 0, 0);
}
BOOL RayHitField(XMFLOAT3 pos, XMFLOAT3 *HitPosition, XMFLOAT3 *Normal)
{
XMFLOAT3 start = pos;
XMFLOAT3 end = pos;
if (start.x == 0.0f)
{
start.x += 0.5f;
end.x += 0.5f;
}
if (start.z == 0.0f)
{
start.z -= 0.5f;
end.z -= 0.5f;
}
// 少し上から、ズドーンと下へレイを飛ばす
start.y += 100.0f;
end.y -= 1000.0f;
// 処理を高速化する為に全検索ではなくて、座標からポリゴンを割り出している
float fz = (g_nNumBlockXField / 2.0f) * g_fBlockSizeXField;
float fx = (g_nNumBlockZField / 2.0f) * g_fBlockSizeZField;
int sz = (int)((-start.z+fz) / g_fBlockSizeZField);
int sx = (int)(( start.x+fx) / g_fBlockSizeXField);
int ez = sz + 1;
int ex = sx + 1;
sz = sx = 0; // ここ態とやってるの気づくかなぁー
ez = g_nNumBlockZField-1;
ex = g_nNumBlockXField-1;
if ((sz < 0) || (sz > g_nNumBlockZField-1) ||
(sx < 0) || (sx > g_nNumBlockXField-1))
{
*Normal = {0.0f, 1.0f, 0.0f};
return FALSE;
}
// 必要数分検索を繰り返す
for (int z = sz; z < ez; z++)
{
for (int x = sx; x < ex; x++)
{
XMFLOAT3 p0 = g_Vertex[z * (g_nNumBlockXField + 1) + x].Position;
XMFLOAT3 p1 = g_Vertex[z * (g_nNumBlockXField + 1) + (x + 1)].Position;
XMFLOAT3 p2 = g_Vertex[(z + 1) * (g_nNumBlockXField + 1) + x].Position;
XMFLOAT3 p3 = g_Vertex[(z + 1) * (g_nNumBlockXField + 1) + (x + 1)].Position;
// 三角ポリゴンだから2枚分の当たり判定
BOOL ans = RayCast(p0, p2, p1, start, end, HitPosition, Normal);
if (ans)
{
return TRUE;
}
ans = RayCast(p1, p2, p3, start, end, HitPosition, Normal);
if (ans)
{
return TRUE;
}
}
}
return FALSE;
}