Last active
December 31, 2015 21:29
-
-
Save PirosB3/8047662 to your computer and use it in GitHub Desktop.
LoadMesh function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SortOfMeshSubset *LoadMesh(LPSTR filename) { | |
std::wifstream fileStream; | |
std::wstring line; | |
// Instead of using VertexXYZ I am just using XMFLOAT3 and XMFLOAT2 | |
std::vector <XMFLOAT3> vectorVertices(0); | |
std::vector <XMFLOAT2> vectorTextureVertices(0); | |
std::vector <XMFLOAT3> vectorNormalVertices(0); | |
std::vector<SimpleVertex> vertices; | |
std::vector<UINT> indexes; | |
// These two structures are to avoid duplication in our vertex buffer | |
std::unordered_map<std::string, UINT> vertexIndexes; | |
int count = 0; | |
fileStream.open(filename); | |
bool isOpen = fileStream.is_open(); //debugging only. | |
while(std::getline(fileStream, line)) | |
{ | |
line = TrimStart(line); | |
if (line.compare(0, 2, L"v ") == 0) { | |
WCHAR first[5]; | |
float x, y, z; | |
WCHAR oldStyleStr[200]; | |
wcscpy(oldStyleStr, line.c_str()); | |
swscanf(oldStyleStr, L"%2s%f%f%f", first, &x, &y, &z); | |
// makeFloat is just a commodity function to make a XMFLOAT3 | |
vectorVertices.push_back(makeFloat3(x, y, z)); | |
} | |
else if (line.compare(0, 3, L"vn ") == 0) { | |
WCHAR first[5]; | |
float x, y, z; | |
WCHAR oldStyleStr[200]; | |
wcscpy(oldStyleStr, line.c_str()); | |
swscanf(oldStyleStr, L"%2s%f%f%f", first, &x, &y, &z); | |
vectorNormalVertices.push_back(makeFloat3(x, y, z)); | |
} | |
else if (line.compare(0, 3, L"vt ") == 0) | |
{ | |
WCHAR first[5]; | |
float x, y; | |
WCHAR oldStyleStr[200]; | |
wcscpy(oldStyleStr, line.c_str()); | |
swscanf(oldStyleStr, L"%2s%f%f", first, &x, &y); | |
vectorTextureVertices.push_back(makeFloat2(x, y)); | |
} | |
if (line.compare(0, 2, L"f ") == 0) //"f space" | |
{ | |
WCHAR first[5]; | |
WCHAR slash1[5]; | |
WCHAR slash2[5]; | |
WCHAR slash3[5]; | |
WCHAR slash4[5]; | |
WCHAR slash5[5]; | |
WCHAR slash6[5]; | |
UINT v1, vt1, vn1, v2, vt2, vn2, v3, vt3, vn3; | |
WCHAR oldStyleStr[200]; | |
wcscpy(oldStyleStr, line.c_str()); | |
swscanf(oldStyleStr, L"%2s%d%1s%d%1s%d%d%1s%d%1s%d%d%1s%d%1s%d", first, | |
&v1, slash1, &vt1, slash2, &vn1, | |
&v2, slash3, &vt2, slash4, &vn2, | |
&v3, slash5, &vt3, slash6, &vn3); | |
/* Here we finally build the SimpleVertices. For each face that we find: | |
- get the corresponding V, VT, VN by (index -1) | |
- build a SimpleVertex | |
- add to vertices vector | |
As we also have to deal with the index buffer, we change this slightly: | |
for each vertex in a face | |
- create a unique string that identifies our SimpleVertex | |
- check if this vertex has already been added to our vertex buffer. If it has, retreieve it's index. | |
if not, push_back and keep track of it's index. | |
- add our index to the index buffer. | |
*/ | |
// Create a unique identifier for our vertex | |
std::string uid1; | |
uid1 += std::to_string(v1) + "-" + std::to_string(vt1) + "-" + std::to_string(vn1); | |
// Check if it's in our lookup table | |
if (vertexIndexes[uid1]) { | |
UINT id = vertexIndexes[uid1]; | |
// Vertex is already present, just push_back it's index. | |
indexes.push_back(id); | |
} else { | |
// Vertex is not present. Create one, add it to the lookup table and push_back new vertex and index. | |
SimpleVertex sv1; | |
sv1.Pos = vectorVertices[v1-1]; | |
sv1.TexUV = vectorTextureVertices[vt1-1]; | |
sv1.VecNormal = vectorNormalVertices[vn1-1]; | |
vertices.push_back(sv1); | |
vertexIndexes[uid1] = count; | |
indexes.push_back(count); | |
count++; | |
} | |
// Get the second | |
std::string uid2; | |
uid2 += std::to_string(v2) + "-" + std::to_string(vt2) + "-" + std::to_string(vn2); | |
if (vertexIndexes[uid2]) { | |
UINT id = vertexIndexes[uid2]; | |
indexes.push_back(id); | |
} else { | |
SimpleVertex sv2; | |
sv2.Pos = vectorVertices[v2-1]; | |
sv2.TexUV = vectorTextureVertices[vt2-1]; | |
sv2.VecNormal = vectorNormalVertices[vn2-1]; | |
vertices.push_back(sv2); | |
vertexIndexes[uid2] = count; | |
indexes.push_back(count); | |
count++; | |
} | |
// Get the third | |
std::string uid3; | |
uid3 += std::to_string(v3) + "-" + std::to_string(vt3) + "-" + std::to_string(vn3); | |
if (vertexIndexes[uid3]) { | |
UINT id = vertexIndexes[uid3]; | |
indexes.push_back(id); | |
} else { | |
SimpleVertex sv3; | |
sv3.Pos = vectorVertices[v3-1]; | |
sv3.TexUV = vectorTextureVertices[vt3-1]; | |
sv3.VecNormal = vectorNormalVertices[vn3-1]; | |
vertices.push_back(sv3); | |
vertexIndexes[uid3] = count; | |
indexes.push_back(count); | |
count++; | |
} | |
} | |
} | |
SortOfMeshSubset *mesh = new SortOfMeshSubset; | |
// Set number and array for vertices | |
mesh->numVertices = (USHORT) vertices.size(); | |
mesh->vertices = new SimpleVertex[vertices.size()]; | |
for (int i = 0; i < vertices.size(); i++) { | |
mesh->vertices[0] = vertices[0]; | |
} | |
mesh->numIndices = (USHORT) indexes.size(); | |
mesh->indexes = new USHORT[indexes.size()]; | |
for (int i = 0; i < indexes.size(); i++) { | |
mesh->indexes[i] = indexes[i]; | |
} | |
return mesh; // WHAT AM I DOING WRONG?? :( | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment