/
Model.cpp
325 lines (235 loc) · 7.8 KB
/
Model.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
#include "Model.h"
Model::Model()
{
nModelVert = 0;
nModelIndex = 0;
modelVertices = NULL;
modelIndices = NULL;
worldMatrix = XMMatrixIdentity();
}
Model::~Model()
{
ReleaseCOM(modelVertices);
ReleaseCOM(modelIndices);
}
void Model::OpenTXT(char * file_name, ID3D11Device* device)
{
std::fstream fin(file_name);
if (!fin) {
MessageBox(0, L"Erro ao carregar o arquivo de modelo (Arquivo nao existe ou esta corrompido).", L"Model Error", 0);
return;
}
std::string ignore;
fin >> ignore >> nModelVert;
fin >> ignore >> nModelIndex;
fin >> ignore;
vertices.resize(nModelVert);
for (int i = 0; i < nModelVert; ++i) {
fin >> vertices[i].Pos.x >> vertices[i].Pos.y >> vertices[i].Pos.z;
fin >> vertices[i].Normal.x >> vertices[i].Normal.y >> vertices[i].Normal.z;
//vertices[i].Cor.w = 1.0f;
}
fin >> ignore >> ignore;
indices.resize(nModelIndex);
UINT tNumber = nModelIndex;
for (int i = 0; i < tNumber / 3; ++i) {
fin >> indices[i * 3 + 0] >> indices[i * 3 + 1] >> indices[i * 3 + 2];
}
fin.close();
Build(device);
}
void Model::OpenBookTXT(char * file_name, ID3D11Device * device)
{
std::fstream fin(file_name);
if (!fin) {
MessageBox(0, L"Erro ao carregar o arquivo de modelo (Arquivo nao existe ou esta corrompido).", L"Model Error", 0);
return;
}
std::string ignore;
fin >> ignore >> nModelVert;
fin >> ignore >> nModelIndex;
fin >> ignore >> ignore >> ignore >> ignore;
vertices.resize(nModelVert);
for (int i = 0; i < nModelVert; ++i) {
fin >> vertices[i].Pos.x >> vertices[i].Pos.y >> vertices[i].Pos.z;
//Seria a normal no lugar da cor aqui
fin >> vertices[i].Normal.x >> vertices[i].Normal.y >> vertices[i].Normal.z;
}
fin >> ignore >> ignore >> ignore;
UINT mTriangleCount = nModelIndex;
nModelIndex *= 3;
indices.resize(nModelIndex);
for (int i = 0; i < mTriangleCount; ++i) {
fin >> indices[i * 3 + 0] >> indices[i * 3 + 1] >> indices[i * 3 + 2];
}
fin.close();
Build(device);
}
void Model::Build(ID3D11Device * device)
{
//
// Criando a descricao para o buffer dos vertices
//
D3D11_BUFFER_DESC bdVertex;
ZeroMemory(&bdVertex, sizeof(bdVertex));
bdVertex.Usage = D3D11_USAGE_IMMUTABLE;
bdVertex.ByteWidth = sizeof(Vertice) * nModelVert;
bdVertex.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bdVertex.CPUAccessFlags = 0;
bdVertex.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA sdVertex;
ZeroMemory(&sdVertex, sizeof(sdVertex));
sdVertex.pSysMem = &vertices[0];
//
// Criando a descricao para o buffer dos indices
//
D3D11_BUFFER_DESC bdIndex;
ZeroMemory(&bdIndex, sizeof(bdIndex));
bdIndex.Usage = D3D11_USAGE_IMMUTABLE;
bdIndex.ByteWidth = sizeof(UINT) * nModelIndex;
bdIndex.BindFlags = D3D11_BIND_INDEX_BUFFER;
bdIndex.CPUAccessFlags = 0;
bdIndex.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA sdIndex;
ZeroMemory(&sdIndex, sizeof(sdIndex));
sdIndex.pSysMem = &indices[0];
//
// Criando os buffers
//
HRESULT hr = device->CreateBuffer(&bdVertex, &sdVertex, &modelVertices);
if (FAILED(hr)) {
MessageBox(0, L"CreateBuffer falhou.", L"Erro Fatal", 0);
return;
}
hr = device->CreateBuffer(&bdIndex, &sdIndex, &modelIndices);
if (FAILED(hr)) {
MessageBox(0, L"CreateBuffer falhou.", L"Erro Fatal", 0);
return;
}
}
/*
void Model::LoadCube(ID3D11Device * device)
{
Vertice vertices[] =
{
//FRONT
{ XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f) }, //0
{ XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) }, //1
{ XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, //2
{ XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f) }, //3
//BACK
{ XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT4(1.0f, 0.0f, 1.0f, 1.0f) }, //4
{ XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f) }, //5
{ XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT4(0.4f, 0.2f, 0.0f, 1.0f) }, //6
{ XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT4(0.8f, 1.0f, 0.25f, 1.0f) }, //7
//TOP
{ XMFL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f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f) }, //0
};
nModelVert = 24;
UINT indices[] =
{
//FRONT
0, 2, 1,
0, 3, 2,
//BACK
7, 5, 6,
7, 4, 5,
//TOP
8, 10, 9,
8, 11, 10,
//BOTTOM
12, 14, 13,
12, 15, 14,
//RIGHT
16, 18, 17,
16, 19, 18,
//LEFT
20, 22, 21,
20, 23, 22,
};
nModelIndex = 36;
//VERTEX BUFFER
D3D11_BUFFER_DESC vertexDesc;
ZeroMemory(&vertexDesc, sizeof(D3D11_BUFFER_DESC));
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof(Vertice) * nModelVert;
vertexDesc.CPUAccessFlags = 0;
vertexDesc.MiscFlags = 0;
vertexDesc.StructureByteStride = 0;
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(D3D11_SUBRESOURCE_DATA));
resourceData.pSysMem = vertices;
HRESULT hr = device->CreateBuffer(&vertexDesc, &resourceData, &modelVertices);
if (FAILED(hr))
return;
//INDEX BUFFER
D3D11_BUFFER_DESC indexDesc;
ZeroMemory(&indexDesc, sizeof(D3D11_BUFFER_DESC));
indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexDesc.ByteWidth = sizeof(UINT) * nModelIndex;
indexDesc.CPUAccessFlags = 0;
indexDesc.MiscFlags = 0;
indexDesc.StructureByteStride = 0;
indexDesc.Usage = D3D11_USAGE_DEFAULT;
resourceData.pSysMem = indices;
hr = device->CreateBuffer(&indexDesc, &resourceData, &modelIndices);
if (FAILED(hr))
return;
}
*/
void Model::Render(ID3D11DeviceContext *conDevice, ID3D11RasterizerState *rasterizerState)
{
conDevice->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
conDevice->RSSetState(rasterizerState);
UINT strides = sizeof(Vertice);
UINT offset = 0;
conDevice->IASetVertexBuffers(0, 1, &modelVertices, &strides, &offset);
conDevice->IASetIndexBuffer(modelIndices, DXGI_FORMAT_R32_UINT, 0);
}
void Model::Update(float dt)
{
XMMATRIX rotMatrix = XMMatrixRotationRollPitchYaw(dt, dt, dt);
worldMatrix = ((XMMATRIX)rotMatrix) * ((XMMATRIX)worldMatrix);
}
void Model::SetWorldMatrix(const XMMATRIX& newWorld)
{
worldMatrix = newWorld;
}
XMMATRIX Model::GetWorldMatrix()
{
return worldMatrix;
}
void Model::SetMaterial(XMFLOAT4 Ambiente, XMFLOAT4 Difuso, XMFLOAT4 Especular, XMFLOAT4 Refletivo)
{
material.Ambiente = Ambiente;
material.Difuso = Difuso;
material.Especular = Especular;
material.Refletivo = Refletivo;
}
void Model::SetMaterial(Material newMat)
{
material = newMat;
}
Material Model::GetMaterial()
{
return material;
}