worked for that box.
However I downloaded another .obj file. And now I get another problem.
This is where I read after I found and 'f' line:
// Read the face data in backwards to convert it to a
left hand system from right hand system.
fileStream >> faces[dwFaceIndex].m_VertexIndex.z >>
input2 >> faces[dwFaceIndex].m_TextureIndex.z
>> faces[dwFaceIndex].m_VertexIndex.y >> input2 >>
faces[dwFaceIndex].m_TextureIndex.y
>> faces[dwFaceIndex].m_VertexIndex.x >> input2 >>
faces[dwFaceIndex].m_TextureIndex.x;
It has only v and vt attributes so it looks like these:
The orginal however did only have 3 sets of numbers but this one can vary from 3 to 4,
any idea on how I solve this? This model varies from 3-4 vertices
f 2/2 1/1 64/64 65/65
f 41/41 64/64 1/1
f 4/4 3/3 1/1 2/2
f 41/41 1/1 3/3
f 6/6 5/5 3/3 4/4
f 41/41 3/3 5/5 f 8/8 7/7 5/5 6/6
Looks like this:
And it's read because I'm just returning it by default but I'm guessing that 4th value is missing and messing up faces.
Full Code: Normals are nulled since there aren't any in my .OBJ files
bool ObjMeshLoader::LoadDataStructures(std::string& aFileName, Model* aModel)
{
Vector3f *vertices, *texcoords, *normals;
Face *faces;
int vIndex, tIndex, nIndex;
char input, input2;
std::ifstream fileStream;
std::ofstream fileOutput;vertices = new Vector3f[m_dwVertexCount];
if (!vertices)
{
return false;
}
texcoords = new Vector3f[m_dwTextureCount];
if (!texcoords)
{
return false;
}normals = new Vector3f[m_dwNormalCount];
if (!normals)
{
return false;
}
faces = new Face[m_dwFaceCount];
if (!faces)
{
return false;
}int dwVertexIndex = 0;
int dwTexcoordIndex = 0;
int dwNormalIndex = 0;
int dwFaceIndex = 0;
fileStream.open(aFileName);if (fileStream.fail() == true)
{
return false;
}
// Read in the vertices, texture coordinates, and normals into the data structures.
// Important: Also convert to left hand coordinate system since Maya uses right hand coordinate system.
fileStream.get(input);
while (!fileStream.eof())
{
if (input == 'v')
{
fileStream.get(input); // Read in the vertices.
if (input == ' ')
{
fileStream >> vertices[dwVertexIndex].x >> vertices[dwVertexIndex].y >> vertices[dwVertexIndex].z;
// Invert the Z vertex to change to left hand system.
vertices[dwVertexIndex].z = vertices[dwVertexIndex].z * -1.0f;
dwVertexIndex++;
} // Read in the texture uv coordinates.
if (input == 't')
{
fileStream >> texcoords[dwTexcoordIndex].x >> texcoords[dwTexcoordIndex].y;
// Invert the V texture coordinates to left hand system.
texcoords[dwTexcoordIndex].y = 1.0f - texcoords[dwTexcoordIndex].y;
dwTexcoordIndex++;
} // Read in the normals.
if (input == 'n')
{
fileStream >> normals[dwNormalIndex].x >> normals[dwNormalIndex].y >> normals[dwNormalIndex].z;
// Invert the Z normal to change to left hand system.
normals[dwNormalIndex].z = normals[dwNormalIndex].z * -1.0f;
dwNormalIndex++;
}
} // Read in the faces.
if(input == 'f')
{
fileStream.get(input);
if (input == ' ')
{
// Read the face data in backwards to convert it to a left hand system from right hand system.
fileStream >> faces[dwFaceIndex].m_VertexIndex.z >> input2 >> faces[dwFaceIndex].m_TextureIndex.z// >> input2 >> faces[dwFaceIndex].m_NormalIndex.z
>> faces[dwFaceIndex].m_VertexIndex.y >> input2 >> faces[dwFaceIndex].m_TextureIndex.y// >> input2 >> faces[dwFaceIndex].m_NormalIndex.y
>> faces[dwFaceIndex].m_VertexIndex.x >> input2 >> faces[dwFaceIndex].m_TextureIndex.x;// >> input2 >> faces[dwFaceIndex].m_NormalIndex.x;
dwFaceIndex++;
}
}
// Read in the remainder of the line.
while (input != '\n')
{
fileStream.get(input);
} fileStream.get(input);
}
fileStream.close();
VertexPosUVNorm *vertexes = new VertexPosUVNorm[m_dwFaceCount * 3];
if (!vertexes)
{
return false;
}int *indices = new int[m_dwFaceCount * 3];
if(!indices)
{
return false;
}
int vertexCount = 0;
m_dwModelIndexCount = 0;
// Now loop through all the faces and output the three vertices for each face.
for (int index = 0; index < dwFaceIndex; index++)
{
vIndex = faces[index].m_VertexIndex.x - 1;
tIndex = faces[index].m_TextureIndex.x - 1;
nIndex = faces[index].m_NormalIndex.x - 1; vertexes[vertexCount].m_Pos.x = vertices[vIndex].x;
vertexes[vertexCount].m_Pos.y = vertices[vIndex].y;
vertexes[vertexCount].m_Pos.z = vertices[vIndex].z;
vertexes[vertexCount].m_Tex.x = texcoords[tIndex].x;
vertexes[vertexCount].m_Tex.y = texcoords[tIndex].y; //vertexes[vertexCount].m_Norm.x = normals[nIndex].x;
//vertexes[vertexCount].m_Norm.y = normals[nIndex].y;
//vertexes[vertexCount].m_Norm.z = normals[nIndex].z;
indices[vertexCount] = vertexCount;
m_dwModelIndexCount++; vertexCount++;
vIndex = faces[index].m_VertexIndex.y - 1;
tIndex = faces[index].m_TextureIndex.y - 1;
nIndex = faces[index].m_NormalIndex.y - 1;
vertexes[vertexCount].m_Pos.x = vertices[vIndex].x;
vertexes[vertexCount].m_Pos.y = vertices[vIndex].y;
vertexes[vertexCount].m_Pos.z = vertices[vIndex].z; vertexes[vertexCount].m_Tex.x = texcoords[tIndex].x;
vertexes[vertexCount].m_Tex.y = texcoords[tIndex].y;
//vertexes[vertexCount].m_Norm.x = normals[nIndex].x;
//vertexes[vertexCount].m_Norm.y = normals[nIndex].y;
//vertexes[vertexCount].m_Norm.z = normals[nIndex].z; indices[vertexCount] = vertexCount;
m_dwModelIndexCount++;
vertexCount++; vIndex = faces[index].m_VertexIndex.z - 1;
tIndex = faces[index].m_TextureIndex.z - 1;
nIndex = faces[index].m_NormalIndex.z - 1;
vertexes[vertexCount].m_Pos.x = vertices[vIndex].x;
vertexes[vertexCount].m_Pos.y = vertices[vIndex].y;
vertexes[vertexCount].m_Pos.z = vertices[vIndex].z; vertexes[vertexCount].m_Tex.x = texcoords[tIndex].x;
vertexes[vertexCount].m_Tex.y = texcoords[tIndex].y;
//vertexes[vertexCount].m_Norm.x = normals[nIndex].x;
//vertexes[vertexCount].m_Norm.y = normals[nIndex].y;
//vertexes[vertexCount].m_Norm.z = normals[nIndex].z; indices[vertexCount] = vertexCount;
m_dwModelIndexCount++;
vertexCount++;
}m_dwModelVertexCount = vertexCount;
int dwVertexSize = sizeof(VertexPosUVNorm)*m_dwModelVertexCount;
aModel->m_VertexDataWrapper.m_pVertexData = new char[dwVertexSize];
memcpy(aModel->m_VertexDataWrapper.m_pVertexData, vertexes, dwVertexSize);
int dwIndexSize = sizeof(int)*m_dwModelIndexCount;
aModel->m_IndexDataWrapper.m_pIndexData = new char[dwIndexSize];
memcpy(aModel->m_IndexDataWrapper.m_pIndexData, indices, dwIndexSize);//Surface Set
Surface* pSurface = new Surface();
pSurface->SetIndexCount(m_dwModelIndexCount);
pSurface->SetIndexStart(0);
pSurface->SetVertexCount(m_dwModelVertexCount); //Not used.
pSurface->SetVertexStart(0);pSurface->SetPrimologyType(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pSurface->SetDiffuseTexture("seafloor.dds");aModel->m_vecSurfaces.push_back(pSurface);
//
if (vertices)
{
delete [] vertices;
vertices = NULL;
}if (texcoords)
{
delete [] texcoords;
texcoords = NULL;
}
if (normals)
{
delete [] normals;
normals = NULL;
}if (faces)
{
delete [] faces;
faces = NULL;
}
if (vertexes)
{
delete [] vertexes;
vertexes = NULL;
}if (indices)
{
delete [] indices;
indices = NULL;
}
return true;
}