-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.cpp
240 lines (212 loc) · 6.04 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
#include "Model.h"
Model::Model(void)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_indices = 0;
m_colorVerts = 0;
m_textureVerts = 0;
m_flatTextureVerts = 0;
m_explosionTextureVerts = 0;
m_textureFileName = 0;
}
Model::Model(ColorVertex* vertices, int num_verts, WORD* inds, int num_inds, D3D_PRIMITIVE_TOPOLOGY topology)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_texture = 0;
m_textureView = 0;
m_textureFileName = 0;
m_colorVerts = vertices;
m_textureVerts = 0;
m_colorNormVerts = 0;
m_flatTextureVerts = 0;
m_explosionTextureVerts = 0;
m_numVert = num_verts;
m_indices = inds;
m_numInds = num_inds;
m_primTop = topology;
}
Model::Model(TextureVertex* vertices, WCHAR* filename, int num_verts, WORD* inds, int num_inds, D3D_PRIMITIVE_TOPOLOGY topology)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_texture = 0;
m_textureView = 0;
m_textureFileName = filename;
m_colorVerts = 0;
m_textureVerts = vertices;
m_colorNormVerts = 0;
m_flatTextureVerts = 0;
m_explosionTextureVerts = 0;
m_numVert = num_verts;
m_indices = inds;
m_numInds = num_inds;
m_primTop = topology;
}
Model::Model(ColorNormVertex* vertices, int num_verts, WORD* inds, int num_inds, D3D_PRIMITIVE_TOPOLOGY topology)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_texture = 0;
m_textureView = 0;
m_textureFileName = 0;
m_colorVerts = 0;
m_textureVerts = 0;
m_colorNormVerts = vertices;
m_flatTextureVerts = 0;
m_explosionTextureVerts = 0;
m_numVert = num_verts;
m_indices = inds;
m_numInds = num_inds;
m_primTop = topology;
}
Model::Model(FlatTextureVertex* vertices, WCHAR* filename, int num_verts, WORD* inds, int num_inds, D3D_PRIMITIVE_TOPOLOGY topology)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_texture = 0;
m_textureView = 0;
m_textureFileName = filename;
m_colorVerts = 0;
m_textureVerts = 0;
m_colorNormVerts = 0;
m_flatTextureVerts = vertices;
m_explosionTextureVerts = 0;
m_numVert = num_verts;
m_indices = inds;
m_numInds = num_inds;
m_primTop = topology;
}
Model::Model(ExplosionTextureVertex* vertices, WCHAR* filename, int num_verts, WORD* inds, int num_inds, D3D_PRIMITIVE_TOPOLOGY topology)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_texture = 0;
m_textureView = 0;
m_textureFileName = filename;
m_colorVerts = 0;
m_textureVerts = 0;
m_colorNormVerts = 0;
m_flatTextureVerts = 0;
m_explosionTextureVerts = vertices;
m_numVert = num_verts;
m_indices = inds;
m_numInds = num_inds;
m_primTop = topology;
}
Model::~Model(void)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
m_colorVerts = 0;
m_textureVerts = 0;
m_colorNormVerts = 0;
m_flatTextureVerts = 0;
m_explosionTextureVerts = 0;
m_indices = 0;
m_texture = 0;
m_textureView = 0;
}
void Model::shutdown()
{
if (m_indexBuffer) m_indexBuffer->Release();
if (m_vertexBuffer) m_vertexBuffer->Release();
if (m_textureView) m_textureView->Release();
if (m_texture) m_texture->Release();
}
HRESULT Model::initialize(ID3D11Device* device)
{
HRESULT hr;
int vertSize = 0;
if (m_colorVerts)
vertSize = sizeof(ColorVertex);
else if (m_textureVerts)
{
vertSize = sizeof(TextureVertex);
initTexture(device, m_textureFileName);
}
else if (m_colorNormVerts)
vertSize = sizeof(ColorNormVertex);
else if (m_flatTextureVerts)
{
vertSize = sizeof(FlatTextureVertex);
initTexture(device, m_textureFileName);
}
else if (m_explosionTextureVerts)
{
vertSize = sizeof(ExplosionTextureVertex);
initTexture(device, m_textureFileName);
}
// Create Vertex Buffer
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = vertSize * m_numVert;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory(&InitData, sizeof(InitData));
if (m_colorVerts)
InitData.pSysMem = m_colorVerts;
else if (m_textureVerts)
InitData.pSysMem = m_textureVerts;
else if (m_colorNormVerts)
InitData.pSysMem = m_colorNormVerts;
else if (m_flatTextureVerts)
InitData.pSysMem = m_flatTextureVerts;
else if (m_explosionTextureVerts)
InitData.pSysMem = m_explosionTextureVerts;
hr = device->CreateBuffer(&bd, &InitData, &m_vertexBuffer);
if (FAILED(hr))
return hr;
if(m_numInds > 0)
{
// Create Index Buffer
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(WORD) * m_numInds;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0;
InitData.pSysMem = m_indices;
hr = device->CreateBuffer(&bd, &InitData, &m_indexBuffer);
if (FAILED(hr))
return hr;
}
return true;
}
HRESULT Model::initTexture(ID3D11Device* device, WCHAR* filename)
{
HRESULT rc;
rc = CreateDDSTextureFromFile(device, filename, &m_texture, &m_textureView, NULL);
return rc;
}
void Model::render(ID3D11DeviceContext* context)
{
unsigned int stride;
unsigned int offset = 0;
if (m_colorVerts)
stride = sizeof(ColorVertex);
else if (m_textureVerts)
stride = sizeof(TextureVertex);
else if (m_colorNormVerts)
stride = sizeof(ColorNormVertex);
else if (m_flatTextureVerts)
stride = sizeof(FlatTextureVertex);
else if (m_explosionTextureVerts)
stride = sizeof(ExplosionTextureVertex);
// Set vertex buffer
context->IASetVertexBuffers( 0, 1, &m_vertexBuffer, &stride, &offset );
// Set Index Buffer
if(m_numInds > 0)
context->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R16_UINT, 0);
// Set primitive topology
context->IASetPrimitiveTopology(m_primTop);
}
int Model::getIndexCount() { return m_numInds; }
int Model::getVertexCount() { return m_numVert; }
ColorVertex* Model::getColorVerts() { return m_colorVerts; }
TextureVertex* Model::getTextureVerts() { return m_textureVerts; }
ColorNormVertex* Model::getColorNormVerts() { return m_colorNormVerts; }
FlatTextureVertex* Model::getFlatTextureVerts() { return m_flatTextureVerts; }
WORD* Model::getIndicies() { return m_indices; }
ID3D11ShaderResourceView* Model::getTexShadResView() { return m_textureView; }