-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
296 lines (239 loc) · 5.66 KB
/
Model.cpp
File metadata and controls
296 lines (239 loc) · 5.66 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
#include "Model.h"
Model::Model()
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_texture = 0;
m_model = 0;
}
Model::Model(const Model& other)
{
}
Model::~Model()
{
}
bool Model::initialize(ID3D11Device* device, char* modelFilename, WCHAR* textureFilename)
{
bool result;
// 모델 데이터를 로드합니다.
result = loadModel(modelFilename);
if (!result)
{
return false;
}
// 삼각형의 형상을 유지하는 정점 및 인덱스 버퍼를 초기화합니다.
result = initializeBuffers(device);
if (!result)
{
return false;
}
// 이 모델의 텍스처를 로드합니다.
result = loadTexture(device, textureFilename);
if (!result)
{
return false;
}
return true;
}
void Model::shutdown()
{
// 모델 텍스처를 해제합니다.
releaseTexture();
// 정점 버퍼와 인덱스 버퍼를 해제합니다.
shutdownBuffers();
// 모델 데이터를 해제합니다.
releaseModel();
return;
}
void Model::render(ID3D11DeviceContext* deviceContext)
{
// 정점 버퍼와 인덱스 버퍼를 그래픽스 파이프라인에 넣어 화면에 그릴 준비를 합니다.
renderBuffers(deviceContext);
return;
}
int Model::getIndexCount()
{
return m_indexCount;
}
ID3D11ShaderResourceView* Model::getTexture()
{
return m_texture->getTexture();
}
bool Model::initializeBuffers(ID3D11Device* device)
{
HRESULT result;
// 정점 배열을 생성합니다.
VertexType* vertices = new VertexType[m_vertexCount];
if (!vertices)
{
return false;
}
// 인덱스 배열을 생성합니다.
unsigned long* indices = new unsigned long[m_indexCount];
if (!indices)
{
return false;
}
// 정점 배열과 인덱스 배열을 데이터로 로드합니다.
for (int i = 0; i < m_vertexCount; ++i)
{
vertices[i].position = D3DXVECTOR3(m_model[i].x, m_model[i].y, m_model[i].z);
vertices[i].texture = D3DXVECTOR2(m_model[i].tu, m_model[i].tv);
vertices[i].normal = D3DXVECTOR3(m_model[i].nx, m_model[i].ny, m_model[i].nz);
indices[i] = i;
}
// 정점 버퍼의 description을 작성합니다.
D3D11_BUFFER_DESC vertexBufferDesc;
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// 정점 데이터를 가리키는 보조 리소스 구조체를 작성합니다.
D3D11_SUBRESOURCE_DATA vertexData;
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// 정점 버퍼를 생성합니다.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if (FAILED(result))
{
return false;
}
// 인덱스 버퍼의 description을 작성합니다.
D3D11_BUFFER_DESC indexBufferDesc;
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// 인덱스 데이터를 가리키는 보조 리소스 구조체를 작성합니다.
D3D11_SUBRESOURCE_DATA indexData;
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// 인덱스 버퍼를 생성합니다.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
if (FAILED(result))
{
return false;
}
// 생성되고 값이 할당된 정점 버퍼와 인덱스 버퍼를 해제합니다.
delete[] vertices;
vertices = 0;
delete[] indices;
indices = 0;
return true;
}
void Model::shutdownBuffers()
{
// 인덱스 버퍼를 해제합니다.
if (m_indexBuffer)
{
m_indexBuffer->Release();
m_indexBuffer = 0;
}
// 정점 버퍼를 해제합니다.
if (m_vertexBuffer)
{
m_vertexBuffer->Release();
m_vertexBuffer = 0;
}
return;
}
void Model::renderBuffers(ID3D11DeviceContext* deviceContext)
{
// 정점 버퍼의 단위와 오프셋을 설정합니다.
unsigned int stride = sizeof(VertexType);
unsigned int offset = 0;
// input assembler에 정점 버퍼를 활성화하여 그려질 수 있게 합니다.
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
// input assembler에 인덱스 버퍼를 활성화하여 그려질 수 있게 합니다.
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
// 정점 버퍼로 그릴 기본형을 설정합니다. 여기서는 삼각형입니다.
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
return;
}
bool Model::loadTexture(ID3D11Device* device, WCHAR* filename)
{
bool result;
// 텍스쳐 객체를 생성합니다.
m_texture = new Texture;
if (!m_texture)
{
return false;
}
// 텍스쳐 객체를 초기화합니다.
result = m_texture->initialize(device, filename);
if (!result)
{
return false;
}
return true;
}
void Model::releaseTexture()
{
// 텍스쳐 객체를 해제합니다.
if (m_texture)
{
m_texture->shutdown();
delete m_texture;
m_texture = 0;
}
return;
}
bool Model::loadModel(char* filename)
{
char input;
// 모델 파일을 엽니다.
ifstream fin;
fin.open(filename);
// 파일이 열리지 않으면 종료합니다.
if (fin.fail())
{
return false;
}
// 정점 카운트의 값을 읽습니다.
do
{
fin.get(input);
} while (input != ':');
// 정점 카운트를 읽습니다.
fin >> m_vertexCount;
// 정점 수와 동일하게 인덱스 수를 설정합니다.
m_indexCount = m_vertexCount;
// 읽은 정점 카운트를 사용하여 모델을 생성합니다.
m_model = new ModelType[m_vertexCount];
if (!m_model)
{
return false;
}
// 데이터의 시작 부분까지 읽어 옵니다.
do
{
fin.get(input);
} while (input != ':');
fin.get();
fin.get();
// 정점 데이터를 읽습니다.
for (int i = 0; i < m_vertexCount; ++i)
{
fin >> m_model[i].x >> m_model[i].y >> m_model[i].z;
fin >> m_model[i].tu >> m_model[i].tv;
fin >> m_model[i].nx >> m_model[i].ny >> m_model[i].nz;
}
// 모델 파일을 닫습니다.
fin.close();
return true;
}
void Model::releaseModel()
{
if (m_model)
{
delete[] m_model;
m_model = 0;
}
return;
}