-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshcylinder.cpp
More file actions
179 lines (144 loc) · 6.17 KB
/
Copy pathmeshcylinder.cpp
File metadata and controls
179 lines (144 loc) · 6.17 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
//--------------------------------------------------------------------------------------------------------
//
//メッシュシリンダーの地面の断面[meshcylinder.cpp]
//Author:菊地陽人
//
//--------------------------------------------------------------------------------------------------------
#include "main.h"
#include "meshcylinder.h"
//グローバル変数
LPDIRECT3DTEXTURE9 g_pTextureMeshCylinder = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVtxBuffMeshCylinder = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIdxBuffMeshCylinder = NULL;
D3DXVECTOR3 g_posMeshCylinder;
D3DXVECTOR3 g_rotMeshCylinder;
D3DXMATRIX g_mtxWorldMeshCylinder; //ワールドマトリックス
//--------------------------------------------------------------------------------------------------------
//ポリゴンの初期化処理
//--------------------------------------------------------------------------------------------------------
void InitMeshCylinder(void)
{
//デバイスの取得
LPDIRECT3DDEVICE9 pDevice = GetDevice();
//テクスチャーの読み込み
D3DXCreateTextureFromFile(pDevice, "data\\TEXTURE\\sky002.png", &g_pTextureMeshCylinder);
//ポリゴンの初期化
g_posMeshCylinder =
g_rotMeshCylinder = ZERO_SET;
//頂点バッファの生成
pDevice->CreateVertexBuffer(sizeof(VERTEX_3D) * (MESHSYLINDER_SPLIT * 2 + 2), D3DUSAGE_WRITEONLY, FVF_VERTEX_3D, D3DPOOL_MANAGED, &g_pVtxBuffMeshCylinder, NULL);
VERTEX_3D *pVtx;
//頂点バッファのロック
g_pVtxBuffMeshCylinder->Lock(0, 0, (void**)&pVtx, 0);
//頂点座標の設定
for (int nCntMeshCylinder = 0; nCntMeshCylinder < MESHSYLINDER_SPLIT + 1; nCntMeshCylinder++)
{
pVtx[nCntMeshCylinder].pos = D3DXVECTOR3(
sinf(D3DX_PI * (1.0f - (2.0f / MESHSYLINDER_SPLIT * nCntMeshCylinder))) *MESHSYLINDER_WIDTH,
MESHSYLINDER_HEIGHT,
cosf(D3DX_PI * (1.0f - (2.0f / MESHSYLINDER_SPLIT * nCntMeshCylinder))) * MESHSYLINDER_WIDTH);
pVtx[MESHSYLINDER_SPLIT + nCntMeshCylinder + 1].pos = D3DXVECTOR3(
sinf(D3DX_PI * (1.0f - (2.0f / MESHSYLINDER_SPLIT * nCntMeshCylinder))) *MESHSYLINDER_WIDTH,
0.0f,
cosf(D3DX_PI * (1.0f - (2.0f / MESHSYLINDER_SPLIT * nCntMeshCylinder))) * MESHSYLINDER_WIDTH);
if (nCntMeshCylinder == MESHSYLINDER_SPLIT)
{
pVtx[nCntMeshCylinder].pos = D3DXVECTOR3(
sinf(D3DX_PI * 1.0f) *MESHSYLINDER_WIDTH,
MESHSYLINDER_HEIGHT,
cosf(D3DX_PI * 1.0f) * MESHSYLINDER_WIDTH);
pVtx[MESHSYLINDER_SPLIT + 1 + nCntMeshCylinder].pos = D3DXVECTOR3(
sinf(D3DX_PI * 1.0f) *MESHSYLINDER_WIDTH,
0.0f,
cosf(D3DX_PI * 1.0f) * MESHSYLINDER_WIDTH);
}
//法線ベクトルの設定
pVtx[nCntMeshCylinder].nor = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
pVtx[MESHSYLINDER_SPLIT + 1 + nCntMeshCylinder].nor = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
//頂点カラーの設定
pVtx[nCntMeshCylinder].col = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
pVtx[MESHSYLINDER_SPLIT + 1 + nCntMeshCylinder].col = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
//テクスチャ座標の設定
pVtx[nCntMeshCylinder].tex = D3DXVECTOR2(nCntMeshCylinder * (MESHSYLINDER_TEX_RESOLUTION / MESHSYLINDER_SPLIT), 0.0f);
pVtx[MESHSYLINDER_SPLIT + 1 + nCntMeshCylinder].tex = D3DXVECTOR2(nCntMeshCylinder * (MESHSYLINDER_TEX_RESOLUTION / MESHSYLINDER_SPLIT), 1.0f);
}
//頂点バッファのアンロック
g_pVtxBuffMeshCylinder->Unlock();
//インデックスバッファの生成
pDevice->CreateIndexBuffer(sizeof(WORD) * (MESHSYLINDER_SPLIT * 2 + 2), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_pIdxBuffMeshCylinder, NULL);
WORD *pIdx;
//インデックスバッファをロックし、頂点番号へのポインタを取得
g_pIdxBuffMeshCylinder->Lock(0, 0, (void**)&pIdx, 0);
//頂点番号データの設定
for (int nCntMeshCylinder = 0; nCntMeshCylinder < MESHSYLINDER_SPLIT * 2 + 2; nCntMeshCylinder++)
{
if (nCntMeshCylinder % EVENPARITY == NOPARITY)
{
pIdx[nCntMeshCylinder] = nCntMeshCylinder / EVENPARITY;
}
if (nCntMeshCylinder % EVENPARITY == ODDPARITY)
{
pIdx[nCntMeshCylinder] = (nCntMeshCylinder / EVENPARITY) + ODDPARITY + MESHSYLINDER_SPLIT;
}
}
//インデックスバッファのアンロック
g_pIdxBuffMeshCylinder->Unlock();
}
//--------------------------------------------------------------------------------------------------------
//ポリゴンの終了処理
//--------------------------------------------------------------------------------------------------------
void UninitMeshCylinder(void)
{
//インデックスの破棄
if (g_pIdxBuffMeshCylinder != NULL)
{
g_pIdxBuffMeshCylinder->Release();
g_pIdxBuffMeshCylinder = NULL;
}
//テクスチャの破棄
if (g_pTextureMeshCylinder != NULL)
{
g_pTextureMeshCylinder->Release();
g_pTextureMeshCylinder = NULL;
}
//バッファの破棄
if (g_pVtxBuffMeshCylinder != NULL)
{
g_pVtxBuffMeshCylinder->Release();
g_pVtxBuffMeshCylinder = NULL;
}
}
//--------------------------------------------------------------------------------------------------------
//ポリゴンの更新処理
//--------------------------------------------------------------------------------------------------------
void UpdateMeshCylinder(void)
{
}
//--------------------------------------------------------------------------------------------------------
//ポリゴンの描画処理
//--------------------------------------------------------------------------------------------------------
void DrawMeshCylinder(void)
{
LPDIRECT3DDEVICE9 pDevice = GetDevice();
D3DXMATRIX mtxRot, mtxTrans;
//ワールドマトリックスの初期化
D3DXMatrixIdentity(&g_mtxWorldMeshCylinder);
//向きを反映
D3DXMatrixRotationYawPitchRoll(&mtxRot, g_rotMeshCylinder.y, g_rotMeshCylinder.x, g_rotMeshCylinder.z);
D3DXMatrixMultiply(&g_mtxWorldMeshCylinder, &g_mtxWorldMeshCylinder, &mtxRot);
//位置を反映
D3DXMatrixTranslation(&mtxTrans, g_posMeshCylinder.x, g_posMeshCylinder.y, g_posMeshCylinder.z);
D3DXMatrixMultiply(&g_mtxWorldMeshCylinder, &g_mtxWorldMeshCylinder, &mtxTrans);
//ワールドマトリックスの設定
pDevice->SetTransform(D3DTS_WORLD, &g_mtxWorldMeshCylinder);
//頂点バッファをデータストリームに設定
pDevice->SetStreamSource(0, g_pVtxBuffMeshCylinder, 0, sizeof(VERTEX_3D));
//インデックスバッファをデータストリームに設定
pDevice->SetIndices(g_pIdxBuffMeshCylinder);
//頂点フォーマットの設定
pDevice->SetFVF(FVF_VERTEX_3D);
//テクスチャの設定
pDevice->SetTexture(0, g_pTextureMeshCylinder);
//ポリゴン描画
pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, (MESHSYLINDER_SPLIT * 2 + 2), 0, (MESHSYLINDER_SPLIT * 2 + 2));
}