Skip to content

Instantly share code, notes, and snippets.

@jamieshepherd
Created November 6, 2015 13:44
Show Gist options
  • Save jamieshepherd/5b1012263993ce4cb3be to your computer and use it in GitHub Desktop.
Save jamieshepherd/5b1012263993ce4cb3be to your computer and use it in GitHub Desktop.
mesh loader
bool Model::LoadModel(char* filename, wchar_t* texturename, bool leftHanded, bool isTextured)
{
m_Textured = isTextured;
std::ifstream fileStream;
char streamChar;
fileStream.open(filename);
while (!fileStream.eof()) {
// READ LINES
fileStream.get(streamChar);
// The line is a comment
if (streamChar == '#') {
while (streamChar != '\n') {
fileStream.get(streamChar);
}
}
// Could be 'v' or 'vt' or 'vn'
else if (streamChar == 'v') {
fileStream.get(streamChar);
// Vertex position
if (streamChar == ' ') {
float vx, vy, vz;
fileStream >> std::skipws >> vx >> vy >> vz;
if (leftHanded) {
m_VertexPositions.push_back(XMFLOAT3(vx, vy, vz));
} else {
m_VertexPositions.push_back(XMFLOAT3(vx, vy, vz*-1.0f));
}
}
// Vertex texture coordinates
else if (streamChar == 't') {
float vtcu, vtcv;
fileStream >> std::skipws >> vtcu >> vtcv;
if (leftHanded) {
m_VertexTextureCoords.push_back(XMFLOAT2(vtcu, vtcv));
} else {
m_VertexTextureCoords.push_back(XMFLOAT2(vtcu, 1.0f - vtcv));
}
}
// Vertex normal
else if (streamChar == 'n') {
float vnx, vny, vnz;
fileStream >> vnx >> vny >> vnz;
m_VertexNormals.push_back(XMFLOAT3(vnx, vny, vnz));
}
}
// Group
else if (streamChar == 'g') {
}
// Index or face
else if (streamChar == 'f') {
UINT value = 0;
m_TriangleCount = 0;
// As long as we're on the same line
while (streamChar != '\n') {
//fileStream.get(streamChar);
// TAKE THE FIRST CHAR, THIS IS AN INDEX
fileStream >> std::skipws >> value;
fileStream.get(streamChar);
while (streamChar != ' ') {
fileStream.get(streamChar);
if (streamChar == '\n') {
break;
}
}
m_Indices.push_back((UINT)value - 1);
}
m_TriangleCount++;
}
// Material library filename
else if (streamChar == 'm') {
}
// Use material
else if (streamChar == 'u') {
}
}
// Load vertices
for (UINT i = 0; i < m_VertexPositions.size(); i++) {
VERTEX newVertex;
if (m_Textured) {
newVertex = { m_VertexPositions[i], m_VertexTextureCoords[i] };
//newVertex = { m_VertexPositions[i], XMFLOAT4(randColor(), randColor(), randColor(), 1.0f) };
} else {
newVertex = { m_VertexPositions[i], XMFLOAT2(0.0f, 0.0f) };
//newVertex = { m_VertexPositions[i], XMFLOAT4(randColor(), randColor(), randColor(), 1.0f) };
}
m_Vertices.push_back(newVertex);
}
if (m_Textured) {
LoadTexture(texturename);
}
LoadBuffers();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment