forked from megayuchi/D3D12Lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertexUtil.cpp
131 lines (108 loc) · 3.37 KB
/
VertexUtil.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
#include "pch.h"
#include <Windows.h>
#include "typedef.h"
DWORD AddVertex(BasicVertex* pVertexList, DWORD dwMaxVertexCount, DWORD* pdwInOutVertexCount, const BasicVertex* pVertex);
DWORD CreateBoxMesh(BasicVertex** ppOutVertexList, WORD* pOutIndexList, DWORD dwMaxBufferCount, float fHalfBoxLen)
{
const DWORD INDEX_COUNT = 36;
if (dwMaxBufferCount < INDEX_COUNT)
__debugbreak();
const WORD pIndexList[INDEX_COUNT] =
{
// +z
3, 0, 1,
3, 1, 2,
// -z
4, 7, 6,
4, 6, 5,
// -x
0, 4, 5,
0, 5, 1,
// +x
7, 3, 2,
7, 2, 6,
// +y
0, 3, 7,
0, 7, 4,
// -y
2, 1, 5,
2, 5, 6
};
TVERTEX pTexCoordList[INDEX_COUNT] =
{
// +z
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
{0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
// -z
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
{0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
// -x
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
{0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
// +x
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
{0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
// +y
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
{0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f},
// -y
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f},
{0.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
};
FLOAT3 pWorldPosList[8];
pWorldPosList[0] = { -fHalfBoxLen, fHalfBoxLen, fHalfBoxLen };
pWorldPosList[1] = { -fHalfBoxLen, -fHalfBoxLen, fHalfBoxLen };
pWorldPosList[2] = { fHalfBoxLen, -fHalfBoxLen, fHalfBoxLen };
pWorldPosList[3] = { fHalfBoxLen, fHalfBoxLen, fHalfBoxLen };
pWorldPosList[4] = { -fHalfBoxLen, fHalfBoxLen, -fHalfBoxLen };
pWorldPosList[5] = { -fHalfBoxLen, -fHalfBoxLen, -fHalfBoxLen };
pWorldPosList[6] = { fHalfBoxLen, -fHalfBoxLen, -fHalfBoxLen };
pWorldPosList[7] = { fHalfBoxLen, fHalfBoxLen, -fHalfBoxLen };
const DWORD MAX_WORKING_VERTEX_COUNT = 65536;
BasicVertex* pWorkingVertexList = new BasicVertex[MAX_WORKING_VERTEX_COUNT];
memset(pWorkingVertexList, 0, sizeof(BasicVertex)*MAX_WORKING_VERTEX_COUNT);
DWORD dwBasicVertexCount = 0;
for (DWORD i = 0; i < INDEX_COUNT; i++)
{
BasicVertex v;
v.color = { 1.0f, 1.0f, 1.0f, 1.0f };
v.position = { pWorldPosList[pIndexList[i]].x, pWorldPosList[pIndexList[i]].y, pWorldPosList[pIndexList[i]].z };
v.texCoord = { pTexCoordList[i].u, pTexCoordList[i].v };
pOutIndexList[i] = AddVertex(pWorkingVertexList, MAX_WORKING_VERTEX_COUNT, &dwBasicVertexCount, &v);
}
BasicVertex* pNewVertexList = new BasicVertex[dwBasicVertexCount];
memcpy(pNewVertexList, pWorkingVertexList, sizeof(BasicVertex) * dwBasicVertexCount);
*ppOutVertexList = pNewVertexList;
delete[] pWorkingVertexList;
pWorkingVertexList = nullptr;
return dwBasicVertexCount;
}
void DeleteBoxMesh(BasicVertex* pVertexList)
{
delete[] pVertexList;
}
DWORD AddVertex(BasicVertex* pVertexList, DWORD dwMaxVertexCount, DWORD* pdwInOutVertexCount, const BasicVertex* pVertex)
{
DWORD dwFoundIndex = -1;
DWORD dwExistVertexCount = *pdwInOutVertexCount;
for (DWORD i = 0; i < dwExistVertexCount; i++)
{
const BasicVertex* pExistVertex = pVertexList + i;
if (!memcmp(pExistVertex, pVertex, sizeof(BasicVertex)))
{
dwFoundIndex = i;
goto lb_return;
}
}
if (dwExistVertexCount + 1 > dwMaxVertexCount)
{
__debugbreak();
goto lb_return;
}
// »õ·Î¿î vertexÃß°¡
dwFoundIndex = dwExistVertexCount;
pVertexList[dwFoundIndex] = *pVertex;
*pdwInOutVertexCount = dwExistVertexCount + 1;
lb_return:
return dwFoundIndex;
}