-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
152 lines (95 loc) · 3.51 KB
/
Copy pathModel.cpp
File metadata and controls
152 lines (95 loc) · 3.51 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
#include "Model.h"
Model::Model() {
m_rIds.m_topoID = D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
m_position = { 0.0f, 0.0f, 0.0f };
}
void Model::AssignResources(int texID, int vsID, int psID) {
m_rIds.m_textureID = texID;
m_rIds.m_vsID = vsID;
m_rIds.m_psID = psID;
}
void Model::MoveBy(XMFLOAT3 moveBy){
m_position.x += moveBy.x;
m_position.y += moveBy.y;
m_position.z += moveBy.z;
}
void Model::MoveTo(XMFLOAT3 moveTo){
m_position.x = moveTo.x;
m_position.y = moveTo.y;
m_position.z = moveTo.z;
}
void Model::SetResources() {
//gDat.SetResources(m_rIds);
UINT stride = sizeof(VertexPNU); // make automatic
UINT offset = 0;
gContext->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(), &stride, &offset);
gContext->IASetIndexBuffer(m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
}
void Model::Draw() {
if (!m_isVisible)return;
//SetResources();
UINT stride = sizeof(VertexPNU); // make automatic
UINT offset = 0;
gContext->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(), &stride, &offset);
gContext->IASetIndexBuffer(m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
XMMATRIX tmpWorldMatrix = XMMatrixTranslation(m_position.x, m_position.y, m_position.z);
gContext->UpdateSubresource(gcbPerMesh.Get(), 0, 0, &tmpWorldMatrix, 0, 0);
gContext->DrawIndexed(indexCount, 0 , 0);
}
void Model::DrawAt(XMFLOAT3 toDrawAt){
if (!m_isVisible)return;
//SetResources();
UINT stride = sizeof(VertexPNU); // make automatic
UINT offset = 0;
gContext->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(), &stride, &offset);
gContext->IASetIndexBuffer(m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
XMMATRIX tmpWorldMatrix = XMMatrixTranslation(toDrawAt.x, toDrawAt.y, toDrawAt.z);
gContext->UpdateSubresource(gcbPerMesh.Get(), 0, 0, &tmpWorldMatrix, 0, 0);
gContext->DrawIndexed(indexCount, 0, 0);
}
void Model::Update() {
}
void Model::Hide(){
m_isVisible = false;
}
void Model::Unhide(){
m_isVisible = true;
}
void Model::LoadMesh(std::wstring fN) {
std::wstring completePathAndName = CV_baseDir + CV_meshDir + fN + CV_meshFileType;
std::ifstream file(completePathAndName);
std::string materialName, textureName;
if (file) {
file >> materialName >> textureName >> numOfIndices >> numOfVertices;
unsigned short * indices = new unsigned short[numOfIndices];
for (int i = 0; i < numOfIndices; i++) file >> indices[i];
indexCount = numOfIndices;
VertexPNU* vertices = new VertexPNU[numOfVertices];
for (int i = 0; i < numOfVertices; i++) {
file >> vertices[i].position.x
>> vertices[i].position.y
>> vertices[i].position.z
>> vertices[i].normal.x
>> vertices[i].normal.y
>> vertices[i].normal.z
>> vertices[i].texCoord.x
>> vertices[i].texCoord.y;
}
file.close();
D3D11_SUBRESOURCE_DATA vertexBufferData = { 0 };
vertexBufferData.pSysMem = vertices;
vertexBufferData.SysMemPitch = 0;
vertexBufferData.SysMemSlicePitch = 0;
CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(VertexPNU)*numOfVertices, D3D11_BIND_VERTEX_BUFFER);
D3D11_SUBRESOURCE_DATA indexBufferData = { 0 };
indexBufferData.pSysMem = indices;
indexBufferData.SysMemPitch = 0;
indexBufferData.SysMemSlicePitch = 0;
CD3D11_BUFFER_DESC indexBufferDesc(sizeof(unsigned short)*numOfIndices, D3D11_BIND_INDEX_BUFFER);
gDevice->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &m_vertexBuffer);
gDevice->CreateBuffer(&indexBufferDesc, &indexBufferData, &m_indexBuffer);
delete[] vertices;
delete[] indices;
}
else Error(L"Mesh Load Error", completePathAndName.c_str());
}