I am currently working on a 3d game engine, but am complete stumped on a problem involving texture rendering. Here is the rundown.
My game engine renders meshes of the OBJ wavefront type. I have created a OBJ mesh parser that simply looks through all the vertice/normal/texture indexes(Line by Line) and loads the corresponding data into the right places without altering it.
Mesh Parser Code: (NOTE pseudo codeish)
MY VERTEX STRUCTURE: struct VERTEX { D3DXVECTOR3 Pos; D3DXVECTOR3 Normal; D3DXVECTOR2 TexCoords; };
if (line[i]->c_str()[0] == 'v' && line[i]->c_str()[1] == ' ') {
float tmpx = 0;
float tmpy = 0;
float tmpz = 0;
sscanf(line[i]->c_str(), "v %f %f %f", &tmpx, &tmpy, &tmpz);
D3DXVECTOR3 myVec = {tmpx, tmpy, tmpz };
vertices.push_back(myVec);
}
else if (line[i]->c_str()[0] == 'v' && line[i]->c_str()[1] == 't') {
float tmpx = 0;
float tmpy = 0;
sscanf(line[i]->c_str(), "vt %f %f", &tmpx, &tmpy);
D3DXVECTOR2 myTexCoords = { tmpx, tmpy };
texCoords.push_back(myTexCoords);
}
else if (line[i]->c_str()[0] == 'v' && line[i]->c_str()[1] == 'n') {
float tmpx = 0;
float tmpy = 0;
float tmpz = 0;
sscanf(line[i]->c_str(), "vn %f %f %f", &tmpx, &tmpy, &tmpz);
D3DXVECTOR3 myNorm = { tmpx, tmpy, tmpz };
normals.push_back(myNorm);
}
sscanf(line[i]->c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",
&tmpindices1, &tmpTexCoordsIndex1, &tmpNormalsIndex1,
&tmpindices2, &tmpTexCoordsIndex2, &tmpNormalsIndex2,
&tmpindices3, &tmpTexCoordsIndex3, &tmpNormalsIndex3,
&tmpindices4, &tmpTexCoordsIndex4, &tmpNormalsIndex4);
verticeIndex.push_back(tmpindices1 - 1);
verticeIndex.push_back(tmpindices2 - 1);
verticeIndex.push_back(tmpindices3 - 1);
verticeIndex.push_back(tmpindices4 - 1);
texCoordsIndex.push_back(tmpTexCoordsIndex1 - 1);
texCoordsIndex.push_back(tmpTexCoordsIndex2 - 1);
texCoordsIndex.push_back(tmpTexCoordsIndex3 - 1);
texCoordsIndex.push_back(tmpTexCoordsIndex4 - 1);
normalsIndex.push_back(tmpNormalsIndex1 - 1);
normalsIndex.push_back(tmpNormalsIndex2 - 1);
normalsIndex.push_back(tmpNormalsIndex3 - 1);
normalsIndex.push_back(tmpNormalsIndex4 - 1);
for (int i = 0; i < texCoordsIndex.size(); i++) {
ModelToFill.Vertices[i].TexCoords = texCoords.at(texCoordsIndex.at(i));
}
for (int i = 0; i < normalsIndex.size(); i++) {
ModelToFill.Vertices[i].Normal = normals.at(normalsIndex.at(i));
}
for (int i = 0; i < vertices.size(); i++) {
ModelToFill.Vertices[i].Pos = vertices.at(i);
}
for (int i = 0; i < verticeIndex.size(); i++) {
ModelToFill.Indices[i] = verticeIndex.at(i);
}
But now when my renderer renders the mesh it looks like this:
As You can see the texture coordinates are all messed up, (for some surfaces). The Bottom, Leftmost and back are messed up but the front and top are fine.
Now I should mention that my mesh is exported from blender with materials, and the mesh is also triangulated. Before importing I also performed the smart UV unwrap.
Finally, I should mention that the parts of the mesh which are not texture mapped correctly vary depending on the mesh.
MY QUESTION though quite general: What is the most probable cause of this issue, the mesh parser, form of blender mesh exporting, or renderer.